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
|
// -*- c++ -*-
// Generated by assa-genesis
//------------------------------------------------------------------------------
// $Id: MakeData.cpp,v 1.7 2006/07/20 02:30:55 vlg Exp $
//------------------------------------------------------------------------------
// MakeData.cpp
//------------------------------------------------------------------------------
// 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 : Fri Jul 4 13:55:47 2003
//
//------------------------------------------------------------------------------
static const char help_msg[]=
" \n"
" NAME: \n"
" \n"
" make-data \n"
" \n"
" DESCRIPTION: \n"
" \n"
" Generate ASCII test files for testing ASSA log server. \n"
" An optional input file is repetitevly copied into the output file \n"
" until the desired output file size is reached. If an input file \n"
" option is omitted, MakeData.cpp is used instead. \n"
" \n"
" USAGE: \n"
" \n"
" shell> make-data [OPTIONS] -o <name> -n <size> \n"
" \n"
" EXAMPLE: \n"
" \n"
" shell> make-data --output-file 1Mb -n 1 \n"
" \n"
" Create ASCII file of 1 megabyte long with name 1Mb by repetetively \n"
" copying MakeData.cpp. \n"
" \n"
" OPTIONS: \n"
" \n"
" -o, --output-file NAME - Name of the output data file \n"
" -i, --input-file NAME - Name of the input data file (optional) \n"
" -n, --size NUM - Output file size (in megabytes) \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"
" -h, --help - Print this messag \n"
" -v, --version - Print version number \n";
//------------------------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <assa/Assure.h>
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string>
using std::string;
#include <assa/GenServer.h>
#include <assa/Singleton.h>
#include <assa/TimeVal.h>
class MakeData :
public ASSA::GenServer,
public ASSA::Singleton<MakeData>
{
public:
MakeData ();
virtual void init_service ();
virtual void process_events ();
private:
std::string m_ofname;
int m_ofsize; // In Megabytes
std::string m_ifname;
};
/* Useful definitions */
#define MAKEDATA MakeData::get_instance()
#define REACTOR MAKEDATA->get_reactor()
// Static declarations mandated by Singleton class
ASSA_DECL_SINGLETON(MakeData);
MakeData::
MakeData () :
m_ofname ("1Mb"),
m_ofsize (1),
m_ifname ("MakeData.cpp")
{
// ---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");
add_opt ('o', "output-file", &m_ofname);
add_opt ('i', "input-file", &m_ifname);
add_opt ('n', "size", &m_ofsize);
/*---
* By defauil disable all debugging
*---*/
// m_debug_mask = ASSA::APP | ASSA::ASSAERR;
m_mask = 0x0;
}
void
MakeData::
init_service ()
{
trace("MakeData::init_service");
if (m_ofname.size () == 0) {
std::cerr << "Missing parameter: --output-name NAME\n";
set_exit_value (1);
stop_service ();
}
if (m_ifname.size () == 0) {
std::cerr << "Missing parameter: --input-name NAME\n";
set_exit_value (1);
stop_service ();
}
}
void
MakeData::
process_events ()
{
trace("MakeData::process_events");
register char c;
register unsigned long so_far = 0;
unsigned long total_sz = m_ofsize * 1024 * 1024; // In Megabytes
if (service_is_active ())
{
std::ofstream ofile (m_ofname.c_str (),
std::ios::out | std::ios::trunc);
if (!ofile) {
DL((ASSA::APP,"Failed to open \"%s\"\n", m_ofname.c_str ()));
goto done;
}
std::ifstream ifile (m_ifname.c_str (), std::ios::in);
if (!ifile) {
DL((ASSA::APP,"Failed to open \"%s\"\n", m_ifname.c_str ()));
goto done;
}
rewind:
while (ifile.get (c)) {
ofile.put (c);
if (++so_far >= total_sz) {
ofile.put ('\n');
ofile.close ();
ifile.close ();
goto done;
}
}
ifile.clear (); // clear eofbit and failbit set due to EOF
ifile.seekg (0, std::ios::beg);
goto rewind;
}
done:
m_reactor.stopReactor ();
DL((ASSA::APP,"Service stopped!\n"));
}
int
main (int argc, char* argv[])
{
static const char release[] = "1.0";
int patch_level = 0;
MAKEDATA->set_version (release, patch_level);
MAKEDATA->set_author ("Vladislav Grinchenko");
MAKEDATA->set_flags (ASSA::GenServer::RMLOG);
MAKEDATA->init (&argc, argv, help_msg);
MAKEDATA->init_service ();
MAKEDATA->process_events ();
return MAKEDATA->get_exit_value ();
}
|