Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Objref_Sequence_Test / server.cpp
blobdebc674c64831fdfe2ac7f15782628bf1c7dabe2
1 #include "TestS.h"
2 #include "ace/Get_Opt.h"
3 #include "ace/OS_NS_stdio.h"
5 /// Implement the Server Interface
6 class ServerServant :
7 public POA_Server
9 public:
10 /// Ctor
11 ServerServant (PortableServer::POA_ptr poa,
12 CORBA::ORB_ptr orb);
14 void CreateExtra (CORBA::ULong length,
15 ServerSequence_out seq);
17 void DeleteExtra (const ServerSequence &seq);
19 //FUZZ: disable check_for_lack_ACE_OS
20 void shutdown (void);
21 //FUZZ: enable check_for_lack_ACE_OS
23 private:
24 /// Our root POA
25 PortableServer::POA_var root_poa_;
27 /// The ORB on which we are running
28 CORBA::ORB_var orb_;
31 /// Ctor
32 ServerServant::ServerServant (PortableServer::POA_ptr poa,
33 CORBA::ORB_ptr orb)
34 :root_poa_ (PortableServer::POA::_duplicate (poa)),
35 orb_ (CORBA::ORB::_duplicate (orb))
39 /// Servant implementations
40 void
41 ServerServant::CreateExtra (CORBA::ULong len,
42 ServerSequence_out seq)
44 ACE_DEBUG ((LM_DEBUG,
45 "(%P|%t) Create extra called with "
46 " length [%d]\n", len));
48 ACE_NEW_THROW_EX (seq,
49 ServerSequence (len),
50 CORBA::NO_MEMORY ());
52 seq->length (len);
54 for (CORBA::ULong cnt = 0 ;
55 cnt < len ;
56 cnt ++)
58 ServerServant *servant = 0;
60 ACE_NEW_THROW_EX (servant,
61 ServerServant (this->root_poa_.in (),
62 this->orb_.in ()),
63 CORBA::NO_MEMORY ());
65 PortableServer::ObjectId_var id =
66 this->root_poa_->activate_object (servant);
67 CORBA::Object_var object = this->root_poa_->id_to_reference (id.in ());
68 (*seq)[cnt] = Server::_narrow (object.in ());
71 ACE_DEBUG ((LM_DEBUG,
72 "(%P|%t) Returned from CreateExtra ()\n"));
77 void
78 ServerServant::DeleteExtra (const ServerSequence &seq)
80 ACE_DEBUG ((LM_DEBUG,
81 "(%P|%t) Deleting %d sequences\n", seq.length ()));
83 PortableServer::ObjectId_var oid;
84 PortableServer::ServantBase *servant = 0;
86 for (CORBA::ULong cnt = 0;
87 cnt < seq.length ();
88 cnt++)
90 oid =
91 this->root_poa_->reference_to_id (seq [cnt]);
92 servant =
93 this->root_poa_->reference_to_servant (seq [cnt]);
95 this->root_poa_->deactivate_object (oid.in ());
96 servant->_remove_ref ();
97 servant->_remove_ref ();
100 ACE_DEBUG ((LM_DEBUG,
101 "(%P|%t) Returned after deleting sequences\n"));
104 void
105 ServerServant::shutdown (void)
107 this->orb_->shutdown (0);
110 /******************************************************/
111 const ACE_TCHAR *ior_output_file = ACE_TEXT("test.ior");
114 parse_args (int argc, ACE_TCHAR *argv[])
116 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
117 int c;
119 while ((c = get_opts ()) != -1)
120 switch (c)
122 case 'o':
123 ior_output_file = get_opts.opt_arg ();
124 break;
126 case '?':
127 default:
128 ACE_ERROR_RETURN ((LM_ERROR,
129 "usage: %s "
130 "-o <iorfile>"
131 "\n",
132 argv [0]),
133 -1);
135 // Indicates successful parsing of the command line
136 return 0;
140 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
145 // Initialize the broker
146 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
148 if (parse_args (argc, argv) == -1)
149 return -1;
151 CORBA::Object_var vRootPOABase =
152 orb->resolve_initial_references ("RootPOA");
154 PortableServer::POA_var root_poa =
155 PortableServer::POA::_narrow (vRootPOABase.in ());
157 if (CORBA::is_nil (root_poa.in ()))
158 ACE_ERROR_RETURN ((LM_ERROR,
159 " (%P|%t) Panic: nil RootPOA\n"),
162 PortableServer::POAManager_ptr pRootPOAManager =
163 root_poa->the_POAManager ();
165 // Instantiate the server
166 ServerServant *servant = 0;
168 ACE_NEW_RETURN (servant,
169 ServerServant (root_poa.in (),
170 orb.in ()),
173 PortableServer::ServantBase_var owner_transfer(servant);
175 PortableServer::ObjectId_var id =
176 root_poa->activate_object (servant);
178 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
180 Server_var server =
181 Server::_narrow (object.in ());
183 // Announce the server
184 CORBA::String_var obj_ref =
185 orb->object_to_string (server.in ());
187 // Output the IOR to the <ior_output_file>
188 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
189 if (output_file == 0)
190 ACE_ERROR_RETURN ((LM_ERROR,
191 "Cannot open output file for writing IOR: %s",
192 ior_output_file),
194 ACE_OS::fprintf (output_file, "%s", obj_ref.in ());
195 ACE_OS::fclose (output_file);
197 pRootPOAManager->activate ();
199 orb->run ();
201 ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - event loop finished\n"));
203 root_poa->destroy (1, 1);
205 orb->destroy ();
207 catch (const CORBA::Exception& ex)
209 ex._tao_print_exception ("Exception caught:");
210 return 1;
213 return 0;