1 //=============================================================================
5 * The Server for the Quoter Factory
7 * @author Darrell Brunsch (brunsch@cs.wustl.edu) Michael Kircher (mk1@cs.wustl.edu)
9 //=============================================================================
13 #include "tao/ORB_Core.h"
15 Quoter_Server::Quoter_Server ()
17 quoter_Factory_i_ptr_ (0),
23 Quoter_Server::parse_args ()
25 ACE_Get_Opt
get_opts (argc_
, argv_
, ACE_TEXT("d:n:"));
28 while ((c
= get_opts ()) != -1)
31 case 'd': // debug flag.
32 this->debug_level_
= ACE_OS::atoi (get_opts
.opt_arg ());
34 case 'n': // number of Quoter objects we export
35 this->num_of_objs_
= ACE_OS::atoi (get_opts
.opt_arg ());
39 ACE_ERROR_RETURN ((LM_ERROR
,
42 " [-n] <num of Quoter objects>"
49 // Indicates successful parsing of command line.
54 //Initialize the Quoter Server
57 Quoter_Server::init (int argc
,
60 const char *exception_message
= "Null Message";
63 exception_message
= "While ORB Manager init";
64 int result
= this->orb_manager_
.init (argc
, argv
);
67 ACE_ERROR_RETURN ((LM_ERROR
,
72 // Copy them, because parse_args expects them there.
76 // Make a copy of argv since ORB_init will change it.
77 this->argv_
= new ACE_TCHAR
*[argc
];
79 for (i
= 0; i
< argc
; i
++)
80 this->argv_
[i
] = argv
[i
];
84 // Obtain the RootPOA.
85 CORBA::Object_var obj
=
86 this->orb_manager_
.orb()->resolve_initial_references ("RootPOA");
88 // Get the POA_var object from Object_var.
89 exception_message
= "While narrowing the root pos";
90 PortableServer::POA_var root_poa
=
91 PortableServer::POA::_narrow (obj
.in ());
93 // Get the POAManager of the RootPOA.
94 exception_message
= "While getting the POA Manager";
95 PortableServer::POAManager_var poa_manager
=
96 root_poa
->the_POAManager ();
98 ACE_NEW_RETURN (quoter_Factory_i_ptr_
,
99 Quoter_Factory_i(this->num_of_objs_
,
103 exception_message
= "While initing the quoter factory";
104 quoter_Factory_i_ptr_
->init ();
106 PortableServer::ObjectId_var quoter_Factory_oid
=
107 PortableServer::string_to_ObjectId ("Quoter_Factory");
109 exception_message
= "While activating quoter factory";
110 root_poa
->activate_object_with_id (quoter_Factory_oid
.in (),
111 quoter_Factory_i_ptr_
);
113 // Get Object reference for first_foo_impl object.
114 exception_message
= "While quoter_Factor::_this";
115 Stock::Quoter_Factory_var quoter_Factory_var
= quoter_Factory_i_ptr_
->_this ();
117 // Stringify the object reference and print it out.
118 exception_message
= "While object_to_string";
119 CORBA::String_var quoter_Factory_ior
=
120 this->orb_manager_
.orb()->object_to_string (quoter_Factory_var
.in ());
122 exception_message
= "While activating the POA Manager";
123 poa_manager
->activate ();
126 if (this->debug_level_
>= 2)
127 ACE_DEBUG ((LM_DEBUG
, "Quoter Server: IOR is: <%C>\n", quoter_Factory_ior
.in ()));
129 catch (const CORBA::Exception
& ex
)
131 ACE_ERROR ((LM_ERROR
, "Quote_Server::init - %C\n", exception_message
));
132 ex
._tao_print_exception ("Exception");
136 return this->init_naming_service ();
140 // Initialization of Naming Service and register IDL_Quoter Context
141 // and Quoter_factory object.
144 Quoter_Server::init_naming_service ()
146 const char *exception_message
= "Null Message";
149 CORBA::ORB_ptr orb_ptr
= TAO_ORB_Core_instance()->orb();
151 CORBA::Object_var naming_obj
=
152 orb_ptr
->resolve_initial_references ("NameService");
154 if (CORBA::is_nil (naming_obj
.in ()))
155 ACE_ERROR_RETURN ((LM_ERROR
,
156 " (%P|%t) Unable to resolve the Name Service.\n"),
159 exception_message
= "While narrowing naming context";
161 CosNaming::NamingContext::_narrow (naming_obj
.in ());
163 CosNaming::Name
quoterContextName (1);
164 quoterContextName
.length (1);
165 quoterContextName
[0].id
= CORBA::string_dup ("IDL_Quoter");
167 exception_message
= "While binding a new context";
168 CosNaming::NamingContext_var quoterNameContext
=
169 namingContext_var_
->bind_new_context (quoterContextName
);
171 //Register the quoter_factory name with the IDL_quoter Naming
173 CosNaming::Name
quoterFactoryContextName (1);
174 quoterFactoryContextName
.length (1);
175 quoterFactoryContextName
[0].id
= CORBA::string_dup ("Quoter_Factory");
177 exception_message
= "While using factory _this";
178 Stock::Quoter_Factory_var quoter_factory_var
= quoter_Factory_i_ptr_
->_this ();
180 exception_message
= "While binding factory";
181 quoterNameContext
->bind (quoterFactoryContextName
,
182 quoter_factory_var
.in ());
184 catch (const CORBA::Exception
& ex
)
186 ACE_ERROR ((LM_ERROR
, "Quote_Server::init_naming_service - %C", exception_message
));
187 ex
._tao_print_exception ("Exception");
195 Quoter_Server::run ()
197 if (this->debug_level_
>= 1)
198 ACE_DEBUG ((LM_DEBUG
,
199 "\nQuoter Example: Quoter_Server is running\n"));
201 orb_manager_
.orb()->run ();
206 Quoter_Server::~Quoter_Server ()
210 // Unbind quoter factory context and name.
211 CosNaming::Name
factory_name (2);
212 factory_name
.length (2);
213 factory_name
[0].id
= CORBA::string_dup ("IDL_Quoter");
214 factory_name
[1].id
= CORBA::string_dup ("Quoter_Factory");
215 if (!CORBA::is_nil (this->namingContext_var_
.in ()))
216 this->namingContext_var_
->unbind (factory_name
);
218 factory_name
.length (1);
219 if (!CORBA::is_nil (this->namingContext_var_
.in ()))
220 this->namingContext_var_
->unbind (factory_name
);
222 catch (const CORBA::Exception
& ex
)
224 ACE_ERROR ((LM_ERROR
,
225 "Could not unbind Quoter Server from the Name Service\n"));
226 ex
._tao_print_exception ("~Quoter_Server");
229 delete [] this->argv_
;
233 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
235 Quoter_Server quoter_server
;
239 int result
= quoter_server
.init (argc
, argv
);
244 quoter_server
.run ();
246 catch (const CORBA::SystemException
& sysex
)
248 sysex
._tao_print_exception ("System Exception");
251 catch (const CORBA::UserException
& userex
)
253 userex
._tao_print_exception ("User Exception");