Skip to content

Commit

Permalink
Add WIP test_tcp_channel inside riot examples
Browse files Browse the repository at this point in the history
  • Loading branch information
LasseRosenow committed Oct 24, 2024
1 parent 78810a8 commit 8c68249
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
18 changes: 18 additions & 0 deletions examples/riot/test_tcp_channel/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# name of your application
APPLICATION = lf-test

# If no BOARD is found in the environment, use this default:
BOARD ?= native

# Comment this out to disable code in RIOT that does safety checking
# which is not needed in a production environment but helps in the
# development process:
DEVELHELP ?= 1

# Change this to 0 show compiler invocation lines by default:
QUIET ?= 1

# Enable reactor-uc features
CFLAGS += -DNETWORK_POSIX_TCP

include $(CURDIR)/../../../make/riot/riot.mk
83 changes: 83 additions & 0 deletions examples/riot/test_tcp_channel/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "reactor-uc/platform/posix/tcp_ip_channel.h"
#include "reactor-uc/reactor-uc.h"
#include <inttypes.h>
#include <sys/socket.h>

NetworkChannel *server_channel;
NetworkChannel *client_channel;

void callback_handler(FederatedConnectionBundle *self, TaggedMessage *msg) {
(void)self;
printf("Received message with connection number %i and content %s\n", msg->conn_id, (char *)msg->payload.bytes);
// channel.super.send_blocking(&channel.super, msg);
}

NetworkChannel *server_init(TcpIpChannel *tcp_channel, const char *host, unsigned short port) {
NetworkChannel *channel = &tcp_channel->super;

// creating a server that listens on loopback device on port 8903
TcpIpChannel_ctor(tcp_channel, host, port, AF_INET, true);

// binding to that address
channel->open_connection(channel);

// register receive callback for handling incoming messages
channel->register_receive_callback(channel, callback_handler, NULL);

return channel;
}

NetworkChannel *client_init(TcpIpChannel *tcp_channel, const char *host, unsigned short port) {
NetworkChannel *channel = &tcp_channel->super;

// creating a server that listens on loopback device on port 8900
TcpIpChannel_ctor(tcp_channel, host, port, AF_INET, false);

// binding to that address
channel->open_connection(channel);

return channel;
}

int main() {
const char *host = "127.0.0.1";
unsigned short port = 8903; // NOLINT

/* init server and wait for messages */
TcpIpChannel _server_tcp_channel;
server_channel = server_init(&_server_tcp_channel, host, port);

/* init client and send messages to server */
TcpIpChannel _client_tcp_channel;
client_channel = client_init(&_client_tcp_channel, host, port);

bool client_connected = false;
bool server_connected = false;
while (!client_connected && !server_connected) {
/* client tries to connect to server */
if (client_channel->try_connect(client_channel) == LF_OK) {
client_connected = true;
}

/* server tries to connect to client */
if (server_channel->try_connect(server_channel) == LF_OK) {
server_connected = true;
}
}

/* send data to server */
TaggedMessage port_message;
port_message.conn_id = 42; // NOLINT
const char *message = "Hello World1234";
memcpy(port_message.payload.bytes, message, sizeof("Hello World1234")); // NOLINT
port_message.payload.size = sizeof("Hello World1234");
for (int i = 0; i < 10; i++) {
client_channel->send_blocking(client_channel, &port_message);

// waiting for reply
// TaggedMessage *received_message = channel->receive(channel);

// printf("Received message with connection number %i and content %s\n", received_message->conn_id, (char
// *)received_message->payload.bytes);
}
}

0 comments on commit 8c68249

Please sign in to comment.