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

cis_config

ServerComm.h
1
2 #ifndef CISSERVERCOMM_H_
3 #define CISSERVERCOMM_H_
4
5 #include <CommBase.h>
6 #include <DefaultComm.h>
7 #include <comm_header.h>
8
9 #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
10 extern "C" {
11 #endif
12
13 // Handle is recv address
14 // Info is response
15
21 static inline
22 int new_server_address(comm_t *comm) {
23 comm->type = _default_comm;
24 return new_default_address(comm);
25 };
26
32 static inline
33 int init_server_comm(comm_t *comm) {
34 int ret = 0;
35 // Called to create temp comm for send/recv
36 if ((strlen(comm->name) == 0) && (strlen(comm->address) > 0)) {
37 comm->type = _default_comm;
38 return init_default_comm(comm);
39 }
40 // Called to initialize/create server comm
41 char *seri_in = (char*)malloc(strlen(comm->direction) + 1);
42 if (seri_in == NULL) {
43 cislog_error("init_server_comm: Failed to malloc seri_in.");
44 return -1;
45 }
46 strcpy(seri_in, comm->direction);
47 // printf("init_server_comm(%s): seri: %s\n", comm->name, seri_in);
48 comm_t *handle;
49 if (strlen(comm->name) == 0) {
50 handle = new_comm_base(comm->address, "recv", _default_comm, (void*)seri_in);
51 sprintf(handle->name, "server_request.%s", comm->address);
52 } else {
53 handle = init_comm_base(comm->name, "recv", _default_comm, (void*)seri_in);
54 }
55 ret = init_default_comm(handle);
56 strcpy(comm->address, handle->address);
57 // printf("init_server_comm: name = %s, type=%d, address = %s\n",
58 // handle->name, handle->type, handle->address);
59 strcpy(comm->direction, "recv");
60 comm->handle = (void*)handle;
61 if (_default_comm == ZMQ_COMM) {
62 comm->always_send_header = 1;
63 } else {
64 comm->always_send_header = 0;
65 }
66 comm_t **info = (comm_t**)malloc(sizeof(comm_t*));
67 if (info == NULL) {
68 cislog_error("init_server_comm: Failed to malloc info.");
69 return -1;
70 }
71 info[0] = NULL;
72 comm->info = (void*)info;
73 return ret;
74 };
75
81 static inline
82 int free_server_comm(comm_t *x) {
83 if (x->handle != NULL) {
84 comm_t *handle = (comm_t*)(x->handle);
85 free_default_comm(handle);
86 free_comm_base(handle);
87 free(x->handle);
88 x->handle = NULL;
89 }
90 if (x->info != NULL) {
91 comm_t **info = (comm_t**)(x->info);
92 if (*info != NULL) {
93 free_default_comm(*info);
94 free_comm_base(*info);
95 free(*info);
96 }
97 free(info);
98 x->info = NULL;
99 }
100 return 0;
101 };
102
108 static inline
109 int server_comm_nmsg(const comm_t x) {
110 comm_t *handle = (comm_t*)(x.handle);
111 int ret = default_comm_nmsg(*handle);
112 return ret;
113 };
114
122 static inline
123 int server_comm_send(const comm_t x, const char *data, const size_t len) {
124 cislog_debug("server_comm_send(%s): %d bytes", x.name, len);
125 if (x.info == NULL) {
126 cislog_error("server_comm_send(%s): no response comm registered", x.name);
127 return -1;
128 }
129 comm_t **res_comm = (comm_t**)(x.info);
130 if (res_comm[0] == NULL) {
131 cislog_error("server_comm_send(%s): no response comm registered", x.name);
132 return -1;
133 }
134 int ret = default_comm_send((*res_comm)[0], data, len);
135 // Wait for msg to be received?
136 free_default_comm(res_comm[0]);
137 free_comm_base(res_comm[0]);
138 free(res_comm[0]);
139 res_comm[0] = NULL;
140 return ret;
141 };
142
154 static inline
155 int server_comm_recv(comm_t x, char **data, const size_t len, const int allow_realloc) {
156 cislog_debug("server_comm_recv(%s)", x.name);
157 if (x.handle == NULL) {
158 cislog_error("server_comm_recv(%s): no request comm registered", x.name);
159 return -1;
160 }
161 comm_t *req_comm = (comm_t*)(x.handle);
162 int ret = default_comm_recv(*req_comm, data, len, allow_realloc);
163 if (ret < 0) {
164 return ret;
165 }
166 // Return EOF
167 if (is_eof(*data)) {
168 req_comm->recv_eof[0] = 1;
169 return ret;
170 }
171 // Initialize new comm from received address
172 comm_head_t head = parse_comm_header(*data, ret);
173 if (!(head.valid)) {
174 cislog_error("server_comm_recv(%s): Error parsing header.", x.name);
175 return -1;
176 }
177 // Return EOF
178 if (is_eof((*data) + head.bodybeg)) {
179 req_comm->recv_eof[0] = 1;
180 return ret;
181 }
182 // If there is not a response address
183 if (strlen(head.response_address) == 0) {
184 cislog_error("server_comm_recv(%s): No response address in message.", x.name);
185 return -1;
186 }
187 strcpy(x.address, head.id);
188 char *seri_copy = (char*)malloc(strlen((char*)(x.serializer->info)) + 1);
189 if (seri_copy == NULL) {
190 cislog_error("server_comm_recv(%s): Failed to malloc seri_copy.");
191 return -1;
192 }
193 strcpy(seri_copy, (char*)(x.serializer->info));
194 comm_t **res_comm = (comm_t**)(x.info);
195 res_comm[0] = new_comm_base(head.response_address, "send", _default_comm,
196 seri_copy);
197 /* sprintf(res_comm[0]->name, "server_response.%s", res_comm[0]->address); */
198 int newret;
199 newret = init_default_comm(res_comm[0]);
200 if (newret < 0) {
201 cislog_error("server_comm_recv(%s): Could not initialize response comm.", x.name);
202 return newret;
203 }
204 res_comm[0]->sent_eof[0] = 1;
205 res_comm[0]->recv_eof[0] = 1;
206 return ret;
207 };
208
209 #ifdef __cplusplus /* If this is a C++ compiler, end C linkage */
210 }
211 #endif
212
213 #endif /*CISSERVERCOMM_H_*/
comm_t::handle
void * handle
Pointer to handle for comm.
Definition: CommBase.h:29
comm_head_t::response_address
char response_address[COMMBUFFSIZ]
Response address.
Definition: comm_header.h:27
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
seri_t::info
void * info
Pointer to any extra info serializer requires.
Definition: SerializeBase.h:21
comm_head_t::bodybeg
size_t bodybeg
Start of body in header.
Definition: comm_header.h:24
comm_t::type
comm_type type
Comm type.
Definition: CommBase.h:24
comm_head_t::valid
int valid
1 if the header is valid, 0 otherwise.
Definition: comm_header.h:25
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::recv_eof
int * recv_eof
Flag specifying if EOF has been received.
Definition: CommBase.h:37
comm_head_t::id
char id[COMMBUFFSIZ]
Unique ID associated with this message.
Definition: comm_header.h:26
comm_head_t
Header information passed by comms for multipart messages.
Definition: comm_header.h:19