cis_config
RPCComm.h
1 #include <CommBase.h>
2 #include <DefaultComm.h>
3 #include <comm_header.h>
4 
6 #ifndef CISRPCCOMM_H_
7 #define CISRPCCOMM_H_
8 
9 // Handle is output comm
10 // Info is input comm
11 
17 static inline
18 int new_rpc_address(comm_t *comm) {
19  comm->type = _default_comm;
20  return new_default_address(comm);
21 };
22 
28 static inline
29 int init_rpc_comm(comm_t *comm) {
30  int ret;
31  // Input comm
32  comm_t *info = init_comm_base(comm->name, "recv", _default_comm,
33  comm->serializer->info);
34  ret = init_default_comm(info);
35  if (ret < 0) {
36  cislog_error("init_rpc_comm(%s): Failed to initialize input comm", comm->name);
37  return -1;
38  }
39  comm->info = (void*)info;
40  // Output comm
41  char *seri_out = (char*)malloc(strlen(comm->direction) + 1);
42  if (seri_out == NULL) {
43  cislog_error("init_rpc_comm(%s): Failed to malloc seri_out.");
44  return -1;
45  }
46  strcpy(seri_out, comm->direction);
47  comm_t *handle = init_comm_base(comm->name, "send", _default_comm,
48  (void*)seri_out);
49  ret = init_default_comm(handle);
50  if (ret < 0) {
51  cislog_error("init_rpc_comm(%s): Failed to initialize output comm", comm->name);
52  return -1;
53  }
54  comm->handle = (void*)handle;
55  // Clean up
56  strcpy(comm->direction, "send");
57  free(seri_out);
58  return ret;
59 };
60 
66 static inline
67 int free_rpc_comm(comm_t *x) {
68  if (x->handle != NULL) {
69  comm_t *handle = (comm_t*)(x->handle);
70  free_default_comm(handle);
71  free(x->handle);
72  x->handle = NULL;
73  }
74  if (x->info != NULL) {
75  comm_t *info = (comm_t*)(x->info);
76  free_default_comm(info);
77  free(x->info);
78  x->info = NULL;
79  }
80  return 0;
81 };
82 
88 static inline
89 int rpc_comm_nmsg(const comm_t x) {
90  comm_t *info = (comm_t*)(x.info);
91  int ret = default_comm_nmsg(*info);
92  return ret;
93 };
94 
102 static inline
103 int rpc_comm_send(const comm_t x, const char *data, const size_t len) {
104  cislog_debug("rpc_comm_send(%s): %d bytes", x.name, len);
105  if (x.handle == NULL) {
106  cislog_error("rpc_comm_send(%s): no output comm registered", x.name);
107  return -1;
108  }
109  comm_t *res_comm = (comm_t*)(x.handle);
110  return default_comm_send(*res_comm, data, len);
111 };
112 
124 static inline
125 int rpc_comm_recv(const comm_t x, char **data, const size_t len,
126  const int allow_realloc) {
127  cislog_debug("rpc_comm_recv(%s)", x.name);
128  if (x.info == NULL) {
129  cislog_error("rpc_comm_recv(%s): no input comm registered", x.name);
130  return -1;
131  }
132  comm_t *req_comm = (comm_t*)(x.info);
133  return default_comm_recv(*req_comm, data, len, allow_realloc);
134 };
135 
136 #endif /*CISRPCCOMM_H_*/
void * handle
Pointer to handle for comm.
Definition: CommBase.h:25
void * info
Pointer to any extra info comm requires.
Definition: CommBase.h:26
Communication structure.
Definition: CommBase.h:19
void * info
Pointer to any extra info serializer requires.
Definition: SerializeBase.h:17
comm_type type
Comm type.
Definition: CommBase.h:20
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