forked from accre/lstore-toolbox
/
rr_svr.c
190 lines (154 loc) · 6.12 KB
/
rr_svr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
Advanced Computing Center for Research and Education Proprietary License
Version 1.0 (September 2012)
Copyright (c) 2012, Advanced Computing Center for Research and Education,
Vanderbilt University, All rights reserved.
This Work is the sole and exclusive property of the Advanced Computing Center
for Research and Education department at Vanderbilt University. No right to
disclose or otherwise disseminate any of the information contained herein is
granted by virtue of your possession of this software except in accordance with
the terms and conditions of a separate License Agreement entered into with
Vanderbilt University.
THE AUTHOR OR COPYRIGHT HOLDERS PROVIDES THE "WORK" ON AN "AS IS" BASIS,
WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, TITLE, FITNESS FOR A PARTICULAR
PURPOSE, AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Vanderbilt University
Advanced Computing Center for Research and Education
230 Appleton Place
Nashville, TN 37203
http://www.accre.vanderbilt.edu
*/
//*************************************************************************
//*************************************************************************
#include "rr_svr.h"
//*************************************************************************
// _rrsvr_init - Initialize rr server
//*************************************************************************
void _rrsvr_init(rrsvr_t *self)
{
self->mode = SYNC_MODE;
self->timeout = TIMEOUT_DFT;
self->ctx = NULL;
self->socket = NULL;
self->pattern = NULL;
self->endpoint = NULL;
self->cli_identity = NULL;
}
//*************************************************************************
// rrsvr_new - Construct a request response server
//*************************************************************************
rrsvr_t *rrsvr_new()
{
rrsvr_t *svr;
type_malloc_clear(svr, rrsvr_t, 1);
_rrsvr_init(svr);
svr->ctx = zctx_new();
if (!svr->ctx) {
free(svr);
return NULL;
}
return svr;
}
//************************************************************************
// rrsvr_destroy - Destroy rrsvr
//************************************************************************
void rrsvr_destroy(rrsvr_t **self_p)
{
assert(self_p);
if (*self_p) {
rrsvr_t *self = *self_p;
zctx_destroy(&self->ctx);
free(self->pattern);
free(self->endpoint);
free(self->cli_identity);
free(self);
*self_p = NULL;
}
}
//*************************************************************************
// _rrsvr_bind_lpp - Bind to an endpoint
//*************************************************************************
void _rrsvr_bind_lpp(rrsvr_t *self)
{
self->socket = rr_create_socket(self->ctx, self->mode, ZMQ_REP, ZMQ_ROUTER);
zsocket_bind(self->socket, "%s", self->endpoint);
}
//***********************************************************************
// rrsvr_construct_envelope - Construct the envelope
//***********************************************************************
void rrsvr_construct_envelope(rrsvr_t *self, zmsg_t *msg)
{
zmsg_pushstr(msg, "%s", self->cli_identity);
}
//***********************************************************************
// rrsvr_send - Send len of bytes in buf through zmq
// OUTPUTS:
// Returns the number of bytes in the sent message if successful. Otherwise, retruns -1.
//***********************************************************************
int rrsvr_send(rrsvr_t *self, void *buf, size_t len)
{
zmsg_t *msg = zmsg_new();
if (self->mode == ASYNC_MODE) {
rrsvr_construct_envelope(self, msg);
}
zmsg_addmem(msg, buf, len);
int rc = zmsg_send(&msg, self->socket);
return rc;
}
//*************************************************************************
// rrsvr_recv - Receive data through zmq
// OUTPUTS:
// Returns the number of bytes in the MESSAGE if successful. Note the value
// can exceed the value of len parameter in case the message was truncated.
// (nbytes < len ? nbytes : len) bytes are stored in the buf. If failed, returns -1.
//*************************************************************************
int rrsvr_recv(rrsvr_t *self, void *buf, size_t len)
{
int nbytes = 0;
if (zsocket_poll(self->socket, self->timeout)) {
zmsg_t *msg = zmsg_recv(self->socket);
if (self->mode == ASYNC_MODE) { //** Retrieves envelope
free(self->cli_identity);
self->cli_identity = zmsg_popstr(msg);
}
nbytes = zmsg_content_size(msg);
size_t to_copy = (size_t)nbytes < len ? (size_t)nbytes : len;
zframe_t *frame = zmsg_pop(msg);
memcpy (buf, zframe_data(frame), to_copy);
zframe_destroy(&frame);
zmsg_destroy(&msg);
}
return nbytes;
}
//**************************************************************************
// rrsvr_config_lpp - Config rrsvr to use lpp pattern
//**************************************************************************
void _rrsvr_config_lpp(rrsvr_t *self, inip_file_t *keyfile)
{
assert(keyfile);
rr_set_mode_tm(keyfile, "lppsvr", &self->mode, &self->timeout);
self->endpoint = inip_get_string(keyfile, "lppsvr", "endpoint", NULL);
assert(self->endpoint);
_rrsvr_bind_lpp(self);
}
//*************************************************************************
// rrsvr_load_config - Config the rr server
//*************************************************************************
void rrsvr_load_config(rrsvr_t *self, char *fname)
{
assert(fname);
inip_file_t *keyfile = inip_read(fname);
self->pattern = inip_get_string(keyfile, "zsock", "pattern", NULL);
assert(self->pattern);
if (strcmp(self->pattern, "lpp") == 0) {
_rrsvr_config_lpp(self, keyfile);
} else {
log_printf(0, "Unknown ZMQ Pattern: %s.\n", self->pattern);
exit(0);
}
inip_destroy(keyfile);
}