Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / tests / Bug_3333_Regression / server.cpp
blobd04f2c7b19fbab842234f8aafee89632e06b3bb4
1 #include "Hello.h"
2 #include "ace/Get_Opt.h"
3 #include "ace/OS_NS_stdio.h"
4 #include "ace/SString.h"
5 #include "tao/IORTable/IORTable.h"
7 const ACE_TCHAR *ior_output_file = ACE_TEXT ("test.ior");
8 const ACE_TCHAR *ior_output_file2 = ACE_TEXT ("forward_forever.ior");
10 int
11 parse_args (int argc, ACE_TCHAR *argv[])
13 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:p:"));
14 int c;
16 while ((c = get_opts ()) != -1)
17 switch (c)
19 case 'o':
20 ior_output_file = get_opts.opt_arg ();
21 break;
22 case 'p':
23 ior_output_file2 = get_opts.opt_arg ();
24 break;
25 case '?':
26 default:
27 ACE_ERROR_RETURN ((LM_ERROR,
28 "usage: %s "
29 "-o <iorfile>"
30 "\n",
31 argv [0]),
32 -1);
34 // Indicates successful parsing of the command line
35 return 0;
38 int
39 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
41 try
43 ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - ORB_init\n"));
44 CORBA::ORB_var orb =
45 CORBA::ORB_init (argc, argv);
47 ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - Obtain RootPOA\n"));
48 CORBA::Object_var poa_object =
49 orb->resolve_initial_references("RootPOA");
51 PortableServer::POA_var root_poa =
52 PortableServer::POA::_narrow (poa_object.in ());
54 if (CORBA::is_nil (root_poa.in ()))
55 ACE_ERROR_RETURN ((LM_ERROR,
56 " (%P|%t) Panic: nil RootPOA\n"),
57 1);
59 PortableServer::POAManager_var poa_manager = root_poa->the_POAManager ();
61 ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - parse args\n"));
62 if (parse_args (argc, argv) != 0)
63 return 1;
65 ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - init hello\n"));
66 Hello *hello_impl = 0;
67 ACE_NEW_RETURN (hello_impl,
68 Hello (orb.in ()),
69 1);
70 PortableServer::ServantBase_var owner_transfer(hello_impl);
72 PortableServer::ObjectId_var id =
73 root_poa->activate_object (hello_impl);
75 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
77 Test::Hello_var hello = Test::Hello::_narrow (object.in ());
79 CORBA::String_var ior = orb->object_to_string (hello.in ());
81 ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - Create the forwarding loop\n"));
82 CORBA::Object_var iorTableObj =
83 orb->resolve_initial_references ("IORTable");
85 IORTable::Table_var iorTable
86 = IORTable::Table::_narrow (iorTableObj.in ());
88 // We are using this test with ObjRefStyle == URL ... or at
89 // least we better be. The IOR is therefore a corbaloc
90 ACE_CString full_corbaloc (ior.in (), 0, 1);
92 // Create a corbaloc for an IOR table binding that points to
93 // itself. Acessing this will make the server reply with LOCATION_FORWARD
94 // indefinitely.
96 ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - Corbaloc is\n \"%C\"\n", full_corbaloc.c_str()));
97 // Get the endpoint info only...
98 CORBA::ULong first_slash = full_corbaloc.find ("/", 0);
99 ACE_CString forward_forever = full_corbaloc.substring (0,
100 first_slash);
102 // .. add the string we are going to bind against and then bind
103 forward_forever += "/hello";
104 iorTable->bind("hello", forward_forever.c_str ());
106 ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - Forward forever is\n \"%C\"\n", forward_forever.c_str()));
107 // Output the IORs
108 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
109 if (output_file == 0)
110 ACE_ERROR_RETURN ((LM_ERROR,
111 "Cannot open output file for writing IOR: %s\n",
112 ior_output_file),
114 ACE_OS::fprintf (output_file, "%s", ior.in ());
115 ACE_OS::fclose (output_file);
117 output_file = ACE_OS::fopen (ior_output_file2, "w");
118 if (output_file == 0)
119 ACE_ERROR_RETURN ((LM_ERROR,
120 "Cannot open output file for writing IOR: %s\n",
121 ior_output_file2),
123 ACE_OS::fprintf (output_file, "%s", forward_forever.c_str ());
124 ACE_OS::fclose (output_file);
126 poa_manager->activate ();
128 ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - orb->run()\n"));
129 orb->run ();
131 ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - event loop finished\n"));
133 root_poa->destroy (true, true);
135 orb->destroy ();
137 catch (const CORBA::Exception& ex)
139 ex._tao_print_exception ("Exception caught:");
140 return 1;
143 return 0;