My Project
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros
baremetal_socket_msu.c
Go to the documentation of this file.
3 #include "local_msu.h"
4 #include "epollops.h"
5 #include "logging.h"
6 #include "msu_message.h"
7 #include "runtime_dfg.h"
8 #include "communication.h"
9 #include "msu_calls.h"
10 #include "unused_def.h"
11 
12 #include <stdlib.h>
13 
14 #define MAX_RECV_BUF_LEN 64
15 
16 static struct local_msu *static_self;
17 static int static_n_hops;
18 static int static_sock_fd;
19 static int static_epoll_fd;
20 
21 static int read_and_forward(int fd, void UNUSED *v_state) {
22  char buf[MAX_RECV_BUF_LEN];
23  int rtn = read(fd, buf, MAX_RECV_BUF_LEN);
24  if (rtn < 0) {
25  log_error("Error reading from fd %d!", fd);
26  return -1;
27  }
28  int id = atoi(buf);
29 
30  struct msu_msg_key key;
31 
32  rtn = set_msg_key(id, &key);
33  if (rtn < 0) {
34  log_error("Error setting message key to %d", id);
35  return -1;
36  }
37 
38  struct baremetal_msg *msg = malloc(sizeof(*msg));
39  msg->fd = fd;
40  msg->n_hops = static_n_hops;
41 
42  rtn = init_call_msu_type(static_self, &BAREMETAL_MSU_TYPE, &key, sizeof(*msg), msg);
43  if (rtn < 0) {
44  log_error("Error enqueing to baremetal MSU");
45  return -1;
46  }
47  return 0;
48 }
49 
50 #define BAREMETAL_EPOLL_TIMEOUT 1000
51 #define BAREMETAL_EPOLL_BATCH 1000
52 
53 static int socket_handler_main_loop(struct local_msu *self) {
57  NULL,
58  NULL);
59  return rtn;
60 }
61 
62 static struct msu_msg_key self_key = {};
63 
64 static int bm_sock_receive(struct local_msu *self, struct msu_msg *data) {
65  int rtn = socket_handler_main_loop(self);
66  if (rtn < 0) {
67  log_error("Error exiting socket handler main loop");
68  return -1;
69  }
70  init_call_local_msu(self, self, &self_key, 0, NULL);
71  return 0;
72 }
73 
74 #define INIT_SYNTAX "<PORT> <N_HOPS>"
75 
76 static int bm_sock_init(struct local_msu *self, struct msu_init_data *init_data) {
77 
78  if (static_self != NULL) {
79  log_error("Baremental Socket MSU already instantiated. There can be only one!");
80  return -1;
81  }
82  char *init = init_data->init_data;
83 
84  char *str, *saveptr;
85  if ((str = strtok_r(init, " ", &saveptr)) == NULL) {
86  log_error("Baremetal socket must be initialized with syntax: " INIT_SYNTAX);
87  return -1;
88  }
89  int port = atoi(str);
90 
91  if ((str = strtok_r(NULL, " ", &saveptr)) == NULL) {
92  log_error("Baremetal socket must be initialized with syntax: " INIT_SYNTAX);
93  return -1;
94  }
95  static_n_hops = atoi(str);
96 
98  if (static_sock_fd == -1) {
99  log_error("Couldn't initialize socket for baremetal sock MSU %d", self->id);
100  return -1;
101  }
102 
104  if (static_epoll_fd == -1) {
105  log_error("Couldn't initialize epoll_Fd for baremetal sock MSU %d", self->id);
106  return -1;
107  }
108 
109  static_self = self;
110 
111  init_call_local_msu(self, self, &self_key, 0, NULL);
112 
113  return 0;
114 }
115 
117  .name = "baremetal_sock_msu",
119  .init = bm_sock_init,
120  .receive = bm_sock_receive
121 };
122 
#define BAREMETAL_SOCK_MSU_TYPE_ID
static int bm_sock_init(struct local_msu *self, struct msu_init_data *init_data)
Interface for general-purpose socket communication.
static int static_sock_fd
static int socket_handler_main_loop(struct local_msu *self)
Macro for declaring functions or variables as unused to avoid compiler warnings.
Wrapper functions for epoll to manage event-based communication.
#define INIT_SYNTAX
#define BAREMETAL_EPOLL_BATCH
struct msu_type BAREMETAL_SOCK_MSU_TYPE
static int bm_sock_receive(struct local_msu *self, struct msu_msg *data)
struct msu_type BAREMETAL_MSU_TYPE
Definition: baremetal_msu.c:39
static int static_n_hops
int init_call_msu_type(struct local_msu *sender, struct msu_type *dst_type, struct msu_msg_key *key, size_t data_size, void *data)
Sends an MSU message to a destination of the specified type.
Definition: msu_calls.c:205
#define MAX_RECV_BUF_LEN
int set_msg_key(int32_t id, struct msu_msg_key *key)
Sets the key's ID and composite-ID to be equal to the provided id.
Definition: msu_message.c:54
Logging of status messages to the terminal.
static int read_and_forward(int fd, void UNUSED *v_state)
Interactions with global dfg from individual runtime.
#define BAREMETAL_EPOLL_TIMEOUT
Declares the methods available for calling an MSU from another MSU.
int init_epoll(int socket_fd)
Initializes a new instance of an epoll file descriptor and adds a socket to it, listening for input o...
Definition: epollops.c:182
#define log_error(fmt,...)
Definition: logging.h:101
Declares the structures and functions applicable to MSUs on the local machine.
The structure that represents an MSU located on the local machine.
Definition: local_msu.h:38
Data with which an MSU is initialized, and the payload for messages of type CTRL_CREATE_MSU.
Definition: dfg.h:66
int init_call_local_msu(struct local_msu *sender, struct local_msu *dest, struct msu_msg_key *key, size_t data_size, void *data)
Enqueues a new message in the queue of a local MSU.
Definition: msu_calls.c:134
static int static_epoll_fd
int epoll_loop(int socket_fd, int epoll_fd, int batch_size, int timeout, bool oneshot, int(*connection_handler)(int, void *), int(*accept_handler)(int, void *), void *data)
The event-based loop for epoll_wait.
Definition: epollops.c:132
#define UNUSED
Defines a type of MSU.
Definition: msu_type.h:46
static struct local_msu * static_self
int init_listening_socket(int port)
Initializes a socket which is bound to and listening on the given port.
char init_data[64]
Definition: dfg.h:67
A message that is to be delivered to an instance of an MSU.
Definition: msu_message.h:101
Messages passed to MSUs.
static struct msu_msg_key self_key
char * name
Name for the msu type.
Definition: msu_type.h:48
Used to uniquely identify the source of a message, used in state storage as well as routing...
Definition: msu_message.h:46