cis_config: /root/cis_interface/cis_interface/cis_interface/communication/RPCComm.h Source File

cis_config

RPCComm.h
1
2 #ifndef CISRPCCOMM_H_
3 #define CISRPCCOMM_H_
4
5 #include <CommBase.h>
6 #include <DefaultComm.h>
7 #include <comm_header.h>
8
9 #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
10 extern "C" {
11 #endif
12
13 // Handle is output comm
14 // Info is input comm
15
21 static inline
22 int new_rpc_address(comm_t *comm) {
23 comm->type = _default_comm;
24 return new_default_address(comm);
25 };
26
32 static inline
33 int init_rpc_comm(comm_t *comm) {
34 int ret;
35 // Input comm
36 comm_t *info = init_comm_base(comm->name, "recv", _default_comm,
37 comm->serializer->info);
38 ret = init_default_comm(info);
39 if (ret < 0) {
40 cislog_error("init_rpc_comm(%s): Failed to initialize input comm", comm->name);
41 return -1;
42 }
43 comm->info = (void*)info;
44 // Output comm
45 char *seri_out = (char*)malloc(strlen(comm->direction) + 1);
46 if (seri_out == NULL) {
47 cislog_error("init_rpc_comm(%s): Failed to malloc seri_out.");
48 return -1;
49 }
50 strcpy(seri_out, comm->direction);
51 comm_t *handle = init_comm_base(comm->name, "send", _default_comm,
52 (void*)seri_out);
53 ret = init_default_comm(handle);
54 if (ret < 0) {
55 cislog_error("init_rpc_comm(%s): Failed to initialize output comm", comm->name);
56 return -1;
57 }
58 comm->handle = (void*)handle;
59 // Clean up
60 strcpy(comm->direction, "send");
61 free(seri_out);
62 return ret;
63 };
64
70 static inline
71 int free_rpc_comm(comm_t *x) {
72 if (x->handle != NULL) {
73 comm_t *handle = (comm_t*)(x->handle);
74 free_default_comm(handle);
75 free(x->handle);
76 x->handle = NULL;
77 }
78 if (x->info != NULL) {
79 comm_t *info = (comm_t*)(x->info);
80 free_default_comm(info);
81 free(x->info);
82 x->info = NULL;
83 }
84 return 0;
85 };
86
92 static inline
93 int rpc_comm_nmsg(const comm_t x) {
94 comm_t *info = (comm_t*)(x.info);
95 int ret = default_comm_nmsg(*info);
96 return ret;
97 };
98
106 static inline
107 int rpc_comm_send(const comm_t x, const char *data, const size_t len) {
108 cislog_debug("rpc_comm_send(%s): %d bytes", x.name, len);
109 if (x.handle == NULL) {
110 cislog_error("rpc_comm_send(%s): no output comm registered", x.name);
111 return -1;
112 }
113 comm_t *res_comm = (comm_t*)(x.handle);
114 return default_comm_send(*res_comm, data, len);
115 };
116
128 static inline
129 int rpc_comm_recv(const comm_t x, char **data, const size_t len,
130 const int allow_realloc) {
131 cislog_debug("rpc_comm_recv(%s)", x.name);
132 if (x.info == NULL) {
133 cislog_error("rpc_comm_recv(%s): no input comm registered", x.name);
134 return -1;
135 }
136 comm_t *req_comm = (comm_t*)(x.info);
137 return default_comm_recv(*req_comm, data, len, allow_realloc);
138 };
139
140 #ifdef __cplusplus /* If this is a C++ compiler, end C linkage */
141 }
142 #endif
143
144 #endif /*CISRPCCOMM_H_*/
comm_t::handle
void * handle
Pointer to handle for comm.
Definition: CommBase.h:29
comm_t::info
void * info
Pointer to any extra info comm requires.
Definition: CommBase.h:30
comm_t
Communication structure.
Definition: CommBase.h:23
seri_t::info
void * info
Pointer to any extra info serializer requires.
Definition: SerializeBase.h:21
comm_t::type
comm_type type
Comm type.
Definition: CommBase.h:24
comm_t::serializer
seri_t * serializer
Serializer for comm messages.
Definition: CommBase.h:31
comm_t::direction
char direction[COMM_DIR_SIZE]
send or recv for direction messages will go.
Definition: CommBase.h:27
comm_t::name
char name[COMM_NAME_SIZE]
Comm name.
Definition: CommBase.h:25