2 #include <../dataio/AsciiTable.h> 5 #ifndef CISCOMMHEADER_H_ 6 #define CISCOMMHEADER_H_ 8 #define CIS_MSG_HEAD "CIS_MSG_HEAD" 9 #define HEAD_VAL_SEP ":CIS:" 10 #define HEAD_KEY_SEP ",CIS," 11 #define COMMBUFFSIZ 2000 42 comm_head_t init_header(
const size_t size,
const char *address,
const char *
id) {
50 out.address[0] =
'\0';
52 strcpy(out.address, address);
57 out.response_address[0] =
'\0';
58 out.request_id[0] =
'\0';
59 out.serializer_type = -1;
60 out.format_str[0] =
'\0';
78 int format_header_entry(
char *head,
const char *key,
const char *value,
79 const size_t headsiz) {
80 int ret = snprintf(head, headsiz,
"%s%s%s%s",
81 key, HEAD_VAL_SEP, value, HEAD_KEY_SEP);
82 if (ret > (
int)headsiz) {
83 cislog_error(
"format_header_entry: Formatted header is larger than bufer.\n");
98 int parse_header_entry(
const char *head,
const char *key,
char *value,
99 const size_t valsiz) {
106 char regex_text[200];
107 regex_text[0] =
'\0';
108 strcat(regex_text, HEAD_KEY_SEP);
109 strcat(regex_text, key);
110 strcat(regex_text, HEAD_VAL_SEP);
111 strcat(regex_text,
"([^(");
112 strcat(regex_text, HEAD_KEY_SEP);
113 strcat(regex_text,
")]*)");
114 strcat(regex_text, HEAD_KEY_SEP);
118 int n_sub_matches = find_matches(regex_text, head, &sind, &eind);
120 if (n_sub_matches < 2) {
121 cislog_debug(
"parse_header_entry: Could not find match to %s in %s.",
123 if (sind != NULL) free(sind);
124 if (eind != NULL) free(eind);
128 size_t value_size = eind[1] - sind[1];
129 if (value_size > valsiz) {
130 cislog_error(
"parse_header_entry: Value is larger than buffer.\n");
131 if (sind != NULL) free(sind);
132 if (eind != NULL) free(eind);
135 memcpy(value, head + sind[1], value_size);
136 value[value_size] =
'\0';
137 if (sind != NULL) free(sind);
138 if (eind != NULL) free(eind);
139 return (
int)value_size;
151 int format_comm_header(
const comm_head_t head,
char *buf,
const size_t bufsiz) {
156 strcpy(buf, CIS_MSG_HEAD);
157 pos += strlen(CIS_MSG_HEAD);
159 cislog_error(
"First header tag would exceed buffer size\n");
163 if (strlen(head.
address) > 0) {
164 ret = format_header_entry(buf + pos,
"address", head.
address, bufsiz - pos);
166 cislog_error(
"Adding address entry would exceed buffer size\n");
174 sprintf(size_str,
"%d", (
int)(head.
size));
175 ret = format_header_entry(buf + pos,
"size", size_str, bufsiz - pos);
177 cislog_error(
"Adding size entry would exceed buffer size\n");
183 if (strlen(head.
id) > 0) {
184 ret = format_header_entry(buf + pos,
"id", head.
id, bufsiz - pos);
186 cislog_error(
"Adding id entry would exceed buffer size\n");
194 ret = format_header_entry(buf + pos,
"request_id",
197 cislog_error(
"Adding request_id entry would exceed buffer size\n");
205 ret = format_header_entry(buf + pos,
"response_address",
208 cislog_error(
"Adding response_address entry would exceed buffer size\n");
218 ret = format_header_entry(buf + pos,
"stype", stype_str, bufsiz - pos);
220 cislog_error(
"Adding stype entry would exceed buffer size\n");
228 ret = format_header_entry(buf + pos,
"format_str",
231 cislog_error(
"Adding format_str entry would exceed buffer size\n");
239 char as_array_str[100];
240 sprintf(as_array_str,
"%d", head.
as_array);
241 ret = format_header_entry(buf + pos,
"as_array", as_array_str, bufsiz - pos);
243 cislog_error(
"Adding as_array entry would exceed buffer size\n");
250 pos -= strlen(HEAD_KEY_SEP);
252 pos += strlen(CIS_MSG_HEAD);
254 cislog_error(
"Closing header tag would exceed buffer size\n");
257 strcat(buf, CIS_MSG_HEAD);
278 comm_head_t parse_comm_header(
const char *buf,
const size_t bufsiz) {
284 size_t sind1, eind1, sind2, eind2;
285 char re_head_tag[COMMBUFFSIZ];
286 sprintf(re_head_tag,
"(%s)", CIS_MSG_HEAD);
287 ret = find_match(re_head_tag, buf, &sind1, &eind1);
290 ret = find_match(re_head_tag, buf + eind1, &sind2, &eind2);
292 eind = eind1 + eind2;
296 char re_head[COMMBUFFSIZ] = CIS_MSG_HEAD;
297 strcat(re_head,
"(.*)");
298 strcat(re_head, CIS_MSG_HEAD);
300 ret = find_match(re_head, buf, &sind, &eind);
303 cislog_error(
"parse_comm_header: could not find header in '%.1000s'", buf);
306 }
else if (ret == 0) {
307 cislog_debug(
"parse_comm_header: No header in '%.1000s...'", buf);
313 size_t headsiz = (eind-sind);
314 out.bodysiz = bufsiz - headsiz;
316 headsiz -= (2*strlen(CIS_MSG_HEAD));
317 char *head = (
char*)malloc(headsiz + 2*strlen(HEAD_KEY_SEP) + 1);
318 strcpy(head, HEAD_KEY_SEP);
319 memcpy(head + strlen(HEAD_KEY_SEP), buf + sind + strlen(CIS_MSG_HEAD), headsiz);
320 head[headsiz + strlen(HEAD_KEY_SEP)] =
'\0';
321 strcat(head, HEAD_KEY_SEP);
323 ret = parse_header_entry(head,
"address", out.address, COMMBUFFSIZ);
325 char size_str[COMMBUFFSIZ];
326 ret = parse_header_entry(head,
"size", size_str, COMMBUFFSIZ);
328 cislog_error(
"parse_comm_header: could not find size in header");
333 out.size = atoi(size_str);
335 ret = parse_header_entry(head,
"id", out.id, COMMBUFFSIZ);
336 ret = parse_header_entry(head,
"response_address", out.response_address, COMMBUFFSIZ);
337 ret = parse_header_entry(head,
"request_id", out.request_id, COMMBUFFSIZ);
339 char stype_str[COMMBUFFSIZ];
340 ret = parse_header_entry(head,
"stype", stype_str, COMMBUFFSIZ);
342 out.serializer_type = atoi(stype_str);
344 char array_str[COMMBUFFSIZ];
345 ret = parse_header_entry(head,
"as_array", array_str, COMMBUFFSIZ);
347 out.as_array = atoi(array_str);
349 ret = parse_header_entry(head,
"format_str", out.format_str, COMMBUFFSIZ);
350 ret = parse_header_entry(head,
"field_names", out.field_names, COMMBUFFSIZ);
351 ret = parse_header_entry(head,
"field_units", out.field_units, COMMBUFFSIZ);
char field_names[COMMBUFFSIZ]
String containing field names.
Definition: comm_header.h:27
char response_address[COMMBUFFSIZ]
Response address.
Definition: comm_header.h:23
char request_id[COMMBUFFSIZ]
Request id.
Definition: comm_header.h:24
int multipart
1 if message is multipart, 0 if it is not.
Definition: comm_header.h:18
size_t bodybeg
Start of body in header.
Definition: comm_header.h:20
char address[COMMBUFFSIZ]
Address that message will comm in on.
Definition: comm_header.h:17
int valid
1 if the header is valid, 0 otherwise.
Definition: comm_header.h:21
char format_str[COMMBUFFSIZ]
Format string for serializer.
Definition: comm_header.h:26
size_t bodysiz
Size of body.
Definition: comm_header.h:19
size_t size
Size of incoming message.
Definition: comm_header.h:16
int as_array
1 if messages will be serialized arrays.
Definition: comm_header.h:29
int serializer_type
Code indicating the type of serializer.
Definition: comm_header.h:25
char field_units[COMMBUFFSIZ]
String containing field units.
Definition: comm_header.h:28
char id[COMMBUFFSIZ]
Unique ID associated with this message.
Definition: comm_header.h:22
Header information passed by comms for multipart messages.
Definition: comm_header.h:15