summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/AVStreams/mpeg/source/server/server.cpp
blob: 2541d5f3fa809e3486946b9cad20a6aaa2706ebc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#include "server.h"
#include "../mpeg_server/Video_Server.h"

// creates a svc handler by passing "this", i.e. 
// a reference to the acceptor that created it
// this is needed by the svc_handler to remove the
// acceptor handle from the reactor
// called by the acceptor to create a new svc_handler to 
// handle the new connection.
int
Mpeg_Acceptor::make_svc_handler (Mpeg_Svc_Handler *&sh)
{
  ACE_NEW_RETURN (sh,
                  Mpeg_Svc_Handler (ACE_Reactor::instance (),
                                    this),
                  -1);
  return 0;
}

// initialize the svc_handler, and the acceptor. 
Mpeg_Svc_Handler::Mpeg_Svc_Handler (ACE_Reactor *reactor,
                                    Mpeg_Acceptor *acceptor)
  : ACE_Svc_Handler <ACE_SOCK_STREAM, 
                     ACE_NULL_SYNCH> (0, 0, reactor),
    acceptor_ (acceptor)
{
  
}

// Client connected to our control port
// called by the reactor (acceptor)
int
Mpeg_Svc_Handler::open (void *)
{

  // Lets use threads at a later point. The current scheme works fine
  // with fork..  
  // this will activate a thread 
  //  this->activate (THR_BOUND);
  switch (ACE_OS::fork ("child"))
    {
    case -1:
      // fork failed!!
      ACE_ERROR_RETURN ((LM_ERROR, 
                         "(%P|%t) fork failed\n"),
                        -1);
    case 0:
      // I am the child. i should handle this connection
      // close down the "listen-mode" socket
      ACE_Reactor::instance ()->remove_handler
        (this->acceptor_->get_handle (),
         ACCEPT_MASK);
      
      // handle this connection in the same thread
      this->svc ();

      ACE_DEBUG ((LM_DEBUG,
                  "(%P|%t) Child returning from Mpeg_Svc_handler::open\n"));
      return 0;
      
    default:
      // i am the parent. i should go back and listen for more
      // connections

      // (1) "this" will commit suicide, because this svc_handler is not required
      // in the parent. otherwise, a new mpeg_svc_handler will be created
      // for each connection, and will never go away, i.e. a memory leak
      // will result. 
      // (2) also, this closes down the "connected socket" in the
      // parent, so that when the child closes down its connected
      // socket the connection is actually closed. otherwise, the
      // connection would remain open forever because the parent still
      // has a connected socket.
      this->destroy ();
      ACE_DEBUG ((LM_DEBUG,
                  "(%P|%t) Parent Returning from Mpeg_Svc_Handler::open\n"));
      return 0;
    }
  return 0;
}


// this will handle the connection
int
Mpeg_Svc_Handler::svc (void)
{
  int result;
  result = this->handle_connection ();

  if (result != 0)

  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) Mpeg_Svc_Handler::svc exiting\n"));

  return result;
}

// handles the connection
int
Mpeg_Svc_Handler::handle_connection (ACE_HANDLE)
{
  int junk;
  int result;
  u_short port;
  ACE_UINT32 ip;

  // Client is sending us JUNK
  this->peer ().recv_n (&junk, sizeof junk);
  
  // Client is sending us it's port number
  this->peer ().recv_n (&port, sizeof port);
  
  // Client is sending us it's IP addr
  this->peer ().recv_n (&ip, sizeof ip);
  
  this->client_data_addr_.set (port,
                               ip,
                               0);

  ACE_DEBUG ((LM_DEBUG, 
              "(%P|%t) Client IP == %s, "
              "Client Port == %d\n",
              client_data_addr_.get_host_name (),
              client_data_addr_.get_port_number ()));

  this->server_data_addr_.set ((unsigned short)0);

  // "Connect" our dgram to the client endpoint.
  if (this->dgram_.open (client_data_addr_) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, 
                       "(%P|%t) UDP open failed: %p\n"),
                      -1);
  
  if (this->dgram_.get_local_addr (this->server_data_addr_) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, 
                       "(%P|%t) UDP get_local_addr failed: %p\n"),
                      -1);

  ACE_DEBUG ((LM_DEBUG, 
              "(%P|%t) UDP IP address is %s, and the port number is %d\n",
              this->server_data_addr_.get_host_addr (),
              this->server_data_addr_.get_port_number ()));

  port = this->server_data_addr_.get_port_number ();

  // %% we need to fix this ?
  // XXX this is a hack to get my IP address set correctly! By default,
  // get_ip_address is returning 0.0.0.0, even after calling
  // get_local_addr () !!
  this->server_data_addr_.set (port,
                               this->server_data_addr_.get_host_name ());

  ip = this->server_data_addr_.get_ip_address ();

  port = htons (port);
  ip = htonl (ip);
  // Client wants us to send the port number first
  this->peer ().send_n (&port,
                        (int) sizeof (u_short));

  // Client wants us to send it the IP address
  this->peer ().send_n (&ip,
                        (int) sizeof (ACE_UINT32));

  // Client is sending us a command
  u_char cmd;
  if (this->peer ().recv_n (&cmd,
                            1) == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "%P|%t, Command recieve failed: %p"),
                       -1);
  // Change these CMD's to enums and put them in a "appropriate" namespace
  switch (cmd)
    {
    case CmdINITvideo:
      {
        // %% what does this do ?!!
        if (Mpeg_Global::live_audio) LeaveLiveAudio ();
        /* result = VideoServer (this->peer ().get_handle (), 
                              this->dgram_.get_handle (), 
                              Mpeg_Global::rttag, 
                              -INET_SOCKET_BUFFER_SIZE); 
        */
               
        ACE_NEW_RETURN (this->vs_,
                        Video_Server (this->peer ().get_handle (),
                                      this->dgram_.get_handle (),
                                      Mpeg_Global::rttag,
                                      -INET_SOCKET_BUFFER_SIZE),
                        -1);

        // enters the Video_Server run method
        result = this->vs_->run ();

        if (result != 0)
          ACE_ERROR_RETURN ((LM_ERROR,
                             "(%P|%t) handle_connection: "),
                             result);
        return result;
                 
      }
      break;
    default:
      if (Mpeg_Global::live_audio) LeaveLiveAudio();
      result = AudioServer (this->peer ().get_handle (), 
                            this->dgram_.get_handle (), 
                            Mpeg_Global::rttag, 
                            -INET_SOCKET_BUFFER_SIZE);
      ACE_Reactor::instance ()->end_event_loop ();
      if (result != 0)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "(%P|%t)handle_connection : "),
                          result);
      return result;
     
    }

  return 0;
}

int
Mpeg_Svc_Handler::close (u_long)
{
  ACE_DEBUG ((LM_DEBUG, "(%P|%t)Mpeg_Svc_Handler::close called \n"));
  return 0;
}

int
Mpeg_Svc_Handler::handle_timeout (const ACE_Time_Value &,
                                  const void *arg)
{
  ACE_DEBUG ((LM_DEBUG, "(%P|%t)Mpeg_Svc_Handler::handle_timeout called \n"));
  return 0;
}

// AV_Server_Sig_Handler routines
// Video_Sig_Handler methods
// handles the timeout SIGALRM signal
// %% this should *not* register itself,but it should
// be registered by the Video_Server::run, alongwith
// the remaining  handlers.
AV_Server_Sig_Handler::AV_Server_Sig_Handler (void)
{
}

int
AV_Server_Sig_Handler::register_handler (void)
{
  // Assign the Sig_Handler a dummy I/O descriptor.  Note that even
  // though we open this file "Write Only" we still need to use the
  // ACE_Event_Handler::NULL_MASK when registering this with the
  // ACE_Reactor (see below).
  this->handle_ = ACE_OS::open (ACE_DEV_NULL, O_WRONLY);
  ACE_ASSERT (this->handle_ != -1);

  // Register signal handler object.  Note that NULL_MASK is used to
  // keep the ACE_Reactor from calling us back on the "/dev/null"
  // descriptor.
  if (ACE_Reactor::instance ()->register_handler 
      (this, ACE_Event_Handler::NULL_MASK) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, 
                       "%p\n", 
                       "register_handler"),
                      -1);

  // Create a sigset_t corresponding to the signals we want to catch.
  ACE_Sig_Set sig_set;

  //  sig_set.sig_add (SIGINT);
  // sig_set.sig_add (SIGQUIT);
  sig_set.sig_add (SIGCHLD);  
  sig_set.sig_add (SIGBUS);
  sig_set.sig_add (SIGINT);
  sig_set.sig_add (SIGTERM);

  // Register the signal handler object to catch the signals.
  if (ACE_Reactor::instance ()->register_handler (sig_set, 
                                                  this) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, 
                       "%p\n", 
                       "register_handler"),
                      -1);

  return 0;
}
// Called by the ACE_Reactor to extract the fd.

ACE_HANDLE
AV_Server_Sig_Handler::get_handle (void) const
{
  return this->handle_;
}

int 
AV_Server_Sig_Handler::handle_input (ACE_HANDLE)
{
  ACE_DEBUG ((LM_DEBUG, "(%t) handling asynchonrous input...\n"));
  return 0;
}

int 
AV_Server_Sig_Handler::shutdown (ACE_HANDLE, ACE_Reactor_Mask)
{
  ACE_DEBUG ((LM_DEBUG, "(%t) closing down Sig_Handler...\n"));
  return 0;
}

// This method handles all the signals that are being caught by this
// object.  In our simple example, we are simply catching SIGALRM,
// SIGINT, and SIGQUIT.  Anything else is logged and ignored.
//
// There are several advantages to using this approach.  First, 
// the behavior triggered by the signal is handled in the main event
// loop, rather than in the signal handler.  Second, the ACE_Reactor's 
// signal handling mechanism eliminates the need to use global signal 
// handler functions and data. 

int
AV_Server_Sig_Handler::handle_signal (int signum, siginfo_t *, ucontext_t *)
{
  //  ACE_DEBUG ((LM_DEBUG, "(%t) received signal %S\n", signum));

  switch (signum)
    {
    case SIGCHLD:
      // Handle the timeout
      AV_Server::clear_child (SIGCHLD);
      break;
    case SIGBUS:
    case SIGINT:
    case SIGTERM:
      AV_Server::int_handler (signum);
      ACE_DEBUG ((LM_DEBUG, 
		  "(%t) %S: not handled, returning to program\n", 
                  signum));
      break;
    }
  return 0;
}


// AV_Server routines

// Default Constructor
AV_Server::AV_Server ()
{
  this->sh_ = new AV_Server_Sig_Handler ;
}

//  Cluttering the code with various signal handlers here.

//  ctrl-c handler,Bus error handler,interrupt sig handler
void
AV_Server::int_handler (int sig)
{
  ACE_DEBUG ((LM_DEBUG, 
              "(%P|%t) killed by signal %d",
              sig));
  exit (0);
}

void
AV_Server::on_exit_routine (void)
{
  // %% what does the following do
  if (Mpeg_Global::parentpid != ACE_OS::getpid ()) 
    {
      ACE_DEBUG ((LM_DEBUG, 
                  "(%P|%t) Process is exiting\n"));
      return;
    }
  
  // %% what does the following do
  if (Mpeg_Global::live_audio > 1) ExitLiveAudio ();
  if (Mpeg_Global::live_video > 1) ExitLiveVideo ();
  //  ComCloseServer();
}

// SIGCHLD handler
void
AV_Server::clear_child (int sig)
{
  int pid;
  int status;
  
  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) Reaping the children\n"));
  // reap the children
  while ((pid = ACE_OS::waitpid (-1, 
                                 &status, 
                                 WNOHANG)) > 0)
  {
    // decrement the count of number of active children
    Mpeg_Global::session_num --;
    
    if (status == 0) 
      {
        continue;
      }
    
    ACE_DEBUG ((LM_DEBUG, 
                "(%P|%t) VCRS: child %d (status %d) ", 
                pid, 
                status));

    // %% what does the following do
    if (WIFEXITED(status)) 
      {
        fprintf(stderr, "exited with status %d\n", WEXITSTATUS(status));
      }
    else if (WIFSIGNALED(status)) 
      {
        // %% can we remove the below ?
#if defined(_HPUX_SOURCE) || defined(__svr4__) || defined(IRIX)
        fprintf(stderr, "terminated at signal %d%s.\n", WTERMSIG(status),
                WCOREDUMP(status) ? ", core dumped" : "");
#else
        fprintf(stderr, "terminated at signal %d.\n", WTERMSIG(status));
#endif
      }
    else if (WIFSTOPPED(status)) 
      {
        fprintf(stderr, "stopped at signal %d\n", WSTOPSIG(status));
      }
    
  }
}

// Parses the command line arguments
int
AV_Server::parse_args (int argc,
                         char **argv)
{
  ACE_Get_Opt get_opts (argc, argv, "rd:s:vamh");
  int c;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'r': // real time flag
        Mpeg_Global::rttag = 1;
        break;
      case 'd': // clock drift in ppm
        Mpeg_Global::drift_ppm = ACE_OS::atoi (get_opts.optarg);
        break;
      case 's':// limit on number of sessions
        Mpeg_Global::session_limit = ACE_OS::atoi (get_opts.optarg);
        break;
      case 'v':// live video flag
        Mpeg_Global::live_video = 1;
        break;
      case 'a':// live audio flag
        Mpeg_Global::live_audio = 1;
        break;
      case 'm':// remove flag
        ACE_OS::unlink (VCR_UNIX_PORT);
        break;
      case '?':
      case 'h':// help flag
        ACE_DEBUG ((LM_DEBUG,
                    "Usage: %s [-r ] [-m]\n"
                    "          [-d#int(clock drift in ppm)]\n"
                    "          [-s#int(limit on number of sessions)]\n"
                    "          [-v] [-a] [-?] [-h]",
                    argv [0]));
        return -1;
      }
  return 0;
}

// sets the handlers for the various signals
int
AV_Server::set_signals ()
{
  setsignal (SIGCHLD, clear_child);
  setsignal (SIGPIPE, SIG_IGN);    
  setsignal (SIGBUS, int_handler); 
  setsignal (SIGINT, int_handler); 
  setsignal (SIGTERM, int_handler);
  //  setsignal(SIGALRM, SIG_IGN);
  return 0;
}

        
// Initializes the mpeg server
int
AV_Server::init (int argc,
                   char **argv)
{
  int result;

  result = this->parse_args (argc, argv);
  if (result < 0)
    return result;

  // This code has become obsolete with the new AV_Server_Sig_Handler class..
  //  this->set_signals ();
  // Register the various signal handlers with the reactor.
  result = this->sh_->register_handler ();

  if (result < 0)
    return result;

  Mpeg_Global::parentpid = ACE_OS::getpid ();
  
  ::atexit (on_exit_routine);
  
  if (Mpeg_Global::live_audio) 
    {
      if (InitLiveAudio (argc, argv) == -1)
        Mpeg_Global::live_audio = 0;
      else
        Mpeg_Global::live_audio = 2;
    }

  if (Mpeg_Global::live_video) 
    {
      if (InitLiveVideo (argc, argv) == -1)
        Mpeg_Global::live_video = 0;
      else
        Mpeg_Global::live_video = 2;
    }
  
  // open LOG_DIR/vcrsSession.log as the stdout
  // if not, use /dev/null
  {
    char buf [100];
    ACE_OS::sprintf (buf, 
                     "%s%s", 
                     LOG_DIR, 
                     "vcrsSession.log");

    if (::freopen (buf, 
                   "a", 
                   stdout) == NULL) 
      {
        ::freopen ("/dev/null", 
                   "w", 
                   stdout);
      }
  }
  return 0;
}

// Runs the mpeg server
int
AV_Server::run ()
{
  int result;
  this->server_control_addr_.set (VCR_TCP_PORT);

  // "listen" on the socket
  if (this->acceptor_.open (this->server_control_addr_) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "open"), -1);

  ACE_Reactor::instance ()->run_event_loop ();

  ACE_DEBUG ((LM_DEBUG,
              "(%P)AV_Server::run () "
              "came out of the (acceptor) "
              "event loop %p\n",
              "run_event_loop\n"));
}

AV_Server::~AV_Server (void)
{
  if (this->sh_ != 0)
    delete this->sh_;
}

int
main (int argc, char **argv)
{
  AV_Server vcr_server;
  
  // parses the arguments, and initializes the server
  if (vcr_server.init (argc, argv) < 0)
    return 1;
  
  // runs the reactor event loop
  vcr_server.run ();
  
  return 0;
}