Merge pull request #2220 from DOCGroup/revert-2217-jwi-inetwraning
[ACE_TAO.git] / ACE / apps / drwho / server.cpp
blob40d1bea6f8057c265507d5ee475d20a49936ed33
2 //=============================================================================
3 /**
4 * @file server.cpp
6 * Driver program for the server. Note that it is easy to reuse the
7 * server for other distributed programs. Pretty much all that must
8 * change are the functions registered with the communciations
9 * manager.
11 * @author Douglas C. Schmidt
13 //=============================================================================
16 #include "Options.h"
17 #include "SMR_Server.h"
18 #include "ace/ACE.h"
19 #include "ace/Log_Msg.h"
20 #include "ace/OS_NS_signal.h"
21 #include "ace/OS_NS_time.h"
22 #include "ace/OS_NS_stdlib.h"
23 #include "ace/OS_NS_sys_socket.h"
25 static char *
26 time_stamp ()
28 time_t time_now;
29 char *temp;
31 time_now = ACE_OS::time (0);
32 temp = ACE_OS::asctime (ACE_OS::localtime (&time_now));
33 temp[12] = 0;
34 return temp;
37 // Catch the obvious signals and die with dignity...
39 static void
40 exit_server (int sig)
42 ACE_DEBUG ((LM_DEBUG,
43 "%s exiting on signal %S\n",
44 time_stamp (),
45 sig));
46 ACE_OS::exit (0);
49 // Returns TRUE if the program was started by INETD.
51 static int
52 started_by_inetd ()
54 sockaddr_in sin;
55 int size = sizeof sin;
57 return ACE_OS::getsockname (0,
58 reinterpret_cast<sockaddr *> (&sin),
59 &size) == 0;
62 // Does the drwho service.
64 static void
65 do_drwho (SMR_Server &smr_server)
67 if (smr_server.receive () == -1)
68 ACE_ERROR ((LM_ERROR,
69 "%p\n",
70 Options::program_name));
72 if (smr_server.send () == -1)
73 ACE_ERROR ((LM_ERROR,
74 "%p\n",
75 Options::program_name));
78 // If the server is started with any argument at all then it doesn't
79 // fork off a child process to do the work. This is useful when
80 // debugging!
82 int
83 ACE_TMAIN (int argc, ACE_TCHAR *argv[])
85 ACE_OS::signal (SIGTERM, (ACE_SignalHandler)exit_server);
86 ACE_OS::signal (SIGINT, (ACE_SignalHandler)exit_server);
87 ACE_OS::signal (SIGQUIT, (ACE_SignalHandler)exit_server);
89 Options::set_options (argc, argv);
90 Options::set_opt (Options::STAND_ALONE_SERVER);
92 int inetd_controlled = started_by_inetd ();
94 if (!inetd_controlled && Options::get_opt (Options::BE_A_DAEMON))
95 ACE::daemonize ();
97 SMR_Server smr_server (Options::port_number);
99 if (inetd_controlled)
100 do_drwho (smr_server);
101 else
103 for (;;)
104 do_drwho (smr_server);
106 /* NOTREACHED */
109 return 0;