cis_config
ServerComm.h
1 #include <CommBase.h>
2 #include <DefaultComm.h>
3 #include <comm_header.h>
4 
6 #ifndef CISSERVERCOMM_H_
7 #define CISSERVERCOMM_H_
8 
9 // Handle is recv address
10 // Info is response
11 
17 static inline
18 int new_server_address(comm_t *comm) {
19  comm->type = _default_comm;
20  return new_default_address(comm);
21 };
22 
28 static inline
29 int init_server_comm(comm_t *comm) {
30  int ret = 0;
31  // Called to create temp comm for send/recv
32  if ((strlen(comm->name) == 0) && (strlen(comm->address) > 0)) {
33  comm->type = _default_comm;
34  return init_default_comm(comm);
35  }
36  // Called to initialize/create server comm
37  char *seri_in = (char*)malloc(strlen(comm->direction) + 1);
38  if (seri_in == NULL) {
39  cislog_error("init_server_comm: Failed to malloc seri_in.");
40  return -1;
41  }
42  strcpy(seri_in, comm->direction);
43  // printf("init_server_comm(%s): seri: %s\n", comm->name, seri_in);
44  comm_t *handle;
45  if (strlen(comm->name) == 0) {
46  handle = new_comm_base(comm->address, "recv", _default_comm, (void*)seri_in);
47  sprintf(handle->name, "server_request.%s", comm->address);
48  } else {
49  handle = init_comm_base(comm->name, "recv", _default_comm, (void*)seri_in);
50  }
51  ret = init_default_comm(handle);
52  strcpy(comm->address, handle->address);
53  // printf("init_server_comm: name = %s, type=%d, address = %s\n",
54  // handle->name, handle->type, handle->address);
55  strcpy(comm->direction, "recv");
56  comm->handle = (void*)handle;
57  comm->always_send_header = 0;
58  comm_t **info = (comm_t**)malloc(sizeof(comm_t*));
59  if (info == NULL) {
60  cislog_error("init_server_comm: Failed to malloc info.");
61  return -1;
62  }
63  info[0] = NULL;
64  comm->info = (void*)info;
65  return ret;
66 };
67 
73 static inline
74 int free_server_comm(comm_t *x) {
75  if (x->handle != NULL) {
76  comm_t *handle = (comm_t*)(x->handle);
77  free_default_comm(handle);
78  free_comm_base(handle);
79  free(x->handle);
80  x->handle = NULL;
81  }
82  if (x->info != NULL) {
83  comm_t **info = (comm_t**)(x->info);
84  if (*info != NULL) {
85  free_default_comm(*info);
86  free_comm_base(*info);
87  free(*info);
88  }
89  free(info);
90  x->info = NULL;
91  }
92  return 0;
93 };
94 
100 static inline
101 int server_comm_nmsg(const comm_t x) {
102  comm_t *handle = (comm_t*)(x.handle);
103  int ret = default_comm_nmsg(*handle);
104  return ret;
105 };
106 
114 static inline
115 int server_comm_send(const comm_t x, const char *data, const size_t len) {
116  cislog_debug("server_comm_send(%s): %d bytes", x.name, len);
117  if (x.info == NULL) {
118  cislog_error("server_comm_send(%s): no response comm registered", x.name);
119  return -1;
120  }
121  comm_t **res_comm = (comm_t**)(x.info);
122  if (res_comm[0] == NULL) {
123  cislog_error("server_comm_send(%s): no response comm registered", x.name);
124  return -1;
125  }
126  int ret = default_comm_send((*res_comm)[0], data, len);
127  // Wait for msg to be received?
128  free_default_comm(res_comm[0]);
129  free_comm_base(res_comm[0]);
130  free(res_comm[0]);
131  res_comm[0] = NULL;
132  return ret;
133 };
134 
146 static inline
147 int server_comm_recv(comm_t x, char **data, const size_t len, const int allow_realloc) {
148  cislog_debug("server_comm_recv(%s)", x.name);
149  if (x.handle == NULL) {
150  cislog_error("server_comm_recv(%s): no request comm registered", x.name);
151  return -1;
152  }
153  comm_t *req_comm = (comm_t*)(x.handle);
154  int ret = default_comm_recv(*req_comm, data, len, allow_realloc);
155  if (ret < 0) {
156  return ret;
157  }
158  // Return EOF
159  if (is_eof(*data)) {
160  req_comm->recv_eof[0] = 1;
161  return ret;
162  }
163  // Initialize new comm from received address
164  comm_head_t head = parse_comm_header(*data, ret);
165  if (!(head.valid)) {
166  cislog_error("server_comm_recv(%s): Error parsing header.", x.name);
167  return -1;
168  }
169  // If there is not a response address
170  if (strlen(head.response_address) == 0) {
171  cislog_error("server_comm_recv(%s): No response address in message.", x.name);
172  return -1;
173  }
174  strcpy(x.address, head.id);
175  char *seri_copy = (char*)malloc(strlen((char*)(x.serializer->info)) + 1);
176  if (seri_copy == NULL) {
177  cislog_error("server_comm_recv(%s): Failed to malloc seri_copy.");
178  return -1;
179  }
180  strcpy(seri_copy, (char*)(x.serializer->info));
181  comm_t **res_comm = (comm_t**)(x.info);
182  res_comm[0] = new_comm_base(head.response_address, "send", _default_comm,
183  seri_copy);
184  /* sprintf(res_comm[0]->name, "server_response.%s", res_comm[0]->address); */
185  int newret;
186  newret = init_default_comm(res_comm[0]);
187  if (newret < 0) {
188  cislog_error("server_comm_recv(%s): Could not initialize response comm.", x.name);
189  return newret;
190  }
191  res_comm[0]->sent_eof[0] = 1;
192  res_comm[0]->recv_eof[0] = 1;
193  return ret;
194 };
195 
196 #endif /*CISSERVERCOMM_H_*/
void * handle
Pointer to handle for comm.
Definition: CommBase.h:25
char response_address[COMMBUFFSIZ]
Response address.
Definition: comm_header.h:23
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
void * info
Pointer to any extra info serializer requires.
Definition: SerializeBase.h:17
comm_type type
Comm type.
Definition: CommBase.h:20
int valid
1 if the header is valid, 0 otherwise.
Definition: comm_header.h:21
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 * recv_eof
Flag specifying if EOF has been received.
Definition: CommBase.h:33
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