Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / tests / Bug_3531_Regression / server.cpp
blob21abfe9d5c92f7772382633c3459a365638c2ac8
1 #include "test_i.h"
2 #include "ace/Get_Opt.h"
3 #include "ace/Task.h"
4 #include "tao/ORB_Core.h"
6 const ACE_TCHAR *ior_output_file = ACE_TEXT("file://test.ior");
7 const ACE_TCHAR *srv_shutdown_file = ACE_TEXT("server_terminated");
9 int nthreads = 4;
11 int
12 parse_args (int argc, ACE_TCHAR *argv[])
14 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:n:x:"));
15 int c;
17 while ((c = get_opts ()) != -1)
18 switch (c)
20 case 'o':
21 ior_output_file = get_opts.opt_arg ();
22 break;
24 case 'n':
25 nthreads = ACE_OS::atoi (get_opts.opt_arg ());
26 break;
28 case 'x':
29 srv_shutdown_file = get_opts.opt_arg ();
30 break;
32 case '?':
33 default:
34 ACE_ERROR_RETURN ((LM_ERROR,
35 "usage: %s "
36 "-o <iorfile>"
37 "\n",
38 argv [0]),
39 -1);
41 // Indicates successful parsing of the command line
42 return 0;
45 /**
46 * Run a server thread
48 * Use the ACE_Task_Base class to run server threads
50 class Worker : public ACE_Task_Base
52 public:
53 Worker (CORBA::ORB_ptr orb);
54 // ctor
56 virtual int svc ();
57 // The thread entry point.
59 private:
60 CORBA::ORB_var orb_;
61 // The orb
64 int
65 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
67 try
69 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
71 CORBA::Object_var poa_object =
72 orb->resolve_initial_references("RootPOA");
74 if (CORBA::is_nil (poa_object.in ()))
75 ACE_ERROR_RETURN ((LM_ERROR,
76 " (%P|%t) Unable to initialize the POA.\n"),
77 1);
79 PortableServer::POA_var root_poa =
80 PortableServer::POA::_narrow (poa_object.in ());
82 PortableServer::POAManager_var poa_manager = root_poa->the_POAManager ();
84 if (parse_args (argc, argv) != 0)
85 return 1;
87 Simple_Server_i server_impl (orb.in ());
89 PortableServer::ObjectId_var id =
90 root_poa->activate_object (&server_impl);
92 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
94 Simple_Server_var server = Simple_Server::_narrow (object.in ());
96 CORBA::String_var ior = orb->object_to_string (server.in ());
98 ACE_DEBUG ((LM_DEBUG, "Activated as <%s>\n", ior.in ()));
100 // If the ior_output_file exists, output the ior to it
101 if (ior_output_file != 0)
103 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
104 if (output_file == 0)
105 ACE_ERROR_RETURN ((LM_ERROR,
106 "Cannot open output file for writing IOR: %s",
107 ior_output_file),
109 ACE_OS::fprintf (output_file, "%s", ior.in ());
110 ACE_OS::fclose (output_file);
113 poa_manager->activate ();
115 Worker worker (orb.in ());
116 if (worker.activate (THR_NEW_LWP | THR_JOINABLE,
117 nthreads) != 0)
118 ACE_ERROR_RETURN ((LM_ERROR,
119 "Cannot activate client threads\n"),
122 worker.thr_mgr ()->wait ();
124 ACE_DEBUG ((LM_DEBUG, "event loop finished\n"));
126 FILE *output_file= ACE_OS::fopen (ACE_TEXT_ALWAYS_CHAR(srv_shutdown_file), "w");
127 if (output_file == 0)
128 ACE_ERROR_RETURN ((LM_ERROR,
129 "Cannot open output file for writing: ",
130 ACE_TEXT_ALWAYS_CHAR(srv_shutdown_file)),
132 ACE_OS::fprintf (output_file, "%s", "OK\n");
133 ACE_OS::fclose (output_file);
135 catch (const CORBA::Exception& ex)
137 ex._tao_print_exception ("Exception caught:");
138 return 1;
141 return 0;
144 // ****************************************************************
146 Worker::Worker (CORBA::ORB_ptr orb)
147 : orb_ (CORBA::ORB::_duplicate (orb))
152 Worker::svc ()
156 this->orb_->run ();
158 catch (const CORBA::Exception&)
161 return 0;