Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / APG / Svc_Config / HA_Status_Dynamic.cpp
blob0cfcf30998231e5161d39d926ab3a958f41917de
1 /**
2 * Home Automation Status server. Sample code from The ACE Programmer's Guide,
3 * Copyright 2003 Addison-Wesley. All Rights Reserved.
4 */
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
14 int
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)
21 return -1;
22 int option;
23 ACE_TCHAR config_file[MAXPATHLEN];
24 ACE_OS::strcpy (config_file, ACE_TEXT ("HAStatus.conf"));
25 while ((option = cmd_opts ()) != EOF)
26 switch (option)
28 case 'f':
29 ACE_OS::strncpy (config_file,
30 cmd_opts.opt_arg (),
31 MAXPATHLEN);
32 break;
33 case ':':
34 ACE_ERROR_RETURN ((LM_ERROR,
35 ACE_TEXT ("-%c requires an argument\n"),
36 cmd_opts.opt_opt ()),
37 -1);
38 default:
39 ACE_ERROR_RETURN ((LM_ERROR,
40 ACE_TEXT ("Parse error.\n")),
41 -1);
44 ACE_Configuration_Heap config;
45 config.open ();
46 ACE_Registry_ImpExp config_importer (config);
47 if (config_importer.import_config (config_file) == -1)
48 ACE_ERROR_RETURN ((LM_ERROR,
49 ACE_TEXT ("%p\n"),
50 config_file),
51 -1);
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,
59 ACE_TEXT ("%p\n"),
60 ACE_TEXT ("Can't open HAStatus section")),
61 -1);
63 u_int status_port;
64 if (config.get_integer_value (status_section,
65 ACE_TEXT ("ListenPort"),
66 status_port) == -1)
67 ACE_ERROR_RETURN ((LM_ERROR,
68 ACE_TEXT ("HAStatus ListenPort ")
69 ACE_TEXT ("does not exist\n")),
70 -1);
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"),
76 ACE_TEXT ("accept")),
77 -1);
79 return 0;
81 // Listing 1
83 // Listing 2 code/ch19
84 int
85 HA_Status::fini ()
87 this->acceptor_.close ();
88 return 0;
90 // Listing 2
92 // Listing 3 code/ch19
93 int
94 HA_Status::info (ACE_TCHAR **str, size_t len) const
96 ACE_TCHAR buf[BUFSIZ];
97 ACE_OS::sprintf (buf,
98 ACE_TEXT ("HAStatus listening on port %hu\n"),
99 this->listen_addr_.get_port_number ());
100 if (*str == 0)
101 *str = ACE::strnew (buf);
102 else
103 ACE_OS::strncpy (*str, buf, len);
104 return static_cast<int> (ACE_OS::strlen (*str));
106 // Listing 3
108 // Listing 4 code/ch19
109 ACE_FACTORY_DEFINE (HASTATUS, HA_Status)
110 // Listing 4