-
Notifications
You must be signed in to change notification settings - Fork 14
/
e2_client_api.c
125 lines (108 loc) · 3.23 KB
/
e2_client_api.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
//
// Created by Eugenio Moro on 28/06/22.
//
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <unistd.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <pb_encode.h>
#include <pb_decode.h>
#include <arpa/inet.h>
#include <stdlib.h>
int socket_fd;
struct sockaddr_in servaddr;
pb_ostream_t ostream;
pb_istream_t istream;
bool e2_api_init(char *server_str_addr, int port) {
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);//inet_addr(server_str_addr);
servaddr.sin_port = htons(port);
if ((socket_fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
perror("socket creation failed");
exit(EXIT_FAILURE);
}
E2DummyResponse rsp = E2_DUMMY_RESPONSE__INIT;
rsp.req_id = 100;
rsp.req_id = req_id;
//char st[] = "Hi, this is a dummy response!!";
rsp.mess_string = "Hi, this is a dummy response!!";
rsp.result = 1;
// encode to buffer
unsigned buflen = e2_dummy_response__get_packed_size(&rsp);
void* buf = malloc(buflen);
e2_dummy_response__pack(&rsp, buf);
printf("sending buffer\n");
int sent_len;
sendto(socket_fd, buf, buflen, MSG_CONFIRM, (const struct sockaddr *) &servaddr, sizeof(servaddr));
printf("done\n");
/* socket_fd = socket(AF_INET, SOCK_STREAM, 0);
if (socket_fd == -1) {
printf("Socket creation failed...\n");
return false;
} else {
//e2_api_connect();
return true;
}*/
}
bool e2_api_connect() {
socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (socket_fd == -1) {
printf("Socket creation failed...\n");
return false;
}
int ret = connect(socket_fd, (struct sockaddr *)&servaddr, sizeof(servaddr));
if (ret == 0){
ostream = pb_ostream_from_socket(socket_fd);
istream = pb_istream_from_socket(socket_fd);
return true;
} else {
printf("Socket connection failed with error %d...\n",ret);
return false;
}
}
bool e2_api_DUMMY_transaction(E2_dummy_response *rsp) {
// build and send request
rqst_p->req_id = E2_REQID_DUMMY1;
e2_api_connect();
if(!pb_encode_delimited(&ostream,E2_request_fields,rqst_p)){
printf("Encoding failed\n");
return false;
}
// wait for answer, write on provided pointer
istream = pb_istream_from_socket(socket_fd);
if (pb_decode_delimited(&istream, E2_dummy_response_fields, rsp)){
return true;
} else {
printf("Decoding failed\n");
return false;
}
}
bool e2_api_DUMMY_transaction_type2(E2_dummy_response *rsp) {
// build and send request
rqst_p->req_id = E2_REQID_DUMMY2;
e2_api_connect();
if(!pb_encode_delimited(&ostream,E2_request_fields,rqst_p)){
printf("Encoding failed\n");
return false;
}
// wait for answer, write on provided pointer
istream = pb_istream_from_socket(socket_fd);
if (pb_decode_delimited(&istream, E2_dummy_response_fields, rsp)){
return true;
} else {
printf("Decoding failed\n");
return false;
}
}
void e2_api_register_callback() {
//TODO
}
void e2_api_teardown() {
if (rqst_p != NULL){
free(rqst_p);
}
}