Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / tests / Objref_Sequence_Test / server.cpp
blob6a53f680d9643ccf5c7c6774f2ccc790cae07fc8
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 ();
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"));
76 void
77 ServerServant::DeleteExtra (const ServerSequence &seq)
79 ACE_DEBUG ((LM_DEBUG,
80 "(%P|%t) Deleting %d sequences\n", seq.length ()));
82 PortableServer::ObjectId_var oid;
83 PortableServer::ServantBase *servant = 0;
85 for (CORBA::ULong cnt = 0;
86 cnt < seq.length ();
87 cnt++)
89 oid =
90 this->root_poa_->reference_to_id (seq [cnt]);
91 servant =
92 this->root_poa_->reference_to_servant (seq [cnt]);
94 this->root_poa_->deactivate_object (oid.in ());
95 servant->_remove_ref ();
96 servant->_remove_ref ();
99 ACE_DEBUG ((LM_DEBUG,
100 "(%P|%t) Returned after deleting sequences\n"));
103 void
104 ServerServant::shutdown ()
106 this->orb_->shutdown (false);
109 /******************************************************/
110 const ACE_TCHAR *ior_output_file = ACE_TEXT("test.ior");
113 parse_args (int argc, ACE_TCHAR *argv[])
115 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:"));
116 int c;
118 while ((c = get_opts ()) != -1)
119 switch (c)
121 case 'o':
122 ior_output_file = get_opts.opt_arg ();
123 break;
125 case '?':
126 default:
127 ACE_ERROR_RETURN ((LM_ERROR,
128 "usage: %s "
129 "-o <iorfile>"
130 "\n",
131 argv [0]),
132 -1);
134 // Indicates successful parsing of the command line
135 return 0;
139 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
144 // Initialize the broker
145 CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);
147 if (parse_args (argc, argv) == -1)
148 return -1;
150 CORBA::Object_var vRootPOABase =
151 orb->resolve_initial_references ("RootPOA");
153 PortableServer::POA_var root_poa =
154 PortableServer::POA::_narrow (vRootPOABase.in ());
156 if (CORBA::is_nil (root_poa.in ()))
157 ACE_ERROR_RETURN ((LM_ERROR,
158 " (%P|%t) Panic: nil RootPOA\n"),
161 PortableServer::POAManager_ptr pRootPOAManager =
162 root_poa->the_POAManager ();
164 // Instantiate the server
165 ServerServant *servant = 0;
167 ACE_NEW_RETURN (servant,
168 ServerServant (root_poa.in (),
169 orb.in ()),
172 PortableServer::ServantBase_var owner_transfer(servant);
174 PortableServer::ObjectId_var id =
175 root_poa->activate_object (servant);
177 CORBA::Object_var object = root_poa->id_to_reference (id.in ());
179 Server_var server =
180 Server::_narrow (object.in ());
182 // Announce the server
183 CORBA::String_var obj_ref =
184 orb->object_to_string (server.in ());
186 // Output the IOR to the <ior_output_file>
187 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
188 if (output_file == 0)
189 ACE_ERROR_RETURN ((LM_ERROR,
190 "Cannot open output file for writing IOR: %s",
191 ior_output_file),
193 ACE_OS::fprintf (output_file, "%s", obj_ref.in ());
194 ACE_OS::fclose (output_file);
196 pRootPOAManager->activate ();
198 orb->run ();
200 ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - event loop finished\n"));
202 root_poa->destroy (true, true);
204 orb->destroy ();
206 catch (const CORBA::Exception& ex)
208 ex._tao_print_exception ("Exception caught:");
209 return 1;
212 return 0;