1 // -*- c++ -*-
2 // Generated by assa-genesis
3 //------------------------------------------------------------------------------
4 // $Id: MakeData.cpp,v 1.7 2006/07/20 02:30:55 vlg Exp $
5 //------------------------------------------------------------------------------
6 // MakeData.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 : Fri Jul 4 13:55:47 2003
17 //
18 //------------------------------------------------------------------------------
19
20 static const char help_msg[]=
21 " \n"
22 " NAME: \n"
23 " \n"
24 " make-data \n"
25 " \n"
26 " DESCRIPTION: \n"
27 " \n"
28 " Generate ASCII test files for testing ASSA log server. \n"
29 " An optional input file is repetitevly copied into the output file \n"
30 " until the desired output file size is reached. If an input file \n"
31 " option is omitted, MakeData.cpp is used instead. \n"
32 " \n"
33 " USAGE: \n"
34 " \n"
35 " shell> make-data [OPTIONS] -o <name> -n <size> \n"
36 " \n"
37 " EXAMPLE: \n"
38 " \n"
39 " shell> make-data --output-file 1Mb -n 1 \n"
40 " \n"
41 " Create ASCII file of 1 megabyte long with name 1Mb by repetetively \n"
42 " copying MakeData.cpp. \n"
43 " \n"
44 " OPTIONS: \n"
45 " \n"
46 " -o, --output-file NAME - Name of the output data file \n"
47 " -i, --input-file NAME - Name of the input data file (optional) \n"
48 " -n, --size NUM - Output file size (in megabytes) \n"
49 " \n"
50 " -D, --log-file NAME - Write debug to NAME file \n"
51 " -d, --log-stdout - Write debug to standard output \n"
52 " -z, --log-size NUM - Maximum size debug file can reach (dfl: is 10Mb) \n"
53 " \n"
54 " -m, --mask MASK - Mask (default: ALL = 0x7fffffff) \n"
55 " -h, --help - Print this messag \n"
56 " -v, --version - Print version number \n";
57 //------------------------------------------------------------------------------
58
59 #include <iostream>
60 #include <fstream>
61 #include <assa/Assure.h>
62
63 #ifdef HAVE_CONFIG_H
64 # include "config.h"
65 #endif
66 #include <string>
67 using std::string;
68
69 #include <assa/GenServer.h>
70 #include <assa/Singleton.h>
71 #include <assa/TimeVal.h>
72
73 class MakeData :
74 public ASSA::GenServer,
75 public ASSA::Singleton<MakeData>
76 {
77 public:
78 MakeData ();
79
80 virtual void init_service ();
81 virtual void process_events ();
82
83 private:
84 std::string m_ofname;
85 int m_ofsize; // In Megabytes
86 std::string m_ifname;
87 };
88
89 /* Useful definitions */
90
91 #define MAKEDATA MakeData::get_instance()
92 #define REACTOR MAKEDATA->get_reactor()
93
94 // Static declarations mandated by Singleton class
95 ASSA_DECL_SINGLETON(MakeData);
96
97 MakeData::
MakeData()98 MakeData () :
99 m_ofname ("1Mb"),
100 m_ofsize (1),
101 m_ifname ("MakeData.cpp")
102 {
103 // ---Configuration---
104 rm_opt ('f', "config-file" );
105 rm_opt ('n', "instance" );
106 rm_opt ('p', "port" );
107
108 // ---Process bookkeeping---
109 rm_opt ('b', "daemon" );
110 rm_opt ('l', "pidfile" );
111 rm_opt ('L', "ommit-pidfile");
112
113 add_opt ('o', "output-file", &m_ofname);
114 add_opt ('i', "input-file", &m_ifname);
115 add_opt ('n', "size", &m_ofsize);
116
117 /*---
118 * By defauil disable all debugging
119 *---*/
120 // m_debug_mask = ASSA::APP | ASSA::ASSAERR;
121 m_mask = 0x0;
122 }
123
124 void
125 MakeData::
init_service()126 init_service ()
127 {
128 trace("MakeData::init_service");
129
130 if (m_ofname.size () == 0) {
131 std::cerr << "Missing parameter: --output-name NAME\n";
132 set_exit_value (1);
133 stop_service ();
134 }
135 if (m_ifname.size () == 0) {
136 std::cerr << "Missing parameter: --input-name NAME\n";
137 set_exit_value (1);
138 stop_service ();
139 }
140 }
141
142 void
143 MakeData::
process_events()144 process_events ()
145 {
146 trace("MakeData::process_events");
147
148 register char c;
149 register unsigned long so_far = 0;
150 unsigned long total_sz = m_ofsize * 1024 * 1024; // In Megabytes
151
152 if (service_is_active ())
153 {
154 std::ofstream ofile (m_ofname.c_str (),
155 std::ios::out | std::ios::trunc);
156 if (!ofile) {
157 DL((ASSA::APP,"Failed to open \"%s\"\n", m_ofname.c_str ()));
158 goto done;
159 }
160
161 std::ifstream ifile (m_ifname.c_str (), std::ios::in);
162
163 if (!ifile) {
164 DL((ASSA::APP,"Failed to open \"%s\"\n", m_ifname.c_str ()));
165 goto done;
166 }
167
168 rewind:
169 while (ifile.get (c)) {
170 ofile.put (c);
171 if (++so_far >= total_sz) {
172 ofile.put ('\n');
173 ofile.close ();
174 ifile.close ();
175 goto done;
176 }
177 }
178 ifile.clear (); // clear eofbit and failbit set due to EOF
179 ifile.seekg (0, std::ios::beg);
180 goto rewind;
181 }
182
183 done:
184 m_reactor.stopReactor ();
185 DL((ASSA::APP,"Service stopped!\n"));
186 }
187
188 int
main(int argc,char * argv[])189 main (int argc, char* argv[])
190 {
191 static const char release[] = "1.0";
192 int patch_level = 0;
193
194 MAKEDATA->set_version (release, patch_level);
195 MAKEDATA->set_author ("Vladislav Grinchenko");
196 MAKEDATA->set_flags (ASSA::GenServer::RMLOG);
197
198 MAKEDATA->init (&argc, argv, help_msg);
199
200 MAKEDATA->init_service ();
201 MAKEDATA->process_events ();
202
203 return MAKEDATA->get_exit_value ();
204 }
205
206