File: LogConn.cpp

package info (click to toggle)
libassa 3.5.1-8.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,424 kB
  • sloc: cpp: 15,703; sh: 12,083; makefile: 379; perl: 51
file content (99 lines) | stat: -rw-r--r-- 2,460 bytes parent folder | download | duplicates (9)
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
// -*- c++ -*-
//------------------------------------------------------------------------------
//                               LogConn.cpp
//------------------------------------------------------------------------------
// $Id: LogConn.cpp,v 1.4 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.
//------------------------------------------------------------------------------
// Date: Wed Apr 30 19:07:30 EDT 2003
//------------------------------------------------------------------------------

#include <unistd.h>
#include <iostream>
#include <algorithm>

#include "LogConn.h"
#include "LogMon.h"				// REACTOR declaration

int
LogConn::
open ()
{
	trace ("LogConn::open");

	ASSA::IPv4Socket& s = *this;

	REACTOR->registerIOHandler (this, s.getHandler (), ASSA::READ_EVENT);
	REACTOR->registerIOHandler (this, STDIN_FILENO, ASSA::READ_EVENT);

	DL((ASSA::APP,"Connected to assa-logd\n"));
	return 0;
}

void
LogConn::
close ()
{
	trace ("LogConn::close");

	DL((ASSA::APP, "Failed to connection to assa-logd!\n"));
	LOGMON->stop_service ();
}

int 
LogConn::
handle_close (int fd_)
{
	trace ("LogConn::handle_close");

	if (fd_ != STDIN_FILENO) { 
		DL((ASSA::APP,"Disconnected from assa-logd\n"));
	}
	return 0;
}

int
LogConn::
handle_read (int fd_)
{
	trace ("LogConn::handle_read");
	ASSA::IPv4Socket& s = *this;

	if (fd_ == STDIN_FILENO) {
		return process_user_request (s);
	}

    int ret = 0;
    char buf [256];
	if (s.getHandler () != fd_) { return -1; }

    if ((ret = s.read (buf, 256)) < 0) {
		DL((ASSA::ASSAERR,"Error reading from socket\n"));
		return (-1);
    }
	buf [ret] = '\0';
	std::cout << buf << std::flush;

	return BYTES_LEFT_IN_SOCKBUF(s);
}

int
LogConn::
process_user_request (ASSA::IPv4Socket& s_)
{
	trace("LogConn::process_user_request");

	std::string input;
	getline (std::cin, input);
	DL((ASSA::APP,"Requiest from user: \"%s\"\n", input.c_str ()));
	s_.write (input.c_str (), input.length ());
	s_.write (m_eor, 2);
	s_ << ASSA::flush;
	return 0;
}