Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / tests / Bug_3163_Regression / server.cpp
blob3085980d4082ae707ec9021ab47c79350578cee9
1 #include "tao/corba.h"
2 #include "tao/PortableServer/PortableServer.h"
3 #include "testS.h"
4 #include "ace/Get_Opt.h"
5 #include "ace/OS_NS_stdio.h"
7 const ACE_TCHAR *ior_output_file = ACE_TEXT ("test.ior");
9 int
10 parse_args (int argc, ACE_TCHAR *argv[])
12 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
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 '?':
23 default:
24 ACE_ERROR_RETURN ((LM_ERROR,
25 "usage: %s "
26 "-o <iorfile>"
27 "\n",
28 argv [0]),
29 -1);
31 // Indicates successful parsing of the command line
32 return 0;
35 class ctest_impl : public virtual POA_ctest
37 public:
38 /// Constructor
39 ctest_impl (CORBA::ORB_ptr orb) : orb_ (CORBA::ORB::_duplicate (orb)) {}
41 CORBA::Long ctestfn(CORBA::Long size, ctest::UCSeq_out data)
43 data = new ctest::UCSeq;
45 data->length(size);
47 for (CORBA::ULong i = 0; i < data->length(); ++i)
49 data[i] = (rand() % 26) + 'A';
52 return 0;
54 void shutdown ()
56 this->orb_->shutdown (false);
59 private:
60 /// Use an ORB reference to convert strings to objects and shutdown
61 /// the application.
62 CORBA::ORB_var orb_;
65 int 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 PortableServer::POA_var root_poa =
75 PortableServer::POA::_narrow (poa_object.in ());
77 if (CORBA::is_nil (root_poa.in ()))
78 ACE_ERROR_RETURN ((LM_ERROR,
79 " (%P|%t) Panic: nil RootPOA\n"),
80 1);
82 PortableServer::POAManager_var poa_manager = root_poa->the_POAManager ();
84 if (parse_args (argc, argv) != 0)
85 return 1;
87 ctest_impl *hello_impl = 0;
88 ACE_NEW_RETURN (hello_impl,
89 ctest_impl (orb.in ()),
90 1);
91 PortableServer::ServantBase_var owner_transfer(hello_impl);
93 PortableServer::ObjectId_var id =
94 root_poa->activate_object (hello_impl);
96 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
98 ctest_var hello = ctest::_narrow (object.in ());
100 CORBA::String_var ior = orb->object_to_string (hello.in ());
102 // Output the IOR to the <ior_output_file>
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\n",
107 ior_output_file),
109 ACE_OS::fprintf (output_file, "%s", ior.in ());
110 ACE_OS::fclose (output_file);
112 poa_manager->activate ();
114 orb->run();
115 ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - event loop finished\n"));
117 root_poa->destroy (true, true);
119 orb->destroy ();
121 catch (const CORBA::Exception& ex)
123 ex._tao_print_exception ("Exception caught:");
124 return 1;
127 return 0;