cis_config: /root/cis_interface/cis_interface/cis_interface/communication/CommBase.h Source File

cis_config

CommBase.h
1
2 #ifndef CISCOMMBASE_H_
3 #define CISCOMMBASE_H_
4
5 #include <../tools.h>
6
7 #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
8 extern "C" {
9 #endif
10
12 enum comm_enum { NULL_COMM, IPC_COMM, ZMQ_COMM,
13 RPC_COMM, SERVER_COMM, CLIENT_COMM,
14 ASCII_FILE_COMM, ASCII_TABLE_COMM, ASCII_TABLE_ARRAY_COMM };
15 typedef enum comm_enum comm_type;
16 #define COMM_NAME_SIZE 100
17 #define COMM_ADDRESS_SIZE 500
18 #define COMM_DIR_SIZE 100
19
23 typedef struct comm_t {
24 comm_type type;
25 char name[COMM_NAME_SIZE];
26 char address[COMM_ADDRESS_SIZE];
27 char direction[COMM_DIR_SIZE];
28 int valid;
29 void *handle;
30 void *info;
31 seri_t *serializer;
32 size_t maxMsgSize;
33 int always_send_header;
34 int index_in_register;
35 time_t *last_send;
36 int *sent_eof;
37 int *recv_eof;
38 int *used;
39 void *reply;
40 int is_file;
41 int is_work_comm;
42 } comm_t;
43
44
49 static inline
50 comm_t empty_comm_base() {
51 comm_t ret;
52 ret.type = NULL_COMM;
53 ret.name[0] = '\0';
54 ret.address[0] = '\0';
55 ret.direction[0] = '\0';
56 ret.valid = 0;
57 ret.handle = NULL;
58 ret.info = NULL;
59 ret.serializer = NULL;
60 ret.maxMsgSize = 0;
61 ret.always_send_header = 0;
62 ret.index_in_register = -1;
63 ret.last_send = NULL;
64 ret.sent_eof = NULL;
65 ret.recv_eof = NULL;
66 ret.used = NULL;
67 ret.reply = NULL;
68 ret.is_file = 0;
69 ret.is_work_comm = 0;
70 return ret;
71 };
72
82 static inline
83 comm_t* new_comm_base(char *address, const char *direction, const comm_type t,
84 const void *seri_info) {
85 comm_t* ret = (comm_t*)malloc(sizeof(comm_t));
86 if (ret == NULL) {
87 cislog_error("new_comm_base: Failed to malloc comm.");
88 return ret;
89 }
90 ret[0] = empty_comm_base();
91 ret->type = t;
92 ret->valid = 1;
93 if (address != NULL)
94 strcpy(ret->address, address);
95 if (direction == NULL) {
96 ret->valid = 0;
97 } else {
98 strcpy(ret->direction, direction);
99 }
100 ret->serializer = init_serializer(-1, seri_info);
101 ret->maxMsgSize = CIS_MSG_MAX;
102 ret->last_send = (time_t*)malloc(sizeof(time_t));
103 ret->last_send[0] = 0;
104 ret->sent_eof = (int*)malloc(sizeof(int));
105 ret->recv_eof = (int*)malloc(sizeof(int));
106 ret->used = (int*)malloc(sizeof(int));
107 ret->sent_eof[0] = 0;
108 ret->recv_eof[0] = 0;
109 ret->used[0] = 0;
110 return ret;
111 };
112
125 static inline
126 comm_t* init_comm_base(const char *name, const char *direction,
127 const comm_type t, const void *seri_info) {
128 char full_name[COMM_NAME_SIZE];
129 char *address = NULL;
130 if (name != NULL) {
131 strcpy(full_name, name);
132 if (t != RPC_COMM) {
133 if ((direction != NULL) && (strlen(direction) > 0)) {
134 if (is_send(direction))
135 strcat(full_name, "_OUT");
136 else if (is_recv(direction))
137 strcat(full_name, "_IN");
138 }
139 }
140 address = getenv(full_name);
141 }
142 comm_t *ret = new_comm_base(address, direction, t, seri_info);
143 if (ret == NULL) {
144 cislog_error("init_comm_base: Error in new_comm_base");
145 return ret;
146 }
147 if (name == NULL) {
148 ret->valid = 0;
149 } else
150 strcpy(ret->name, full_name);
151 if ((strlen(ret->address) == 0) && (t != SERVER_COMM) && (t != CLIENT_COMM)) {
152 cislog_error("init_comm_base: %s not registered as environment variable.\n",
153 full_name);
154 ret->valid = 0;
155 }
156 cislog_debug("init_comm_base(%s): Done", ret->name);
157 return ret;
158 };
159
165 static inline
166 int free_comm_base(comm_t *x) {
167 if (x == NULL)
168 return 0;
169 if (x->last_send != NULL) {
170 free(x->last_send);
171 x->last_send = NULL;
172 }
173 if (x->sent_eof != NULL) {
174 free(x->sent_eof);
175 x->sent_eof = NULL;
176 }
177 if (x->recv_eof != NULL) {
178 free(x->recv_eof);
179 x->recv_eof = NULL;
180 }
181 if (x->used != NULL) {
182 free(x->used);
183 x->used = NULL;
184 }
185 if (x->serializer != NULL) {
186 free_serializer(x->serializer);
187 free(x->serializer);
188 x->serializer = NULL;
189 }
190 x->valid = 0;
191 return 0;
192 };
193
203 static inline
204 int comm_base_send(const comm_t x, const char *data, const size_t len) {
205 // Prevent C4100 warning on windows by referencing param
206 #ifdef _WIN32
207 x;
208 data;
209 len;
210 #endif
211 // Make sure you arn't sending a message that is too big
212 if (len > CIS_MSG_MAX) {
213 cislog_error("comm_base_send(%s): message too large for single packet (CIS_MSG_MAX=%d, len=%d)",
214 x.name, CIS_MSG_MAX, len);
215 return -1;
216 }
217 return 0;
218 };
219
220
221 #ifdef __cplusplus /* If this is a C++ compiler, end C linkage */
222 }
223 #endif
224
225 #endif /*CISCOMMBASE_H_*/
comm_t::handle
void * handle
Pointer to handle for comm.
Definition: CommBase.h:29
comm_t::info
void * info
Pointer to any extra info comm requires.
Definition: CommBase.h:30
comm_t
Communication structure.
Definition: CommBase.h:23
comm_t::address
char address[COMM_ADDRESS_SIZE]
Comm address.
Definition: CommBase.h:26
comm_t::is_work_comm
int is_work_comm
Flag specifying if comm is a temporary work comm.
Definition: CommBase.h:41
seri_t
Serializer structure.
Definition: SerializeBase.h:19
comm_t::last_send
time_t * last_send
Clock output at time of last send.
Definition: CommBase.h:35
comm_t::maxMsgSize
size_t maxMsgSize
The maximum message size.
Definition: CommBase.h:32
comm_t::used
int * used
Flag specifying if the comm has been used.
Definition: CommBase.h:38
comm_t::type
comm_type type
Comm type.
Definition: CommBase.h:24
comm_t::is_file
int is_file
Flag specifying if the comm connects directly to a file.
Definition: CommBase.h:40
comm_t::always_send_header
int always_send_header
1 if comm should always send a header.
Definition: CommBase.h:33
comm_t::serializer
seri_t * serializer
Serializer for comm messages.
Definition: CommBase.h:31
comm_t::direction
char direction[COMM_DIR_SIZE]
send or recv for direction messages will go.
Definition: CommBase.h:27
comm_t::name
char name[COMM_NAME_SIZE]
Comm name.
Definition: CommBase.h:25
comm_t::sent_eof
int * sent_eof
Flag specifying if EOF has been sent.
Definition: CommBase.h:36
comm_t::valid
int valid
1 if communicator initialized, 0 otherwise.
Definition: CommBase.h:28
comm_t::recv_eof
int * recv_eof
Flag specifying if EOF has been received.
Definition: CommBase.h:37
comm_t::index_in_register
int index_in_register
Index of the comm in the comm register.
Definition: CommBase.h:34
comm_t::reply
void * reply
Reply information.
Definition: CommBase.h:39