Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Bug_3531_Regression / client.cpp
blob966d2157fb8c541dbeaa2c70b35acd2cb4c74bed
1 #include "ace/Get_Opt.h"
2 #include "ace/Task.h"
3 #include "testS.h"
4 #include "ace/OS_NS_unistd.h"
6 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
7 int do_shutdown = 0;
9 /**
10 * Simpler Server Client implementation
12 * Implements the Simple_Server interface in test.idl
14 class Simple_Server_i : public POA_Simple_Server
16 public:
17 Simple_Server_i (CORBA::ORB_ptr orb)
18 : orb_ (CORBA::ORB::_duplicate (orb))
19 {};
20 // ctor
22 // = The Simple_Server methods.
23 char *test_method (Simple_Server_ptr objref)
25 ACE_DEBUG ((LM_DEBUG, "Client test_method() - sleeping\n"));
26 ACE_OS::sleep (3);
27 if (!CORBA::is_nil (objref))
29 ACE_DEBUG ((LM_DEBUG, "Client test_method() - signalling server\n"));
30 objref->client_done ();
32 ACE_DEBUG ((LM_DEBUG, "Client test_method() - returning\n"));
33 return CORBA::string_dup ("hello from client");
36 virtual void client_done ()
38 ACE_ERROR ((LM_ERROR, "Unexpected call\n"));
41 void shutdown ()
45 private:
46 CORBA::ORB_var orb_;
47 // The ORB
51 int
52 parse_args (int argc, ACE_TCHAR *argv[])
54 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("xk:s:"));
55 int c;
57 while ((c = get_opts ()) != -1)
58 switch (c)
60 case 'x':
61 do_shutdown = 1;
62 break;
64 case 'k':
65 ior = get_opts.opt_arg ();
66 break;
68 case '?':
69 default:
70 ACE_ERROR_RETURN ((LM_ERROR,
71 "usage: %s "
72 "-k <ior> "
73 "[-x]"
74 "\n",
75 argv [0]),
76 -1);
78 // Indicates successful parsing of the command line
79 return 0;
82 /**
83 * Run a server thread
85 * Use the ACE_Task_Base class to run server threads
87 class Worker : public ACE_Task_Base
89 public:
90 Worker (CORBA::ORB_ptr orb);
91 // ctor
93 virtual int svc (void);
94 // The thread entry point.
96 private:
97 CORBA::ORB_var orb_;
98 // The orb
102 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
106 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
108 CORBA::Object_var poa_object =
109 orb->resolve_initial_references("RootPOA");
111 if (CORBA::is_nil (poa_object.in ()))
112 ACE_ERROR_RETURN ((LM_ERROR,
113 " (%P|%t) Unable to initialize the POA.\n"),
116 PortableServer::POA_var root_poa =
117 PortableServer::POA::_narrow (poa_object.in ());
119 PortableServer::POAManager_var poa_manager = root_poa->the_POAManager ();
121 if (parse_args (argc, argv) != 0)
122 return 1;
124 Simple_Server_i server_impl (orb.in ());
126 PortableServer::ObjectId_var id =
127 root_poa->activate_object (&server_impl);
129 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
131 Simple_Server_var server = Simple_Server::_narrow (object.in ());
133 CORBA::String_var local_ior = orb->object_to_string (server.in ());
134 ACE_DEBUG ((LM_DEBUG,
135 "Client interface started on <%C>\n",
136 local_ior.in ()));
137 CORBA::Object_var remote_object = orb->string_to_object (ior);
139 Simple_Server_var remote_server =
140 Simple_Server::_narrow (remote_object.in ());
142 if (CORBA::is_nil (remote_server.in ()))
144 ACE_ERROR_RETURN ((LM_ERROR,
145 "Object reference <%s> is nil\n",
146 ior), 1);
149 if (do_shutdown)
151 remote_server->shutdown ();
153 else
155 poa_manager->activate ();
156 Worker worker (orb.in ());
157 if (worker.activate (THR_NEW_LWP | THR_JOINABLE,
158 1) != 0)
159 ACE_ERROR_RETURN ((LM_ERROR,
160 "Cannot activate client thread\n"),
162 ACE_DEBUG ((LM_DEBUG, "ORB Thread started\n"));
163 CORBA::String_var str = remote_server->test_method (server.in ());
164 ACE_DEBUG ((LM_DEBUG, "Received \"%C\"\n", str.in ()));
165 orb->shutdown (true);
166 worker.thr_mgr ()->wait ();
167 ACE_DEBUG ((LM_DEBUG, "ORB Thread completed\n"));
169 orb->destroy ();
171 catch (const CORBA::Exception& ex)
173 ACE_DEBUG ((LM_ERROR, ACE_TEXT ("(%P|%t) Exception caught: \n%s\n"),
174 ACE_TEXT_CHAR_TO_TCHAR (ex._info ().c_str ())));
175 return 1;
178 return 0;
181 // ****************************************************************
183 Worker::Worker (CORBA::ORB_ptr orb)
184 : orb_ (CORBA::ORB::_duplicate (orb))
189 Worker::svc (void)
193 this->orb_->run ();
195 catch (const CORBA::Exception&)
198 return 0;