-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscriber.cpp
More file actions
315 lines (260 loc) · 8.42 KB
/
subscriber.cpp
File metadata and controls
315 lines (260 loc) · 8.42 KB
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
#include <bits/stdc++.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include "helpers.h"
class Subscriber {
public:
char id[10];
char *eth_addr;
int portno;
Subscriber(char *id_client, char *addr, char *port) {
// La initializarea clientului setez port-ul
// si ip-ul serverului si id-ul de client
memcpy(id, id_client, 10);
eth_addr = addr;
portno = atoi(port);
DIE(portno == 0, "atoi");
};
~Subscriber() {
// Inchid socket-ul TCP
close(socketTCP);
};
/* Eroare de utilizare la pornirea clientului */
static void usage(char *file) {
fprintf(stderr, "Usage: %s server_address server_port\n", file);
exit(0);
}
/* Functia publica care ruleaza clientul */
void run() {
initialize();
run_client();
}
private:
int n, ret, running, socketTCP;
struct sockaddr_in serv_addr;
fd_set read_fds; // Multimea de citire folosita in select()
fd_set tmp_fds; // Multime folosita temporar
int fdmax; // Valoare maxima fd din multimea read_fds
// Buffer-ul pentru primirea pachetelor
int buffer_size;
char buffer[sizeof(TCPpacket)];
/* Functia initializeaza serverul pentru o buna functionare */
void initialize() {
// Initializez buffer-ul pentru primirea pachetelor
buffer_size = 0;
memset(buffer, 0, sizeof(TCPpacket));
// Initializez socket-ul TCP
socketTCP = socket(AF_INET, SOCK_STREAM, 0);
DIE(socketTCP < 0, "socket");
// Dezactivez algoritmul Nagle
int one = NAGLE_OFF;
ret = setsockopt(socketTCP, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
DIE(ret < 0, "nagle algo error");
// Socket-ul de ethernet pentru server
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
ret = inet_aton(eth_addr, &serv_addr.sin_addr);
DIE(ret == 0, "inet_aton");
// Ma conectez la server
ret = connect(socketTCP, (struct sockaddr*) &serv_addr, sizeof(serv_addr));
DIE(ret < 0, "connect");
// Se trimite ID-ul catre server
send_id_connect();
// Se goleste multimea de descriptori de citire si multimea temporara
FD_ZERO(&read_fds);
FD_ZERO(&tmp_fds);
// Se adauga socket-urile pe care se asculta conexiunea si stdin-ul in multimea read_fds
FD_SET(socketTCP, &read_fds);
FD_SET(STDIN_FILENO,&read_fds);
fdmax = socketTCP + STDIN_FILENO;
// Se seteaza starea TRUE pentru a rula clientul
running = TRUE;
}
/* Functia executa rularea clientului */
void run_client() {
while (running) {
tmp_fds = read_fds;
// Folosesc functia select pentru multiplexare intrarilor
int rc = select(fdmax + 1, &tmp_fds, NULL, NULL, NULL);
DIE(rc < 0, "select");
// Se verifica daca se citeste input
if(FD_ISSET(STDIN_FILENO, &tmp_fds)) {
char buf[256];
if (!fgets(buf, sizeof(buf), stdin))
DIE(ferror(stdin), "stdin error");
// Inchid server-ul daca se primeste comanda exit
if (close_client(buf))
break;
// Impart comanda in substring-uri
std::vector<std::string> strings;
char delim[3] = " \n";
split(buf, delim, strings);
// Verific daca s-a primit comanda de subscribe
if (strings.size() == 3 && !strings[0].compare("subscribe")) {
// Trimit comanda de subscribe catre server
subscribe(strings[1], strings[2]);
// Verific daca s-a primit comanda de unsubscribe
} else if (strings.size() == 2 && !strings[0].compare("unsubscribe")) {
// Trimit comanda de unsubscribe catre server
unsubscribe(strings[1]);
}
// Se verifica daca se primit pachete pe socket-ul TCP
} else if (FD_ISSET(socketTCP, &tmp_fds)) {
if (receive())
break;
}
}
}
/* Functia verifica daca se primeste comanda
exit si inchide rularea clientului */
bool close_client(char *buf) {
if (!strcmp(buf, "exit\n")) {
// Inchid rularea clientului
running = FALSE;
return true;
}
return false;
}
/* Functia trimite ID-ul catre server la conectare */
void send_id_connect() {
TCPrequest tcp_req;
memcpy(&tcp_req.id, id, sizeof(id));
tcp_req.type = 1;
memset(&tcp_req.payload, 0, sizeof(tcp_req.payload));
tcp_req.data_size = 0;
n = send(socketTCP, &tcp_req, sizeof(id), 0);
DIE(n < 0, "send id error");
}
/* Functia trimite comanda de subscribe catre server */
void subscribe(std::string input, std::string type) {
TCPrequest tcp_req;
memcpy(&tcp_req.id, id, sizeof(id));
tcp_req.type = 2;
char *sf = (char *)malloc(2 * sizeof(char));
strcpy(sf, "0");
if (type == "1")
strcpy(sf, "1");
tcp_req.data_size = htonl(sizeof(char) + input.size()
+ sizeof(tcp_req.id) + sizeof(tcp_req.data_size));
memset(&tcp_req.payload, 0, sizeof(tcp_req.payload));
memcpy(&tcp_req.payload, input.c_str(), input.size());
strcat(tcp_req.payload, sf);
n = send(socketTCP, &tcp_req, sizeof(TCPrequest), 0);
DIE(n < 0, "send subscribe error");
free(sf);
printf("Subscribed to topic.\n");
}
/* Functia trimite comanda de unsubscribe catre server */
void unsubscribe(std::string input) {
TCPrequest tcp_req;
memcpy(&tcp_req.id, id, sizeof(id));
tcp_req.type = 3;
tcp_req.data_size = htonl(input.size() + sizeof(tcp_req.id)
+ sizeof(tcp_req.data_size));
memset(&tcp_req.payload, 0, sizeof(tcp_req.payload));
memcpy(&tcp_req.payload, input.c_str(), input.size());
n = send(socketTCP, &tcp_req, sizeof(TCPrequest), 0);
DIE(n < 0, "send subscribe error");
printf("Unsubscribed from topic.\n");
}
/* Functia primeste mesaje pe socket-ul TCP */
bool receive() {
// Se receptioneaza packet-ul TCP
char *buf = buffer;
int rc = recv(socketTCP, buffer + buffer_size, sizeof(TCPpacket), 0);
DIE(rc < 0, "recv");
// Daca se primeste 0 serverul s-a inchis si inchid clientul
if(rc == 0) {
running = FALSE;
return true;
}
// Verific daca sunt mai multe mesaj concatenate
int size_msg = *((int *)(buffer));
while(rc >= size_msg - buffer_size && rc >= 4) {
// Printez mesajele complete
TCPpacket *tcp_msg = (TCPpacket *) buf;
print_received(*tcp_msg);
buf = buf + size_msg;
rc -= size_msg - buffer_size;
size_msg = *((int *)buf);
buffer_size = 0;
}
// Daca buffer-ul mai are ceva in el
// mut totul la inceputul bufferului
if (buf == buffer) {
buffer_size += rc;
} else {
memmove(buffer, buf, rc);
buffer_size = rc;
}
return false;
}
/* Functia printeaza mesajele primite pe
pe TCP sub forma de string-uri */
void print_received(TCPpacket tcp_msg) {
printf ("%s:%i - %s - ",
inet_ntoa(tcp_msg.source.sin_addr), ntohs(tcp_msg.source.sin_port), tcp_msg.payload.topic);
// INT
if (tcp_msg.payload.type == 0) {
uint8_t sign = tcp_msg.payload.content[0];
uint32_t value;
memcpy(&value, tcp_msg.payload.content + sizeof(sign), 4);
int x = ntohl(value);
if (sign)
x = -x;
std::string x_str = std::to_string(x);
printf("INT - %s\n", x_str.c_str());
// SHORT INT
} else if (tcp_msg.payload.type == 1) {
uint16_t value;
memcpy(&value, tcp_msg.payload.content, 2);
double x = ntohs(value);
x = x / 100;
std::stringstream stream;
stream << std::fixed << std::setprecision(2) << x;
std::string x_str = stream.str();
printf("SHORT_REAL - %s\n", x_str.c_str());
// FLOAT
} else if (tcp_msg.payload.type == 2) {
uint8_t sign = tcp_msg.payload.content[0];
uint32_t value;
uint8_t power;
memcpy(&value, tcp_msg.payload.content + sizeof(sign), 4);
memcpy(&power, tcp_msg.payload.content + sizeof(sign) + sizeof(value), 1);
double x = (double)ntohl(value) / pow(10, power);
if (sign)
x = -x;
std::string x_str = std::to_string(x);
x_str.erase(x_str.find_last_not_of('0') + 1, std::string::npos );
printf("FLOAT - %s\n", x_str.c_str());
// STRING
} else if (tcp_msg.payload.type == 3) {
int size = tcp_msg.size - sizeof(TCPpacket) + 1500;
char *string = (char *)malloc(size * sizeof(char));
memset(string, 0, size);
memmove(string, tcp_msg.payload.content, size);
printf("STRING - %s\n", string);
}
}
};
int main(int argc, char *argv[]) {
// Eroare daca programul nu primeste parametrii corespunzatori
if (argc < 3) {
Subscriber::usage(argv[0]);
}
// Am dezactivat buffering-ul la afisare
setvbuf(stdout, NULL, _IONBF, BUFSIZ);
// Initializez o instanta pentru CLIENT
auto* subscriber = new (std::nothrow) Subscriber(argv[1], argv[2], argv[3]);
if (!subscriber) {
std::cerr << "new failed!\n";
return -1;
}
// Pornesc rularea clientului
subscriber->run();
// Dealoc memoria alocata pentru client
delete subscriber;
return 0;
}