Merge pull request #1930 from jwillemsen/jwi-sslcert
[ACE_TAO.git] / TAO / tests / Leader_Followers / server.cpp
blob8416a589990cc63f19fb0b929c849e16c9b25f59
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 (void)
54 try
56 this->orb_->run ();
58 catch (const CORBA::Exception& ex)
60 ex._tao_print_exception ("Exception caught in thread:");
61 return -1;
65 return 0;
68 private:
69 CORBA::ORB_var orb_;
70 // ORB reference.
73 int
74 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
76 try
78 CORBA::ORB_var orb =
79 CORBA::ORB_init (argc,
80 argv);
82 CORBA::Object_var poa_object =
83 orb->resolve_initial_references ("RootPOA");
85 PortableServer::POA_var root_poa =
86 PortableServer::POA::_narrow (poa_object.in ());
88 PortableServer::POAManager_var poa_manager =
89 root_poa->the_POAManager ();
91 poa_manager->activate ();
93 if (parse_args (argc, argv) != 0)
94 return -1;
96 test_i servant (orb.in ());
98 PortableServer::ObjectId_var id =
99 root_poa->activate_object (&servant);
101 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
103 test_var server =
104 test::_narrow (object.in ());
106 CORBA::String_var ior =
107 orb->object_to_string (server.in ());
109 ACE_DEBUG ((LM_DEBUG, "Activated as <%C>\n", ior.in ()));
111 FILE *output_file = ACE_OS::fopen (ior_output_file, "w");
112 if (output_file == 0)
113 ACE_ERROR_RETURN ((LM_ERROR,
114 "Cannot open output file for writing IOR: %s",
115 ior_output_file),
116 -1);
117 ACE_OS::fprintf (output_file, "%s", ior.in ());
118 ACE_OS::fclose (output_file);
120 Event_Loop_Task event_loop_task (orb.in ());
122 if (event_loop_task.activate (THR_NEW_LWP | THR_JOINABLE,
123 number_of_event_loop_threads) != 0)
124 ACE_ERROR_RETURN ((LM_ERROR,
125 "Cannot activate event_loop threads\n"),
126 -1);
128 event_loop_task.thr_mgr ()->wait ();
130 ACE_DEBUG ((LM_DEBUG, "Server: Event loop finished\n"));
132 root_poa->destroy (1,
135 catch (const CORBA::Exception& ex)
137 ex._tao_print_exception ("Exception caught:");
138 return -1;
141 return 0;