Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Bug_3567_Regression / server.cpp
blob70949ace98e4c0cd5204de9c423df45c4362c018
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 //=============================================================================
14 #include "ami_test_i.h"
15 #include "tao/debug.h"
16 #include "ace/OS_NS_stdio.h"
17 #include "ace/Get_Opt.h"
18 #include "ace/Task.h"
20 const ACE_TCHAR *ior_output_file = 0;
21 int nthreads = 5;
23 /**
24 * Run a server thread
26 * Use the ACE_Task_Base class to run server threads
28 class Worker : public ACE_Task_Base
30 public:
31 Worker (CORBA::ORB_ptr orb);
32 // ctor
34 virtual int svc (void);
35 // The thread entry point.
37 private:
38 CORBA::ORB_var orb_;
39 // The orb
42 int
43 parse_args (int argc, ACE_TCHAR *argv[])
45 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:d"));
46 int c;
48 while ((c = get_opts ()) != -1)
49 switch (c)
51 case 'o':
52 ior_output_file = get_opts.opt_arg ();
53 break;
54 case 'd':
55 ++TAO_debug_level;
56 break;
57 case '?':
58 default:
59 ACE_ERROR_RETURN ((LM_ERROR,
60 "usage: %s "
61 "-o <iorfile>"
62 "\n",
63 argv [0]),
64 -1);
66 // Indicates successful parsing of the command line
67 return 0;
70 int
71 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
73 try
75 CORBA::ORB_var orb =
76 CORBA::ORB_init (argc, argv);
78 CORBA::Object_var poa_object =
79 orb->resolve_initial_references("RootPOA");
81 if (CORBA::is_nil (poa_object.in ()))
82 ACE_ERROR_RETURN ((LM_ERROR,
83 " (%P|%t) Unable to initialize the POA.\n"),
84 1);
86 PortableServer::POA_var root_poa =
87 PortableServer::POA::_narrow (poa_object.in ());
89 PortableServer::POAManager_var poa_manager =
90 root_poa->the_POAManager ();
92 if (parse_args (argc, argv) != 0)
93 return 1;
95 AMI_Test_i ami_test_i (orb.in ());
97 PortableServer::ObjectId_var id =
98 root_poa->activate_object (&ami_test_i);
100 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
102 A::AMI_Test_var ami_test_var =
103 A::AMI_Test::_narrow (object.in ());
105 CORBA::String_var ior =
106 orb->object_to_string (ami_test_var.in ());
108 ACE_DEBUG ((LM_DEBUG, "Activated as <%C>\n", ior.in ()));
110 // If the ior_output_file exists, output the ior to it
111 if (ior_output_file != 0)
113 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
114 if (output_file == 0)
115 ACE_ERROR_RETURN ((LM_ERROR,
116 "Cannot open output file for writing IOR: %s",
117 ior_output_file),
119 ACE_OS::fprintf (output_file, "%s", ior.in ());
120 ACE_OS::fclose (output_file);
123 poa_manager->activate ();
125 Worker worker (orb.in ());
126 if (worker.activate (THR_NEW_LWP | THR_JOINABLE,
127 nthreads) != 0)
128 ACE_ERROR_RETURN ((LM_ERROR,
129 "Cannot activate client threads\n"),
132 worker.thr_mgr ()->wait ();
134 root_poa->destroy (1, // ethernalize objects
135 0); // wait for completion
137 orb->destroy ();
139 ACE_DEBUG ((LM_DEBUG, "event loop finished\n"));
141 catch (const CORBA::Exception& ex)
143 ex._tao_print_exception ("Caught exception:");
144 return 1;
147 return 0;
150 Worker::Worker (CORBA::ORB_ptr orb)
151 : orb_ (CORBA::ORB::_duplicate (orb))
156 Worker::svc (void)
160 this->orb_->run ();
162 catch (const CORBA::Exception&)
165 return 0;