Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / tests / Alt_Mapping / server.cpp
blob9dc0bacc8a6eda7b4087d4cecf71a0bf12e705d0
2 //=============================================================================
3 /**
4 * @file server.cpp
6 * @author Jeff Parsons
7 */
8 //=============================================================================
11 #include "alt_mapping_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 PortableServer::POA_var oa_ptr;
54 Alt_Mapping_i *svnt = 0;
56 try
58 const char *orb_name = "";
59 CORBA::ORB_var orb_ptr =
60 CORBA::ORB_init (argc, argv, orb_name);
62 CORBA::Object_var temp; // holder for the myriad of times we get
63 // an object which we then have to narrow.
65 // Get the Root POA
67 temp = orb_ptr->resolve_initial_references ("RootPOA");
69 if (CORBA::is_nil (temp.in()))
70 ACE_ERROR_RETURN ((LM_ERROR,
71 "(%P|%t) Unable to get root poa reference.\n"),
72 1);
74 oa_ptr = PortableServer::POA::_narrow (temp.in());
76 PortableServer::POAManager_var poa_manager =
77 oa_ptr->the_POAManager ();
79 // Parse remaining command line and verify parameters.
80 parse_args (argc, argv);
82 // initialize a param_test target object and register it with the object
83 // adapter
85 // Create the implementation object
86 ACE_NEW_RETURN (svnt,
87 Alt_Mapping_i (orb_ptr.in ()),
88 -1);
90 // Register with GoodPOA with a specific name
91 PortableServer::ObjectId_var id =
92 PortableServer::string_to_ObjectId ("alt_mapping");
93 oa_ptr->activate_object_with_id (id.in (),
94 svnt);
96 // Stringify the objref we'll be implementing, and print it to
97 // stdout. Someone will take that string and give it to a
98 // client. Then release the object.
100 temp = oa_ptr->id_to_reference (id.in ());
102 CORBA::String_var str =
103 orb_ptr->object_to_string (temp.in ());
105 if (TAO_debug_level > 0)
107 ACE_DEBUG ((LM_DEBUG,
108 "(%P|%t) The IOR is <%s>\n",
109 str.in ()));
112 ior_output_file = ACE_OS::fopen (ior_output_filename, "w");
114 if (ior_output_file == 0)
116 ACE_ERROR_RETURN ((LM_ERROR,
117 "Unable to open %s for writing: %p\n",
118 ior_output_filename),
119 -1);
122 ACE_OS::fprintf (ior_output_file,
123 "%s",
124 str.in ());
125 ACE_OS::fclose (ior_output_file);
128 // Make the POAs controlled by this manager active
129 poa_manager->activate ();
131 orb_ptr->run ();
133 oa_ptr->destroy (true, true);
135 catch (const CORBA::SystemException& sysex)
137 sysex._tao_print_exception ("System Exception");
138 return -1;
140 catch (const CORBA::UserException& userex)
142 userex._tao_print_exception ("User Exception");
143 return -1;
146 /// Free resources.
147 delete svnt;
149 return 0;