Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / tests / Param_Test / server.cpp
blob64c2d74ee73261feb0e1d27d4afc15cb3a6a67c5
2 //=============================================================================
3 /**
4 * @file server.cpp
6 * @author Aniruddha Gokhale
7 */
8 //=============================================================================
11 #include "param_test_i.h"
12 #include "tao/Codeset/Codeset.h"
13 #include "tao/debug.h"
14 #include "ace/Get_Opt.h"
15 #include "ace/Log_Msg.h"
16 #include "ace/OS_NS_stdio.h"
18 // Parses the command line arguments and returns an error status.
19 static FILE *ior_output_file = 0;
20 static const ACE_TCHAR *ior_output_filename = ACE_TEXT("test.ior");
22 static int
23 parse_args (int argc, ACE_TCHAR *argv[])
25 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("do:"));
26 int c;
28 while ((c = get_opts ()) != -1)
29 switch (c)
31 case 'd': // debug flag
32 TAO_debug_level++;
33 break;
34 case 'o':
35 ior_output_filename = get_opts.opt_arg ();
36 break;
37 case '?':
38 default:
39 ACE_ERROR_RETURN ((LM_ERROR,
40 "usage: %s"
41 " [-d]"
42 "\n", argv [0]), 1);
45 return 0; // Indicates successful parsing of command line
48 // Standard command line parsing utilities used.
50 int
51 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
53 Param_Test_i *param_test = 0;
55 try
57 const char *orb_name = "";
58 CORBA::ORB_var orb_ptr =
59 CORBA::ORB_init (argc, argv, orb_name);
61 CORBA::Object_var temp; // holder for the myriad of times we get
62 // an object which we then have to narrow.
64 // Get the Root POA
65 temp = orb_ptr->resolve_initial_references ("RootPOA");
67 if (CORBA::is_nil (temp.in()))
68 ACE_ERROR_RETURN ((LM_ERROR,
69 "(%P|%t) Unable to get root poa reference.\n"),
70 1);
72 PortableServer::POA_var oa_ptr = PortableServer::POA::_narrow (temp.in());
74 PortableServer::POAManager_var poa_manager =
75 oa_ptr->the_POAManager ();
77 CORBA::PolicyList policies (2);
78 policies.length (2);
79 policies[0] =
80 oa_ptr->create_id_assignment_policy (PortableServer::USER_ID);
82 policies[1] =
83 oa_ptr->create_lifespan_policy (PortableServer::PERSISTENT);
85 // We use a different POA, otherwise the user would have to
86 // change the object key each time it invokes the server.
87 PortableServer::POA_var good_poa =
88 oa_ptr->create_POA ("child_poa",
89 poa_manager.in (),
90 policies);
92 // Parse remaining command line and verify parameters.
93 parse_args (argc, argv);
95 // initialize a param_test target object and register it with the object
96 // adapter
98 // Create the implementation object
99 ACE_NEW_RETURN (param_test,
100 Param_Test_i ("unknown",
101 orb_ptr.in ()), 1);
103 // Register with GoodPOA with a specific name
104 PortableServer::ObjectId_var id =
105 PortableServer::string_to_ObjectId ("param_test");
106 good_poa->activate_object_with_id (id.in (),
107 param_test);
109 // Stringify the objref we'll be implementing, and print it to
110 // stdout. Someone will take that string and give it to a
111 // client. Then release the object.
113 temp = good_poa->id_to_reference (id.in ());
115 CORBA::String_var str =
116 orb_ptr->object_to_string (temp.in ());
118 if (TAO_debug_level > 0)
120 ACE_DEBUG ((LM_DEBUG,
121 "(%P|%t) The IOR is <%s>\n",
122 str.in ()));
125 ior_output_file = ACE_OS::fopen (ior_output_filename, "w");
127 if (ior_output_file == 0)
129 ACE_ERROR_RETURN ((LM_ERROR,
130 "Unable to open %s for writing: %p\n",
131 ior_output_filename),
132 -1);
135 ACE_OS::fprintf (ior_output_file,
136 "%s",
137 str.in ());
138 ACE_OS::fclose (ior_output_file);
141 // Make the POAs controlled by this manager active
142 poa_manager->activate ();
144 orb_ptr->run ();
146 good_poa->destroy (true, true);
148 oa_ptr->destroy (1, 1);
150 catch (const CORBA::SystemException& sysex)
152 sysex._tao_print_exception ("System Exception");
153 return -1;
155 catch (const CORBA::UserException& userex)
157 userex._tao_print_exception ("User Exception");
158 return -1;
160 // Free resources
161 delete param_test;
163 return 0;