Merge pull request #2222 from jwillemsen/jwi-dllexportwarning
[ACE_TAO.git] / TAO / DevGuideExamples / RTCORBA / MessengerServer.cpp
blobb51422064477fbb8a910d958072043cab01160f0
1 #include "Messenger_i.h"
2 #include "common.h"
3 #include <iostream>
4 #include <fstream>
5 #include <fstream>
6 #include "tao/RTCORBA/RTCORBA.h"
7 #include "ace/Get_Opt.h"
9 const ACE_TCHAR *ior_output_file = ACE_TEXT ("Messenger.ior");
11 int
12 parse_args (int argc, ACE_TCHAR *argv[])
14 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
15 int c;
17 while ((c = get_opts ()) != -1)
18 switch (c)
20 case 'o':
21 ior_output_file = get_opts.opt_arg ();
22 break;
24 case '?':
25 default:
26 ACE_ERROR_RETURN ((LM_ERROR,
27 "usage: %s "
28 "-o <iorfile>"
29 "\n",
30 argv [0]),
31 -1);
33 // Indicates successful parsing of the command line
34 return 0;
37 int
38 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
40 try {
41 // Initialize orb
42 CORBA::ORB_var orb = CORBA::ORB_init( argc, argv );
44 if (parse_args (argc, argv) != 0)
45 return 1;
47 // Get the RTORB.
48 CORBA::Object_var obj = orb->resolve_initial_references("RTORB");
49 RTCORBA::RTORB_var rt_orb = RTCORBA::RTORB::_narrow(obj.in());
51 // PolicyCurrent.
52 obj = orb->resolve_initial_references("PolicyCurrent");
53 CORBA::PolicyCurrent_var policy_current =
54 CORBA::PolicyCurrent::_narrow(obj.in());
55 if (CORBA::is_nil(policy_current.in())) {
56 std::cerr << "Unable to narrow the PolicyCurrent" << std::endl;
57 return 1;
60 //Get reference to Root POA
61 obj = orb->resolve_initial_references( "RootPOA" );
62 PortableServer::POA_var poa = PortableServer::POA::_narrow( obj.in() );
64 // Activate POA Manager
65 PortableServer::POAManager_var mgr = poa->the_POAManager();
66 mgr->activate();
68 // Create the thread-pool
69 const CORBA::Short increment = get_increment();
70 RTCORBA::ThreadpoolLanes lanes(get_total_lanes());
71 lanes.length(get_total_lanes());
72 std::cout << "Creating " << get_total_lanes() << " lane"
73 << (get_total_lanes() == 1 ? "" : "s") << std::endl;
74 for(CORBA::ULong i = 0; i < get_total_lanes(); i++) {
75 lanes[i].static_threads = 1;
76 lanes[i].dynamic_threads = 0;
78 lanes[i].lane_priority = i * increment;
79 std::cout << " Priority: " << lanes[i].lane_priority << std::endl;
82 RTCORBA::ThreadpoolId threadpool_id =
83 rt_orb->create_threadpool_with_lanes (0, // Stack Size
84 lanes,
85 false, // Allow borrowing
86 false, // Allow request buffering
87 0, // Max buffered requests
88 0); // Max request buffer size
90 CORBA::PolicyList poa_policy_list(2);
91 poa_policy_list.length (2);
93 poa_policy_list[0] =
94 rt_orb->create_priority_model_policy(RTCORBA::CLIENT_PROPAGATED, 0);
95 poa_policy_list[1] =
96 rt_orb->create_threadpool_policy(threadpool_id);
98 PortableServer::POA_var client_propagated_poa =
99 poa->create_POA ("client_propagated_poa",
100 mgr.in (),
101 poa_policy_list);
103 // Create an object
104 PortableServer::Servant_var<Messenger_i> messenger_servant
105 = new Messenger_i(orb.in());
107 // Register the servant with the RootPOA, obtain its object
108 // reference, stringify it, and write it to a file.
109 PortableServer::ObjectId_var oid =
110 client_propagated_poa->activate_object( messenger_servant.in() );
111 CORBA::Object_var messenger_obj =
112 client_propagated_poa->id_to_reference( oid.in() );
113 CORBA::String_var str = orb->object_to_string( messenger_obj.in() );
114 std::ofstream iorFile(ACE_TEXT_ALWAYS_CHAR(ior_output_file));
115 iorFile << str.in() << std::endl;
116 iorFile.close();
117 std::cout << "IOR written to file " << ACE_TEXT_ALWAYS_CHAR(ior_output_file) << std::endl;
119 // Accept requests
120 orb->run();
121 orb->destroy();
123 catch(const CORBA::Exception& ex) {
124 std::cerr << "Caught CORBA exception: " << ex << std::endl;
125 return 1;
128 return 0;