Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / orbsvcs / examples / ORT / server.cpp
blob63689117a765ddd07c641f6bbdd4331fff11ccea
1 #include "sum_server_i.h"
2 #include "Server_IORInterceptor_ORBInitializer.h"
3 #include "tao/ORBInitializer_Registry.h"
5 #include "ace/Get_Opt.h"
6 #include "ace/OS_NS_stdio.h"
8 const ACE_TCHAR *ior_output_file = 0;
10 int
11 parse_args (int argc, ACE_TCHAR *argv[])
13 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
14 int c;
16 while ((c = get_opts ()) != -1)
17 switch (c)
19 case 'o':
20 ior_output_file = get_opts.optarg;
21 break;
22 default:
23 ACE_ERROR_RETURN ((LM_ERROR,
24 "Usage: %s "
25 "-o <iorfile>"
26 "\n",
27 argv[0]),
28 -1);
31 // Indicates successful parsing of the command line
32 return 0;
35 int
36 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
38 try
40 #if TAO_HAS_INTERCEPTORS == 1
42 PortableInterceptor::ORBInitializer_ptr orb_initializer =
43 PortableInterceptor::ORBInitializer::_nil ();
45 ACE_NEW_RETURN (orb_initializer,
46 Server_IORInterceptor_ORBInitializer,
47 -1); // No CORBA exceptions yet!
49 PortableInterceptor::ORBInitializer_var orb_initializer_var =
50 orb_initializer;
52 PortableInterceptor::register_orb_initializer (orb_initializer_var.in ());
54 #endif /* TAO_HAS_INTERCEPTORS == 1 */
56 // The usual initialization stuff
58 // Initialize the ORB.
59 CORBA::ORB_var orb = CORBA::ORB_init (argc,
60 argv,
61 "server_sum_orb");
63 if (parse_args (argc, argv) != 0)
64 return -1;
66 // Resolve reference to RootPOA
67 CORBA::Object_var obj =
68 orb->resolve_initial_references ("RootPOA");
70 // Narrow it down correctly.
71 PortableServer::POA_var root_poa =
72 PortableServer::POA::_narrow (obj.in ());
74 // Check for nil references
75 if (CORBA::is_nil (root_poa.in ()))
76 ACE_ERROR_RETURN ((LM_ERROR,
77 "Unable to obtain RootPOA reference.\n"),
78 -1);
80 // Get poa_manager reference
81 PortableServer::POAManager_var poa_manager =
82 root_poa->the_POAManager ();
84 // Activate it.
85 poa_manager->activate ();
87 // initialize the sum_server
88 sum_server_i sum_server_impl;
90 // Activate
91 obj = sum_server_impl._this ();
93 // Narrow it down.
94 ORT::sum_server_var sum_server =
95 ORT::sum_server::_narrow (obj.in ());
97 // Check for nil reference
98 if (CORBA::is_nil (sum_server.in ()))
99 ACE_ERROR_RETURN ((LM_ERROR,
100 "Unable to obtain reference to ORT::sum_server "
101 "object.\n"),
102 -1);
104 // Convert the object reference to a string format.
105 CORBA::String_var ior =
106 orb->object_to_string (sum_server.in ());
108 // If the ior_output_file exists, output the IOR to it.
109 if (ior_output_file != 0)
111 FILE *output_file = ACE_OS::fopen (ior_output_file, "w");
112 if (output_file == 0)
113 ACE_ERROR_RETURN ((LM_ERROR,
114 "Cannot open output file for writing "
115 "IOR: %s",
116 ior_output_file),
118 ACE_OS::fprintf (output_file, "%s", ior.in ());
119 ACE_OS::fclose (output_file);
122 orb->run ();
124 ACE_DEBUG ((LM_INFO, "Successful.\n"));
126 catch (const CORBA::Exception& ex)
128 ex._tao_print_exception ("ORT example server:");
129 return -1;
132 return 0;