2 #ifndef CISCOMMHEADER_H_ 3 #define CISCOMMHEADER_H_ 6 #include <../dataio/AsciiTable.h> 12 #define CIS_MSG_HEAD "CIS_MSG_HEAD" 13 #define HEAD_VAL_SEP ":CIS:" 14 #define HEAD_KEY_SEP ",CIS," 15 #define COMMBUFFSIZ 2000 48 comm_head_t init_header(
const size_t size,
const char *address,
const char *
id) {
56 out.address[0] =
'\0';
58 strcpy(out.address, address);
63 out.response_address[0] =
'\0';
64 out.request_id[0] =
'\0';
65 out.serializer_type = -1;
66 out.format_str[0] =
'\0';
68 out.zmq_reply[0] =
'\0';
69 out.zmq_reply_worker[0] =
'\0';
86 int format_header_entry(
char *head,
const char *key,
const char *value,
87 const size_t headsiz) {
88 int ret = snprintf(head, headsiz,
"%s%s%s%s",
89 key, HEAD_VAL_SEP, value, HEAD_KEY_SEP);
90 if (ret > (
int)headsiz) {
91 cislog_error(
"format_header_entry: Formatted header is larger than bufer.\n");
106 int parse_header_entry(
const char *head,
const char *key,
char *value,
107 const size_t valsiz) {
114 char regex_text[200];
115 regex_text[0] =
'\0';
116 strcat(regex_text, HEAD_KEY_SEP);
117 strcat(regex_text, key);
118 strcat(regex_text, HEAD_VAL_SEP);
119 strcat(regex_text,
"([^(");
120 strcat(regex_text, HEAD_KEY_SEP);
121 strcat(regex_text,
")]*)");
122 strcat(regex_text, HEAD_KEY_SEP);
126 int n_sub_matches = find_matches(regex_text, head, &sind, &eind);
128 if (n_sub_matches < 2) {
129 cislog_debug(
"parse_header_entry: Could not find match to %s in %s.",
131 if (sind != NULL) free(sind);
132 if (eind != NULL) free(eind);
136 size_t value_size = eind[1] - sind[1];
137 if (value_size > valsiz) {
138 cislog_error(
"parse_header_entry: Value is larger than buffer.\n");
139 if (sind != NULL) free(sind);
140 if (eind != NULL) free(eind);
143 memcpy(value, head + sind[1], value_size);
144 value[value_size] =
'\0';
145 if (sind != NULL) free(sind);
146 if (eind != NULL) free(eind);
147 return (
int)value_size;
159 int format_comm_header(
const comm_head_t head,
char *buf,
const size_t bufsiz) {
164 strcpy(buf, CIS_MSG_HEAD);
165 pos += strlen(CIS_MSG_HEAD);
167 cislog_error(
"First header tag would exceed buffer size\n");
171 if (strlen(head.
address) > 0) {
172 ret = format_header_entry(buf + pos,
"address", head.
address, bufsiz - pos);
174 cislog_error(
"Adding address entry would exceed buffer size\n");
182 sprintf(size_str,
"%d", (
int)(head.
size));
183 ret = format_header_entry(buf + pos,
"size", size_str, bufsiz - pos);
185 cislog_error(
"Adding size entry would exceed buffer size\n");
191 if (strlen(head.
id) > 0) {
192 ret = format_header_entry(buf + pos,
"id", head.
id, bufsiz - pos);
194 cislog_error(
"Adding id entry would exceed buffer size\n");
202 ret = format_header_entry(buf + pos,
"request_id",
205 cislog_error(
"Adding request_id entry would exceed buffer size\n");
213 ret = format_header_entry(buf + pos,
"response_address",
216 cislog_error(
"Adding response_address entry would exceed buffer size\n");
226 ret = format_header_entry(buf + pos,
"stype", stype_str, bufsiz - pos);
228 cislog_error(
"Adding stype entry would exceed buffer size\n");
236 ret = format_header_entry(buf + pos,
"format_str",
239 cislog_error(
"Adding format_str entry would exceed buffer size\n");
247 char as_array_str[100];
248 sprintf(as_array_str,
"%d", head.
as_array);
249 ret = format_header_entry(buf + pos,
"as_array", as_array_str, bufsiz - pos);
251 cislog_error(
"Adding as_array entry would exceed buffer size\n");
259 ret = format_header_entry(buf + pos,
"zmq_reply",
262 cislog_error(
"Adding zmq_reply entry would exceed buffer size\n");
270 ret = format_header_entry(buf + pos,
"zmq_reply_worker",
273 cislog_error(
"Adding zmq_reply_worker entry would exceed buffer size\n");
280 pos -= strlen(HEAD_KEY_SEP);
282 pos += strlen(CIS_MSG_HEAD);
284 cislog_error(
"Closing header tag would exceed buffer size\n");
287 strcat(buf, CIS_MSG_HEAD);
308 comm_head_t parse_comm_header(
const char *buf,
const size_t bufsiz) {
314 size_t sind1, eind1, sind2, eind2;
315 char re_head_tag[COMMBUFFSIZ];
316 sprintf(re_head_tag,
"(%s)", CIS_MSG_HEAD);
317 ret = find_match(re_head_tag, buf, &sind1, &eind1);
320 ret = find_match(re_head_tag, buf + eind1, &sind2, &eind2);
322 eind = eind1 + eind2;
326 char re_head[COMMBUFFSIZ] = CIS_MSG_HEAD;
327 strcat(re_head,
"(.*)");
328 strcat(re_head, CIS_MSG_HEAD);
330 ret = find_match(re_head, buf, &sind, &eind);
333 cislog_error(
"parse_comm_header: could not find header in '%.1000s'", buf);
336 }
else if (ret == 0) {
337 cislog_debug(
"parse_comm_header: No header in '%.1000s...'", buf);
343 size_t headsiz = (eind-sind);
344 out.bodysiz = bufsiz - headsiz;
346 headsiz -= (2*strlen(CIS_MSG_HEAD));
347 char *head = (
char*)malloc(headsiz + 2*strlen(HEAD_KEY_SEP) + 1);
348 strcpy(head, HEAD_KEY_SEP);
349 memcpy(head + strlen(HEAD_KEY_SEP), buf + sind + strlen(CIS_MSG_HEAD), headsiz);
350 head[headsiz + strlen(HEAD_KEY_SEP)] =
'\0';
351 strcat(head, HEAD_KEY_SEP);
353 ret = parse_header_entry(head,
"address", out.address, COMMBUFFSIZ);
355 char size_str[COMMBUFFSIZ];
356 ret = parse_header_entry(head,
"size", size_str, COMMBUFFSIZ);
358 cislog_error(
"parse_comm_header: could not find size in header");
363 out.size = atoi(size_str);
365 ret = parse_header_entry(head,
"id", out.id, COMMBUFFSIZ);
366 ret = parse_header_entry(head,
"response_address", out.response_address, COMMBUFFSIZ);
367 ret = parse_header_entry(head,
"request_id", out.request_id, COMMBUFFSIZ);
369 char stype_str[COMMBUFFSIZ];
370 ret = parse_header_entry(head,
"stype", stype_str, COMMBUFFSIZ);
372 out.serializer_type = atoi(stype_str);
374 char array_str[COMMBUFFSIZ];
375 ret = parse_header_entry(head,
"as_array", array_str, COMMBUFFSIZ);
377 out.as_array = atoi(array_str);
379 ret = parse_header_entry(head,
"format_str", out.format_str, COMMBUFFSIZ);
380 ret = parse_header_entry(head,
"field_names", out.field_names, COMMBUFFSIZ);
381 ret = parse_header_entry(head,
"field_units", out.field_units, COMMBUFFSIZ);
383 ret = parse_header_entry(head,
"zmq_reply", out.zmq_reply, COMMBUFFSIZ);
384 ret = parse_header_entry(head,
"zmq_reply_worker", out.zmq_reply_worker, COMMBUFFSIZ);
char field_names[COMMBUFFSIZ]
String containing field names.
Definition: comm_header.h:31
char response_address[COMMBUFFSIZ]
Response address.
Definition: comm_header.h:27
char request_id[COMMBUFFSIZ]
Request id.
Definition: comm_header.h:28
char zmq_reply_worker[COMMBUFFSIZ]
Reply address for worker socket.
Definition: comm_header.h:35
int multipart
1 if message is multipart, 0 if it is not.
Definition: comm_header.h:22
size_t bodybeg
Start of body in header.
Definition: comm_header.h:24
char address[COMMBUFFSIZ]
Address that message will comm in on.
Definition: comm_header.h:21
int valid
1 if the header is valid, 0 otherwise.
Definition: comm_header.h:25
char zmq_reply[COMMBUFFSIZ]
Reply address for ZMQ sockets.
Definition: comm_header.h:34
char format_str[COMMBUFFSIZ]
Format string for serializer.
Definition: comm_header.h:30
size_t bodysiz
Size of body.
Definition: comm_header.h:23
size_t size
Size of incoming message.
Definition: comm_header.h:20
int as_array
1 if messages will be serialized arrays.
Definition: comm_header.h:33
int serializer_type
Code indicating the type of serializer.
Definition: comm_header.h:29
char field_units[COMMBUFFSIZ]
String containing field units.
Definition: comm_header.h:32
char id[COMMBUFFSIZ]
Unique ID associated with this message.
Definition: comm_header.h:26
Header information passed by comms for multipart messages.
Definition: comm_header.h:19