Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / tests / MT_NoUpcall_Client_Leader / server.cpp
blob464e8c2494b6d673ebbb1a9203c1754f6d8a7aba
1 #include "SharedIntf_i.h"
2 #include "worker.h"
3 #include "chatter.h"
4 #include "police.h"
6 #include "ace/SString.h"
7 #include "ace/Get_Opt.h"
9 const ACE_TCHAR *ior_output_file = ACE_TEXT ("server.ior");
10 int nr_threads = 1;
12 int
13 parse_args (int argc, ACE_TCHAR *argv[])
15 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("t:"));
16 int c;
18 while ((c = get_opts ()) != -1)
19 switch (c)
21 case 't':
22 nr_threads = ACE_OS::atoi(get_opts.opt_arg ());
23 break;
25 case '?':
26 default:
27 ACE_ERROR_RETURN ((LM_ERROR,
28 "usage: %s "
29 "-t threads "
30 "\n",
31 argv [0]),
32 -1);
34 // Indicates successful parsing of the command line
35 return 0;
38 int
39 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
41 CORBA::ORB_var orb_;
42 int result = 0;
44 try
46 ACE_DEBUG((LM_INFO,"(%P|%t) START OF SERVER TEST\n"));
48 orb_ = CORBA::ORB_init (argc, argv, "myorb-server");
50 if (parse_args (argc, argv) != 0)
51 return 1;
53 CORBA::Object_var poa_object =
54 orb_->resolve_initial_references("RootPOA");
56 PortableServer::POA_var root_poa =
57 PortableServer::POA::_narrow (poa_object.in());
59 PortableServer::POAManager_var poa_manager =
60 root_poa->the_POAManager ();
61 PortableServer::POA_var poa = root_poa;
63 poa_manager->activate ();
65 ACE_DEBUG((LM_INFO,"(%P|%t) ORB initialized\n"));
67 // Creating the servant and activating it
69 Test_Idl_SharedIntf_i* intf_i = new Test_Idl_SharedIntf_i(orb_.in());
71 PortableServer::ServantBase_var base_var = intf_i;
72 PortableServer::ObjectId_var intfId_var =
73 poa->activate_object(base_var.in());
75 CORBA::Object_var obj_var =
76 poa->id_to_reference(intfId_var.in());
78 Test_Idl::SharedIntf_var intf_var =
79 Test_Idl::SharedIntf::_narrow(obj_var.in());
81 // Creating stringified IOR of the servant and writing it to a file.
83 CORBA::String_var intfString_var =
84 orb_->object_to_string(intf_var.in());
86 // Output the IOR to the <ior_output_file>
87 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
88 if (output_file == 0)
89 ACE_ERROR_RETURN ((LM_ERROR,
90 "Cannot open output file for writing IOR: %s\n",
91 ior_output_file),
92 1);
93 ACE_OS::fprintf (output_file, "%s", intfString_var.in ());
94 ACE_OS::fclose (output_file);
96 ACE_DEBUG((LM_INFO,"(%P|%t) server IOR to %s\n",
97 ior_output_file));
99 // Running ORB in separate thread
100 Worker worker (orb_.in ());
101 if (worker.activate (THR_NEW_LWP | THR_JOINABLE, nr_threads) != 0)
102 ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) %p\n", "Cannot activate server thread(s)"), -1);
104 ACE_DEBUG((LM_INFO,"(%P|%t) Await client initialization\n"));
105 poll ("./client.ior");
106 ACE_DEBUG((LM_INFO,"(%P|%t) Client IOR file was detected\n"));
108 ACE_Mutex mutex;
109 ACE_Condition<ACE_Mutex> stop_condition (mutex);
111 ACE_GUARD_RETURN (ACE_Mutex, guard, mutex, -1);
113 Chatter worker2 (orb_.in (), ACE_TEXT("file://client.ior"), stop_condition);
114 if (worker2.activate (THR_NEW_LWP | THR_JOINABLE, 1) != 0)
116 ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) %p\n", "Cannot activate chatty client threads"), -1);
119 do {
120 stop_condition.wait ();
121 ACE_DEBUG((LM_INFO,"(%P|%t) So far, %d/%d requests/replies have been processed\n",
122 worker2.nrequests (), worker2.nreplies ()));
123 } while (worker2.nrequests () < 1);
125 worker.thr_mgr()->wait ();
127 root_poa->destroy(1, 1);
129 orb_->destroy();
131 ACE_DEBUG((LM_INFO,"(%P|%t) Server Test %C\n",
132 (worker2.nrequests() == worker2.nreplies())?"succeeded":"failed"));
133 result = (worker2.nrequests() == worker2.nreplies())? 0 : -1;
136 catch (const CORBA::Exception& ex)
138 ex._tao_print_exception ("Error: Exception caught:");
141 ACE_OS::unlink ("server.ior");
142 return result;