Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / examples / APG / Config / HA_Status.cpp
blob4f1f8e7f1d033beab16405e29fce6e3fb103673b
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_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
16 public:
17 virtual int init (int argc, ACE_TCHAR *argv[]);
19 private:
20 ACE_INET_Addr listen_addr_;
24 int
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)
35 return -1;
36 int option;
37 ACE_TCHAR config_file[MAXPATHLEN];
38 ACE_OS::strcpy (config_file, ACE_TEXT ("HAStatus.conf"));
39 while ((option = cmd_opts ()) != EOF)
40 switch (option) {
41 case 'f':
42 ACE_OS::strncpy (config_file,
43 cmd_opts.opt_arg (),
44 MAXPATHLEN);
45 break;
46 case ':':
47 ACE_ERROR_RETURN
48 ((LM_ERROR, ACE_TEXT ("-%c requires an argument\n"),
49 cmd_opts.opt_opt ()), -1);
50 default:
51 ACE_ERROR_RETURN
52 ((LM_ERROR, ACE_TEXT ("Parse error.\n")), -1);
54 // Listing 1
56 // Listing 2 code/ch04
57 ACE_Configuration_Heap config;
58 if (config.open () == -1)
59 ACE_ERROR_RETURN
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)
63 ACE_ERROR_RETURN
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")),
73 -1);
75 u_int status_port;
76 if (config.get_integer_value (status_section,
77 ACE_TEXT ("ListenPort"),
78 status_port) == -1)
79 ACE_ERROR_RETURN
80 ((LM_ERROR,
81 ACE_TEXT ("HAStatus ListenPort does not exist\n")),
82 -1);
83 this->listen_addr_.set (static_cast<u_short> (status_port));
84 // Listing 2
86 return 0;
89 int
90 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
92 HA_Status status;
93 status.init (argc, argv);
94 return 0;