-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.c
346 lines (277 loc) · 9.42 KB
/
client.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//client
#include <sys/socket.h>
#include <msgpack.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <unistd.h> /* for read */
#define PORT 1234
msgpack_sbuffer sbuf; /* buffer */
msgpack_packer pk; /* packer */
msgpack_zone mempool;
int id_session, sock;
void send_helo();
void create_sheet();
void remove_sheet();
int get_val(int row, int col, float * res);
int set_val(int row, int col, float val);
int bye();
int valread;
char buffer[1024] = {0};
int main() {
struct sockaddr_in address;
struct sockaddr_in serv_addr;
sock = 0;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
printf("Socket creation error \n");
return -1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0) {
printf("Invalid address/ Address not supported \n");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
printf("Connection Failed \n");
return -1;
}
/* Put the socket in non-blocking mode:
if(fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK) < 0) {
printf("cannot set socket in non blocking mode");
return -1;
}*/
valread = read(sock, buffer, 1024);
if (valread != -1) {
printf("got server inf.: %s\n", buffer);
buffer[0]='\0';
} else
printf("valread :%d\n", valread);
msgpack_sbuffer_init(&sbuf); /* initialize buffer */
msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write); /* initialize packer */
/* deserialize the buffer into msgpack_object instance. */
/* deserialized object is valid during the msgpack_zone instance alive. */
msgpack_zone_init(&mempool, 1024);
printf("sending helo\n");
send_helo();
printf("creating sheet\n");
create_sheet();
printf("\nsetting value\n");
set_val(0, 0, 12.4);
//sleep(5);
float f;
int res = get_val(0, 0, &f);
if (res == 0) printf("\ngetting value: %f.\n", f);
printf("deleting sheet\n");
remove_sheet();
printf("\nsaying bye\n");
bye();
msgpack_zone_destroy(&mempool);
msgpack_sbuffer_destroy(&sbuf);
return 0;
}
int bye() {
msgpack_pack_map(&pk, 2);
msgpack_pack_str(&pk, 2);
msgpack_pack_str_body(&pk, "id", 2);
msgpack_pack_short(&pk, id_session);
msgpack_pack_str(&pk, 6);
msgpack_pack_str_body(&pk, "method", 6);
msgpack_pack_str(&pk, 3);
msgpack_pack_str_body(&pk, "bye", 3);
send(sock, sbuf.data, sbuf.size, 0 );
msgpack_sbuffer_clear(&sbuf);
//get OK return status
valread = read(sock, buffer, 1024);
if (valread > 0) {
msgpack_object p;
//FIXME sizeof(..) may differ on sender and receiver machine
msgpack_unpack(buffer, sizeof(buffer), NULL, &mempool, &p);
msgpack_object_print(stdout, p);
// return 0
if ( p.type == MSGPACK_OBJECT_MAP && p.via.map.size == 2 &&
! strncmp(p.via.map.ptr->key.via.str.ptr, "ret", 3) &&
((int) p.via.map.ptr->val.via.u64 == 0)) {
return 0;
}
} else if (valread < 0) {
printf("nothing to read in socket\n");
return -2;
}
return -1;
}
// TODO should return 0 when 0 return status is received from server
int set_val(int row, int col, float val) {
msgpack_pack_map(&pk, 3);
msgpack_pack_str(&pk, 2);
msgpack_pack_str_body(&pk, "id", 2);
msgpack_pack_short(&pk, id_session);
msgpack_pack_str(&pk, 6);
msgpack_pack_str_body(&pk, "method", 6);
msgpack_pack_str(&pk, 7);
msgpack_pack_str_body(&pk, "set_val", 7);
msgpack_pack_str(&pk, 6);
msgpack_pack_str_body(&pk, "params", 6);
msgpack_pack_map(&pk, 3);
msgpack_pack_str(&pk, 3);
msgpack_pack_str_body(&pk, "row", 3);
msgpack_pack_int(&pk, row);
msgpack_pack_str(&pk, 3);
msgpack_pack_str_body(&pk, "col", 3);
msgpack_pack_int(&pk, col);
msgpack_pack_str(&pk, 3);
msgpack_pack_str_body(&pk, "val", 3);
msgpack_pack_float(&pk, val);
send(sock, sbuf.data, sbuf.size, 0 );
msgpack_sbuffer_clear(&sbuf);
//get OK return status
valread = read(sock, buffer, 1024);
if (valread > 0) {
msgpack_object p;
//FIXME sizeof(..) may differ on sender and receiver machine
msgpack_unpack(buffer, sizeof(buffer), NULL, &mempool, &p);
msgpack_object_print(stdout, p);
// return 0
if ( p.type == MSGPACK_OBJECT_MAP && p.via.map.size == 2 &&
! strncmp(p.via.map.ptr->key.via.str.ptr, "ret", 3) &&
((int) p.via.map.ptr->val.via.u64 == 0)) {
return 0;
}
} else if (valread < 0) {
printf("nothing to read in socket\n");
return -2;
}
return -1;
}
// TODO return 0 when ok. -1 when not found/error
int get_val(int row, int col, float * res) {
int found=0;
msgpack_pack_map(&pk, 3);
msgpack_pack_str(&pk, 2);
msgpack_pack_str_body(&pk, "id", 2);
msgpack_pack_short(&pk, id_session);
msgpack_pack_str(&pk, 6);
msgpack_pack_str_body(&pk, "method", 6);
msgpack_pack_str(&pk, 7);
msgpack_pack_str_body(&pk, "get_val", 7);
msgpack_pack_str(&pk, 6);
msgpack_pack_str_body(&pk, "params", 6);
msgpack_pack_map(&pk, 2);
msgpack_pack_str(&pk, 3);
msgpack_pack_str_body(&pk, "row", 3);
msgpack_pack_int(&pk, row);
msgpack_pack_str(&pk, 3);
msgpack_pack_str_body(&pk, "col", 3);
msgpack_pack_int(&pk, col);
send(sock, sbuf.data, sbuf.size, 0 );
msgpack_sbuffer_clear(&sbuf);
msgpack_unpacker unp;
bool result = msgpack_unpacker_init(&unp, 1024);
if (result) printf("\nok init\n");
// get value
valread = read(sock, buffer, 1024);
if (valread > 0) {
memcpy(msgpack_unpacker_buffer(&unp), buffer, 1024);
msgpack_unpacker_buffer_consumed(&unp, 1024);
msgpack_unpacked und;
msgpack_unpack_return ret;
msgpack_unpacked_init(&und);
ret = msgpack_unpacker_next(&unp, &und);
if (ret == MSGPACK_UNPACK_SUCCESS) printf("ok unpacked\n");
msgpack_object q = und.data;
msgpack_object_print(stdout, q);
if (
q.type == MSGPACK_OBJECT_MAP && q.via.map.size == 2 &&
! strncmp(q.via.map.ptr->key.via.str.ptr, "ret", 3) &&
((int) q.via.map.ptr->val.via.u64 == 0) &&
! strncmp(((q.via.map.ptr)+1)->key.via.str.ptr, "val", 3)
) {
*res = ((q.via.map.ptr)+1)->val.via.f64;
found = 1;
}
msgpack_unpacked_destroy(&und);
}
msgpack_unpacker_destroy(&unp);
return found ? 0 : -1;
}
// TODO return 0 when ok. -1 when not found/error
void send_helo() {
// pack HELO
msgpack_pack_str(&pk, 4);
msgpack_pack_str_body(&pk, "HELO", 4); // this requests a new session
// send HELO
send(sock, sbuf.data, sbuf.size, 0 );
msgpack_sbuffer_clear(&sbuf);
// get session ID
valread = read(sock, buffer, 1024);
if (valread > 0) {
msgpack_object o;
//FIXME sizeof(..) may differ on sender and receiver machine
msgpack_unpack(buffer, sizeof(buffer), NULL, &mempool, &o);
//msgpack_object_print(stdout, o);
if (o.type == MSGPACK_OBJECT_MAP && o.via.map.size == 1 && ! strncmp(o.via.map.ptr->key.via.str.ptr, "id", 2)) {
id_session = (int) o.via.map.ptr->val.via.u64;
printf("got session #: %d.\n", id_session);
}
}
}
// TODO return 0 when ok. -1 when not found/error
void create_sheet() {
//msgpack_sbuffer_clear(&sbuf);
msgpack_pack_map(&pk, 3);
msgpack_pack_str(&pk, 2);
msgpack_pack_str_body(&pk, "id", 2);
msgpack_pack_int(&pk, id_session);
msgpack_pack_str(&pk, 6);
msgpack_pack_str_body(&pk, "method", 6);
msgpack_pack_str(&pk, 12);
msgpack_pack_str_body(&pk, "create_sheet", 12);
msgpack_pack_str(&pk, 6);
msgpack_pack_str_body(&pk, "params", 6);
msgpack_pack_map(&pk, 1);
msgpack_pack_str(&pk, 4);
msgpack_pack_str_body(&pk, "name", 4);
msgpack_pack_str(&pk, 6);
msgpack_pack_str_body(&pk, "sheet1", 6);
send(sock, sbuf.data, sbuf.size, 0 );
msgpack_sbuffer_clear(&sbuf);
// get OK
valread = read(sock, buffer, 1024);
if (valread > 0) {
msgpack_object o;
msgpack_unpack(buffer, sizeof(buffer), NULL, &mempool, &o);
msgpack_object_print(stdout, o);
}
}
// TODO return 0 when ok. -1 when not found/error
void remove_sheet() {
msgpack_pack_map(&pk, 3);
msgpack_pack_str(&pk, 2);
msgpack_pack_str_body(&pk, "id", 2);
msgpack_pack_int(&pk, id_session);
msgpack_pack_str(&pk, 6);
msgpack_pack_str_body(&pk, "method", 6);
msgpack_pack_str(&pk, 12);
msgpack_pack_str_body(&pk, "delete_sheet", 12);
msgpack_pack_str(&pk, 6);
msgpack_pack_str_body(&pk, "params", 6);
msgpack_pack_map(&pk, 1);
msgpack_pack_str(&pk, 4);
msgpack_pack_str_body(&pk, "name", 4);
msgpack_pack_str(&pk, 6);
msgpack_pack_str_body(&pk, "sheet1", 6);
send(sock, sbuf.data, sbuf.size, 0 );
msgpack_sbuffer_clear(&sbuf);
// get OK
valread = read(sock, buffer, 1024);
if (valread > 0) {
msgpack_object o;
msgpack_unpack(buffer, sizeof(buffer), NULL, &mempool, &o);
msgpack_object_print(stdout, o);
}
return;
}