/**
* Copyright (c) 2018 Cornell University.
*
* Author: Ted Yin <tederminant@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "salticidae/event.h"
#include "salticidae/network.h"
#include "salticidae/stream.h"
/** Hello Message. */
const uint8_t msg_hello_opcode = 0x0;
typedef struct MsgHello {
const char *name;
const char *text;
} MsgHello;
/** Defines how to serialize the msg. */
msg_t *msg_hello_serialize(const char *name, const char *text) {
datastream_t *serialized = datastream_new();
size_t name_len = strlen(name);
datastream_put_i32(serialized, (uint32_t)htole32(name_len));
datastream_put_data(serialized, name, name + name_len);
datastream_put_data(serialized, text, text + strlen(text));
msg_t *msg = msg_new(msg_hello_opcode, datastream_to_bytearray(serialized));
return msg;
}
/** Defines how to parse the msg. */
MsgHello msg_hello_unserialize(const msg_t *msg) {
datastream_t *s = msg_get_payload(msg);
MsgHello res;
uint32_t len;
len = datastream_get_u32(s);
len = le32toh(len);
char *name = (char *)malloc(len + 1);
memmove(name, datastream_get_data_inplace(s, len), len);
name[len] = 0;
len = datastream_size(s);
char *text = (char *)malloc(len + 1);
memmove(text, datastream_get_data_inplace(s, len), len);
text[len] = 0;
res.name = name;
res.text = text;
datastream_free(s);
return res;
}
bytearray_t *msg_ack_serialize() { return bytearray_new(); }
typedef struct MyNet {
msgnetwork_t *net;
const char *name;
} MyNet;
MyNet alice, bob;
void on_receive_hello(const msg_t *_msg, const msgnetwork_conn_t *conn) {
msgnetwork_t *net = msgnetwork_conn_get_net(conn);
const char *name = net == alice.net ? alice.name : bob.name;
MsgHello msg = msg_hello_unserialize(_msg);
printf("[%s] %s says %s\n", name, msg.name, msg.text);
msg_t *ack = msg_new(0x1, msg_ack_serialize());
/* send acknowledgement */
send_msg(ack, conn);
msg_free(ack);
}
void on_receive_ack(const msg_t *msg, const msgnetwork_conn_t *conn) {
msgnetwork_t *net = msgnetwork_conn_get_net(conn);
const char *name = net == alice.net ? alice.name : bob.name;
printf("[%s] the peer knows\n", name);
}
void conn_handler(const msgnetwork_conn_t *conn, bool connected) {
msgnetwork_t