Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / tests / ForwardOnceUponException / server.cpp
blob56e31ae6a7130e6bb131b23054d99c218fb6b22c
1 #include "test_i.h"
2 #include "tao/IORTable/IORTable.h"
3 #include "ace/Get_Opt.h"
4 #include "ace/OS_NS_stdio.h"
5 #include "tao/Invocation_Utils.h"
7 const ACE_TCHAR *ior_output_file = ACE_TEXT("test.ior");
8 int expect_servant_calls = 1;
9 int raise_exception = TAO::FOE_NON;
10 int num_requests = 1;
12 int
13 parse_args (int argc, ACE_TCHAR *argv[])
15 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:e:r:i:"));
16 int c;
18 while ((c = get_opts ()) != -1)
19 switch (c)
21 case 'o':
22 ior_output_file = get_opts.opt_arg ();
23 break;
24 case 'e':
25 raise_exception = ACE_OS::atoi (get_opts.opt_arg ());
26 break;
27 case 'r':
28 expect_servant_calls = ACE_OS::atoi (get_opts.opt_arg ());
29 break;
30 case 'i':
31 num_requests = ACE_OS::atoi (get_opts.opt_arg ());
32 break;
33 case '?':
34 default:
35 ACE_ERROR_RETURN ((LM_ERROR,
36 "usage: %s "
37 "-o <iorfile> -e <raise_exception> -r <expect_servant_calls>"
38 "\n",
39 argv [0]),
40 -1);
42 // Indicates successful parsing of the command line
43 return 0;
46 int
47 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
49 try
51 CORBA::ORB_var orb =
52 CORBA::ORB_init (argc, argv);
54 CORBA::Object_var poa_object =
55 orb->resolve_initial_references("RootPOA");
57 PortableServer::POA_var root_poa =
58 PortableServer::POA::_narrow (poa_object.in ());
59 if (CORBA::is_nil (root_poa.in ()))
60 ACE_ERROR_RETURN ((LM_ERROR,
61 " (%P|%t) Unable to initialize the POA.\n"),
62 1);
64 PortableServer::POAManager_var poa_manager =
65 root_poa->the_POAManager ();
67 if (parse_args (argc, argv) != 0)
68 return 1;
70 Simple_Server_i server_impl (orb.in ());
72 PortableServer::ObjectId_var id =
73 root_poa->activate_object (&server_impl);
75 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
77 Simple_Server_var server =
78 Simple_Server::_narrow (object.in ());
80 CORBA::String_var ior =
81 orb->object_to_string (server.in ());
83 CORBA::Object_var table_object =
84 orb->resolve_initial_references("IORTable");
86 IORTable::Table_var table =
87 IORTable::Table::_narrow (table_object.in ());
88 if (CORBA::is_nil (table.in ()))
89 ACE_ERROR_RETURN ((LM_ERROR,
90 " (%P|%t) Unable to initialize the IORTable.\n"),
91 1);
92 table->bind ("Simple_Server", ior.in ());
94 //ACE_DEBUG ((LM_DEBUG, "Activated as <%C>\n", ior.in ()));
96 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
97 if (output_file == 0)
98 ACE_ERROR_RETURN ((LM_ERROR,
99 "Cannot open output file for writing IOR: %s",
100 ior_output_file),
102 ACE_OS::fprintf (output_file, "%s", ior.in ());
103 ACE_OS::fclose (output_file);
105 poa_manager->activate ();
107 orb->run ();
109 ACE_DEBUG ((LM_DEBUG, "(%P|%t)server: event loop finished\n"));
111 // If expect_servant_calls is 0, it means it's the request forward looping case so the
112 // servant continue receiving calls from a single request. We can not determine how many
113 // servant calls but it should be more than the number of requests.
114 if ((expect_servant_calls == 0 && (server_impl.ncalls () > num_requests))
115 || (expect_servant_calls > 0 && (server_impl.ncalls () == expect_servant_calls)))
117 return 0;
119 else
120 ACE_ERROR_RETURN ((LM_ERROR,
121 "server: Test failed - expected %d servant calls but got %d calls \n",
122 expect_servant_calls, server_impl.ncalls ()),
125 catch (const CORBA::Exception& ex)
127 ex._tao_print_exception ("Exception caught:");
128 return 1;
131 return 0;