From 70e87644ed4150c5667b0967715eb26931b10d75 Mon Sep 17 00:00:00 2001 From: Lasse Rosenow Date: Thu, 24 Oct 2024 15:30:11 +0000 Subject: [PATCH] Add WIP test_tcp_channel inside riot examples --- examples/riot/test_tcp_channel/Makefile | 18 ++++++ examples/riot/test_tcp_channel/main.c | 83 +++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100755 examples/riot/test_tcp_channel/Makefile create mode 100755 examples/riot/test_tcp_channel/main.c diff --git a/examples/riot/test_tcp_channel/Makefile b/examples/riot/test_tcp_channel/Makefile new file mode 100755 index 00000000..61759277 --- /dev/null +++ b/examples/riot/test_tcp_channel/Makefile @@ -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 diff --git a/examples/riot/test_tcp_channel/main.c b/examples/riot/test_tcp_channel/main.c new file mode 100755 index 00000000..158f6987 --- /dev/null +++ b/examples/riot/test_tcp_channel/main.c @@ -0,0 +1,83 @@ +#include "reactor-uc/platform/posix/tcp_ip_channel.h" +#include "reactor-uc/reactor-uc.h" +#include +#include + +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); + } +}