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
|
// -*- c++ -*-
//------------------------------------------------------------------------------
// Conn.cpp
//------------------------------------------------------------------------------
// $Id: Conn.cpp,v 1.5 2006/07/20 02:30:55 vlg Exp $
//------------------------------------------------------------------------------
// Copyright (c) 2003 by Vladislav Grinchenko
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version
// 2 of the License, or (at your option) any later version.
//------------------------------------------------------------------------------
// Created: Thu Apr 17 23:22:39 EDT 2003
//------------------------------------------------------------------------------
#include "Conn.h"
#include "LogServer.h"
#include "MonitorConn.h"
static const char sep[]="-------------------------------------------------";
int
Conn::
open ()
{
trace_with_mask("Conn::open",LSVRTRACE);
REACTOR->registerIOHandler (this, get_stream ().getHandler (),
ASSA::READ_EVENT);
DL((LSVR,"+--------------------------------+\n"));
DL((LSVR,"| Accepted new client connection |\n"));
DL((LSVR,"+--------------------------------+\n"));
return 0;
}
int
Conn::
handle_close (int /* fd */)
{
trace_with_mask("Conn::handle_close", LSVRTRACE);
DL((LSVR,"+---------------------+\n"));
DL((LSVR,"| Client disconnected |\n"));
DL((LSVR,"+---------------------+\n"));
/** Disconnect from observers
*/
if (m_observers.size ()) {
ASSA::Repository<MonitorConn>::const_iterator cit;
cit = m_observers.begin ();
while (cit != m_observers.end ()) {
(*cit++)->notify (NULL);
}
m_observers.clear ();
}
REPO->erase (this);
delete (this);
return 0;
}
int
Conn::
handle_read (int fd_)
{
trace_with_mask ("Conn::handle_read", LSVRTRACE);
if (get_stream ().getHandler () != fd_) {
return (-1);
}
if (m_wstate == wait_for_header) {
int preamble = 0;
m_msg_type = m_msg_size = 0;
get_stream () >> preamble >> m_msg_type >> m_msg_size;
if (preamble != 1234567890) {
DL((LSVRERROR,"Message stream is out of sync - Abort!\n"));
return -1;
}
DL((LSVR,"=> Detected Header\n"));
DL((LSVR,"rcvd: Preamble = %d\n", preamble));
DL((LSVR,"rcvd: Type = %d\n", m_msg_type));
DL((LSVR,"rcvd: Size = %d\n", m_msg_size));
switch (m_msg_type)
{
case SIGN_ON: m_wstate = wait_for_signon; break;
case SIGN_OFF: m_wstate = wait_for_signoff; break;
case LOG_MSG: m_wstate = wait_for_logmsg; break;
}
}
if (m_wstate == wait_for_signon) {
m_maxsize = 0;
DL((LSVR,"=> Incoming SIGN_ON message\n"));
Assure_exit (m_state == closed);
get_stream () >> m_maxsize >> m_app_name >> m_logfname;
DL((LSVR,"rcvd: MaxLogSize = %d, AppName = \"%s\"\n",
m_maxsize, m_app_name.c_str ()));
DL((LSVR,"rcvd: LogFileName = \"%s\"\n", m_logfname.c_str ()));
std::string path = LOGSERVER->get_log_dir () + "/" + m_logfname;
if (LOGSERVER->recycle ()) {
::unlink (path.c_str ());
}
m_sink.open (path.c_str (), std::ios::out | std::ios::app);
if (!m_sink) {
DL((LSVRERROR,"m_sink.open (\"%s\",...) = -1\n", path.c_str ()));
return -1;
}
m_state = opened;
m_wstate = wait_for_header;
REPO->push_back (this);
}
else if (m_wstate == wait_for_signoff) {
DL((LSVR,"=> Incoming SIGN_OFF message\n"));
Assure_exit (m_state == opened);
m_sink << std::flush;
m_sink.close ();
m_state = closed;
return -1;
}
else if (m_wstate == wait_for_logmsg) {
DL((LSVR,"=> Incoming LOG_MSG message\n"));
Assure_exit (m_state == opened);
std::string msg;
if (get_stream () >> msg) {
if (msg.length () != 0) {
DL((LSVR,"rcvs message:\n%s\n%s%s\n", sep, msg.c_str (), sep));
m_bytecount += msg.length ();
if (m_bytecount > m_maxsize) {
shift_logfile ();
}
}
else {
DL((LSVR,"rcvs EMPTY message!\n"));
Assure_exit (false);
}
m_sink << msg << std::flush;
if (m_observers.size ()) {
ASSA::Repository<MonitorConn>::const_iterator cit;
cit = m_observers.begin ();
while (cit != m_observers.end ()) {
(*cit++)->notify (msg.c_str ());
}
}
}
else {
DL((LSVRERROR,"Peer dropped connection!\n"));
m_sink << std::flush;
m_sink.close ();
DL((LSVR,"m_bytecount = %d\n", m_bytecount));
DL((LSVR,"m_maxsize = %d\n", m_maxsize));
}
m_wstate = wait_for_header;
}
return BYTES_LEFT_IN_SOCKBUF(get_stream ());
}
void
Conn::
shift_logfile ()
{
trace_with_mask("Conn::shift_logfile", LSVRTRACE);
m_sink << std::flush;
m_sink.close ();
m_bytecount = 0;
std::string oldfile = m_logfname + ".0";
::unlink (oldfile.c_str ());
::rename (m_logfname.c_str (), oldfile.c_str ());
m_sink.open (m_logfname.c_str (), std::ios::out | std::ios::app);
if (!m_sink) {
DL((LSVRERROR,"m_sink.open (\"%s\",...) = -1\n", m_logfname.c_str ()));
}
}
|