Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / tests / Collocated_Forwarding / server.cpp
bloba2c2c2425fafb1efdf038f57531d16c597273635
1 // -*- C++ -*-
2 #include "ace/Get_Opt.h"
3 #include "test_i.h"
4 #include "ace/OS_NS_stdio.h"
6 #if TAO_HAS_INTERCEPTORS
8 #include "Server_ORBInitializer.h"
9 #include "Server_Request_Interceptor.h"
10 #include "tao/IORManipulation/IORManipulation.h"
11 #include "tao/ORBInitializer_Registry.h"
13 const CORBA::ULong passes_before_forward = 6;
15 bool direct_collocation = false;
17 int
18 parse_args (int argc, ACE_TCHAR *argv[])
20 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("d"));
21 int c;
23 while ((c = get_opts ()) != -1)
24 switch (c)
26 case 'd':
27 direct_collocation = true;
28 break;
29 default:
30 ACE_ERROR_RETURN ((LM_ERROR,
31 "Usage: %s "
32 "-d\n",
33 argv[0]),
34 -1);
37 return 0;
40 void test_colocal (Collocated_ForwardRequestTest::test_ptr server)
42 CORBA::ULong number = 0;
43 for (int i = 1; i <= 10; ++i)
45 ACE_DEBUG ((LM_INFO,
46 "CLIENT: Issuing colocal request %d.\n",
47 i));
48 CORBA::ULong retval =
49 server->collocated_call ();
51 number += retval;
54 if (number != 15)
56 ACE_ERROR ((LM_ERROR,
57 "(%P|%t) ERROR: Did not forward to new location\n"));
58 ACE_OS::abort ();
62 int
63 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
65 try
67 Server_ORBInitializer *temp_initializer = 0;
68 ACE_NEW_RETURN (temp_initializer,
69 Server_ORBInitializer (passes_before_forward),
70 -1); // No exceptions yet!
71 PortableInterceptor::ORBInitializer_var orb_initializer =
72 temp_initializer;
74 PortableInterceptor::register_orb_initializer (orb_initializer.in ());
76 CORBA::ORB_var orb =
77 CORBA::ORB_init (argc, argv, "Server ORB");
79 if (::parse_args (argc, argv) != 0)
80 return -1;
82 CORBA::Object_var poa_object =
83 orb->resolve_initial_references ("RootPOA");
85 if (CORBA::is_nil (poa_object.in ()))
86 ACE_ERROR_RETURN ((LM_ERROR,
87 " (%P|%t) Unable to initialize the POA.\n"),
88 1);
90 PortableServer::POA_var root_poa =
91 PortableServer::POA::_narrow (poa_object.in ());
93 PortableServer::POAManager_var poa_manager =
94 root_poa->the_POAManager ();
96 test_i servant1 (1, orb.in ());
97 test_i servant2 (2, orb.in ());
99 PortableServer::ObjectId_var oid1 =
100 root_poa->activate_object (&servant1);
102 PortableServer::ObjectId_var oid2 =
103 root_poa->activate_object (&servant2);
105 CORBA::Object_var obj1 =
106 root_poa->servant_to_reference (&servant1);
108 CORBA::Object_var obj2 =
109 root_poa->servant_to_reference (&servant2);
111 poa_manager->activate ();
113 // Set the forward references in the server request interceptor.
114 PortableInterceptor::ServerRequestInterceptor_var
115 server_interceptor = temp_initializer->server_interceptor ();
117 Collocated_ForwardRequestTest::ServerRequestInterceptor_var interceptor =
118 Collocated_ForwardRequestTest::ServerRequestInterceptor::_narrow (
119 server_interceptor.in ());
121 if (CORBA::is_nil (interceptor.in ()))
122 ACE_ERROR_RETURN ((LM_ERROR,
123 "(%P|%t) Could not obtain reference to "
124 "server request interceptor.\n"),
125 -1);
127 if (direct_collocation)
129 servant1.forward (obj2.in (), passes_before_forward);
131 else
133 interceptor->forward (obj2.in ());
136 // Run co-local test
138 Collocated_ForwardRequestTest::test_var server =
139 Collocated_ForwardRequestTest::test::_narrow (obj1.in ());
140 test_colocal (server.in ());
143 root_poa->destroy (true, true);
145 orb->destroy ();
147 ACE_DEBUG ((LM_DEBUG, "Event loop finished.\n"));
149 catch (const CORBA::Exception& ex)
151 ex._tao_print_exception ("Caught exception:");
152 return -1;
155 return 0;
158 #else
161 ACE_TMAIN(int, ACE_TCHAR *[])
163 return 0;
166 #endif /* TAO_HAS_INTERCEPTORS */