241 lines
6.2 KiB
C
241 lines
6.2 KiB
C
#include "engine.h"
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
|
|
void engine_init(Engine *engine)
|
|
{
|
|
memset(engine, 0, sizeof(*engine));
|
|
}
|
|
|
|
static void reject(Engine *engine, Observation *out, int operation)
|
|
{
|
|
engine->rejected++;
|
|
observe_event(out, "REJECT", operation, 0, 0);
|
|
}
|
|
|
|
static int valid_slot(int64_t slot)
|
|
{
|
|
return slot >= 0 && slot < 4;
|
|
}
|
|
|
|
static int valid_payload(const unsigned char *text, size_t length)
|
|
{
|
|
size_t index;
|
|
for (index = 0; index < length; index++) {
|
|
unsigned char value = text[index];
|
|
if ((value >= '0' && value <= '9') || value == '-' || value == '+' ||
|
|
value == ',' || value == ';' || (value >= 'a' && value <= 'z')) {
|
|
continue;
|
|
}
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
static int open_session(Engine *engine, int slot, const char *hex, Observation *out)
|
|
{
|
|
ParseSession next;
|
|
memset(&next, 0, sizeof(next));
|
|
if (!wire_hex(hex, next.text, 256, &next.length)) {
|
|
reject(engine, out, 'O');
|
|
return 0;
|
|
}
|
|
if (!valid_payload(next.text, next.length) || engine->session[slot].active) {
|
|
reject(engine, out, 'O');
|
|
return 0;
|
|
}
|
|
next.active = 1;
|
|
next.exhausted = next.length == 0;
|
|
engine->session[slot] = next;
|
|
engine->shared_position = 0;
|
|
engine->opened++;
|
|
observe_event(out, "OPEN", slot, next.length, 0);
|
|
return 1;
|
|
}
|
|
|
|
static int parse_decimal(const unsigned char *text, size_t length, int64_t *result)
|
|
{
|
|
size_t position = 0;
|
|
int sign = 1;
|
|
int64_t value = 0;
|
|
if (!length) {
|
|
return 0;
|
|
}
|
|
if (text[position] == '-' || text[position] == '+') {
|
|
sign = text[position] == '-' ? -1 : 1;
|
|
position++;
|
|
}
|
|
if (position == length) {
|
|
return 0;
|
|
}
|
|
while (position < length) {
|
|
unsigned char digit = text[position++];
|
|
if (digit < '0' || digit > '9') {
|
|
return 0;
|
|
}
|
|
value = value * 10 + digit - '0';
|
|
if (value > 1000) {
|
|
return 0;
|
|
}
|
|
}
|
|
*result = sign * value;
|
|
return 1;
|
|
}
|
|
|
|
static size_t record_start(Engine *engine, ParseSession *session)
|
|
{
|
|
(void)engine;
|
|
return session->position;
|
|
}
|
|
|
|
static size_t record_end(const ParseSession *session, size_t start)
|
|
{
|
|
const unsigned char *separator = memchr(session->text + start, ';', session->length - start);
|
|
return separator ? (size_t)(separator - session->text) : session->length;
|
|
}
|
|
|
|
static int parse_record(const ParseSession *session, size_t start, size_t end,
|
|
size_t *fields, int64_t *sum)
|
|
{
|
|
size_t cursor = start;
|
|
int valid = 1;
|
|
*fields = 0;
|
|
*sum = 0;
|
|
for (;;) {
|
|
size_t stop = cursor;
|
|
int64_t value = 0;
|
|
while (stop < end && session->text[stop] != ',') {
|
|
stop++;
|
|
}
|
|
(*fields)++;
|
|
if (!parse_decimal(session->text + cursor, stop - cursor, &value)) {
|
|
valid = 0;
|
|
} else {
|
|
*sum += value;
|
|
}
|
|
if (stop == end) {
|
|
break;
|
|
}
|
|
cursor = stop + 1;
|
|
}
|
|
return valid;
|
|
}
|
|
|
|
static void advance_cursor(Engine *engine, ParseSession *session, size_t end)
|
|
{
|
|
if (end == session->length) {
|
|
session->position = session->length;
|
|
session->exhausted = 1;
|
|
} else {
|
|
session->position = end + 1;
|
|
}
|
|
engine->shared_position = session->position;
|
|
}
|
|
|
|
static void next_record(Engine *engine, int slot, Observation *out)
|
|
{
|
|
ParseSession *session = &engine->session[slot];
|
|
size_t start;
|
|
size_t end;
|
|
size_t fields;
|
|
int64_t sum;
|
|
int valid;
|
|
if (!session->active) {
|
|
reject(engine, out, 'N');
|
|
return;
|
|
}
|
|
if (session->exhausted) {
|
|
observe_event(out, "EOF", slot, 0, 0);
|
|
return;
|
|
}
|
|
start = record_start(engine, session);
|
|
if (start > session->length) {
|
|
start = session->length;
|
|
}
|
|
end = record_end(session, start);
|
|
valid = parse_record(session, start, end, &fields, &sum);
|
|
advance_cursor(engine, session, end);
|
|
if (!valid) {
|
|
session->bad++;
|
|
engine->bad++;
|
|
observe_event(out, "BAD", slot, fields, 0);
|
|
return;
|
|
}
|
|
session->good++;
|
|
session->sum += sum;
|
|
engine->good++;
|
|
engine->total += sum;
|
|
observe_event(out, "ROW", slot, fields, sum);
|
|
}
|
|
|
|
static void close_session(Engine *engine, int slot, Observation *out)
|
|
{
|
|
if (!engine->session[slot].active) {
|
|
reject(engine, out, 'C');
|
|
return;
|
|
}
|
|
engine->session[slot].active = 0;
|
|
engine->closed++;
|
|
observe_event(out, "CLOSE", slot, 0, 0);
|
|
}
|
|
|
|
void engine_command(Engine *engine, const Command *command, Observation *out)
|
|
{
|
|
int64_t slot;
|
|
if (command->name == 'R' && command->count == 0) {
|
|
engine_init(engine);
|
|
return;
|
|
}
|
|
if (!wire_integer(command, 0, &slot) || !valid_slot(slot)) {
|
|
reject(engine, out, (unsigned char)command->name);
|
|
return;
|
|
}
|
|
switch (command->name) {
|
|
case 'O':
|
|
if (command->count != 2) {
|
|
reject(engine, out, 'O');
|
|
} else {
|
|
(void)open_session(engine, (int)slot, command->arg[1], out);
|
|
}
|
|
break;
|
|
case 'N':
|
|
if (command->count != 1) {
|
|
reject(engine, out, 'N');
|
|
} else {
|
|
next_record(engine, (int)slot, out);
|
|
}
|
|
break;
|
|
case 'C':
|
|
if (command->count != 1) {
|
|
reject(engine, out, 'C');
|
|
} else {
|
|
close_session(engine, (int)slot, out);
|
|
}
|
|
break;
|
|
default:
|
|
reject(engine, out, (unsigned char)command->name);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void engine_snapshot(const Engine *engine, Observation *out)
|
|
{
|
|
int index;
|
|
observe_value(out, engine->opened);
|
|
observe_value(out, engine->closed);
|
|
observe_value(out, engine->rejected);
|
|
observe_value(out, engine->good);
|
|
observe_value(out, engine->bad);
|
|
observe_value(out, engine->total);
|
|
for (index = 0; index < 4; index++) {
|
|
const ParseSession *session = &engine->session[index];
|
|
observe_value(out, session->active);
|
|
observe_value(out, session->length);
|
|
observe_value(out, session->position);
|
|
observe_value(out, session->exhausted);
|
|
observe_value(out, session->good);
|
|
observe_value(out, session->bad);
|
|
observe_value(out, session->sum);
|
|
}
|
|
}
|