File: smoker.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 (237 lines) | stat: -rw-r--r-- 6,638 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
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
// -*- c++ -*-
// Generated by assa-genesis
//------------------------------------------------------------------------------
// $Id: smoker.cpp,v 1.9 2005/12/07 03:12:55 vlg Exp $
//------------------------------------------------------------------------------
//                            Smoker.cpp
//------------------------------------------------------------------------------
//
// Author : Vladislav Grinchenko
// Date   : Sat Jul 20 18:47:32 2002
//------------------------------------------------------------------------------
static const char help_msg[]=
"                                                                            \n"
" NAME:                                                                      \n"
"   Smoker - helper program for PipeTest                                     \n"
"                                                                            \n"
" DESCRIPTION:                                                               \n"
"                                                                            \n"
"  Smoker is the external command that pipe_test runs to test                \n"
"  its read/write/kill capabilities. Smoker's standard input(read) or        \n"
"  output (write) are connected via UNIX pipe to pipe_test.                  \n"
"                                                                            \n"
" USAGE:                                                                     \n"
"                                                                            \n"
"   shell>  smoker   [OPTIONS]                                               \n"
"                                                                            \n"
" OPTIONS:                                                                   \n"
"                                                                            \n"
"     --write=NUM         - Write NUM messages to standard output.           \n"
"                           If NUM = -1, keep writing messages forever.      \n"
"     --delay NUM         - Delay between consecutive messages.              \n"
"                           (Default: 1 sec).                                \n"
"     --pidfile NAME      - PID file name.                                   \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"
" -m, --mask MASK         - Mask (default: ALL = 0x7fffffff)                 \n"
" -h, --help              - Print this messag                                \n"
" -v, --version           - Print version number                            \n";
/******************************************************************************/

#include <string>
using std::string;

#include <iostream>
#include <unistd.h>
using namespace std;

#include "assa/GenServer.h"
#include "assa/Singleton.h"
#include "assa/TimeVal.h"
#include "assa/IPv4Socket.h"
#include "assa/Pipe.h"
using namespace ASSA;

class Smoker :
    public GenServer,
    public Singleton<Smoker>
{
public:
    Smoker ();

    virtual void init_service ();
    virtual void process_events ();

	virtual int handle_read (int);
	virtual int handle_timeout (TimerId);
	virtual int handle_close (int);

private:
    int     m_write;
	int     m_read;
	int     m_line_count;
	double  m_delay;
	string  m_msg;
	string  m_input_line;		// Buffer that keeps partially-received
                                // characters
};

// Useful defines

#define SMOKER Smoker::get_instance()
#define REACTOR Smoker::get_instance()->get_reactor()

// Static declarations mandated by Singleton class
ASSA_DECL_SINGLETON(Smoker);

Smoker::
Smoker () : 
	m_write (0),
	m_read (0),
	m_line_count (0),
	m_msg ("It's rarely helpful to blame anyone for anything")
{
	set_id ("Smoker");

	add_opt (0, "write", &m_write);
	add_opt (0, "read",  &m_read);
	add_opt (0, "delay", &m_delay);

    // ---General---
    // rm_opt ('h', "help"         );
    // rm_opt ('v', "version"      );

    // ---Debugging---
    // rm_opt ('m', "mask"         );
    // rm_opt ('d', "log-stdout"   );
    // rm_opt ('D', "log-file"     );
    // rm_opt ('z', "log-size"     );

    // ---Configuration---
    rm_opt ('f', "config-file"  );
    rm_opt ('n', "instance"     );
    rm_opt ('p', "port"         );

    // ---Process bookkeeping---
    rm_opt ('b', "daemon"       );
    // rm_opt ('l', "pidfile"      );
    rm_opt ('L', "ommit-pidfile");

    /*---
     * Disable all debugging
     *---*/
    // m_debug_mask = 0x0;
}

void
Smoker::
init_service ()
{
    trace("Smoker::init_service");
    Log::disable_timestamp ();

	if (m_read == 0 && m_write == 0) {
		DL((APP,"Either {--read} or {--write} is required\n"));
		DL((APP,"See 'Smoker --help' for details\n"));
		set_exit_value (1);
		SMOKER->stop_service ();
		return;
	}
	if (m_write != 0) {
		REACTOR->registerTimerHandler (this, m_delay);
	}
	else {
		REACTOR->registerIOHandler (this, STDIN_FILENO, READ_EVENT);
	}

    DL((APP,"Service has been initialized\n"));
}

void
Smoker::
process_events ()
{
    trace("Smoker::process_events");

	REACTOR->waitForEvents ();
    REACTOR->stopReactor ();

    DL((APP,"Service stopped!\n"));
}

int 
Smoker::
handle_read (int fd_)
{
    trace("Smoker::handle_read");

	char buf [128];

	while (cin.getline (buf, 128)) {
		DL((APP,"Line %d: \"%s\"\n", m_line_count++, buf));
		if (m_line_count == m_read) {
			DL((APP,"Wrote last message out ... exiting\n"));
			SMOKER->stop_service ();
			return -1;
		}
	}

	return BYTES_LEFT_IN_SIN;
}

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

	DL((APP,"PipeTest must have exited!\n"));
	return 0;
}

int
Smoker::
handle_timeout (TimerId)
{
	trace("Smoker::handle_timeout");

	if (m_write == 0) {
		DL((APP,"Shouldn't happen!\n"));
		Assure_exit (false);
	}

	cout << m_msg << endl;
	DL((APP,"Message # %d is out\n", m_write));

	if (--m_write == 0) {
		DL((APP,"Wrote last message - stopping server\n"));
		SMOKER->stop_service ();
	}
	else {
		REACTOR->registerTimerHandler (this, m_delay);
	}

	return 0;
}

int
main (int argc, char* argv[])
{
    static const char release[] = "0.1";
    int patch_level = 0;

    Smoker& server = *Smoker::get_instance ();

    server.set_version (release, patch_level);
    server.set_author  ("Vladislav Grinchenko");
	server.set_flags (GenServer::RMLOG);

    server.init (&argc, argv, help_msg);
 
    server.init_service ();
    server.process_events ();

    return server.get_exit_value ();
}