2 * Home Automation Status server. Sample code from The ACE Programmer's Guide,
3 * Copyright 2003 Addison-Wesley. All Rights Reserved.
6 #include "ace/OS_NS_stdio.h"
7 #include "ace/OS_NS_string.h"
8 #include "ace/Configuration.h"
9 #include "ace/Configuration_Import_Export.h"
10 #include "ace/Get_Opt.h"
11 #include "HA_Status_Static.h"
13 // Listing 1 code/ch19
15 HA_Status::init (int argc
, ACE_TCHAR
*argv
[])
17 static const ACE_TCHAR options
[] = ACE_TEXT (":f:");
18 ACE_Get_Opt
cmd_opts (argc
, argv
, options
, 0);
19 if (cmd_opts
.long_option
20 (ACE_TEXT ("config"), 'f', ACE_Get_Opt::ARG_REQUIRED
) == -1)
23 ACE_TCHAR config_file
[MAXPATHLEN
];
24 ACE_OS::strcpy (config_file
, ACE_TEXT ("HAStatus.conf"));
25 while ((option
= cmd_opts ()) != EOF
)
29 ACE_OS::strncpy (config_file
,
35 ((LM_ERROR
, ACE_TEXT ("-%c requires an argument\n"),
40 ((LM_ERROR
, ACE_TEXT ("Parse error.\n")), -1);
43 ACE_Configuration_Heap config
;
45 ACE_Registry_ImpExp
config_importer (config
);
46 if (config_importer
.import_config (config_file
) == -1)
47 ACE_ERROR_RETURN ((LM_ERROR
,
52 ACE_Configuration_Section_Key status_section
;
53 if (config
.open_section (config
.root_section (),
54 ACE_TEXT ("HAStatus"),
56 status_section
) == -1)
57 ACE_ERROR_RETURN ((LM_ERROR
,
59 ACE_TEXT ("Can't open HAStatus section")),
63 if (config
.get_integer_value (status_section
,
64 ACE_TEXT ("ListenPort"),
66 ACE_ERROR_RETURN ((LM_ERROR
,
67 ACE_TEXT ("HAStatus ListenPort does ")
68 ACE_TEXT ("not exist\n")),
70 this->listen_addr_
.set (static_cast<u_short
> (status_port
));
72 if (this->acceptor_
.open (this->listen_addr_
) != 0)
73 ACE_ERROR_RETURN ((LM_ERROR
,
74 ACE_TEXT ("HAStatus %p\n"),
82 // Listing 2 code/ch19
86 this->acceptor_
.close ();
91 // Listing 3 code/ch19
93 HA_Status::info (ACE_TCHAR
**str
, size_t len
) const
95 ACE_TCHAR buf
[BUFSIZ
];
96 ACE_OS::sprintf (buf
, ACE_TEXT ("HAStatus listening on port %hu\n"),
97 this->listen_addr_
.get_port_number ());
99 *str
= ACE::strnew (buf
);
101 ACE_OS::strncpy (*str
, buf
, len
);
102 return static_cast<int> (ACE_OS::strlen (*str
));
106 // Listing 4 code/ch19
107 ACE_FACTORY_DEFINE (ACE_Local_Service
, HA_Status
)
109 ACE_STATIC_SVC_DEFINE (HA_Status_Descriptor
,
110 ACE_TEXT ("HA_Status_Static_Service"),
112 &ACE_SVC_NAME (HA_Status
),
113 ACE_Service_Type::DELETE_THIS
|
114 ACE_Service_Type::DELETE_OBJ
,
115 0) // Service not initially active
117 ACE_STATIC_SVC_REQUIRE (HA_Status_Descriptor
)