Skip to content

Commit

Permalink
recv: Skeleton code for handling Request messages
Browse files Browse the repository at this point in the history
  • Loading branch information
freeekanayaka committed Mar 16, 2024
1 parent f958a97 commit c0fda09
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ libvr_la_LDFLAGS = -version-info 0:0:0
libvr_la_SOURCES = \
src/configuration.c \
src/heap.c \
src/recv.c \
src/recv_request.c \
src/vr.c

bin_PROGRAMS =
Expand Down
14 changes: 9 additions & 5 deletions include/vr.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,15 @@ struct vr_reply
struct vr_buffer result;
};

enum vr_message_type { VR_PREPARE = 1, VR_PREPARE_OK, VR_REPLY };
enum vr_message_type { VR_REQUEST = 1, VR_PREPARE, VR_PREPARE_OK, VR_REPLY };

struct vr_message
{
enum vr_message_type type;
unsigned server_id;
const char *server_address;
union {
struct vr_request request;
struct vr_prepare prepare;
struct vr_prepare_ok prepare_ok;
struct vr_reply reply;
Expand Down Expand Up @@ -185,16 +188,17 @@ enum vr_event_type {
* receiving a message). */
struct vr_event
{
unsigned long time;
enum vr_event_type type;
unsigned char unused;
unsigned short capacity;
unsigned char reserved[4];
unsigned long time;
union {
struct
{
unsigned long view;
} start;
struct
{
struct vr_message *message;
} receive;
};
};

Expand Down
30 changes: 30 additions & 0 deletions src/recv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "recv.h"
#include "recv_request.h"
#include "tracing.h"

#define infof(...) Infof(r->tracer, " " __VA_ARGS__)

/* Dispatch a single message to the appropriate handler. */
int recvMessage(struct vr *v, struct vr_message *message)
{
unsigned id = message->server_id;
const char *address = message->server_address;
int rv = 0;

switch (message->type) {
case VR_REQUEST:
rv = recvRequest(v, id, address, &message->request);
break;
default:
rv = VR_INVALID;
break;
};

if (rv != 0) {
return rv;
}

return 0;
}

#undef infof
11 changes: 11 additions & 0 deletions src/recv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* Receive a message. */

#ifndef RECV_H_
#define RECV_H_

#include "../include/vr.h"

/* Function to be invoked upon receiving a message. */
int recvMessage(struct vr *r, struct vr_message *message);

#endif /* RECV_H_ */
13 changes: 13 additions & 0 deletions src/recv_request.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "recv_request.h"

int recvRequest(struct vr *v,
unsigned id,
const char *address,
const struct vr_request *request)
{
(void)v;
(void)id;
(void)address;
(void)request;
return 0;
}
14 changes: 14 additions & 0 deletions src/recv_request.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* Receive a Request message. */

#ifndef RECV_REQUEST_H_
#define RECV_REQUEST_H_

#include "../include/vr.h"

/* Process a Request message from the given server. */
int recvRequest(struct vr *v,
unsigned id,
const char *address,
const struct vr_request *request);

#endif /* RECV_REQUEST_H_ */
23 changes: 23 additions & 0 deletions src/vr.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "assert.h"
#include "configuration.h"
#include "heap.h"
#include "recv.h"
#include "tracing.h"

#define infof(...) Infof(v->tracer, "> " __VA_ARGS__)
Expand Down Expand Up @@ -49,6 +50,25 @@ static int stepStart(struct vr *v, unsigned long view)
return 0;
}

/* Handle new messages. */
static int stepReceive(struct vr *v, struct vr_message *message)
{
const char *desc;

switch (message->type) {
case VR_REQUEST:
desc = "request";
break;
default:
desc = "unknown message";
break;
}

infof("recv %s from server %u", desc, message->server_id);

return recvMessage(v, message);
}

int vr_step(struct vr *v,
const struct vr_event *event,
struct vr_update *update)
Expand All @@ -62,6 +82,9 @@ int vr_step(struct vr *v,
case VR_START:
rv = stepStart(v, event->start.view);
break;
case VR_RECEIVE:
rv = stepReceive(v, event->receive.message);
break;
default:
rv = VR_INVALID;
break;
Expand Down

0 comments on commit c0fda09

Please sign in to comment.