Skip to content

Commit

Permalink
Simplify NetworkChannel
Browse files Browse the repository at this point in the history
  • Loading branch information
LasseRosenow committed Dec 3, 2024
1 parent 4d3dce6 commit c6eba25
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 160 deletions.
20 changes: 3 additions & 17 deletions include/reactor-uc/network_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,12 @@ struct NetworkChannel {

/**
* @brief Opens the connection to the corresponding NetworkChannel on another federate (non-blocking).
* For client-server channels this usually is implemented as the "bind" call on the server side.
* @return LF_OK if connection is opened, LF_INVALID_VALUE if the channel is configured incorrectly,
* LF_NETWORK_SETUP_FAILED if the connection open operation fails.
* The channel is not connected unless @p get_connection_state returns with NETWORK_CHANNEL_STATE_CONNECTED.
* @return LF_OK if channel opened without error, LF_ERR if the channel is configured incorrectly or the connection
* open operation fails.
*/
lf_ret_t (*open_connection)(NetworkChannel *self);

/**
* @brief Try to connect to corresponding NetworkChannel on another federate (non-blocking).
* @return LF_OK if connection is established, LF_IN_PROGRESS if connection is in progress, LF_TRY_AGAIN if connection
* failed and should be retried, LF_ERR if connection failed and should not be retried.
*/
lf_ret_t (*try_connect)(NetworkChannel *self);

/**
* @brief Try to reconnect to corresponding NetworkChannel after the connection broke of (non-blocking).
* @return LF_OK if connection is established, LF_IN_PROGRESS if connection is in progress, LF_TRY_AGAIN if connection
* failed and should be retried, LF_ERR if connection failed and should not be retried.
*/
lf_ret_t (*try_reconnect)(NetworkChannel *self);

/**
* @brief Closes the connection to the corresponding NetworkChannel on another federate.
*/
Expand Down
1 change: 1 addition & 0 deletions include/reactor-uc/platform/riot/coap_udp_ip_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct CoapUdpIpChannel {

sock_udp_ep_t remote;

bool send_ack_received;
FederateMessage output;
uint8_t write_buffer[COAP_UDP_IP_CHANNEL_BUFFERSIZE];

Expand Down
33 changes: 15 additions & 18 deletions src/federated.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,22 @@ 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;
ret = chan->try_connect(chan);
if (ret == LF_OK) {
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->expected_try_connect_duration < wait_before_retry && chan->expected_try_connect_duration > 0) {
wait_before_retry = chan->expected_try_connect_duration;
}
all_connected = false;
break;
default:
throw("Could not connect to federate during assemble");
break;
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->expected_try_connect_duration < wait_before_retry && chan->expected_try_connect_duration > 0) {
wait_before_retry = chan->expected_try_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
6 changes: 4 additions & 2 deletions src/platform/posix/tcp_ip_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ static void _spawn_receive_thread(TcpIpChannel *self);
static lf_ret_t _reset_socket(TcpIpChannel *self);
static void *_receive_thread(void *untyped_self);

static void _update_state(TcpIpChannel *self, NetworkChannelState state) {
static void _update_state(TcpIpChannel *self, NetworkChannelState new_state) {
// Update the state of the channel itself
self->state = state;
self->state = new_state;

// Inform runtime about new state
_env->platform->new_async_event(_env->platform);
}

static NetworkChannelState _get_state(TcpIpChannel *self) { return self->state; }

static void _socket_set_blocking(int fd, bool blocking) {
// Set socket to blocking
int opts = fcntl(fd, F_GETFL);
Expand Down
Loading

0 comments on commit c6eba25

Please sign in to comment.