Test case for a dynamic input reader.

This commit is contained in:
Robin Sommer 2014-07-31 12:04:27 -07:00
parent f45526f373
commit 3d1442e86b
11 changed files with 423 additions and 72 deletions

View file

@ -0,0 +1,17 @@
project(Bro-Plugin-Demo-Foo)
cmake_minimum_required(VERSION 2.6.3)
if ( NOT BRO_DIST )
message(FATAL_ERROR "BRO_DIST not set")
endif ()
set(CMAKE_MODULE_PATH ${BRO_DIST}/cmake)
include(BroPlugin)
bro_plugin_begin(Demo Foo)
bro_plugin_cc(src/Plugin.cc)
bro_plugin_cc(src/Foo.cc)
bro_plugin_end()

View file

@ -0,0 +1,185 @@
// See the file "COPYING" in the main distribution directory for copyright.
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include "Foo.h"
#include "threading/SerialTypes.h"
#include "threading/Manager.h"
using namespace input::reader;
using threading::Value;
using threading::Field;
Foo::Foo(ReaderFrontend *frontend) : ReaderBackend(frontend)
{
ascii = new threading::formatter::Ascii(this, threading::formatter::Ascii::SeparatorInfo());
}
Foo::~Foo()
{
DoClose();
delete ascii;
}
void Foo::DoClose()
{
}
bool Foo::DoInit(const ReaderInfo& info, int num_fields, const Field* const* fields)
{
DoUpdate();
return true;
}
string Foo::RandomString(const int len)
{
string s(len, ' ');
static const char values[] =
"0123456789!@#$%^&*()-_=+{}[]\\|"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < len; ++i)
s[i] = values[random() / (RAND_MAX / sizeof(values))];
return s;
}
// read the entire file and send appropriate thingies back to InputMgr
bool Foo::DoUpdate()
{
int linestosend = 5;
for ( int i = 0; i < linestosend; i++ )
{
Value** field = new Value*[NumFields()];
for (int j = 0; j < NumFields(); j++ )
field[j] = EntryToVal(Fields()[j]->type, Fields()[j]->subtype);
SendEntry(field);
}
EndCurrentSend();
return true;
}
threading::Value* Foo::EntryToVal(TypeTag type, TypeTag subtype)
{
Value* val = new Value(type, true);
// basically construct something random from the fields that we want.
switch ( type ) {
case TYPE_ENUM:
assert(false); // no enums, please.
case TYPE_STRING:
{
string rnd = RandomString(10);
val->val.string_val.data = copy_string(rnd.c_str());
val->val.string_val.length = rnd.size();
break;
}
case TYPE_BOOL:
val->val.int_val = 1; // we never lie.
break;
case TYPE_INT:
val->val.int_val = random();
break;
case TYPE_TIME:
val->val.double_val = 0;
break;
case TYPE_DOUBLE:
case TYPE_INTERVAL:
val->val.double_val = random();
break;
case TYPE_COUNT:
case TYPE_COUNTER:
val->val.uint_val = random();
break;
case TYPE_PORT:
val->val.port_val.port = random() / (RAND_MAX / 60000);
val->val.port_val.proto = TRANSPORT_UNKNOWN;
break;
case TYPE_SUBNET:
{
val->val.subnet_val.prefix = ascii->ParseAddr("192.168.17.1");
val->val.subnet_val.length = 16;
}
break;
case TYPE_ADDR:
val->val.addr_val = ascii->ParseAddr("192.168.17.1");
break;
case TYPE_TABLE:
case TYPE_VECTOR:
// First - common initialization
// Then - initialization for table.
// Then - initialization for vector.
// Then - common stuff
{
// how many entries do we have...
unsigned int length = random() / (RAND_MAX / 15);
Value** lvals = new Value* [length];
if ( type == TYPE_TABLE )
{
val->val.set_val.vals = lvals;
val->val.set_val.size = length;
}
else if ( type == TYPE_VECTOR )
{
val->val.vector_val.vals = lvals;
val->val.vector_val.size = length;
}
else
assert(false);
if ( length == 0 )
break; //empty
for ( unsigned int pos = 0; pos < length; pos++ )
{
Value* newval = EntryToVal(subtype, TYPE_ENUM);
if ( newval == 0 )
{
Error("Error while reading set");
delete val;
return 0;
}
lvals[pos] = newval;
}
break;
}
default:
Error(Fmt("unsupported field format %d", type));
delete val;
return 0;
}
return val;
}
bool Foo::DoHeartbeat(double network_time, double current_time)
{
return true;
}

View file

@ -0,0 +1,34 @@
#ifndef BRO_PLUGIN_DEMO_FOO_H
#define BRO_PLUGIN_DEMO_FOO_H
#include "input/ReaderBackend.h"
#include "threading/formatters/Ascii.h"
namespace input { namespace reader {
/**
* A Foo reader to measure performance of the input framework.
*/
class Foo : public ReaderBackend {
public:
Foo(ReaderFrontend* frontend);
~Foo();
static ReaderBackend* Instantiate(ReaderFrontend* frontend) { return new Foo(frontend); }
protected:
virtual bool DoInit(const ReaderInfo& info, int arg_num_fields, const threading::Field* const* fields);
virtual void DoClose();
virtual bool DoUpdate();
virtual bool DoHeartbeat(double network_time, double current_time);
private:
string RandomString(const int len);
threading::Value* EntryToVal(TypeTag Type, TypeTag subtype);
threading::formatter::Ascii* ascii;
};
} }
#endif

View file

@ -0,0 +1,19 @@
#include "Plugin.h"
#include "Foo.h"
namespace plugin { namespace Demo_Foo { Plugin plugin; } }
using namespace plugin::Demo_Foo;
plugin::Configuration Plugin::Configure()
{
AddComponent(new ::input::Component("Foo", ::input::reader::Foo::Instantiate));
plugin::Configuration config;
config.name = "Demo::Foo";
config.description = "A Foo test input reader";
config.version.major = 1;
config.version.minor = 0;
return config;
}

View file

@ -0,0 +1,22 @@
#ifndef BRO_PLUGIN_DEMO_FOO
#define BRO_PLUGIN_DEMO_FOO
#include <plugin/Plugin.h>
namespace plugin {
namespace Demo_Foo {
class Plugin : public ::plugin::Plugin
{
protected:
// Overridden from plugin::Plugin.
virtual plugin::Configuration Configure();
};
extern Plugin plugin;
}
}
#endif