=default for generated implementation copy ctor
[ACE_TAO.git] / TAO / tests / BiDirectional / server.cpp
blob304b57deed6b0a68123298d65e2b16200cb75786
1 #include "ace/Get_Opt.h"
2 #include "test_i.h"
3 #include "tao/BiDir_GIOP/BiDirGIOP.h"
4 #include "tao/AnyTypeCode/Any.h"
5 #include "ace/OS_NS_stdio.h"
7 const ACE_TCHAR *ior_output_file = 0;
8 int no_iterations = 10;
10 int
11 parse_args (int argc, ACE_TCHAR *argv[])
13 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:i:"));
14 int c;
16 while ((c = get_opts ()) != -1)
17 switch (c)
19 case 'o':
20 ior_output_file = get_opts.opt_arg ();
21 break;
22 case 'i':
23 no_iterations = ACE_OS::atoi (get_opts.opt_arg ());
24 break;
25 case '?':
26 default:
27 ACE_ERROR_RETURN ((LM_ERROR,
28 "usage: %s "
29 "-o <iorfile>"
30 "-i <no_iterations>"
31 "\n",
32 argv [0]),
33 -1);
35 // Indicates successful parsing of the command line
36 return 0;
39 int
40 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
42 try
44 CORBA::ORB_var orb =
45 CORBA::ORB_init (argc, argv);
47 CORBA::Object_var poa_object =
48 orb->resolve_initial_references ("RootPOA");
50 if (CORBA::is_nil (poa_object.in ()))
51 ACE_ERROR_RETURN ((LM_ERROR,
52 " (%P|%t) Unable to initialize the POA.\n"),
53 1);
55 PortableServer::POA_var root_poa =
56 PortableServer::POA::_narrow (poa_object.in ());
58 PortableServer::POAManager_var poa_manager =
59 root_poa->the_POAManager ();
61 // Policies for the childPOA to be created.
62 CORBA::PolicyList policies (1);
63 policies.length (1);
65 CORBA::Any pol;
66 pol <<= BiDirPolicy::BOTH;
67 policies[0] =
68 orb->create_policy (BiDirPolicy::BIDIRECTIONAL_POLICY_TYPE,
69 pol);
71 // Create POA as child of RootPOA with the above policies. This POA
72 // will receive request in the same connection in which it sent
73 // the request
74 PortableServer::POA_var child_poa =
75 root_poa->create_POA ("childPOA",
76 poa_manager.in (),
77 policies);
79 // Creation of childPOA is over. Destroy the Policy objects.
80 for (CORBA::ULong i = 0;
81 i < policies.length ();
82 ++i)
84 policies[i]->destroy ();
87 poa_manager->activate ();
89 if (parse_args (argc, argv) != 0)
90 return 1;
92 Simple_Server_i server_impl (orb.in (),
93 no_iterations);
95 PortableServer::ObjectId_var id =
96 PortableServer::string_to_ObjectId ("simple_server");
98 child_poa->activate_object_with_id (id.in (),
99 &server_impl);
101 CORBA::Object_var obj =
102 child_poa->id_to_reference (id.in ());
104 CORBA::String_var ior =
105 orb->object_to_string (obj.in ());
107 ACE_DEBUG ((LM_DEBUG, "Activated as <%C>\n", ior.in ()));
109 // If the ior_output_file exists, output the ior to it
110 if (ior_output_file != 0)
112 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
113 if (output_file == 0)
114 ACE_ERROR_RETURN ((LM_ERROR,
115 "Cannot open output file for writing IOR: %s",
116 ior_output_file),
118 ACE_OS::fprintf (output_file, "%s", ior.in ());
119 ACE_OS::fclose (output_file);
122 int retval = 0;
123 while (retval == 0)
125 // Just process one upcall. We know that we would get the
126 // clients IOR in that call.
127 CORBA::Boolean pending =
128 orb->work_pending();
130 if (pending)
132 orb->perform_work();
135 // Now that hopefully we have the clients IOR, just start
136 // making remote calls to the client.
137 retval = server_impl.call_client ();
139 ACE_DEBUG ((LM_DEBUG, "event loop finished\n"));
141 root_poa->destroy (true, true);
143 catch (const CORBA::Exception& ex)
145 ex._tao_print_exception ("Caught exception:");
146 return 1;
149 return 0;