Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / orbsvcs / examples / ORT / gateway_server.cpp
blob49d07e9d9deb1f6b4b220273c93e3f71d1042766
1 #include "Object_Factory_i.h"
2 #include "Gateway_i.h"
4 #include "ace/Get_Opt.h"
5 #include "ace/OS_NS_stdio.h"
7 const ACE_TCHAR *ior_output_file = 0;
9 int
10 parse_args (int argc, ACE_TCHAR *argv[])
12 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
13 int c;
15 while ((c = get_opts ()) != -1)
16 switch (c)
18 case 'o':
19 ior_output_file = get_opts.optarg;
20 break;
21 default:
22 ACE_ERROR_RETURN ((LM_ERROR,
23 "Usage: %s "
24 "-o <iorfile>"
25 "\n",
26 argv[0]),
27 -1);
30 // Indicates successful parsing of the command line
31 return 0;
34 int
35 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
37 try
39 /// Initialize the ORB.
40 CORBA::ORB_var orb = CORBA::ORB_init (argc,
41 argv,
42 "gateway_server_orb");
44 if (parse_args (argc, argv) != 0)
45 return -1;
47 /// Resolve reference to RootPOA
48 CORBA::Object_var obj =
49 orb->resolve_initial_references ("RootPOA");
51 /// Narrow it down correctly.
52 PortableServer::POA_var root_poa =
53 PortableServer::POA::_narrow (obj.in ());
55 /// Check for nil references
56 if (CORBA::is_nil (root_poa.in ()))
57 ACE_ERROR_RETURN ((LM_ERROR,
58 "Unable to obtain RootPOA reference.\n"),
59 -1);
61 /// Get poa_manager reference
62 PortableServer::POAManager_var poa_manager =
63 root_poa->the_POAManager ();
65 /// Activate it.
66 poa_manager->activate ();
68 ///@}
70 CORBA::PolicyList policies (3);
71 policies.length (3);
73 policies [0] =
74 root_poa->create_servant_retention_policy (PortableServer::RETAIN);
76 policies [1] =
77 root_poa->create_request_processing_policy (PortableServer::USE_DEFAULT_SERVANT);
80 policies [2] =
81 root_poa->create_id_uniqueness_policy (PortableServer::MULTIPLE_ID);
83 PortableServer::POA_var gateway_poa =
84 root_poa->create_POA ("Gateway_POA",
85 poa_manager.in (),
86 policies);
88 for (CORBA::ULong i = 0; i != policies.length (); ++i) {
89 policies[i]->destroy ();
92 // Get the POA Current object reference
93 obj =
94 orb->resolve_initial_references ("POACurrent");
96 // Narrow the object reference to a POA Current reference
97 PortableServer::Current_var poa_current =
98 PortableServer::Current::_narrow (obj.in ());
100 Gateway_i *gateway;
102 ACE_NEW_THROW_EX (gateway,
103 Gateway_i (orb.in (),
104 poa_current.in ()),
105 CORBA::NO_MEMORY ());
107 gateway_poa->set_servant (gateway);
109 /// Get the ObjectID
110 PortableServer::ObjectId_var oid =
111 PortableServer::string_to_ObjectId ("Object_Factory");
113 /// This class is used to create a object reference.
114 Object_Factory_i *object_factory;
116 ACE_NEW_THROW_EX (object_factory,
117 Object_Factory_i (orb.in (),
118 gateway_poa.in ()),
119 CORBA::NO_MEMORY ());
121 /// Activate the Object_Factory_i Object
122 gateway_poa->activate_object_with_id (oid.in (),
123 object_factory);
125 // Get the object reference.
126 CORBA::Object_var gateway_object_factory =
127 gateway_poa->id_to_reference (oid.in ());
129 /// Convert the object reference to a string format.
130 CORBA::String_var ior =
131 orb->object_to_string (gateway_object_factory.in ());
133 /// If the ior_output_file exists, output the IOR to it.
134 if (ior_output_file != 0)
136 FILE *output_file = ACE_OS::fopen (ior_output_file, "w");
137 if (output_file == 0)
138 ACE_ERROR_RETURN ((LM_ERROR,
139 "Cannot open output file for writing "
140 "IOR: %s",
141 ior_output_file),
143 ACE_OS::fprintf (output_file, "%s", ior.in ());
144 ACE_OS::fclose (output_file);
147 orb->run ();
149 catch (const CORBA::Exception& ex)
151 ex._tao_print_exception ("ORT test (gateway_server):");
153 return -1;
156 return 0;