Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / DevGuideExamples / BiDirectionalGIOP / server.cpp
blob529d1feff1e369d272a491f5ed511ccf5125b0bb
1 #include "bidir_giop_pch.h"
3 #include "simple_i.h"
4 #include "callbackC.h"
6 #include "ace/Get_Opt.h"
7 #include "tao/BiDir_GIOP/BiDirGIOP.h"
8 #include <iostream>
9 #include <fstream>
11 ACE_TString ior_output_file;
12 int callback_count = 10;
14 int
15 parse_args(int argc, ACE_TCHAR *argv[])
17 ACE_Get_Opt get_opts(argc, argv, ACE_TEXT("o:i:"));
18 int c;
20 while((c = get_opts()) != -1)
21 switch(c)
23 case 'o':
24 ior_output_file = get_opts.optarg;
25 break;
26 case 'i':
27 callback_count = ACE_OS::atoi(get_opts.optarg);
28 break;
29 case '?':
30 default:
31 std::cerr << "usage: " << argv[0] << "-o <iorfile> -i <no_iterations>" << std::endl;
32 return -1;
33 break;
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 = CORBA::ORB_init(argc, argv);
46 if (parse_args(argc, argv) != 0) {
47 return 1;
50 // Create a bidirectional POA
51 CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
52 PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj.in());
53 PortableServer::POAManager_var poa_manager = root_poa->the_POAManager();
54 // Policies for the childPOA to be created.
55 CORBA::PolicyList policies(1);
56 policies.length(1);
57 CORBA::Any pol;
58 pol <<= BiDirPolicy::BOTH;
59 policies[0] =
60 orb->create_policy(BiDirPolicy::BIDIRECTIONAL_POLICY_TYPE, pol);
61 // Create POA as child of RootPOA with the above policies. This POA
62 // will receive request in the same connection in which it sent
63 // the request
64 PortableServer::POA_var poa = root_poa->create_POA("bidirPOA", poa_manager.in(), policies);
65 // Creation of bidirPOA is over. Destroy the Policy objects.
66 for (CORBA::ULong i = 0; i < policies.length (); ++i) {
67 policies[i]->destroy ();
69 poa_manager->activate ();
71 PortableServer::Servant_var<Simple_i> svt = new Simple_i(orb.in(), callback_count);
73 // Register and activate Simple servant
74 PortableServer::ObjectId_var id = poa->activate_object(svt.in());
75 obj = poa->id_to_reference(id.in());
76 Simple_var server = Simple::_narrow(obj.in());
78 CORBA::String_var ior = orb->object_to_string(server.in());
79 if (ior_output_file != ACE_TEXT("")) {
80 std::ofstream outfile(ACE_TEXT_ALWAYS_CHAR(ior_output_file.c_str()));
81 outfile << ior.in();
83 std::cout << "Activated as " << ior.in() << std::endl;
85 // Our own special orb->run() that knows how to callback clients
86 while (true) {
87 // returns 1 as soon as it has successfully called back.
88 if (svt->call_client()) {
89 break;
92 // We don't want to check for work pending, because we really want
93 // to simulate a normal orb->run() while adding the ability to call
94 // our routine which calls back to the client.
95 orb->perform_work();
98 std::cout << "Event loop finished." << std::endl;
100 CORBA::Boolean etherealize = true, wait = true;
101 poa->destroy(etherealize, wait);
102 orb->destroy();
104 return 0;
106 catch(const CORBA::Exception& ex) {
107 std::cerr << "Caught CORBA::Exception: " << std::endl << ex << std::endl;
110 return 1;