-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscriber.cpp
144 lines (112 loc) · 3.98 KB
/
subscriber.cpp
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
#include "message.h"
#define BUFLEN 200
void usage(char *file)
{
fprintf(stderr, "Usage: %s client_id server_address server_port\n", file);
exit(0);
}
int main(int argc, char** argv) {
#pragma region PREP
if (argc < 3) {
usage(argv[0]);
}
setvbuf(stdout, NULL, _IONBF, BUFSIZ);
int sockfd, n, ret;
struct sockaddr_in serv_addr;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
DIE(sockfd < 0, "socket");
serv_addr.sin_family = AF_INET;
ret = inet_aton(argv[2], &serv_addr.sin_addr);
DIE(ret == 0, "inet_aton");
serv_addr.sin_port = htons(atoi(argv[3]));
int enable = 1;
if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(int)) == -1) {
perror("disable Neagle's algorithm");
exit(2);
}
ret = connect(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr));
DIE(ret < 0, "connect");
DIE(strlen(argv[1]) > 10, "ID is too long");
n = send(sockfd, argv[1], 10, 0);
DIE(n < 0, "connect");
{
packet p{};
ret = recv(sockfd, &p, sizeof(packet), 0);
DIE(ret < 0, "recv");
if (p.m.tip_pachet != OK) {
close(sockfd);
exit(0);
}
}
fd_set read_fds; // multimea de citire folosita in select()
fd_set tmp_fds; // multime folosita temporar
FD_ZERO(&read_fds);
FD_ZERO(&tmp_fds);
FD_SET(sockfd, &read_fds);
FD_SET(STDIN_FILENO, &read_fds);
int fdmax = sockfd > STDIN_FILENO ? sockfd : STDIN_FILENO;
char buffer[BUFLEN];
#pragma endregion
while (true) {
tmp_fds = read_fds;
ret = select(fdmax + 1, &tmp_fds, NULL, NULL, NULL);
DIE(ret < 0, "select");
#pragma region HANDLE STDIN
if (FD_ISSET(STDIN_FILENO, &tmp_fds)) {
// se citeste de la tastatura
memset(buffer, 0, BUFLEN);
fgets(buffer, BUFLEN, stdin);
if (strstr(buffer, "unsubscribe")) {
if (DIE(strlen(buffer) <= strlen("unsubscribe"), "Invalid unsubscribe input", false) == FAIL)
goto receive_from_server;
packet* p = createUnSubRequest(buffer + strlen("unsubscribe "), argv[1]);
n = send(sockfd, p, TCPCLIENTLEN, 0);
delete p;
DIE(n < 0, "send");
cout << "Unsubscribed from topic.\n";
} else if (strstr(buffer, "subscribe")) {
char *topic, *sfchr;
int sf;
packet* p;
topic = strtok (buffer + strlen("subscribe "), " ");
if (DIE(topic == nullptr, "No subscription topic", false) == FAIL ||
DIE(strlen(topic) > 50, "Topic length is over 50 chars", false) == FAIL)
goto receive_from_server;
sfchr = strtok (NULL, " ");
if (DIE(sfchr == nullptr, "No subscription sf", false) == FAIL ||
DIE(!isdigit(sfchr[0]), "sf should be a number", false) == FAIL)
goto receive_from_server;
sf = atoi(sfchr);
if (DIE(!(sf != 1 || sf != 0), "Invalid sf value", false) == FAIL)
goto receive_from_server;
p = createSubRequest(topic, sf, argv[1]);
n = send(sockfd, p, TCPCLIENTLEN, 0);
delete p;
DIE(n < 0, "send");
cout << "Subscribed to topic.\n";
} else if (strstr(buffer, "exit")) {
packet p{};
p.m.tip_pachet = EXIT,
p.length = htonl(51);
strcpy(p.id, argv[1]);
n = send(sockfd, &p, TCPCLIENTLEN, 0);
break;
};
}
#pragma endregion
#pragma region HANDLE RECV
receive_from_server:
if (FD_ISSET(sockfd, &tmp_fds)) {
packet p{};
int r;
r = recv(sockfd, &p, HDRLEN, 0);
DIE(r < 0, "recv");
r = recv(sockfd, &p.m, ntohl(p.length), 0);
DIE(r < 0, "recv");
if (p.m.tip_pachet == EXIT) break;
printMessage(&p);
}
#pragma endregion
}
close(sockfd);
}