2 #ifndef CISCLIENTCOMM_H_ 3 #define CISCLIENTCOMM_H_ 7 #include <DefaultComm.h> 8 #include <comm_header.h> 16 static unsigned _client_rand_seeded = 0;
24 int new_client_address(
comm_t *comm) {
25 if (!(_client_rand_seeded)) {
26 srand(ptr2seed(comm));
27 _client_rand_seeded = 1;
29 comm->
type = _default_comm;
30 return new_default_address(comm);
39 int init_client_comm(
comm_t *comm) {
41 if (!(_client_rand_seeded)) {
42 srand(ptr2seed(comm));
43 _client_rand_seeded = 1;
46 if ((strlen(comm->
name) == 0) && (strlen(comm->
address) > 0)) {
47 comm->
type = _default_comm;
48 return init_default_comm(comm);
51 char *seri_out = (
char*)malloc(strlen(comm->
direction) + 1);
52 if (seri_out == NULL) {
53 cislog_error(
"init_client_comm: Failed to malloc output serializer.");
58 if (strlen(comm->
name) == 0) {
59 handle = new_comm_base(comm->
address,
"send", _default_comm, (
void*)seri_out);
60 sprintf(handle->
name,
"client_request.%s", comm->
address);
62 handle = init_comm_base(comm->
name,
"send", _default_comm, (
void*)seri_out);
64 ret = init_default_comm(handle);
66 comm->
handle = (
void*)handle;
68 int *ncomm = (
int*)malloc(
sizeof(
int));
70 cislog_error(
"init_client_comm: Failed to malloc ncomm.");
74 handle->
info = (
void*)ncomm;
79 cislog_error(
"init_client_comm: Failed to malloc info.");
83 comm->
info = (
void*)info;
88 int get_client_response_count(
const comm_t x) {
92 out = ((
int*)(handle->
info))[0];
98 void set_client_response_count(
const comm_t x,
const int new_val) {
100 if (handle != NULL) {
101 int *count = (
int*)(handle->
info);
107 void inc_client_response_count(
const comm_t x) {
109 if (handle != NULL) {
110 int *count = (
int*)(handle->
info);
116 void dec_client_response_count(
const comm_t x) {
118 if (handle != NULL) {
119 int *count = (
int*)(handle->
info);
125 void free_client_response_count(
comm_t *x) {
127 if (handle != NULL) {
128 int *count = (
int*)(handle->
info);
140 int free_client_comm(
comm_t *x) {
141 if (x->
info != NULL) {
143 if (info[0] != NULL) {
144 int ncomm = get_client_response_count(*x);
146 for (i = 0; i < ncomm; i++) {
147 if (info[0][i] != NULL) {
148 free_default_comm(info[0][i]);
149 free_comm_base(info[0][i]);
159 free_client_response_count(x);
162 free_default_comm(handle);
163 free_comm_base(handle);
176 int client_comm_nmsg(
const comm_t x) {
178 int ret = default_comm_nmsg(*handle);
192 int ncomm = get_client_response_count(x);
194 res_comm[0] = (
comm_t**)realloc(res_comm[0],
sizeof(
comm_t*)*(ncomm + 1));
195 if (res_comm[0] == NULL) {
196 cislog_error(
"client_response_header: Failed to realloc response comm.");
200 char *seri_copy = (
char*)malloc(strlen((
char*)(x.
serializer->
info)) + 1);
201 if (seri_copy == NULL) {
202 cislog_error(
"client_response_header: Failed to malloc copy of serializer info.");
207 res_comm[0][ncomm] = new_comm_base(NULL,
"recv", _default_comm, seri_copy);
208 int ret = new_default_address(res_comm[0][ncomm]);
210 cislog_error(
"client_response_header(%s): could not create response comm", x.
name);
214 res_comm[0][ncomm]->
sent_eof[0] = 1;
215 res_comm[0][ncomm]->
recv_eof[0] = 1;
216 inc_client_response_count(x);
217 ncomm = get_client_response_count(x);
218 cislog_debug(
"client_response_header(%s): Created response comm number %d",
223 cislog_debug(
"client_response_header(%s): response_address = %s, request_id = %s",
236 int client_comm_send(
comm_t x,
const char *data,
const size_t len) {
238 cislog_debug(
"client_comm_send(%s): %d bytes", x.
name, len);
240 cislog_error(
"client_comm_send(%s): no request comm registered", x.
name);
244 ret = default_comm_send(*req_comm, data, len);
263 int client_comm_recv(
comm_t x,
char **data,
const size_t len,
const int allow_realloc) {
264 cislog_debug(
"client_comm_recv(%s)", x.
name);
265 if ((x.
info == NULL) || (get_client_response_count(x) == 0)) {
266 cislog_error(
"client_comm_recv(%s): no response comm registered", x.
name);
270 int ret = default_comm_recv(res_comm[0][0][0], data, len, allow_realloc);
272 cislog_error(
"client_comm_recv(%s): default_comm_recv returned %d",
277 cislog_debug(
"client_comm_recv(%s): default_comm_recv returned %d",
279 free_default_comm(res_comm[0][0]);
280 free_comm_base(res_comm[0][0]);
281 free(res_comm[0][0]);
282 dec_client_response_count(x);
283 int nresp = get_client_response_count(x);
284 memmove(*res_comm, *res_comm + 1, nresp*
sizeof(
comm_t*));
void * handle
Pointer to handle for comm.
Definition: CommBase.h:29
char response_address[COMMBUFFSIZ]
Response address.
Definition: comm_header.h:27
char request_id[COMMBUFFSIZ]
Request id.
Definition: comm_header.h:28
void * info
Pointer to any extra info comm requires.
Definition: CommBase.h:30
Communication structure.
Definition: CommBase.h:23
char address[COMM_ADDRESS_SIZE]
Comm address.
Definition: CommBase.h:26
void * info
Pointer to any extra info serializer requires.
Definition: SerializeBase.h:21
comm_type type
Comm type.
Definition: CommBase.h:24
int valid
1 if the header is valid, 0 otherwise.
Definition: comm_header.h:25
int always_send_header
1 if comm should always send a header.
Definition: CommBase.h:33
seri_t * serializer
Serializer for comm messages.
Definition: CommBase.h:31
char direction[COMM_DIR_SIZE]
send or recv for direction messages will go.
Definition: CommBase.h:27
char name[COMM_NAME_SIZE]
Comm name.
Definition: CommBase.h:25
int * sent_eof
Flag specifying if EOF has been sent.
Definition: CommBase.h:36
int * recv_eof
Flag specifying if EOF has been received.
Definition: CommBase.h:37
Header information passed by comms for multipart messages.
Definition: comm_header.h:19