113 lines
2.5 KiB
C
113 lines
2.5 KiB
C
#include "model.h"
|
|
#include <string.h>
|
|
|
|
void model_init(Model *m)
|
|
{
|
|
memset(m, 0, sizeof(*m));
|
|
}
|
|
|
|
static void reject(Model *m, const Command *c, Observation *o)
|
|
{
|
|
m->rejected++;
|
|
observe_event(o, "REJECT", (unsigned char)c->name, 0, 0);
|
|
}
|
|
|
|
static int command_P(Model *m, const Command *c, Observation *o)
|
|
{
|
|
int64_t id;
|
|
int64_t length;
|
|
int64_t seed;
|
|
if (!wire_arguments(c, 3, 3)) {
|
|
return 0;
|
|
}
|
|
id = wire_number(c, 0);
|
|
if (id < 0 || id > 999) {
|
|
return 0;
|
|
}
|
|
length = wire_number(c, 1);
|
|
if (length < 0 || length > 32) {
|
|
return 0;
|
|
}
|
|
seed = wire_number(c, 2);
|
|
if (seed < 0 || seed > 255) {
|
|
return 0;
|
|
}
|
|
if (!(m->count < 3)) {
|
|
return 0;
|
|
}
|
|
spool_put(m, id, length, seed, o);
|
|
return 1;
|
|
}
|
|
|
|
static int command_W(Model *m, const Command *c, Observation *o)
|
|
{
|
|
int64_t allowance;
|
|
int64_t error;
|
|
if (!wire_arguments(c, 2, 2)) {
|
|
return 0;
|
|
}
|
|
allowance = wire_number(c, 0);
|
|
if (allowance < 1 || allowance > 32) {
|
|
return 0;
|
|
}
|
|
error = wire_number(c, 1);
|
|
if (error < 0 || error > 4) {
|
|
return 0;
|
|
}
|
|
if (!(1)) {
|
|
return 0;
|
|
}
|
|
spool_step(m, allowance, error, o);
|
|
return 1;
|
|
}
|
|
|
|
void model_command(Model *m, const Command *c, Observation *o)
|
|
{
|
|
int accepted = 0;
|
|
switch (c->name) {
|
|
case 'R':
|
|
if (c->count == 0) {
|
|
model_init(m);
|
|
accepted = 1;
|
|
}
|
|
break;
|
|
case 'P':
|
|
accepted = command_P(m, c, o);
|
|
break;
|
|
case 'W':
|
|
accepted = command_W(m, c, o);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
if (!accepted) {
|
|
reject(m, c, o);
|
|
}
|
|
}
|
|
|
|
void model_snapshot(const Model *m, Observation *o)
|
|
{
|
|
observe_value(o, m->head);
|
|
observe_value(o, m->count);
|
|
observe_value(o, m->queued);
|
|
observe_value(o, m->done);
|
|
observe_value(o, m->dropped);
|
|
observe_value(o, m->attempts);
|
|
observe_value(o, m->pauses);
|
|
observe_value(o, m->bytes);
|
|
observe_value(o, m->checksum);
|
|
observe_value(o, m->rejected);
|
|
observe_value(o, m->ids[0]);
|
|
observe_value(o, m->lengths[0]);
|
|
observe_value(o, m->seeds[0]);
|
|
observe_value(o, m->offsets[0]);
|
|
observe_value(o, m->ids[1]);
|
|
observe_value(o, m->lengths[1]);
|
|
observe_value(o, m->seeds[1]);
|
|
observe_value(o, m->offsets[1]);
|
|
observe_value(o, m->ids[2]);
|
|
observe_value(o, m->lengths[2]);
|
|
observe_value(o, m->seeds[2]);
|
|
observe_value(o, m->offsets[2]);
|
|
}
|