cis_config
CommBase.h
1 #include <../tools.h>
2 
4 #ifndef CISCOMMBASE_H_
5 #define CISCOMMBASE_H_
6 
8 enum comm_enum { NULL_COMM, IPC_COMM, ZMQ_COMM,
9  RPC_COMM, SERVER_COMM, CLIENT_COMM,
10  ASCII_FILE_COMM, ASCII_TABLE_COMM, ASCII_TABLE_ARRAY_COMM };
11 typedef enum comm_enum comm_type;
12 #define COMM_NAME_SIZE 100
13 #define COMM_ADDRESS_SIZE 500
14 #define COMM_DIR_SIZE 100
15 
19 typedef struct comm_t {
20  comm_type type;
21  char name[COMM_NAME_SIZE];
22  char address[COMM_ADDRESS_SIZE];
23  char direction[COMM_DIR_SIZE];
24  int valid;
25  void *handle;
26  void *info;
28  size_t maxMsgSize;
31  time_t *last_send;
32  int *sent_eof;
33  int *recv_eof;
34  int *used;
35  void *reply;
36  int is_file;
37 } comm_t;
38 
39 
44 static inline
45 comm_t empty_comm_base() {
46  comm_t ret;
47  ret.type = NULL_COMM;
48  ret.name[0] = '\0';
49  ret.address[0] = '\0';
50  ret.direction[0] = '\0';
51  ret.valid = 0;
52  ret.handle = NULL;
53  ret.info = NULL;
54  ret.serializer = NULL;
55  ret.maxMsgSize = 0;
56  ret.always_send_header = 0;
57  ret.index_in_register = -1;
58  ret.last_send = NULL;
59  ret.sent_eof = NULL;
60  ret.recv_eof = NULL;
61  ret.used = NULL;
62  ret.reply = NULL;
63  ret.is_file = 0;
64  return ret;
65 };
66 
76 static inline
77 comm_t* new_comm_base(char *address, const char *direction, const comm_type t,
78  const void *seri_info) {
79  comm_t* ret = (comm_t*)malloc(sizeof(comm_t));
80  if (ret == NULL) {
81  cislog_error("new_comm_base: Failed to malloc comm.");
82  return ret;
83  }
84  ret[0] = empty_comm_base();
85  ret->type = t;
86  ret->valid = 1;
87  if (address != NULL)
88  strcpy(ret->address, address);
89  if (direction == NULL) {
90  ret->valid = 0;
91  } else {
92  strcpy(ret->direction, direction);
93  }
94  ret->serializer = init_serializer(-1, seri_info);
95  ret->maxMsgSize = CIS_MSG_MAX;
96  ret->last_send = (time_t*)malloc(sizeof(time_t));
97  ret->last_send[0] = 0;
98  ret->sent_eof = (int*)malloc(sizeof(int));
99  ret->recv_eof = (int*)malloc(sizeof(int));
100  ret->used = (int*)malloc(sizeof(int));
101  ret->sent_eof[0] = 0;
102  ret->recv_eof[0] = 0;
103  ret->used[0] = 0;
104  return ret;
105 };
106 
119 static inline
120 comm_t* init_comm_base(const char *name, const char *direction,
121  const comm_type t, const void *seri_info) {
122  char full_name[COMM_NAME_SIZE];
123  char *address = NULL;
124  if (name != NULL) {
125  strcpy(full_name, name);
126  if (t != RPC_COMM) {
127  if ((direction != NULL) && (strlen(direction) > 0)) {
128  if (is_send(direction))
129  strcat(full_name, "_OUT");
130  else if (is_recv(direction))
131  strcat(full_name, "_IN");
132  }
133  }
134  address = getenv(full_name);
135  }
136  comm_t *ret = new_comm_base(address, direction, t, seri_info);
137  if (ret == NULL) {
138  cislog_error("init_comm_base: Error in new_comm_base");
139  return ret;
140  }
141  if (name == NULL) {
142  ret->valid = 0;
143  } else
144  strcpy(ret->name, full_name);
145  if ((strlen(ret->address) == 0) && (t != SERVER_COMM) && (t != CLIENT_COMM)) {
146  cislog_error("init_comm_base: %s not registered as environment variable.\n",
147  full_name);
148  ret->valid = 0;
149  }
150  cislog_debug("init_comm_base(%s): Done", ret->name);
151  return ret;
152 };
153 
159 static inline
160 int free_comm_base(comm_t *x) {
161  if (x == NULL)
162  return 0;
163  if (x->last_send != NULL) {
164  free(x->last_send);
165  x->last_send = NULL;
166  }
167  if (x->sent_eof != NULL) {
168  free(x->sent_eof);
169  x->sent_eof = NULL;
170  }
171  if (x->recv_eof != NULL) {
172  free(x->recv_eof);
173  x->recv_eof = NULL;
174  }
175  if (x->used != NULL) {
176  free(x->used);
177  x->used = NULL;
178  }
179  if (x->serializer != NULL) {
180  free_serializer(x->serializer);
181  free(x->serializer);
182  x->serializer = NULL;
183  }
184  return 0;
185 };
186 
196 static inline
197 int comm_base_send(const comm_t x, const char *data, const size_t len) {
198  // Prevent C4100 warning on windows by referencing param
199 #ifdef _WIN32
200  x;
201  data;
202  len;
203 #endif
204  // Make sure you arn't sending a message that is too big
205  if (len > CIS_MSG_MAX) {
206  cislog_error("comm_base_send(%s): message too large for single packet (CIS_MSG_MAX=%d, len=%d)",
207  x.name, CIS_MSG_MAX, len);
208  return -1;
209  }
210  return 0;
211 };
212 
213 
214 #endif /*CISCOMMBASE_H_*/
void * handle
Pointer to handle for comm.
Definition: CommBase.h:25
void * info
Pointer to any extra info comm requires.
Definition: CommBase.h:26
Communication structure.
Definition: CommBase.h:19
char address[COMM_ADDRESS_SIZE]
Comm address.
Definition: CommBase.h:22
Serializer structure.
Definition: SerializeBase.h:15
time_t * last_send
Clock output at time of last send.
Definition: CommBase.h:31
size_t maxMsgSize
The maximum message size.
Definition: CommBase.h:28
int * used
Flag specifying if the comm has been used.
Definition: CommBase.h:34
comm_type type
Comm type.
Definition: CommBase.h:20
int is_file
Flag specifying if the comm connects directly to a file.
Definition: CommBase.h:36
int always_send_header
1 if comm should always send a header.
Definition: CommBase.h:29
seri_t * serializer
Serializer for comm messages.
Definition: CommBase.h:27
char direction[COMM_DIR_SIZE]
send or recv for direction messages will go.
Definition: CommBase.h:23
char name[COMM_NAME_SIZE]
Comm name.
Definition: CommBase.h:21
int * sent_eof
Flag specifying if EOF has been sent.
Definition: CommBase.h:32
int valid
1 if communicator initialized, 0 otherwise.
Definition: CommBase.h:24
int * recv_eof
Flag specifying if EOF has been received.
Definition: CommBase.h:33
int index_in_register
Index of the comm in the comm register.
Definition: CommBase.h:30
void * reply
Reply information.
Definition: CommBase.h:35