Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Bug_2560_Regression / server.cpp
blob83c7f8c47a0ce066ea5d84ff086f584f9d259b39
2 #include "Stock_Factory_i.h"
3 #include "ace/Get_Opt.h"
4 #include "ace/streams.h"
5 #include "ace/OS_NS_stdio.h"
7 const ACE_TCHAR *ior_output_file = ACE_TEXT ("server.ior");
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.opt_arg ();
20 break;
22 case '?':
23 default:
24 ACE_ERROR_RETURN ((LM_ERROR,
25 "usage: %s "
26 "-o <iorfile>"
27 "\n",
28 argv [0]),
29 -1);
31 // Indicates successful parsing of the command line
32 return 0;
35 int ACE_TMAIN (int argc, ACE_TCHAR *argv[])
37 try {
38 // First initialize the ORB, that will remove some arguments...
39 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv, "TestORB");
41 if (parse_args (argc, argv) != 0)
42 return 1;
44 CORBA::Object_var poa_object = orb->resolve_initial_references ("RootPOA");
45 PortableServer::POA_var root_poa = PortableServer::POA::_narrow (poa_object.in());
46 PortableServer::POAManager_var poa_manager = root_poa->the_POAManager ();
47 poa_manager->activate ();
49 // Make policies for child POA
50 CORBA::PolicyList policies(2) ;
51 policies.length(2) ;
52 policies[0] = root_poa->create_lifespan_policy ( PortableServer::PERSISTENT ) ;
53 policies[1] = root_poa->create_id_assignment_policy ( PortableServer::USER_ID ) ;
55 PortableServer::POA_var poa = root_poa->create_POA ( "MyPOA", poa_manager.in(), policies );
57 // Creation of the new POAs over, so destroy the Policy_ptr's.
58 for ( CORBA::ULong i = 0 ; i < policies.length (); ++i ) {
59 CORBA::Policy_ptr policy = policies[i];
60 policy->destroy ();
63 // use this poa for making system objects
64 Quoter_Stock_i::set_default_POA ( poa.in() ) ;
66 // Create the servant
67 Quoter_Stock_Factory_i *stock_factory_i = 0;
68 ACE_NEW_RETURN (stock_factory_i,
69 Quoter_Stock_Factory_i,
70 -1);
71 PortableServer::ServantBase_var safe (stock_factory_i);
73 // Activate it to obtain the object reference
74 PortableServer::ObjectId_var id =
75 root_poa->activate_object (stock_factory_i);
77 CORBA::Object_var object_act = root_poa->id_to_reference (id.in ());
79 Quoter::Stock_Factory_var stock_factory =
80 Quoter::Stock_Factory::_narrow (object_act.in ());
82 // Put the object reference as an IOR string
83 CORBA::String_var ior = orb->object_to_string (stock_factory.in ());
85 // Output the IOR to the <ior_output_file>
86 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
87 if (output_file == 0)
88 ACE_ERROR_RETURN ((LM_ERROR,
89 "Cannot open output file for writing IOR: %s\n",
90 ior_output_file),
91 1);
92 ACE_OS::fprintf (output_file, "%s", ior.in ());
93 ACE_OS::fclose (output_file);
95 ACE_Time_Value timeout (15);
96 orb->run (timeout);
98 // Destroy the POA, waiting until the destruction terminates
99 root_poa->destroy (1, 1);
100 orb->destroy ();
102 catch (const CORBA::Exception & e) {
103 cerr << "CORBA exception raised: " << e << endl;
105 return 0;