Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / tests / MT_BiDir / client.cpp
blobd3840afb3849325182141d38c36494dbbc5a317f
1 #include "ace/Get_Opt.h"
2 #include "Receiver_i.h"
3 #include "SenderC.h"
4 #include "tao/BiDir_GIOP/BiDirGIOP.h"
5 #include "tao/AnyTypeCode/Any.h"
6 #include "Client_Task.h"
8 const ACE_TCHAR *ior = ACE_TEXT("file://test.ior");
10 int
11 parse_args (int argc, ACE_TCHAR *argv[])
13 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("k:"));
14 int c;
16 while ((c = get_opts ()) != -1)
17 switch (c)
19 case 'k':
20 ior = get_opts.optarg;
21 break;
22 case '?':
23 default:
24 ACE_ERROR_RETURN ((LM_ERROR,
25 "usage: %s "
26 "-k <ior> "
27 "\n",
28 argv [0]),
29 -1);
31 // Indicates successful parsing of the command line
32 return 0;
35 int
36 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
38 try
40 CORBA::ORB_var orb =
41 CORBA::ORB_init (argc, argv);
43 if (parse_args (argc, argv) != 0)
44 return 1;
46 CORBA::Object_var poa_object =
47 orb->resolve_initial_references ("RootPOA");
49 if (CORBA::is_nil (poa_object.in ()))
50 ACE_ERROR_RETURN ((LM_ERROR,
51 " (%P|%t) Unable to initialize the POA.\n"),
52 1);
54 PortableServer::POA_var root_poa =
55 PortableServer::POA::_narrow (poa_object.in ());
57 PortableServer::POAManager_var poa_manager =
58 root_poa->the_POAManager ();
60 // Policies for the childPOA to be created.
61 CORBA::PolicyList policies (1);
62 policies.length (1);
64 CORBA::Any pol;
65 pol <<= BiDirPolicy::BOTH;
66 policies[0] =
67 orb->create_policy (BiDirPolicy::BIDIRECTIONAL_POLICY_TYPE,
68 pol);
70 // Create POA as child of RootPOA with the above policies. This POA
71 // will receive request in the same connection in which it sent
72 // the request
73 PortableServer::POA_var child_poa =
74 root_poa->create_POA ("childPOA",
75 poa_manager.in (),
76 policies);
78 // Creation of childPOA is over. Destroy the Policy objects.
79 for (CORBA::ULong i = 0;
80 i < policies.length ();
81 ++i)
83 policies[i]->destroy ();
86 poa_manager->activate ();
88 CORBA::Object_var object =
89 orb->string_to_object (ior);
91 Sender_var sender =
92 Sender::_narrow (object.in ());
94 if (CORBA::is_nil (sender.in ()))
96 ACE_ERROR_RETURN ((LM_ERROR,
97 "Object reference <%s> is nil.\n",
98 ior),
99 1);
103 Receiver_i *receiver;
104 ACE_NEW_RETURN (receiver,
105 Receiver_i (),
106 -1);
108 PortableServer::ServantBase_var owner_transfer (receiver);
110 PortableServer::ObjectId_var id =
111 root_poa->activate_object (receiver);
113 CORBA::Object_var object_act = root_poa->id_to_reference (id.in ());
115 Receiver_var receiver_obj =
116 Receiver::_narrow (object_act.in ());
118 // Send the callback object to the server
119 sender->receiver_object (receiver_obj.in ());
121 // Threads that will handle the call backs
122 Client_Task client_task (orb.in (),
123 ACE_Thread_Manager::instance ());
125 if (client_task.activate (THR_NEW_LWP | THR_JOINABLE, 4, 1) == -1)
127 ACE_ERROR ((LM_ERROR, "Error activating client task\n"));
130 ACE_Thread_Manager::instance ()->wait ();
132 CORBA::Long count =
133 receiver->get_event_count ();
135 ACE_DEBUG ((LM_DEBUG,
136 "(%P|%t) Number of events received.. [%d]\n",
137 count));
139 root_poa->destroy (true, true);
141 catch (const CORBA::Exception& ex)
143 ex._tao_print_exception ("Caught exception:");
144 return 1;
147 return 0;