Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / APG / Svc_Config / HA_Status_Static.cpp
blobd82a2989b2f8759de21b111a224bcaa806212c15
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_Static.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
35 ((LM_ERROR, ACE_TEXT ("-%c requires an argument\n"),
36 cmd_opts.opt_opt ()),
37 -1);
38 default:
39 ACE_ERROR_RETURN
40 ((LM_ERROR, ACE_TEXT ("Parse error.\n")), -1);
43 ACE_Configuration_Heap config;
44 config.open ();
45 ACE_Registry_ImpExp config_importer (config);
46 if (config_importer.import_config (config_file) == -1)
47 ACE_ERROR_RETURN ((LM_ERROR,
48 ACE_TEXT ("%p\n"),
49 config_file),
50 -1);
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,
58 ACE_TEXT ("%p\n"),
59 ACE_TEXT ("Can't open HAStatus section")),
60 -1);
62 u_int status_port;
63 if (config.get_integer_value (status_section,
64 ACE_TEXT ("ListenPort"),
65 status_port) == -1)
66 ACE_ERROR_RETURN ((LM_ERROR,
67 ACE_TEXT ("HAStatus ListenPort does ")
68 ACE_TEXT ("not exist\n")),
69 -1);
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"),
75 ACE_TEXT ("accept")),
76 -1);
78 return 0;
80 // Listing 1
82 // Listing 2 code/ch19
83 int
84 HA_Status::fini ()
86 this->acceptor_.close ();
87 return 0;
89 // Listing 2
91 // Listing 3 code/ch19
92 int
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 ());
98 if (*str == 0)
99 *str = ACE::strnew (buf);
100 else
101 ACE_OS::strncpy (*str, buf, len);
102 return static_cast<int> (ACE_OS::strlen (*str));
104 // Listing 3
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"),
111 ACE_SVC_OBJ_T,
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)
118 // Listing 4