Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / MT_Server / server.cpp
blob82620993a6ca71731d59f74122de7fde829a2ea0
1 #include "test_i.h"
2 #include "ace/Get_Opt.h"
3 #include "ace/Task.h"
5 const ACE_TCHAR *ior_output_file = 0;
7 int nthreads = 4;
9 int
10 parse_args (int argc, ACE_TCHAR *argv[])
12 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:n:"));
13 int c;
15 while ((c = get_opts ()) != -1)
16 switch (c)
18 case 'o':
19 ior_output_file = get_opts.opt_arg ();
20 break;
22 case 'n':
23 nthreads = ACE_OS::atoi (get_opts.opt_arg ());
24 break;
26 case '?':
27 default:
28 ACE_ERROR_RETURN ((LM_ERROR,
29 "usage: %s "
30 "-o <iorfile>"
31 "\n",
32 argv [0]),
33 -1);
35 // Indicates successful parsing of the command line
36 return 0;
39 /**
40 * Run a server thread
42 * Use the ACE_Task_Base class to run server threads
44 class Worker : public ACE_Task_Base
46 public:
47 Worker (CORBA::ORB_ptr orb);
48 // ctor
50 virtual int svc (void);
51 // The thread entry point.
53 private:
54 CORBA::ORB_var orb_;
55 // The orb
58 int
59 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
61 try
63 CORBA::ORB_var orb =
64 CORBA::ORB_init (argc, argv);
66 CORBA::Object_var poa_object =
67 orb->resolve_initial_references("RootPOA");
69 if (CORBA::is_nil (poa_object.in ()))
70 ACE_ERROR_RETURN ((LM_ERROR,
71 " (%P|%t) Unable to initialize the POA.\n"),
72 1);
74 PortableServer::POA_var root_poa =
75 PortableServer::POA::_narrow (poa_object.in ());
77 PortableServer::POAManager_var poa_manager =
78 root_poa->the_POAManager ();
80 if (parse_args (argc, argv) != 0)
81 return 1;
83 Simple_Server_i server_impl (orb.in ());
85 PortableServer::ObjectId_var id =
86 root_poa->activate_object (&server_impl);
88 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
90 Simple_Server_var server =
91 Simple_Server::_narrow (object.in ());
93 CORBA::String_var ior =
94 orb->object_to_string (server.in ());
96 ACE_DEBUG ((LM_DEBUG, "Activated as <%C>\n", ior.in ()));
98 // If the ior_output_file exists, output the ior to it
99 if (ior_output_file != 0)
101 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
102 if (output_file == 0)
103 ACE_ERROR_RETURN ((LM_ERROR,
104 "Cannot open output file for writing IOR: %s",
105 ior_output_file),
107 ACE_OS::fprintf (output_file, "%s", ior.in ());
108 ACE_OS::fclose (output_file);
111 poa_manager->activate ();
113 Worker worker (orb.in ());
114 if (worker.activate (THR_NEW_LWP | THR_JOINABLE,
115 nthreads) != 0)
116 ACE_ERROR_RETURN ((LM_ERROR,
117 "Cannot activate client threads\n"),
120 worker.thr_mgr ()->wait ();
122 ACE_DEBUG ((LM_DEBUG, "event loop finished\n"));
124 catch (const CORBA::Exception& ex)
126 ex._tao_print_exception ("Exception caught:");
127 return 1;
130 return 0;
133 // ****************************************************************
135 Worker::Worker (CORBA::ORB_ptr orb)
136 : orb_ (CORBA::ORB::_duplicate (orb))
141 Worker::svc (void)
145 this->orb_->run ();
147 catch (const CORBA::Exception&)
150 return 0;