Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Portable_Interceptors / PICurrent / server.cpp
blobee757c7daebd8f8c0273ef54133e0897c171b81a
1 // -*- C++ -*-
2 #include "ace/Get_Opt.h"
3 #include "ace/OS_NS_unistd.h"
5 #include "test_i.h"
6 #include "ServerORBInitializer.h"
8 #include "tao/ORBInitializer_Registry.h"
9 #include "tao/PI/PI.h"
10 #include "ace/OS_NS_stdio.h"
11 #include "ace/OS_NS_strings.h"
13 const ACE_TCHAR *ior_output_file = ACE_TEXT("test.ior");
15 extern PortableInterceptor::SlotId slot_id;
17 int
18 parse_args (int argc, ACE_TCHAR *argv[])
20 bool error = false;
21 for(int i = 1; i < argc; i++)
23 if (ACE_OS::strncasecmp(argv[i], ACE_TEXT("-ORB"), 4) != 0)
25 switch (argv[i][1])
27 case 'o':
28 i++;
29 if (i < argc)
30 ior_output_file = argv[i];
31 else
32 error = true;
33 break;
35 case 't':
36 #if defined (ACE_HAS_THREADS)
37 argv[i] = const_cast<ACE_TCHAR*> (ACE_TEXT("-ORBSvcConfDirective"));
38 #endif /* ACE_HAS_THREADS */
39 i++;
40 if (i < argc)
41 #if defined (ACE_HAS_THREADS)
42 argv[i] = const_cast<ACE_TCHAR*> (ACE_TEXT("static Server_Strategy_Factory \"-ORBConcurrency thread-per-connection\""));
43 #else
44 ACE_DEBUG ((LM_DEBUG, "NOTE: Non-threaded build. "
45 "Defaulting to single threaded.\n"));
46 #endif /* ACE_HAS_THREADS */
47 else
48 error = true;
49 break;
51 default:
52 error = true;
57 if (error)
58 ACE_ERROR_RETURN ((LM_ERROR,
59 "Usage: %s "
60 "-o <iorfile> "
61 "-t <1> "
62 "\n",
63 argv[0]),
64 -1);
66 // Indicates successful parsing of the command line
67 return 0;
70 int
71 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
73 try
75 PortableInterceptor::ORBInitializer_ptr temp_initializer =
76 PortableInterceptor::ORBInitializer::_nil ();
78 ACE_NEW_RETURN (temp_initializer,
79 ServerORBInitializer,
80 -1); // No CORBA exceptions yet!
81 PortableInterceptor::ORBInitializer_var orb_initializer =
82 temp_initializer;
84 PortableInterceptor::register_orb_initializer (orb_initializer.in ());
86 CORBA::ORB_var orb = CORBA::ORB_init (argc,
87 argv,
88 "test_orb");
90 if (parse_args (argc, argv) != 0)
91 return -1;
93 CORBA::Object_var obj =
94 orb->resolve_initial_references ("RootPOA");
96 PortableServer::POA_var root_poa =
97 PortableServer::POA::_narrow (obj.in ());
99 if (CORBA::is_nil (root_poa.in ()))
100 ACE_ERROR_RETURN ((LM_ERROR,
101 "Unable to obtain RootPOA reference.\n"),
102 -1);
104 PortableServer::POAManager_var poa_manager =
105 root_poa->the_POAManager ();
107 poa_manager->activate ();
109 obj = orb->resolve_initial_references ("PICurrent");
111 PortableInterceptor::Current_var pi_current =
112 PortableInterceptor::Current::_narrow (obj.in ());
114 if (CORBA::is_nil (pi_current.in ()))
115 ACE_ERROR_RETURN ((LM_ERROR,
116 "Unable to obtain PICurrent reference.\n"),
117 -1);
119 test_i server_impl (pi_current.in (),
120 ::slot_id,
121 orb.in ());
123 obj = server_impl._this ();
125 PICurrentTest::test_var server =
126 PICurrentTest::test::_narrow (obj.in ());
128 if (CORBA::is_nil (server.in ()))
129 ACE_ERROR_RETURN ((LM_ERROR,
130 "Unable to obtain reference to "
131 "PICurrentTest::test object.\n"),
132 -1);
134 CORBA::String_var ior =
135 orb->object_to_string (server.in ());
137 ACE_DEBUG ((LM_INFO, "PICurrentTest::test: <%C>\n", ior.in ()));
139 // If the ior_output_file exists, output the IOR to it.
140 if (ior_output_file != 0)
142 FILE *output_file = ACE_OS::fopen (ior_output_file, "w");
143 if (output_file == 0)
144 ACE_ERROR_RETURN ((LM_ERROR,
145 "Cannot open output file for writing "
146 "IOR: %s",
147 ior_output_file),
149 ACE_OS::fprintf (output_file, "%s", ior.in ());
150 ACE_OS::fclose (output_file);
153 orb->run ();
155 ACE_OS::sleep(1);
156 orb->destroy ();
158 ACE_DEBUG ((LM_INFO, "Event loop finished.\n"));
160 catch (const CORBA::Exception& ex)
162 ex._tao_print_exception ("PICurrent test (server-side):");
164 return -1;
167 return 0;