Commit d3e25d39 authored by Anders Blomdell's avatar Anders Blomdell
Browse files

Version string encapsulated with tag and length, hence version string not

passed to labcomm_writer_alloc any more.
parent 10bbd93d
...@@ -62,14 +62,13 @@ static void set_decimation( ...@@ -62,14 +62,13 @@ static void set_decimation(
static int wrap_reader_alloc( static int wrap_reader_alloc(
struct labcomm_reader *r, struct labcomm_reader *r,
struct labcomm_reader_action_context *action_context, struct labcomm_reader_action_context *action_context)
char *labcomm_version)
{ {
struct decimating_private *decimating = action_context->context; struct decimating_private *decimating = action_context->context;
labcomm_decoder_register_decimating_messages_set_decimation( labcomm_decoder_register_decimating_messages_set_decimation(
r->decoder, set_decimation, decimating); r->decoder, set_decimation, decimating);
return labcomm_reader_alloc(r, action_context->next, labcomm_version); return labcomm_reader_alloc(r, action_context->next);
} }
struct send_set_decimation { struct send_set_decimation {
...@@ -186,14 +185,13 @@ static void register_signatures(void *context) ...@@ -186,14 +185,13 @@ static void register_signatures(void *context)
static int wrap_writer_alloc( static int wrap_writer_alloc(
struct labcomm_writer *w, struct labcomm_writer *w,
struct labcomm_writer_action_context *action_context, struct labcomm_writer_action_context *action_context)
char *labcomm_version)
{ {
struct decimating_private *decimating = action_context->context; struct decimating_private *decimating = action_context->context;
labcomm_scheduler_enqueue(decimating->scheduler, labcomm_scheduler_enqueue(decimating->scheduler,
0, register_signatures, decimating); 0, register_signatures, decimating);
return labcomm_writer_alloc(w, action_context->next, labcomm_version); return labcomm_writer_alloc(w, action_context->next);
} }
static int wrap_writer_start( static int wrap_writer_start(
...@@ -206,20 +204,22 @@ static int wrap_writer_start( ...@@ -206,20 +204,22 @@ static int wrap_writer_start(
struct decimation *decimation; struct decimation *decimation;
int result; int result;
labcomm_scheduler_data_lock(decimating->scheduler); if (index < LABCOMM_USER) {
decimation = LABCOMM_SIGNATURE_ARRAY_REF(decimating->memory,
decimating->writer_decimation,
struct decimation, index);
decimation->current++;
if (decimation->current < decimation->n) {
result = -EALREADY;
} else {
decimation->current = 0;
result = 0; result = 0;
} else {
labcomm_scheduler_data_lock(decimating->scheduler);
decimation = LABCOMM_SIGNATURE_ARRAY_REF(decimating->memory,
decimating->writer_decimation,
struct decimation, index);
decimation->current++;
if (decimation->current < decimation->n) {
result = -EALREADY;
} else {
decimation->current = 0;
result = 0;
}
labcomm_scheduler_data_unlock(decimating->scheduler);
} }
labcomm_scheduler_data_unlock(decimating->scheduler);
if (result == 0) { if (result == 0) {
result = labcomm_writer_start(w, action_context->next, result = labcomm_writer_start(w, action_context->next,
index, signature, value); index, signature, value);
......
...@@ -124,15 +124,14 @@ static void handles_signature( ...@@ -124,15 +124,14 @@ static void handles_signature(
static int wrap_reader_alloc( static int wrap_reader_alloc(
struct labcomm_reader *r, struct labcomm_reader *r,
struct labcomm_reader_action_context *action_context, struct labcomm_reader_action_context *action_context)
char *labcomm_version)
{ {
struct introspecting_private *introspecting = action_context->context; struct introspecting_private *introspecting = action_context->context;
labcomm_decoder_register_introspecting_messages_handles_signature( labcomm_decoder_register_introspecting_messages_handles_signature(
introspecting->introspecting.reader->decoder, introspecting->introspecting.reader->decoder,
handles_signature, introspecting); handles_signature, introspecting);
return labcomm_reader_alloc(r, action_context->next, labcomm_version); return labcomm_reader_alloc(r, action_context->next);
} }
struct handles_signature { struct handles_signature {
...@@ -213,14 +212,13 @@ static void register_encoder_signatures(void *context) ...@@ -213,14 +212,13 @@ static void register_encoder_signatures(void *context)
static int wrap_writer_alloc( static int wrap_writer_alloc(
struct labcomm_writer *w, struct labcomm_writer *w,
struct labcomm_writer_action_context *action_context, struct labcomm_writer_action_context *action_context)
char *labcomm_version)
{ {
struct introspecting_private *introspecting = action_context->context; struct introspecting_private *introspecting = action_context->context;
labcomm_scheduler_enqueue(introspecting->scheduler, labcomm_scheduler_enqueue(introspecting->scheduler,
0, register_encoder_signatures, introspecting); 0, register_encoder_signatures, introspecting);
return labcomm_writer_alloc(w, action_context->next, labcomm_version); return labcomm_writer_alloc(w, action_context->next);
} }
static int wrap_writer_start( static int wrap_writer_start(
...@@ -231,7 +229,7 @@ static int wrap_writer_start( ...@@ -231,7 +229,7 @@ static int wrap_writer_start(
{ {
struct introspecting_private *introspecting = action_context->context; struct introspecting_private *introspecting = action_context->context;
if (value == NULL) { if (index >= LABCOMM_USER && value == NULL) {
struct local *local; struct local *local;
labcomm_scheduler_data_lock(introspecting->scheduler); labcomm_scheduler_data_lock(introspecting->scheduler);
......
...@@ -48,10 +48,9 @@ ...@@ -48,10 +48,9 @@
} }
int labcomm2006_reader_alloc(struct labcomm2006_reader *r, int labcomm2006_reader_alloc(struct labcomm2006_reader *r,
struct labcomm2006_reader_action_context *action_context, struct labcomm2006_reader_action_context *action_context)
char *labcomm2006_version)
{ {
UNWRAP(alloc, r, action_context, labcomm2006_version); UNWRAP(alloc, r, action_context);
} }
int labcomm2006_reader_free(struct labcomm2006_reader *r, int labcomm2006_reader_free(struct labcomm2006_reader *r,
...@@ -92,10 +91,9 @@ int labcomm2006_reader_ioctl(struct labcomm2006_reader *r, ...@@ -92,10 +91,9 @@ int labcomm2006_reader_ioctl(struct labcomm2006_reader *r,
} }
int labcomm2006_writer_alloc(struct labcomm2006_writer *w, int labcomm2006_writer_alloc(struct labcomm2006_writer *w,
struct labcomm2006_writer_action_context *action_context, struct labcomm2006_writer_action_context *action_context)
char *labcomm2006_version)
{ {
UNWRAP(alloc, w, action_context, labcomm2006_version); UNWRAP(alloc, w, action_context);
} }
int labcomm2006_writer_free(struct labcomm2006_writer *w, int labcomm2006_writer_free(struct labcomm2006_writer *w,
......
...@@ -186,7 +186,7 @@ static int decode_typedef_or_sample(struct labcomm2006_decoder *d, int kind) ...@@ -186,7 +186,7 @@ static int decode_typedef_or_sample(struct labcomm2006_decoder *d, int kind)
local_signature = NULL; local_signature = NULL;
local_index = 0; local_index = 0;
labcomm2006_writer_alloc(&writer, writer.action_context, ""); labcomm2006_writer_alloc(&writer, writer.action_context);
labcomm2006_writer_start(&writer, writer.action_context, 0, NULL, NULL); labcomm2006_writer_start(&writer, writer.action_context, 0, NULL, NULL);
remote_index = labcomm2006_read_packed32(d->reader); remote_index = labcomm2006_read_packed32(d->reader);
signature.name = labcomm2006_read_string(d->reader); signature.name = labcomm2006_read_string(d->reader);
...@@ -286,8 +286,7 @@ static void reader_alloc(struct labcomm2006_decoder *d) ...@@ -286,8 +286,7 @@ static void reader_alloc(struct labcomm2006_decoder *d)
{ {
if (!d->reader_allocated) { if (!d->reader_allocated) {
d->reader_allocated = 1; d->reader_allocated = 1;
labcomm2006_reader_alloc(d->reader, d->reader->action_context, labcomm2006_reader_alloc(d->reader, d->reader->action_context);
LABCOMM_VERSION);
} }
} }
......
...@@ -28,8 +28,7 @@ ...@@ -28,8 +28,7 @@
#include "labcomm2006_dynamic_buffer_writer.h" #include "labcomm2006_dynamic_buffer_writer.h"
static int dyn_alloc(struct labcomm2006_writer *w, static int dyn_alloc(struct labcomm2006_writer *w,
struct labcomm2006_writer_action_context *action_context, struct labcomm2006_writer_action_context *action_context)
char *labcomm2006_version)
{ {
w->data_size = 1000; w->data_size = 1000;
w->count = w->data_size; w->count = w->data_size;
......
...@@ -53,7 +53,7 @@ struct labcomm2006_encoder *labcomm2006_encoder_new( ...@@ -53,7 +53,7 @@ struct labcomm2006_encoder *labcomm2006_encoder_new(
result->scheduler = scheduler; result->scheduler = scheduler;
LABCOMM_SIGNATURE_ARRAY_INIT(result->registered, int); LABCOMM_SIGNATURE_ARRAY_INIT(result->registered, int);
labcomm2006_writer_alloc(result->writer, labcomm2006_writer_alloc(result->writer,
result->writer->action_context, LABCOMM_VERSION); result->writer->action_context);
} }
return result; return result;
} }
......
...@@ -36,19 +36,9 @@ struct labcomm2006_fd_reader { ...@@ -36,19 +36,9 @@ struct labcomm2006_fd_reader {
}; };
static int fd_alloc(struct labcomm2006_reader *r, static int fd_alloc(struct labcomm2006_reader *r,
struct labcomm2006_reader_action_context *action_context, struct labcomm2006_reader_action_context *action_context)
char *version)
{ {
int result = 0; int result = 0;
// in-band version is not included in version 2006.
// This may be a place for version checking and/or printing
// a warning message
if (version && version[0]) {
if (strcmp(LABCOMM_VERSION, version) != 0) {
fprintf(stderr, "ERROR: version mismatch: %s != %s\n", version, LABCOMM_VERSION);
return -EINVAL;
}
}
r->count = 0; r->count = 0;
r->pos = 0; r->pos = 0;
......
...@@ -40,20 +40,9 @@ static int fd_flush(struct labcomm2006_writer *w, ...@@ -40,20 +40,9 @@ static int fd_flush(struct labcomm2006_writer *w,
struct labcomm2006_writer_action_context *action_context); struct labcomm2006_writer_action_context *action_context);
static int fd_alloc(struct labcomm2006_writer *w, static int fd_alloc(struct labcomm2006_writer *w,
struct labcomm2006_writer_action_context *action_context, struct labcomm2006_writer_action_context *action_context)
char *version)
{ {
// in-band version is not included in version 2006. w->data = labcomm2006_memory_alloc(w->memory, 0, BUFFER_SIZE);
// This may be a place for version checking and/or printing
// a warning message
if (version && version[0]) {
if (strcmp(LABCOMM_VERSION, version) != 0) {
fprintf(stderr, "ERROR: version mismatch: %s != %s\n", version, LABCOMM_VERSION);
return -EINVAL;
}
}
w->data = labcomm2006_memory_alloc(w->memory, 0, BUFFER_SIZE);
if (! w->data) { if (! w->data) {
w->error = -ENOMEM; w->error = -ENOMEM;
w->data_size = 0; w->data_size = 0;
......
...@@ -96,17 +96,14 @@ struct labcomm2006_reader_action_context; ...@@ -96,17 +96,14 @@ struct labcomm2006_reader_action_context;
struct labcomm2006_reader_action { struct labcomm2006_reader_action {
/* 'alloc' is called at the first invocation of 'labcomm2006_decoder_decode_one' /* 'alloc' is called at the first invocation of 'labcomm2006_decoder_decode_one'
on the decoder containing the reader. If 'labcomm2006_version' != NULL on the decoder containing the reader.
and non-empty the transport layer may use it to ensure that
compatible versions are used.
Returned value: Returned value:
> 0 Number of bytes allocated for buffering > 0 Number of bytes allocated for buffering
<= 0 Error <= 0 Error
*/ */
int (*alloc)(struct labcomm2006_reader *r, int (*alloc)(struct labcomm2006_reader *r,
struct labcomm2006_reader_action_context *action_context, struct labcomm2006_reader_action_context *action_context);
char *labcomm2006_version);
/* 'free' returns the resources claimed by 'alloc' and might have other /* 'free' returns the resources claimed by 'alloc' and might have other
reader specific side-effects as well. reader specific side-effects as well.
...@@ -159,8 +156,7 @@ struct labcomm2006_reader { ...@@ -159,8 +156,7 @@ struct labcomm2006_reader {
}; };
int labcomm2006_reader_alloc(struct labcomm2006_reader *r, int labcomm2006_reader_alloc(struct labcomm2006_reader *r,
struct labcomm2006_reader_action_context *action_context, struct labcomm2006_reader_action_context *action_context);
char *labcomm2006_version);
int labcomm2006_reader_free(struct labcomm2006_reader *r, int labcomm2006_reader_free(struct labcomm2006_reader *r,
struct labcomm2006_reader_action_context *action_context); struct labcomm2006_reader_action_context *action_context);
int labcomm2006_reader_start(struct labcomm2006_reader *r, int labcomm2006_reader_start(struct labcomm2006_reader *r,
...@@ -299,8 +295,7 @@ struct labcomm2006_writer_action_context; ...@@ -299,8 +295,7 @@ struct labcomm2006_writer_action_context;
struct labcomm2006_writer_action { struct labcomm2006_writer_action {
int (*alloc)(struct labcomm2006_writer *w, int (*alloc)(struct labcomm2006_writer *w,
struct labcomm2006_writer_action_context *action_context, struct labcomm2006_writer_action_context *action_context);
char *labcomm2006_version);
int (*free)(struct labcomm2006_writer *w, int (*free)(struct labcomm2006_writer *w,
struct labcomm2006_writer_action_context *action_context); struct labcomm2006_writer_action_context *action_context);
/* 'start' is called right before a sample is to be sent. In the /* 'start' is called right before a sample is to be sent. In the
...@@ -345,8 +340,7 @@ struct labcomm2006_writer { ...@@ -345,8 +340,7 @@ struct labcomm2006_writer {
}; };
int labcomm2006_writer_alloc(struct labcomm2006_writer *w, int labcomm2006_writer_alloc(struct labcomm2006_writer *w,
struct labcomm2006_writer_action_context *action_context, struct labcomm2006_writer_action_context *action_context);
char *labcomm_version);
int labcomm2006_writer_free(struct labcomm2006_writer *w, int labcomm2006_writer_free(struct labcomm2006_writer *w,
struct labcomm2006_writer_action_context *action_context); struct labcomm2006_writer_action_context *action_context);
int labcomm2006_writer_start(struct labcomm2006_writer *w, int labcomm2006_writer_start(struct labcomm2006_writer *w,
......
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
#include "labcomm_ioctl.h" #include "labcomm_ioctl.h"
#include "labcomm_dynamic_buffer_writer.h" #include "labcomm_dynamic_buffer_writer.h"
#define LABCOMM_VERSION "LabComm20141009" #define CURRENT_VERSION "LabComm20141009"
/* Unwrapping reader/writer functions */ /* Unwrapping reader/writer functions */
#define UNWRAP_ac(rw, ac, ...) ac #define UNWRAP_ac(rw, ac, ...) ac
...@@ -50,10 +50,9 @@ ...@@ -50,10 +50,9 @@
} }
int labcomm_reader_alloc(struct labcomm_reader *r, int labcomm_reader_alloc(struct labcomm_reader *r,
struct labcomm_reader_action_context *action_context, struct labcomm_reader_action_context *action_context)
char *labcomm_version)
{ {
UNWRAP(alloc, r, action_context, labcomm_version); UNWRAP(alloc, r, action_context);
} }
int labcomm_reader_free(struct labcomm_reader *r, int labcomm_reader_free(struct labcomm_reader *r,
...@@ -94,10 +93,9 @@ int labcomm_reader_ioctl(struct labcomm_reader *r, ...@@ -94,10 +93,9 @@ int labcomm_reader_ioctl(struct labcomm_reader *r,
} }
int labcomm_writer_alloc(struct labcomm_writer *w, int labcomm_writer_alloc(struct labcomm_writer *w,
struct labcomm_writer_action_context *action_context, struct labcomm_writer_action_context *action_context)
char *labcomm_version)
{ {
UNWRAP(alloc, w, action_context, labcomm_version); UNWRAP(alloc, w, action_context);
} }
int labcomm_writer_free(struct labcomm_writer *w, int labcomm_writer_free(struct labcomm_writer *w,
...@@ -265,11 +263,6 @@ int labcomm_internal_sizeof(struct labcomm_signature *signature, ...@@ -265,11 +263,6 @@ int labcomm_internal_sizeof(struct labcomm_signature *signature,
void *v) void *v)
{ {
int length = signature->encoded_size(v); int length = signature->encoded_size(v);
fprintf(stderr, "SIZEOF(%s) = %d %d %d\n",
signature->name,
labcomm_size_packed32(signature->index),
labcomm_size_packed32(length),
length);
return (labcomm_size_packed32(signature->index) + return (labcomm_size_packed32(signature->index) +
labcomm_size_packed32(length) + labcomm_size_packed32(length) +
length); length);
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#define LABCOMM_VERSION "LabComm20141009" #define CURRENT_VERSION "LabComm20141009"
#include <errno.h> #include <errno.h>
#include "labcomm.h" #include "labcomm.h"
...@@ -37,6 +37,7 @@ struct sample_entry { ...@@ -37,6 +37,7 @@ struct sample_entry {
struct labcomm_decoder { struct labcomm_decoder {
struct labcomm_reader *reader; struct labcomm_reader *reader;
int reader_allocated; int reader_allocated;
int version_ok;
struct labcomm_error_handler *error; struct labcomm_error_handler *error;
struct labcomm_memory *memory; struct labcomm_memory *memory;
struct labcomm_scheduler *scheduler; struct labcomm_scheduler *scheduler;
...@@ -64,6 +65,7 @@ struct labcomm_decoder *labcomm_decoder_new( ...@@ -64,6 +65,7 @@ struct labcomm_decoder *labcomm_decoder_new(
result->reader->pos = 0; result->reader->pos = 0;
result->reader->error = 0; result->reader->error = 0;
result->reader_allocated = 0; result->reader_allocated = 0;
result->version_ok = 0;
result->error = error; result->error = error;
result->memory = memory; result->memory = memory;
result->scheduler = scheduler; result->scheduler = scheduler;
...@@ -105,7 +107,7 @@ static int decode_sample(struct labcomm_decoder *d, int kind) ...@@ -105,7 +107,7 @@ static int decode_sample(struct labcomm_decoder *d, int kind)
signature.size = labcomm_read_packed32(d->reader); signature.size = labcomm_read_packed32(d->reader);
if (d->reader->error < 0) { if (d->reader->error < 0) {
result = d->reader->error; result = d->reader->error;
goto out; goto free_signature_name;
} }
signature.signature = labcomm_memory_alloc(d->memory, 1, signature.size); signature.signature = labcomm_memory_alloc(d->memory, 1, signature.size);
if (d->reader->error < 0) { if (d->reader->error < 0) {
...@@ -184,14 +186,13 @@ static void reader_alloc(struct labcomm_decoder *d) ...@@ -184,14 +186,13 @@ static void reader_alloc(struct labcomm_decoder *d)
{ {
if (!d->reader_allocated) { if (!d->reader_allocated) {
d->reader_allocated = 1; d->reader_allocated = 1;
labcomm_reader_alloc(d->reader, d->reader->action_context, labcomm_reader_alloc(d->reader, d->reader->action_context);
LABCOMM_VERSION);
} }
} }
int labcomm_decoder_decode_one(struct labcomm_decoder *d) int labcomm_decoder_decode_one(struct labcomm_decoder *d)
{ {
int result, remote_index, length __attribute__((__unused__)); int result, remote_index, length;
reader_alloc(d); reader_alloc(d);
remote_index = labcomm_read_packed32(d->reader); remote_index = labcomm_read_packed32(d->reader);
...@@ -204,8 +205,30 @@ int labcomm_decoder_decode_one(struct labcomm_decoder *d) ...@@ -204,8 +205,30 @@ int labcomm_decoder_decode_one(struct labcomm_decoder *d)
result = d->reader->error; result = d->reader->error;
goto out; goto out;
} }
if (remote_index == LABCOMM_SAMPLE) { if (remote_index == LABCOMM_VERSION) {
char *version = labcomm_read_string(d->reader);
if (d->reader->error < 0) {
result = d->reader->error;
goto out;
}
if (strcmp(version, CURRENT_VERSION) == 0) {
result = LABCOMM_VERSION;
d->version_ok = 1;
} else {
result = -ECONNRESET;
}
fprintf(stderr, "VERSION %s %d\n", version, result);
labcomm_memory_free(d->memory, 1, version);
} else if (! d->version_ok) {
fprintf(stderr, "No VERSION %d %d\n", remote_index, length);
result = -ECONNRESET;
} else if (remote_index == LABCOMM_SAMPLE) {
result = decode_sample(d, remote_index); result = decode_sample(d, remote_index);
} else if (remote_index == LABCOMM_PRAGMA && 0 /* d->pragma_handler*/) {
/* d->prama_handler(...); */
} else if (remote_index < LABCOMM_USER) {
fprintf(stderr, "SKIP %d %d\n", remote_index, length);
result = remote_index;
} else { } else {
int *local_index; int *local_index;
struct call_handler_context wrap = { struct call_handler_context wrap = {
...@@ -244,7 +267,7 @@ int labcomm_decoder_decode_one(struct labcomm_decoder *d) ...@@ -244,7 +267,7 @@ int labcomm_decoder_decode_one(struct labcomm_decoder *d)
result = -ENOENT; result = -ENOENT;
} }
} }
out: out:
return result; return result;
} }
......
...@@ -28,8 +28,7 @@ ...@@ -28,8 +28,7 @@
#include "labcomm_dynamic_buffer_writer.h" #include "labcomm_dynamic_buffer_writer.h"
static int dyn_alloc(struct labcomm_writer *w, static int dyn_alloc(struct labcomm_writer *w,
struct labcomm_writer_action_context *action_context, struct labcomm_writer_action_context *action_context)
char *labcomm_version)
{ {
w->data_size = 1000; w->data_size = 1000;
w->count = w->data_size; w->count = w->data_size;
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#define LABCOMM_VERSION "LabComm20141009" #define CURRENT_VERSION "LabComm20141009"
#include <errno.h> #include <errno.h>
#include "labcomm.h" #include "labcomm.h"
...@@ -43,6 +43,8 @@ struct labcomm_encoder *labcomm_encoder_new( ...@@ -43,6 +43,8 @@ struct labcomm_encoder *labcomm_encoder_new(
result = labcomm_memory_alloc(memory, 0, sizeof(*result)); result = labcomm_memory_alloc(memory, 0, sizeof(*result));
if (result) { if (result) {
int length;