1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 //                               Conn.h
4 //------------------------------------------------------------------------------
5 // $Id: Conn.h,v 1.2 2006/07/20 02:30:55 vlg Exp $
6 //------------------------------------------------------------------------------
7 //  Copyright (c) 2002 by Vladislav Grinchenko
8 //
9 //  This program is free software; you can redistribute it and/or
10 //  modify it under the terms of the GNU General Public License
11 //  as published by the Free Software Foundation; either version
12 //  2 of the License, or (at your option) any later version.
13 //------------------------------------------------------------------------------
14 // Created: Thu Apr 17 23:22:39 EDT 2003
15 //------------------------------------------------------------------------------
16 #ifndef CONN_H
17 #define CONN_H
18 
19 #include <assa/ServiceHandler.h>
20 #include <assa/IPv4Socket.h>
21 #include <assa/FileLogger.h>
22 #include <assa/Repository.h>
23 using ASSA::ServiceHandler;
24 using ASSA::IPv4Socket;
25 
26 #include "LogServer-main.h"
27 
28 class MonitorConn;
29 
30 /*******************************************************************************
31  Class Conn
32 *******************************************************************************/
33 
34 class Conn : public ServiceHandler<IPv4Socket>
35 {
36 public:
37 	Conn (IPv4Socket* stream_);
38 	~Conn ();
39 
40 	virtual int open ();
41 
42 	virtual int handle_read (int fd_);
43 	virtual int handle_close (int /* fd */);
44 
get_app_name()45 	const std::string& get_app_name () const { return m_app_name; }
subscribe(MonitorConn * mc_)46 	void subscribe (MonitorConn* mc_) { m_observers.push_back (mc_); }
unsubscribe(MonitorConn * mc_)47 	void unsubscribe (MonitorConn* mc_) { m_observers.erase (mc_); }
48 
49 private:
50 	std::string wstate () const;
51 	void shift_logfile ();
52 
53 private:
54 	enum msg_t {
55 		SIGN_ON,				/**< Login message  */
56 		SIGN_OFF,				/**< Logout message */
57 		LOG_MSG					/**< Payload data   */
58 	};
59 	enum wstate_t {
60 		wait_for_header,		/**< Waiting for the message header */
61 		wait_for_signon,		/**< Waiting for SIGN_ON message    */
62 		wait_for_signoff,       /**< Waiting for SIGN_OFF message   */
63 		wait_for_logmsg         /**< Waiting for LOG_MSG message    */
64 	};
65 	enum state_t {
66 		opened,
67 		closed
68 	};
69 
70 	wstate_t m_wstate;
71 	state_t m_state;
72 
73 	/// Incoming message size
74 	int m_msg_size;
75 
76 	/// Incoming message type
77 	int m_msg_type;
78 
79 	/// Maximum logfile size can reach
80 	int m_maxsize;
81 
82 	/// Logging application name
83 	std::string m_app_name;
84 
85 	/// Logfile name
86 	std::string m_logfname;
87 
88 	/// Logfile stream
89 	std::ofstream m_sink;
90 
91 	/// Bytes written to the sink so far.
92 	u_long m_bytecount;
93 
94 	/// Repository of all connected MonitorConn observers
95 	ASSA::Repository<MonitorConn> m_observers;
96 };
97 
98 /*******************************************************************************
99  Inline functions
100 *******************************************************************************/
101 
102 inline
103 Conn::
Conn(IPv4Socket * stream_)104 Conn (IPv4Socket* stream_) :
105 	ServiceHandler<IPv4Socket> (stream_),
106 	m_maxsize (1048576),
107 	m_bytecount (0),
108 	m_wstate (wait_for_header),
109 	m_state (closed)
110 {
111 	trace_with_mask ("Conn::Conn",LSVRTRACE);
112 	/* no-op */
113 }
114 
115 inline
116 Conn::
~Conn()117 ~Conn ()
118 {
119 	trace_with_mask ("Conn::~Conn",LSVRTRACE);
120 }
121 
122 inline std::string
123 Conn::
wstate()124 wstate () const
125 {
126 	return (m_wstate == wait_for_signon ? "wait_for_signon"   :
127 			m_wstate == wait_for_signoff ? "wait_for_signoff" :
128 			"wait_for_logmsg");
129 }
130 
131 #endif /* CONN_H */
132 
133