Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / examples / Load_Balancing / Load_Balancing_Service.cpp
blob0cb1fc165bac046aba1e57a3ffc283ba90b20ca4
1 //=============================================================================
2 /**
3 * @file Load_Balancing_Service.cpp
5 * @author Marina Spivak <marina@cs.wustl.edu>
6 */
7 //=============================================================================
10 #include "Load_Balancing_Service.h"
11 #include "Load_Balancer_i.h"
12 #include "tao/debug.h"
13 #include "ace/Get_Opt.h"
14 #include "ace/OS_NS_stdio.h"
16 Load_Balancing_Service::Load_Balancing_Service (void)
17 : ior_output_file_ (0)
21 int
22 Load_Balancing_Service::parse_args (int argc, ACE_TCHAR *argv[])
24 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("do:"));
25 int c;
27 while ((c = get_opts ()) != -1)
28 switch (c)
30 case 'd': // debug flag.
31 TAO_debug_level++;
32 break;
33 case 'o': // outputs object ior to the specified file.
34 this->ior_output_file_ =
35 ACE_OS::fopen (get_opts.opt_arg (), "w");
37 if (this->ior_output_file_ == 0)
38 ACE_ERROR_RETURN ((LM_ERROR,
39 "Unable to open %s for writing: %p\n",
40 get_opts.opt_arg ()), -1);
41 break;
42 case '?':
43 default:
44 ACE_ERROR_RETURN ((LM_ERROR,
45 "usage: %s"
46 " [-d]"
47 " [-o] <ior_output_file>"
48 "\n",
49 argv [0]),
50 -1);
53 // Indicates successful parsing of command line.
54 return 0;
57 int
58 Load_Balancing_Service::init (int argc,
59 ACE_TCHAR* argv[])
61 int result;
62 CORBA::String_var ior;
64 try
66 result = this->orb_manager_.init (argc,
67 argv);
68 if (result == -1)
69 return result;
71 // Check the non-ORB arguments.
72 result = this->parse_args (argc, argv);
73 if (result < 0)
74 return result;
76 // Create, ref. count, and activate the servant.
77 Object_Group_Factory_i * factory_servant;
78 ACE_NEW_RETURN (factory_servant,
79 Object_Group_Factory_i (),
80 -1);
81 PortableServer::ServantBase_var s = factory_servant;
82 ior = orb_manager_.activate (factory_servant);
84 if (ior.in () == 0)
85 return -1;
86 else if (TAO_debug_level > 1)
87 ACE_DEBUG ((LM_DEBUG,
88 "Load_Balancer: Object Group Factory ior is %C\n",
89 ior.in ()));
91 catch (const CORBA::Exception& ex)
93 ex._tao_print_exception ("Load_Balancing_Service::init");
94 return -1;
97 if (this->ior_output_file_ != 0)
99 ACE_OS::fprintf (this->ior_output_file_,
100 "%s",
101 ior.in ());
102 ACE_OS::fclose (this->ior_output_file_);
104 return 0;
110 Load_Balancing_Service::run (void)
112 ACE_DEBUG ((LM_DEBUG,
113 "Load_Balancer: Initialized\n"));
115 int result;
117 result = this->orb_manager_.run ();
119 return result;
122 Load_Balancing_Service::~Load_Balancing_Service (void)
127 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
129 int result = 0;
130 Load_Balancing_Service factory;
132 if (factory.init (argc, argv) == -1)
133 return 1;
137 result = factory.run ();
139 catch (const CORBA::Exception& ex)
141 ex._tao_print_exception ("Load_Balancing_Service");
142 return 1;
145 if (result == -1)
146 return 1;
147 else
148 return 0;