Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Bug_3853_Regression / server.cpp
blob4d8dbec41a12a97c248e9b49588cb8dea86dabaa
1 #include "Hello_i.h"
2 #include "HelloC.h"
4 #include "ace/Task.h"
5 #include "ace/Get_Opt.h"
6 #include <fstream>
7 #include <iostream>
9 const ACE_TCHAR *ior_output_file = ACE_TEXT("");
10 const ACE_TCHAR *shutdown_ior_output_file = ACE_TEXT("");
12 int
13 parse_args (int argc, ACE_TCHAR *argv[])
15 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:s:"));
16 int c;
18 while ((c = get_opts ()) != -1)
19 switch (c)
21 case 'o':
22 ior_output_file = get_opts.opt_arg ();
23 break;
24 case 's':
25 shutdown_ior_output_file = get_opts.opt_arg ();
26 break;
27 case '?':
28 default:
29 ACE_ERROR_RETURN ((LM_ERROR,
30 "usage: %s "
31 "-o <iorfile>"
32 "-s <shutdown_iorfile>"
33 "\n",
34 argv [0]),
35 -1);
38 // Indicates successful parsing of the command line
39 return 0;
43 int
44 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
46 try
48 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
50 if (parse_args (argc, argv) != 0)
51 return 1;
53 CORBA::Object_var poa_object = orb->resolve_initial_references ("RootPOA");
54 PortableServer::POA_var rootPOA = PortableServer::POA::_narrow (poa_object.in ());
55 PortableServer::POAManager_var poa_manager = rootPOA->the_POAManager ();
57 // Policies for the firstPOA to be created.
58 CORBA::PolicyList policies (5);
59 policies.length (5);
60 // Lifespan policy
61 policies[0] =
62 rootPOA->create_lifespan_policy (PortableServer::PERSISTENT );
64 // Servant Retention Policy
65 policies[1] =
66 rootPOA->create_servant_retention_policy (PortableServer::RETAIN );
68 // ID Assignment Policy
69 policies[2] =
70 rootPOA->create_id_assignment_policy (PortableServer::USER_ID );
72 // Request Processing Policy
73 policies[3] =
74 rootPOA->create_request_processing_policy (PortableServer::USE_ACTIVE_OBJECT_MAP_ONLY);
76 // Threading policy
77 policies[4] =
78 rootPOA->create_thread_policy (PortableServer::ORB_CTRL_MODEL);
80 PortableServer::POA_var demoPOA
81 = rootPOA->create_POA ("HelloWorldServer",
82 poa_manager.in (),
83 policies);
85 for (CORBA::ULong i = 0;
86 i < policies.length ();
87 ++i)
89 CORBA::Policy_ptr policy = policies[i];
90 policy->destroy ();
93 // Create object for shutdown commanded by client.
95 // create the object
96 Demo_HelloWorld_i * hello = new Demo_HelloWorld_i(orb.in());
98 // Get the Object ID.
99 PortableServer::ObjectId_var oid = PortableServer::string_to_ObjectId ("shutdown");
100 demoPOA->activate_object_with_id (oid.in (), hello);
103 // Create an object reference.
104 CORBA::Object_var myhello = demoPOA->id_to_reference(oid.in());
106 // Put the object reference as an IOR string
107 ofstream out(ACE_TEXT_ALWAYS_CHAR (shutdown_ior_output_file));
108 CORBA::String_var ior = orb->object_to_string (myhello.in ());
109 out << ior.in();
110 // save the reference into a file
111 out.close();
114 // Create object to handle client sayHello requests.
116 // create the object
117 Demo_HelloWorld_i * hello = new Demo_HelloWorld_i(orb.in());
119 // Get the Object ID.
120 PortableServer::ObjectId_var oid = PortableServer::string_to_ObjectId ("hello");
121 demoPOA->activate_object_with_id (oid.in (), hello);
123 // Create an object reference.
124 CORBA::Object_var myhello = demoPOA->id_to_reference(oid.in());
126 // Put the object reference as an IOR string
127 ofstream out(ACE_TEXT_ALWAYS_CHAR (ior_output_file));
128 CORBA::String_var ior = orb->object_to_string (myhello.in ());
129 out << ior.in();
130 // save the reference into a file
131 out.close();
134 //////////////////////////////////////////////////////////////////////////////////////////////////
135 poa_manager->activate ();
137 std::cout << ior_output_file << " is ready " << std::endl;
138 orb->run ();
140 // Destroy the POA, waiting until the destruction terminates
141 rootPOA->destroy (1, 1);
142 orb->destroy ();
144 catch (const CORBA::Exception &e)
146 std::cerr << "Unexpected exception: " << e << std::endl;
147 return 1;
150 return 0;