1 // -*- c++ -*-
2 // Generated by assa-genesis
3 //------------------------------------------------------------------------------
4 // $Id: LogClient.cpp,v 1.5 2006/07/20 02:30:55 vlg Exp $
5 //------------------------------------------------------------------------------
6 //                            LogClient.cpp
7 //------------------------------------------------------------------------------
8 //  Copyright (c) 2003 by Vladislav Grinchenko
9 //
10 //  This program is free software; you can redistribute it and/or
11 //  modify it under the terms of the GNU General Public License
12 //  as published by the Free Software Foundation; either version
13 //  2 of the License, or (at your option) any later version.
14 //------------------------------------------------------------------------------
15 //
16 // Date   : Wed May 21 18:26:28 2003
17 //
18 //------------------------------------------------------------------------------
19 #include <iostream>
20 #include <fstream>
21 
22 #include "LogClient-main.h"
23 #include "LogClient.h"
24 #include "assa/CommonUtils.h"
25 
26 enum { LC = ASSA::APP };
27 
28 // Static declarations mandated by Singleton class
29 ASSA_DECL_SINGLETON(LogClient);
30 
31 LogClient::
LogClient()32 LogClient () :
33 	m_delay (0),
34 	m_message_size (0)
35 {
36 	add_opt (0, "input-file",   &m_input_file);
37 	add_opt (0, "delay",        &m_delay);
38 	add_opt (0, "message-size", &m_message_size);
39 
40     // ---Configuration---
41     rm_opt ('f', "config-file"  );
42     rm_opt ('p', "port"         );
43 
44     // ---Process bookkeeping---
45     rm_opt ('b', "daemon"       );
46 
47     /*---
48      * By default disable all debugging
49      *---*/
50     m_mask = LC;
51     m_log_file = "log-client.log";
52 	m_with_log_server="yes";
53 }
54 
55 void
56 LogClient::
init_service()57 init_service ()
58 {
59     trace("LogClient::init_service");
60 }
61 
62 void
63 LogClient::
process_events()64 process_events ()
65 {
66     trace("LogClient::process_events");
67 
68     const int size = 256;
69     char line [size];
70 	std::string buffer;
71 	std::ifstream in_file;
72 	int delta;
73 
74 	in_file.open (m_input_file.c_str (), std::ios::in);
75 	if (!in_file) {
76 		std::cerr << "Failed to open input file \""
77 				  << m_input_file << "\"\n"
78 				  << "Option \"--input-file=NAME\" is required\n";
79 	}
80 	else {
81 		while (in_file && service_is_active ()) {
82 			in_file.getline (line, size, '\n');
83 			if (m_message_size != 0) {
84 				buffer += line;
85 				buffer += "\n";
86 //				DL((LC,"buffer so far \"%s\"\n", buffer.c_str ()));
87 //				DL((LC,"buffer.size () = %d, m_message_size = %d\n",
88 //					buffer.size (), m_message_size));
89 				if ((delta = buffer.size () - m_message_size) >= 0) {
90 //					DL((LC,"delta = %d\n", delta));
91 					if (delta > 0) {
92 						buffer.erase (m_message_size, delta);
93 					}
94 					DL((LC,"%s\n", buffer.c_str ()));
95 					if (delta > 0) {
96 //						DL((LC,"Line = \"%s\"\n", line));
97 //						DL((LC,"Piece that didn't fit: \"%s\"\n",
98 //							&line [strlen (line) - delta + 1]));
99 						buffer = &line [strlen (line) - delta + 1];
100 						buffer += "\n";
101 					}
102 					else {
103 						buffer = "";
104 					}
105 //					DL((LC,"buffer = \"%s\"\n", buffer.c_str ()));
106 				}
107 			}
108 			else {
109 				std::cout << "Sending \"" << line << "\"" << std::endl;
110 				DL((LC,"%s\n", line));
111 			}
112 			if (m_delay) {
113 				std::cout << m_delay << " sec(s) delay ... " << std::flush;
114 				ASSA::Utils::sleep_for_seconds (m_delay);
115 			}
116 		}
117 		in_file.close ();
118 	}
119 
120     // Shut the service down
121     m_reactor.stopReactor ();
122 }
123 
124 
125