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_Dynamic.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
,
34 ACE_ERROR_RETURN ((LM_ERROR
,
35 ACE_TEXT ("-%c requires an argument\n"),
39 ACE_ERROR_RETURN ((LM_ERROR
,
40 ACE_TEXT ("Parse error.\n")),
44 ACE_Configuration_Heap config
;
46 ACE_Registry_ImpExp
config_importer (config
);
47 if (config_importer
.import_config (config_file
) == -1)
48 ACE_ERROR_RETURN ((LM_ERROR
,
53 ACE_Configuration_Section_Key status_section
;
54 if (config
.open_section (config
.root_section (),
55 ACE_TEXT ("HAStatus"),
57 status_section
) == -1)
58 ACE_ERROR_RETURN ((LM_ERROR
,
60 ACE_TEXT ("Can't open HAStatus section")),
64 if (config
.get_integer_value (status_section
,
65 ACE_TEXT ("ListenPort"),
67 ACE_ERROR_RETURN ((LM_ERROR
,
68 ACE_TEXT ("HAStatus ListenPort ")
69 ACE_TEXT ("does not exist\n")),
71 this->listen_addr_
.set (static_cast<u_short
> (status_port
));
73 if (this->acceptor_
.open (this->listen_addr_
) != 0)
74 ACE_ERROR_RETURN ((LM_ERROR
,
75 ACE_TEXT ("HAStatus %p\n"),
83 // Listing 2 code/ch19
87 this->acceptor_
.close ();
92 // Listing 3 code/ch19
94 HA_Status::info (ACE_TCHAR
**str
, size_t len
) const
96 ACE_TCHAR buf
[BUFSIZ
];
98 ACE_TEXT ("HAStatus listening on port %hu\n"),
99 this->listen_addr_
.get_port_number ());
101 *str
= ACE::strnew (buf
);
103 ACE_OS::strncpy (*str
, buf
, len
);
104 return static_cast<int> (ACE_OS::strlen (*str
));
108 // Listing 4 code/ch19
109 ACE_FACTORY_DEFINE (HASTATUS
, HA_Status
)