Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / tests / Bug_3567_Regression / server.cpp
blobc59bb94fb579a5efb7444096876c9e7f2ff494a9
2 //=============================================================================
3 /**
4 * @file server.cpp
6 * Implementation of the server.
8 * @author Alexander Babu Arulanthu <alex@cs.wustl.edu>
9 * @author Michael Kircher <Michael.Kircher@mchp.siemens.de>
11 //=============================================================================
13 #include "ami_test_i.h"
14 #include "tao/debug.h"
15 #include "ace/OS_NS_stdio.h"
16 #include "ace/Get_Opt.h"
17 #include "ace/Task.h"
19 const ACE_TCHAR *ior_output_file = 0;
20 int nthreads = 5;
22 /**
23 * Run a server thread
25 * Use the ACE_Task_Base class to run server threads
27 class Worker : public ACE_Task_Base
29 public:
30 // ctor
31 Worker (CORBA::ORB_ptr orb);
33 // The thread entry point.
34 virtual int svc ();
36 private:
37 // The orb
38 CORBA::ORB_var orb_;
41 int
42 parse_args (int argc, ACE_TCHAR *argv[])
44 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:d"));
45 int c;
47 while ((c = get_opts ()) != -1)
48 switch (c)
50 case 'o':
51 ior_output_file = get_opts.opt_arg ();
52 break;
53 case 'd':
54 ++TAO_debug_level;
55 break;
56 case '?':
57 default:
58 ACE_ERROR_RETURN ((LM_ERROR,
59 "usage: %s "
60 "-o <iorfile>"
61 "\n",
62 argv [0]),
63 -1);
65 // Indicates successful parsing of the command line
66 return 0;
69 int
70 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
72 try
74 CORBA::ORB_var orb =
75 CORBA::ORB_init (argc, argv);
77 CORBA::Object_var poa_object =
78 orb->resolve_initial_references("RootPOA");
80 if (CORBA::is_nil (poa_object.in ()))
81 ACE_ERROR_RETURN ((LM_ERROR,
82 " (%P|%t) Unable to initialize the POA.\n"),
83 1);
85 PortableServer::POA_var root_poa =
86 PortableServer::POA::_narrow (poa_object.in ());
88 PortableServer::POAManager_var poa_manager =
89 root_poa->the_POAManager ();
91 if (parse_args (argc, argv) != 0)
92 return 1;
94 AMI_Test_i ami_test_i (orb.in ());
96 PortableServer::ObjectId_var id =
97 root_poa->activate_object (&ami_test_i);
99 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
101 A::AMI_Test_var ami_test_var =
102 A::AMI_Test::_narrow (object.in ());
104 CORBA::String_var ior =
105 orb->object_to_string (ami_test_var.in ());
107 ACE_DEBUG ((LM_DEBUG, "Activated as <%C>\n", ior.in ()));
109 // If the ior_output_file exists, output the ior to it
110 if (ior_output_file != 0)
112 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
113 if (output_file == 0)
114 ACE_ERROR_RETURN ((LM_ERROR,
115 "Cannot open output file for writing IOR: %s",
116 ior_output_file),
118 ACE_OS::fprintf (output_file, "%s", ior.in ());
119 ACE_OS::fclose (output_file);
122 poa_manager->activate ();
124 Worker worker (orb.in ());
125 if (worker.activate (THR_NEW_LWP | THR_JOINABLE,
126 nthreads) != 0)
127 ACE_ERROR_RETURN ((LM_ERROR,
128 "Cannot activate client threads\n"),
131 worker.thr_mgr ()->wait ();
133 root_poa->destroy (true, // ethernalize objects
134 false); // wait for completion
136 orb->destroy ();
138 ACE_DEBUG ((LM_DEBUG, "event loop finished\n"));
140 catch (const CORBA::Exception& ex)
142 ex._tao_print_exception ("Caught exception:");
143 return 1;
146 return 0;
149 Worker::Worker (CORBA::ORB_ptr orb)
150 : orb_ (CORBA::ORB::_duplicate (orb))
155 Worker::svc ()
159 this->orb_->run ();
161 catch (const CORBA::Exception&)
164 return 0;