summaryrefslogtreecommitdiff
path: root/ACE/apps/drwho/server.cpp
blob: 6039eb56d45a3d3e4f5fa96d0af870c1d75d4937 (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

//=============================================================================
/**
 *  @file    server.cpp
 *
 *  $Id$
 *
 * Driver program for the server.  Note that it is easy to reuse the
 * server for other distributed programs.  Pretty much all that must
 * change are the functions registered with the communciations
 * manager.
 *
 *
 *  @author Douglas C. Schmidt
 */
//=============================================================================


#include "Options.h"
#include "SMR_Server.h"
#include "ace/ACE.h"
#include "ace/Log_Msg.h"
#include "ace/OS_NS_signal.h"
#include "ace/OS_NS_time.h"
#include "ace/OS_NS_stdlib.h"
#include "ace/OS_NS_sys_socket.h"

static char *
time_stamp (void)
{
  time_t time_now;
  char *temp;

  time_now = ACE_OS::time (0);
  temp = ACE_OS::asctime (ACE_OS::localtime (&time_now));
  temp[12] = 0;
  return temp;
}

// Catch the obvious signals and die with dignity...

static void
exit_server (int sig)
{
  ACE_DEBUG ((LM_DEBUG,
              "%s exiting on signal %S\n",
              time_stamp (),
              sig));
  ACE_OS::exit (0);
}

// Returns TRUE if the program was started by INETD.

static int
started_by_inetd (void)
{
  sockaddr_in sin;
  int size = sizeof sin;

  return ACE_OS::getsockname (0,
                              reinterpret_cast<sockaddr *> (&sin),
                              &size) == 0;
}

// Does the drwho service.

static void
do_drwho (SMR_Server &smr_server)
{
  if (smr_server.receive () == -1)
    ACE_ERROR ((LM_ERROR,
                "%p\n",
                Options::program_name));

  if (smr_server.send () == -1)
    ACE_ERROR ((LM_ERROR,
                "%p\n",
                Options::program_name));
}

// If the server is started with any argument at all then it doesn't
// fork off a child process to do the work.  This is useful when
// debugging!

int
ACE_TMAIN (int argc, ACE_TCHAR *argv[])
{
  ACE_OS::signal (SIGTERM, (ACE_SignalHandler)exit_server);
  ACE_OS::signal (SIGINT, (ACE_SignalHandler)exit_server);
  ACE_OS::signal (SIGQUIT, (ACE_SignalHandler)exit_server);

  Options::set_options (argc, argv);
  Options::set_opt (Options::STAND_ALONE_SERVER);

  int inetd_controlled = started_by_inetd ();

  if (!inetd_controlled && Options::get_opt (Options::BE_A_DAEMON))
    ACE::daemonize ();

  SMR_Server smr_server (Options::port_number);

  if (inetd_controlled)
    do_drwho (smr_server);
  else
    {

      for (;;)
        do_drwho (smr_server);

      /* NOTREACHED */
    }

  return 0;
}