Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / tests / Leader_Followers / server.cpp
blob10b2fa7e6dc0ba7601274f0c61507decd4866686
1 #include "ace/Get_Opt.h"
2 #include "ace/Task.h"
3 #include "test_i.h"
5 #include "tao/Strategies/advanced_resource.h"
7 const ACE_TCHAR *ior_output_file = ACE_TEXT("ior");
9 int number_of_event_loop_threads = 1;
11 int
12 parse_args (int argc, ACE_TCHAR *argv[])
14 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("e:o:"));
15 int c;
17 while ((c = get_opts ()) != -1)
18 switch (c)
20 case 'o':
21 ior_output_file = get_opts.opt_arg ();
22 break;
24 case 'e':
25 number_of_event_loop_threads = ACE_OS::atoi (get_opts.opt_arg ());
26 break;
28 case '?':
29 default:
30 ACE_ERROR_RETURN ((LM_ERROR,
31 "usage: %s "
32 "-o <iorfile> "
33 "-e <number of event loop threads> "
34 "\n",
35 argv [0]),
36 -1);
39 // Indicates successful parsing of the command line
40 return 0;
43 class Event_Loop_Task : public ACE_Task_Base
45 public:
46 Event_Loop_Task (CORBA::ORB_ptr orb)
47 : orb_ (CORBA::ORB::_duplicate (orb))
51 int svc ()
53 try
55 this->orb_->run ();
57 catch (const CORBA::Exception& ex)
59 ex._tao_print_exception ("Exception caught in thread:");
60 return -1;
64 return 0;
67 private:
68 CORBA::ORB_var orb_;
69 // ORB reference.
72 int
73 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
75 try
77 CORBA::ORB_var orb =
78 CORBA::ORB_init (argc,
79 argv);
81 CORBA::Object_var poa_object =
82 orb->resolve_initial_references ("RootPOA");
84 PortableServer::POA_var root_poa =
85 PortableServer::POA::_narrow (poa_object.in ());
87 PortableServer::POAManager_var poa_manager =
88 root_poa->the_POAManager ();
90 poa_manager->activate ();
92 if (parse_args (argc, argv) != 0)
93 return -1;
95 test_i servant (orb.in ());
97 PortableServer::ObjectId_var id =
98 root_poa->activate_object (&servant);
100 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
102 test_var server =
103 test::_narrow (object.in ());
105 CORBA::String_var ior =
106 orb->object_to_string (server.in ());
108 ACE_DEBUG ((LM_DEBUG, "Activated as <%C>\n", ior.in ()));
110 FILE *output_file = ACE_OS::fopen (ior_output_file, "w");
111 if (output_file == 0)
112 ACE_ERROR_RETURN ((LM_ERROR,
113 "Cannot open output file for writing IOR: %s",
114 ior_output_file),
115 -1);
116 ACE_OS::fprintf (output_file, "%s", ior.in ());
117 ACE_OS::fclose (output_file);
119 Event_Loop_Task event_loop_task (orb.in ());
121 if (event_loop_task.activate (THR_NEW_LWP | THR_JOINABLE,
122 number_of_event_loop_threads) != 0)
123 ACE_ERROR_RETURN ((LM_ERROR,
124 "Cannot activate event_loop threads\n"),
125 -1);
127 event_loop_task.thr_mgr ()->wait ();
129 ACE_DEBUG ((LM_DEBUG, "Server: Event loop finished\n"));
131 root_poa->destroy (true, true);
133 catch (const CORBA::Exception& ex)
135 ex._tao_print_exception ("Exception caught:");
136 return -1;
139 return 0;