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_string.h"
7 #include "ace/Configuration.h"
8 #include "ace/Configuration_Import_Export.h"
9 #include "ace/Get_Opt.h"
10 #include "ace/Log_Msg.h"
11 #include "ace/INET_Addr.h"
12 #include "ace/Service_Object.h"
14 class HA_Status
: public ACE_Service_Object
17 virtual int init (int argc
, ACE_TCHAR
*argv
[]);
20 ACE_INET_Addr listen_addr_
;
25 HA_Status::init (int argc
, ACE_TCHAR
*argv
[])
27 // Do ACE_Get_Opt and get conf file name, read out the sections
28 // and print the names.
30 // Listing 1 code/ch04
31 static const ACE_TCHAR options
[] = ACE_TEXT (":f:");
32 ACE_Get_Opt
cmd_opts (argc
, argv
, options
);
33 if (cmd_opts
.long_option
34 (ACE_TEXT ("config"), 'f', ACE_Get_Opt::ARG_REQUIRED
) == -1)
37 ACE_TCHAR config_file
[MAXPATHLEN
];
38 ACE_OS::strcpy (config_file
, ACE_TEXT ("HAStatus.conf"));
39 while ((option
= cmd_opts ()) != EOF
)
42 ACE_OS::strncpy (config_file
,
48 ((LM_ERROR
, ACE_TEXT ("-%c requires an argument\n"),
49 cmd_opts
.opt_opt ()), -1);
52 ((LM_ERROR
, ACE_TEXT ("Parse error.\n")), -1);
56 // Listing 2 code/ch04
57 ACE_Configuration_Heap config
;
58 if (config
.open () == -1)
60 ((LM_ERROR
, ACE_TEXT ("%p\n"), ACE_TEXT ("config")), -1);
61 ACE_Registry_ImpExp
config_importer (config
);
62 if (config_importer
.import_config (config_file
) == -1)
64 ((LM_ERROR
, ACE_TEXT ("%p\n"), config_file
), -1);
66 ACE_Configuration_Section_Key status_section
;
67 if (config
.open_section (config
.root_section (),
68 ACE_TEXT ("HAStatus"),
70 status_section
) == -1)
71 ACE_ERROR_RETURN ((LM_ERROR
, ACE_TEXT ("%p\n"),
72 ACE_TEXT ("Can't open HAStatus section")),
76 if (config
.get_integer_value (status_section
,
77 ACE_TEXT ("ListenPort"),
81 ACE_TEXT ("HAStatus ListenPort does not exist\n")),
83 this->listen_addr_
.set (static_cast<u_short
> (status_port
));
90 ACE_TMAIN (int argc
, ACE_TCHAR
*argv
[])
93 status
.init (argc
, argv
);