Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / tests / Bug_2494_Regression / server.cpp
blob95e93bbebc262134eb2acc8661c84a20926392fa
1 #include "test_i.h"
2 #include "ace/Get_Opt.h"
3 #include "ace/Task.h"
5 const ACE_TCHAR *ior_output_file = ACE_TEXT("test.ior");
6 int nthreads = 4;
8 int
9 parse_args (int argc, ACE_TCHAR *argv[])
11 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:n:"));
12 int c;
14 while ((c = get_opts ()) != -1)
15 switch (c)
17 case 'o':
18 ior_output_file = get_opts.opt_arg ();
19 break;
21 case 'n':
22 nthreads = ACE_OS::atoi (get_opts.opt_arg ());
23 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 /**
39 * Run a server thread
41 * Use the ACE_Task_Base class to run server threads
43 class Worker : public ACE_Task_Base
45 public:
46 Worker (CORBA::ORB_ptr orb);
47 // ctor
49 virtual int svc ();
50 // The thread entry point.
52 private:
53 CORBA::ORB_var orb_;
54 // The orb
57 int
58 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
60 try
62 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
64 CORBA::Object_var poa_object =
65 orb->resolve_initial_references("RootPOA");
67 if (CORBA::is_nil (poa_object.in ()))
68 ACE_ERROR_RETURN ((LM_ERROR,
69 " (%P|%t) Unable to initialize the POA.\n"),
70 1);
72 PortableServer::POA_var root_poa =
73 PortableServer::POA::_narrow (poa_object.in ());
75 PortableServer::POAManager_var poa_manager = root_poa->the_POAManager ();
77 if (parse_args (argc, argv) != 0)
78 return 1;
80 Simple_Server_i server_impl (orb.in ());
82 PortableServer::ObjectId_var id =
83 root_poa->activate_object (&server_impl);
85 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
87 Simple_Server_var server = Simple_Server::_narrow (object.in ());
89 CORBA::String_var ior = orb->object_to_string (server.in ());
91 ACE_DEBUG ((LM_DEBUG, "Activated as <%C>\n", ior.in ()));
93 // If the ior_output_file exists, output the ior to it
94 if (ior_output_file != 0)
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);
106 poa_manager->activate ();
108 Worker worker (orb.in ());
109 if (worker.activate (THR_NEW_LWP | THR_JOINABLE,
110 nthreads) != 0)
111 ACE_ERROR_RETURN ((LM_ERROR,
112 "Cannot activate client threads\n"),
115 worker.thr_mgr ()->wait ();
117 ACE_DEBUG ((LM_DEBUG, "event loop finished\n"));
119 const char *fname = "server_terminated";
120 FILE *output_file= ACE_OS::fopen (fname, "w");
121 if (output_file == 0)
122 ACE_ERROR_RETURN ((LM_ERROR,
123 "Cannot open output file for writing: ",
124 fname),
126 ACE_OS::fprintf (output_file, "%s", "OK\n");
127 ACE_OS::fclose (output_file);
129 catch (const CORBA::Exception& ex)
131 ex._tao_print_exception ("Exception caught:");
132 return 1;
135 return 0;
138 // ****************************************************************
140 Worker::Worker (CORBA::ORB_ptr orb)
141 : orb_ (CORBA::ORB::_duplicate (orb))
146 Worker::svc ()
150 this->orb_->run ();
152 catch (const CORBA::Exception&)
155 return 0;