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
|
// -*- c++ -*-
// Generated by assa-genesis
//------------------------------------------------------------------------------
// $Id: daytime.cpp,v 1.8 2006/07/20 02:30:55 vlg Exp $
//------------------------------------------------------------------------------
// DayTime_Server.cpp
//------------------------------------------------------------------------------
// Copyright (c) 2002,2005 by Vladislav Grinchenko
//
// Permission to use, copy, modify, and distribute this software
// and its documentation for any purpose and without fee is hereby
// granted, provided that the above copyright notice appear in all
// copies. The author makes no representations about the suitability
// of this software for any purpose. It is provided "as is" without
// express or implied warranty.
//------------------------------------------------------------------------------
//
// Date : Fri Oct 18 23:32:23 2002
//
//------------------------------------------------------------------------------
static const char help_msg[]=
" \n"
" NAME: \n"
" \n"
" daytime - a daytime server \n"
" \n"
" DESCRIPTION: \n"
" \n"
" Provides caller with local date. Server ignores any requests, \n"
" writes the date out to the client, discards any requests received, \n"
" and closes connection. \n"
" \n"
" By default, it listens on $ASSAPORT/tcp socket for service requests. \n"
" This can be overwritten with {-p, --port=PORT } option. \n"
" \n"
" USAGE: \n"
" \n"
" shell> daytime [OPTIONS] \n"
" \n"
" OPTIONS: \n"
" \n"
" --one-shot - Serve exactly one request and exit afterwards. \n"
" --with-timestamp - Log messages with timestamp \n"
" -b, --daemon - Run process as true UNIX daemon \n"
" -l, --pidfile PATH - The process ID is written to the lockfile PATH \n"
" instead of default ~/.{procname}.pid \n"
" -L, --no-pidfile - Do not create PID lockfile \n"
" \n"
" -D, --log-file NAME - Write debug to NAME file \n"
" -d, --log-stdout - Write debug to standard output \n"
" -z, --log-size NUM - Maximum size debug file can reach (dfl: is 10Mb) \n"
" \n"
" -m, --mask MASK - Mask (default: ALL = 0x7fffffff) \n"
" -p, --port NAME - The tcp/ip port NAME (default - procname) \n"
" -n, --instance NUM - Process instance NUM (default - none) \n"
" \n"
" -h, --help - Print this messag \n"
" -v, --version - Print version number \n";
//------------------------------------------------------------------------------
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string>
using std::string;
#include <assa/GenServer.h>
#include <assa/Singleton.h>
#include <assa/TimeVal.h>
#include <assa/ServiceHandler.h>
#include <assa/IPv4Socket.h>
#include <assa/INETAddress.h>
#include <assa/Acceptor.h>
using namespace ASSA;
/*******************************************************************************
Class Responder
*******************************************************************************/
class Responder :
public ServiceHandler<IPv4Socket>
{
public:
Responder (IPv4Socket* stream_);
virtual int open (void);
virtual int handle_read (int fd_);
virtual int handle_close (int fd_);
};
/*******************************************************************************
Class DayTime_Server
*******************************************************************************/
class DayTime_Server :
public GenServer,
public Singleton<DayTime_Server>
{
public:
DayTime_Server ();
virtual void init_service ();
virtual void process_events ();
int get_exit_value () const { return m_exit_value; }
void set_exit_value (int v_) { m_exit_value = v_; }
bool one_shot () const { return m_one_shot; }
private:
Acceptor<Responder, IPv4Socket>* m_listener;
int m_exit_value; // Return status of the process
bool m_one_shot;
bool m_with_timestamp;
};
/*******************************************************************************
Useful definitions
*******************************************************************************/
#define DAYTIME_SERVER DayTime_Server::get_instance()
#define REACTOR DAYTIME_SERVER->get_reactor()
// Static declarations mandated by Singleton class
ASSA_DECL_SINGLETON(DayTime_Server);
/*******************************************************************************
Class Responder member functions
*******************************************************************************/
Responder::
Responder (IPv4Socket* stream_)
: ServiceHandler<IPv4Socket> (stream_)
{
trace("Responder::Responder");
// no-op
}
int
Responder::
open (void)
{
trace("Responder::open");
IPv4Socket& stream = get_stream ();
REACTOR->registerIOHandler (this, stream.getHandler (), READ_EVENT);
/*
* Return date in readable format: "Sat Oct 19 23:00:57 EDT 2002"
*/
string response (TimeVal::gettimeofday ().fmtString ("%c"));
response += "\n";
size_t len = response.length ();
DL((APP,"Sending response: \"%s\" (%d bytes)\n",response.c_str (), len));
Assure_exit (stream.write (response.c_str (), len) == len);
stream << flush;
/* By closing stream here we let Reactor detect 'BAD FD' in its
* select() loop and clean up afterwards.
*/
stream.close ();
if (DAYTIME_SERVER->one_shot ()) {
DAYTIME_SERVER->stop_service ();
}
return -1;
}
int
Responder::
handle_read (int fd_)
{
trace("Responder::handle_read");
/* Discard whatever I read from a client - required by RFC, but
* never called.
*/
IPv4Socket& s = get_stream ();
s.ignore ();
return BYTES_LEFT_IN_SOCKBUF(s);
}
int
Responder::
handle_close (int fd_)
{
trace("Responder::handle_close");
delete this;
return 0;
}
/*******************************************************************************
Class DayTime_Server Member Functions
*******************************************************************************/
DayTime_Server::
DayTime_Server () :
m_listener (NULL),
m_exit_value (0),
m_one_shot (false),
m_with_timestamp (false)
{
add_flag_opt (0, "one-shot", &m_one_shot);
add_flag_opt (0, "with-timestamp", &m_with_timestamp);
// ---Configuration---
rm_opt ('f', "config-file" );
/*---
* Disable all debugging
*---*/
m_mask = 0x0;
}
void
DayTime_Server::
init_service ()
{
trace("DayTime_Server::init_service");
if (m_with_timestamp) {
Log::enable_timestamp ();
LOGGER->set_timezone (1);
}
/* We overwrite default m_port value which would be
otherwise set to the process' name - "daytime".
*/
char* assaport = ::getenv("ASSAPORT");
m_port = assaport != NULL ? assaport : "10000";
DL((APP,"m_port = %s\n", m_port.c_str ()));
m_listener = new Acceptor<Responder, IPv4Socket> (get_reactor ());
INETAddress addr (get_port ().c_str ());
addr.dump ();
if (addr.bad ()) {
EL((ASSA::ASSAERR,"Bad listening address!\n"));
set_exit_value (1);
stop_service ();
return;
}
Assure_exit (m_listener->open (addr) == 0);
DL((APP,"Service has been initialized\n"));
}
void
DayTime_Server::
process_events ()
{
trace("DayTime_Server::process_events");
while (service_is_active ()) {
m_reactor.waitForEvents ();
}
m_reactor.stopReactor ();
DL((APP,"Service stopped!\n"));
}
int
main (int argc, char* argv[])
{
static const char release[] = "VERSION";
int patch_level = 0;
DAYTIME_SERVER->set_version (release, patch_level);
DAYTIME_SERVER->set_author ("Vladislav Grinchenko");
DAYTIME_SERVER->set_flags (GenServer::RMLOG);
DAYTIME_SERVER->init (&argc, argv, help_msg);
DAYTIME_SERVER->init_service ();
DAYTIME_SERVER->process_events ();
return DAYTIME_SERVER->get_exit_value ();
}
|