Skip to content

Commit

Permalink
Migrate get_connection_state to is_connected
Browse files Browse the repository at this point in the history
  • Loading branch information
LasseRosenow committed Dec 9, 2024
1 parent 15773eb commit a5bb763
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 36 deletions.
22 changes: 13 additions & 9 deletions include/reactor-uc/network_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
#include "reactor-uc/error.h"
#include "reactor-uc/federated.h"

/**
* @brief The current state of the connection.
* NETWORK_CHANNEL_STATE_UNINITIALIZED if the connection has not been initialized yet,
* NETWORK_CHANNEL_STATE_OPEN if the connection is open and waiting for try_connect to be called,
* NETWORK_CHANNEL_STATE_CONNECTION_IN_PROGRESS if try_connect has been called but it is not yet connected,
* NETWORK_CHANNEL_STATE_CONNECTION_FAILED if the connection failed,
* NETWORK_CHANNEL_STATE_CONNECTED if the channel is successfully connected to another federate,
* NETWORK_CHANNEL_STATE_LOST_CONNECTION if the connection was unexpectedly closed,
* NETWORK_CHANNEL_STATE_CLOSED if the connection was manually closed.
*/
typedef enum {
NETWORK_CHANNEL_STATE_UNINITIALIZED,
NETWORK_CHANNEL_STATE_OPEN,
Expand Down Expand Up @@ -41,19 +51,13 @@ struct NetworkChannel {

/**
* @brief Get the current state of the connection.
* @return NETWORK_CHANNEL_STATE_UNINITIALIZED if the connection has not been initialized yet,
* NETWORK_CHANNEL_STATE_OPEN if the connection is open and waiting for try_connect to be called,
* NETWORK_CHANNEL_STATE_CONNECTION_IN_PROGRESS if try_connect has been called but it is not yet connected,
* NETWORK_CHANNEL_STATE_CONNECTION_FAILED if the connection failed,
* NETWORK_CHANNEL_STATE_CONNECTED if the channel is successfully connected to another federate,
* NETWORK_CHANNEL_STATE_LOST_CONNECTION if the connection was unexpectedly closed,
* NETWORK_CHANNEL_STATE_CLOSED if the connection was manually closed.
* @return true if the channel is connected, false if the channel is not connected
*/
NetworkChannelState (*get_connection_state)(NetworkChannel *self);
bool (*is_connected)(NetworkChannel *self);

/**
* @brief Opens the connection to the corresponding NetworkChannel on another federate (non-blocking).
* The channel is not connected unless @p get_connection_state returns with NETWORK_CHANNEL_STATE_CONNECTED.
* The channel is not connected unless @p is_connected returns true.
* @return LF_OK if channel opened without error, LF_ERR if the channel is configured incorrectly or the connection
* open operation fails.
*/
Expand Down
15 changes: 2 additions & 13 deletions src/federated.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,11 @@ void FederatedConnectionBundle_connect_to_peers(FederatedConnectionBundle **bund
for (size_t i = 0; i < bundles_size; i++) {
FederatedConnectionBundle *bundle = bundles[i];
NetworkChannel *chan = bundle->net_channel;
NetworkChannelState state = chan->get_connection_state(chan);
switch (state) {
case NETWORK_CHANNEL_STATE_CONNECTED:
break;
case NETWORK_CHANNEL_STATE_OPEN:
case NETWORK_CHANNEL_STATE_CONNECTION_IN_PROGRESS:
case NETWORK_CHANNEL_STATE_CONNECTION_FAILED:
case NETWORK_CHANNEL_STATE_LOST_CONNECTION:
if (!chan->is_connected(chan)) {
if (chan->expected_connect_duration < wait_before_retry && chan->expected_connect_duration > 0) {
wait_before_retry = chan->expected_connect_duration;
}
all_connected = false;
break;
default:
throw("Could not connect to federate during assemble");
break;
}
}
if (!all_connected && wait_before_retry < FOREVER) {
Expand Down Expand Up @@ -84,7 +73,7 @@ void FederatedOutputConnection_cleanup(Trigger *trigger) {

EventPayloadPool *pool = trigger->payload_pool;

if (channel->get_connection_state(channel) == NETWORK_CHANNEL_STATE_CONNECTED) {
if (channel->is_connected(channel)) {
assert(self->staged_payload_ptr);
assert(trigger->is_registered_for_cleanup);
assert(trigger->is_present == false);
Expand Down
6 changes: 3 additions & 3 deletions src/platform/posix/tcp_ip_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,15 +528,15 @@ static void TcpIpChannel_free(NetworkChannel *untyped_self) {
self->super.close_connection((NetworkChannel *)self);
}

static NetworkChannelState TcpIpChannel_get_connection_state(NetworkChannel *untyped_self) {
static bool TcpIpChannel_is_connected(NetworkChannel *untyped_self) {
TcpIpChannel *self = (TcpIpChannel *)untyped_self;
NetworkChannelState state;

pthread_mutex_lock(&self->state_mutex);
state = _TcpIpChannel_get_state(self);
pthread_mutex_unlock(&self->state_mutex);

return state;
return state == NETWORK_CHANNEL_STATE_CONNECTED;
}

void TcpIpChannel_ctor(TcpIpChannel *self, Environment *env, const char *host, unsigned short port, int protocol_family,
Expand Down Expand Up @@ -564,7 +564,7 @@ void TcpIpChannel_ctor(TcpIpChannel *self, Environment *env, const char *host, u
self->state = NETWORK_CHANNEL_STATE_UNINITIALIZED;
self->state_mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;

self->super.get_connection_state = TcpIpChannel_get_connection_state;
self->super.is_connected = TcpIpChannel_is_connected;
self->super.open_connection = TcpIpChannel_open_connection;
self->super.close_connection = TcpIpChannel_close_connection;
self->super.send_blocking = TcpIpChannel_send_blocking;
Expand Down
6 changes: 3 additions & 3 deletions src/platform/riot/coap_udp_ip_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,9 @@ static void CoapUdpIpChannel_free(NetworkChannel *untyped_self) {
// Do nothing
}

static NetworkChannelState CoapUdpIpChannel_get_connection_state(NetworkChannel *untyped_self) {
static bool CoapUdpIpChannel_is_connected(NetworkChannel *untyped_self) {
CoapUdpIpChannel *self = (CoapUdpIpChannel *)untyped_self;
return _CoapUdpIpChannel_get_state(self);
return _CoapUdpIpChannel_get_state(self) == NETWORK_CHANNEL_STATE_CONNECTED;
}

void CoapUdpIpChannel_ctor(CoapUdpIpChannel *self, Environment *env, const char *remote_address,
Expand All @@ -356,7 +356,7 @@ void CoapUdpIpChannel_ctor(CoapUdpIpChannel *self, Environment *env, const char
// Super fields
self->super.expected_connect_duration = COAP_UDP_IP_CHANNEL_EXPECTED_CONNECT_DURATION;
self->super.type = NETWORK_CHANNEL_TYPE_COAP_UDP_IP;
self->super.get_connection_state = CoapUdpIpChannel_get_connection_state;
self->super.is_connected = CoapUdpIpChannel_is_connected;
self->super.open_connection = CoapUdpIpChannel_open_connection;
self->super.close_connection = CoapUdpIpChannel_close_connection;
self->super.send_blocking = CoapUdpIpChannel_send_blocking;
Expand Down
4 changes: 2 additions & 2 deletions test/platform/riot/coap_channel_test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void test_open_connection_non_blocking(void) {

ztimer_sleep(ZTIMER_SEC, 1);

TEST_ASSERT_EQUAL(NETWORK_CHANNEL_STATE_CONNECTED, channel->get_connection_state(channel));
TEST_ASSERT_TRUE(channel->is_connected(channel));
}

void server_callback_handler(FederatedConnectionBundle *self, const FederateMessage *_msg) {
Expand All @@ -63,7 +63,7 @@ void test_client_send_and_server_recv(void) {
TEST_ASSERT_OK(channel->open_connection(channel));

// Wait until channel is connected
while (channel->get_connection_state(channel) != NETWORK_CHANNEL_STATE_CONNECTED) {
while (!channel->is_connected(channel)) {
ztimer_sleep(ZTIMER_SEC, 1);
}

Expand Down
10 changes: 4 additions & 6 deletions test/unit/tcp_channel_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ void test_open_connection_non_blocking(void) {

sleep(1);

TEST_ASSERT_EQUAL(NETWORK_CHANNEL_STATE_CONNECTED, server_channel->get_connection_state(server_channel));
TEST_ASSERT_EQUAL(NETWORK_CHANNEL_STATE_CONNECTED, client_channel->get_connection_state(client_channel));
TEST_ASSERT_TRUE(server_channel->is_connected(server_channel));
TEST_ASSERT_TRUE(client_channel->is_connected(client_channel));
}

void server_callback_handler(FederatedConnectionBundle *self, const FederateMessage *_msg) {
Expand All @@ -76,8 +76,7 @@ void test_client_send_and_server_recv(void) {
TEST_ASSERT_OK(client_channel->open_connection(client_channel));

// wait for connection to be established
while (server_channel->get_connection_state(server_channel) != NETWORK_CHANNEL_STATE_CONNECTED &&
client_channel->get_connection_state(client_channel) != NETWORK_CHANNEL_STATE_CONNECTED) {
while (!server_channel->is_connected(server_channel) || !client_channel->is_connected(client_channel)) {
sleep(1);
}

Expand Down Expand Up @@ -122,8 +121,7 @@ void test_server_send_and_client_recv(void) {
TEST_ASSERT_OK(client_channel->open_connection(client_channel));

// wait for connection to be established
while (server_channel->get_connection_state(server_channel) != NETWORK_CHANNEL_STATE_CONNECTED &&
client_channel->get_connection_state(client_channel) != NETWORK_CHANNEL_STATE_CONNECTED) {
while (!server_channel->is_connected(server_channel) || !client_channel->is_connected(client_channel)) {
sleep(1);
}

Expand Down

0 comments on commit a5bb763

Please sign in to comment.