File: reactor_test.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 (248 lines) | stat: -rw-r--r-- 7,629 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
238
239
240
241
242
243
244
245
246
247
248
// -*- c++ -*-
// Generated by assa-genesis
//------------------------------------------------------------------------------
// $Id: reactor_test.cpp,v 1.7 2005/10/08 02:42:01 vlg Exp $
//------------------------------------------------------------------------------
//                            Reactor_Test.cpp
//------------------------------------------------------------------------------
//  Copyright (c) 2002,2005 by Vladislav Grinchenko 
//
//  Permission to use, copy, modify, and distribute this software      
//  and its documentation for any purpose and without fee is hereby    
//  granted, provided that the above copyright notice appear in all    
//  copies.  The author makes no representations about the suitability 
//  of this software for any purpose.  It is provided "as is" without  
//  express or implied warranty.                                       
//------------------------------------------------------------------------------
//
// Date   : Fri Oct 25 14:14:13 2002
//
//------------------------------------------------------------------------------

static const char help_msg[]=
"                                                                            \n"
" NAME:                                                                      \n"
"                                                                            \n"
"   reactor_test                                                             \n"
"                                                                            \n"
" DESCRIPTION:                                                               \n"
"                                                                            \n"
"   A very simple test that illustrates basic Reactor usage.                 \n"
"                                                                            \n"
" USAGE:                                                                     \n"
"                                                                            \n"
"   shell>  reactor_test [OPTIONS]                                           \n"
"                                                                            \n"
" OPTIONS:                                                                   \n"
"                                                                            \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"
"                                                                            \n"
" -m, --mask MASK         - Mask (default: ALL = 0x7fffffff)                 \n"
"                                                                            \n"
" -h, --help              - Print this messag                                \n"
" -v, --version           - Print version number                            \n";
//------------------------------------------------------------------------------

#ifdef HAVE_CONFIG_H
#    include "config.h"
#endif

#include <iostream>
#include <string>
using std::string;

#include <assa/GenServer.h>
#include <assa/Singleton.h>
#include <assa/TimeVal.h>
#include <assa/MemDump.h>
using namespace ASSA;

/*******************************************************************************
  Class Std_In
*******************************************************************************/

class Std_In : public EventHandler
{
public:
	Std_In () {	
		trace_with_mask("Std_In::Std_In", REACTTRACE); 
		set_id ("Std_In");
	}

	~Std_In () { trace_with_mask("Std_In::~Std_In",REACTTRACE);	}

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

/*
 * When run from the script with HERE document as an input
 * construct, when shell reaches the end of the HERE documents,
 * it keeps sending 0x8 or \b (BS) control character.
 * I can't find any reference why it is doing so, but it
 * looks like this is the indication of the end of the stream.
 */
int 
Std_In::
handle_read (int /* fd */) 
{ 
	trace_with_mask("Std_In::handle_read",REACTTRACE);
	int ret;
	char c;

	ret = read (0, &c, 1);
	if (ret == 0 || c == '\b') {
		DL((REACT,"Found end-of-file\n"));
		return -1;
	}

	if (c != '\n') {
		DL((REACT,"read %c\n", c));
		std::cout << "Read < " << c << " >\n";
		MemDump::dump_to_log (APP, "Character received:", &c, 1);
	}

	return 0;
}

int 
Std_In::
handle_timeout (TimerId /* tid */) 
{
	trace_with_mask("Std_In::handle_timeout", REACTTRACE);
	DL((APP,"Timeout occured\n"));
	std::cout << "*** Timeout ***\n";
	return 0;
}

/*******************************************************************************
  Class Reactor_Test
*******************************************************************************/

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

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


/* Useful definitions */

#define REACTOR_TEST  Reactor_Test::get_instance()
#define REACTOR REACTOR_TEST->get_reactor()


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

Reactor_Test::
Reactor_Test ()
{
    // ---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_mask = 0x0;
    m_log_file = "reactor_test.log";
}

void
Reactor_Test::
init_service ()
{
    trace("Reactor_Test::init_service");
    Log::enable_timestamp ();
    DL((APP,"Service has been initialized\n"));
}

void
Reactor_Test::
process_events ()
{
    trace("Reactor_Test::process_events");
	static const char sep[]="*************************";

	std::cout << "= Running reactor_test Test =\n";

	Std_In myin;
	REACTOR->registerIOHandler (&myin, 0, READ_EVENT);

	std::cout << "Type something:\n";

	std::cout << "Non-blocking poll ... ";
	DL((APP,"%s\n",sep));
	DL((APP,"Non-blocking poll: \n"));
	DL((APP,"%s\n",sep));

	TimeVal polltv;
	REACTOR->waitForEvents (&polltv);
	std::cout << " done.\n";

	DL((APP,"%s\n",sep));
	DL((APP,"Wait for 10 seconds with 5 timers: \n"));
	DL((APP,"%s\n",sep));
	std::cout << "Waiting for 10 seconds ... type fast ... ";

	TimeVal onesec(1.0);
	REACTOR->registerTimerHandler (&myin, onesec,   "01 sec");

	TimeVal twosec(2.0);
	REACTOR->registerTimerHandler (&myin, twosec,   "02 secs");

	TimeVal threesec(3.0);
	REACTOR->registerTimerHandler (&myin, threesec, "03 secs");

	TimeVal foursec(4.0);
	REACTOR->registerTimerHandler (&myin, foursec,  "04 secs");

	TimeVal fivesec(5.0);
	REACTOR->registerTimerHandler (&myin, fivesec,  "05 secs");

	TimeVal tensec(10.0);
	REACTOR->registerTimerHandler (&myin, tensec,   "10 secs");

	while (tensec != TimeVal::zeroTime()) {
		REACTOR->waitForEvents (&tensec);
	}

	std::cout << " done.\n"
			  << "Remaining time " << double(tensec) << " secs.\n";

    REACTOR->stopReactor ();
    DL((APP,"Service stopped!\n"));
}

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

    REACTOR_TEST->set_version (release, patch_level);
    REACTOR_TEST->set_author  ("Vladislav Grinchenko");
    REACTOR_TEST->set_flags   (GenServer::RMLOG);

    REACTOR_TEST->init (&argc, argv, help_msg);
 
    REACTOR_TEST->init_service ();
    REACTOR_TEST->process_events ();

    return REACTOR_TEST->get_exit_value ();
}