Commit 245a4f7c authored by Sven Robertz's avatar Sven Robertz
Browse files

started refactoring for separate writer action for sending signatures

parent 4d3a1e49
......@@ -15,7 +15,6 @@ int main(int argc, char *argv[]) {
encoder = labcomm_encoder_new(labcomm_fd_writer, &fd);
labcomm_encoder_register_simple_TwoInts(encoder);
labcomm_encoder_register_simple_IntString(encoder);
labcomm_encoder_register_simple_TwoArrays(encoder);
simple_IntString is;
is.x = 24;
is.s = "Hello, LabComm!";
......@@ -30,6 +29,8 @@ int main(int argc, char *argv[]) {
int foo[20];
labcomm_encoder_register_simple_TwoArrays(encoder);
simple_TwoArrays ta;
ta.fixed.a[0] = 17;
ta.fixed.a[1] = 42;
......
......@@ -181,13 +181,30 @@ static int get_encoder_index(
return result;
}
void labcomm_encode_signature(struct labcomm_encoder *e,
labcomm_signature_t *signature)
{
int i;
e->writer.write(&e->writer, labcomm_writer_start);
labcomm_encode_packed32(e, signature->type);
labcomm_encode_type_index(e, signature);
labcomm_encode_string(e, signature->name);
for (i = 0 ; i < signature->size ; i++) {
if (e->writer.pos >= e->writer.count) {
e->writer.write(&e->writer, labcomm_writer_continue);
}
e->writer.data[e->writer.pos] = signature->signature[i];
e->writer.pos++;
}
e->writer.write(&e->writer, labcomm_writer_end);
}
static void do_encoder_register(struct labcomm_encoder *e,
labcomm_signature_t *signature,
labcomm_encode_typecast_t encode)
{
if (signature->type == LABCOMM_SAMPLE) {
if (get_encoder_index(e, signature) == 0) {
int i;
labcomm_encoder_context_t *context = e->context;
labcomm_sample_entry_t *sample =
(labcomm_sample_entry_t*)malloc(sizeof(labcomm_sample_entry_t));
......@@ -198,18 +215,11 @@ static void do_encoder_register(struct labcomm_encoder *e,
context->index++;
context->sample = sample;
e->writer.write(&e->writer, labcomm_writer_start);
labcomm_encode_packed32(e, signature->type);
labcomm_encode_type_index(e, signature);
labcomm_encode_string(e, signature->name);
for (i = 0 ; i < signature->size ; i++) {
if (e->writer.pos >= e->writer.count) {
e->writer.write(&e->writer, labcomm_writer_continue);
}
e->writer.data[e->writer.pos] = signature->signature[i];
e->writer.pos++;
}
e->writer.write(&e->writer, labcomm_writer_end);
#ifdef OLD_STUFF
labcomm_encode_signature(e, signature);
#else
e->writer.write(&e->writer, labcomm_writer_send_signature, signature, e);
#endif
}
}
}
......@@ -231,7 +241,7 @@ static void do_encode(
}
labcomm_encoder_t *labcomm_encoder_new(
int (*writer)(labcomm_writer_t *, labcomm_writer_action_t),
int (*writer)(labcomm_writer_t *, labcomm_writer_action_t, ...),
void *writer_context)
{
labcomm_encoder_t *result = malloc(sizeof(labcomm_encoder_t));
......@@ -314,7 +324,7 @@ void labcomm_encode_type_index(labcomm_encoder_t *e, labcomm_signature_t *s)
static int signature_writer(
labcomm_writer_t *w,
labcomm_writer_action_t action)
labcomm_writer_action_t action, ...)
{
switch (action) {
case labcomm_writer_alloc: {
......
......@@ -31,6 +31,9 @@ typedef struct {
unsigned char *signature;
} labcomm_signature_t;
//TODO: something along the lines of...
void labcomm_encode_signature(struct labcomm_encoder*, labcomm_signature_t*);
/*
* Error handling.
*/
......@@ -127,6 +130,7 @@ typedef enum {
labcomm_writer_continue,
labcomm_writer_end,
labcomm_writer_available,
labcomm_writer_send_signature
} labcomm_writer_action_t;
typedef struct labcomm_writer {
......@@ -135,12 +139,12 @@ typedef struct labcomm_writer {
int data_size;
int count;
int pos;
int (*write)(struct labcomm_writer *, labcomm_writer_action_t);
int (*write)(struct labcomm_writer *, labcomm_writer_action_t, ...);
labcomm_error_handler_callback on_error;
} labcomm_writer_t;
struct labcomm_encoder *labcomm_encoder_new(
int (*writer)(labcomm_writer_t *, labcomm_writer_action_t),
int (*writer)(labcomm_writer_t *, labcomm_writer_action_t, ...),
void *writer_context);
void labcomm_encoder_free(
struct labcomm_encoder *encoder);
......
......@@ -59,8 +59,9 @@ int labcomm_fd_reader(
int labcomm_fd_writer(
labcomm_writer_t *w,
labcomm_writer_action_t action)
labcomm_writer_action_t action, ...)
{
va_list argp;
int result = 0;
int *fd = w->context;
......@@ -99,6 +100,18 @@ int labcomm_fd_writer(
case labcomm_writer_available: {
result = w->count - w->pos;
} break;
case labcomm_writer_send_signature: {
//TODO: move to generic auxilliary writer functions file in lib?
va_start(argp, action);
labcomm_signature_t *signature = va_arg(argp, labcomm_signature_t*);
struct labcomm_encoder *e = va_arg(argp, struct labcomm_encoder*);
va_end(argp);
printf("Sending signature: %s\n", signature->name);
labcomm_encode_signature(e, signature);
} break;
}
return result;
}
......@@ -335,7 +335,7 @@ static inline void labcomm_encode_string(labcomm_encoder_t *e,
void labcomm_encode_type_index(labcomm_encoder_t *e, labcomm_signature_t *s);
static inline int labcomm_buffer_write(struct labcomm_writer *w,
labcomm_writer_action_t action)
labcomm_writer_action_t action, ...)
{
// If this gets called, it is an error,
// so note error and let producer proceed
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment