Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / TAO / orbsvcs / tests / AVStreams / Pluggable / server.cpp
blob7dfca4aa58df9b66386d30644aed4478827a46d5
3 #include "server.h"
4 #include "ace/Get_Opt.h"
6 static FILE *output_file = 0;
7 // File into which the received data is written.
9 static const ACE_TCHAR *output_file_name = ACE_TEXT ("output");
10 // File handle of the file into which data is written.
12 int done = 0;
15 int
16 FTP_Server_StreamEndPoint::get_callback (const char *,
17 TAO_AV_Callback *&callback)
19 // Return the server application callback to the AVStreams for further upcalls,
20 callback = &this->callback_;
21 return 0;
24 int
25 FTP_Server_Callback::receive_frame (ACE_Message_Block *frame,
26 TAO_AV_frame_info *,
27 const ACE_Addr &)
29 // Upcall from the AVStreams when there is data to be received from the
30 // ftp client.
32 ACE_DEBUG ((LM_DEBUG,
33 "FTP_Server_Callback::receive_frame\n"));
35 while (frame != 0)
37 // Write the received data to the file.
38 size_t result = ACE_OS::fwrite (frame->rd_ptr (),
39 frame->length (),
41 output_file);
43 if (result == frame->length ())
44 ACE_ERROR_RETURN ((LM_ERROR,
45 "FTP_Server_Flow_Handler::fwrite failed\n"),
46 -1);
48 frame = frame->cont ();
50 return 0;
53 int
54 FTP_Server_Callback::handle_destroy ()
56 // Called when the ftp client requests the stream to be shutdown.
57 ACE_DEBUG ((LM_DEBUG,
58 "FTP_Server_Callback::end_stream\n"));
59 done = 1;
60 return 0;
63 Server::Server ()
64 : mmdevice_ (0)
68 Server::~Server ()
70 delete this->mmdevice_;
73 int
74 Server::init (int,
75 ACE_TCHAR *[])
77 int result =
78 this->reactive_strategy_.init (TAO_AV_CORE::instance ()->orb (),
79 TAO_AV_CORE::instance ()->poa ());
80 if (result != 0)
81 return result;
83 // Register the server mmdevice object with the ORB
84 ACE_NEW_RETURN (this->mmdevice_,
85 TAO_MMDevice (&this->reactive_strategy_),
86 -1);
88 // Register the mmdevice with the naming service.
89 CosNaming::Name server_mmdevice_name (1);
90 server_mmdevice_name.length (1);
91 server_mmdevice_name [0].id = CORBA::string_dup ("Server_MMDevice");
93 CORBA::Object_var mmdevice =
94 this->mmdevice_->_this ();
96 // Initialize the naming services
97 if (this->my_naming_client_.init (TAO_AV_CORE::instance ()->orb ()) != 0)
98 ACE_ERROR_RETURN ((LM_ERROR,
99 "Unable to initialize "
100 "the TAO_Naming_Client\n"),
101 -1);
103 // Register the server object with the naming server.
104 this->my_naming_client_->rebind (server_mmdevice_name,
105 mmdevice.in ());
107 return 0;
111 parse_args (int argc,
112 ACE_TCHAR *argv[])
114 ACE_Get_Opt opts (argc,
115 argv,
116 "f:");
118 int c;
119 while ((c = opts ()) != -1)
121 switch (c)
123 case 'f':
124 output_file_name = opts.opt_arg ();
125 break;
126 default:
127 ACE_ERROR_RETURN ((LM_ERROR,
128 "Usage: server -f filename"),
129 -1);
133 return 0;
137 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
141 // Initialize the ORB first.
142 CORBA::ORB_var orb = CORBA::ORB_init (argc,
143 argv);
145 int result =
146 parse_args (argc,
147 argv);
149 if (result == -1)
150 return -1;
152 // Make sure we have a valid <output_file>
153 output_file = ACE_OS::fopen (output_file_name,
154 "w");
155 if (output_file == 0)
156 ACE_ERROR_RETURN ((LM_DEBUG,
157 "Cannot open output file %s\n",
158 output_file_name),
159 -1);
161 else ACE_DEBUG ((LM_DEBUG,
162 "File Opened Successfully\n"));
164 CORBA::Object_var obj
165 = orb->resolve_initial_references ("RootPOA");
167 // Get the POA_var object from Object_var.
168 PortableServer::POA_var root_poa =
169 PortableServer::POA::_narrow (obj.in ());
171 PortableServer::POAManager_var mgr
172 = root_poa->the_POAManager ();
174 mgr->activate ();
176 // Initialize the AVStreams components.
177 TAO_AV_CORE::instance ()->init (orb.in (),
178 root_poa.in ());
180 Server server;
181 result =
182 server.init (argc,
183 argv);
185 if (result != 0)
186 return result;
188 while ( !done )
190 if ( orb->work_pending())
192 orb->perform_work ();
196 orb->shutdown(1);
198 catch (const CORBA::Exception& ex)
200 ex._tao_print_exception ("server::init");
201 return -1;
204 ACE_OS::fclose (output_file);
206 return 0;