-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscriber.cpp
More file actions
149 lines (134 loc) · 4.15 KB
/
subscriber.cpp
File metadata and controls
149 lines (134 loc) · 4.15 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
#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include <poll.h>
#include <netinet/tcp.h>
#include <cstring>
bool string_contains(std::string s1, std::string s2)
{
if (s1.find(s2) != std::string::npos)
return true;
return false;
}
std::string getSubString(
const std::string &strValue,
const char &startChar,
const char &endChar)
{
std::string subString = "";
std::size_t startPos = strValue.find(startChar);
std::size_t endPos = strValue.find(endChar, startPos + 1);
if (startPos != std::string::npos &&
endPos != std::string::npos)
{
subString = strValue.substr(startPos + 1, endPos - startPos - 1);
}
return subString;
}
int main(int argc, char const *argv[])
{
setvbuf(stdout, NULL, _IONBF, BUFSIZ);
int status, valread, client_fd;
struct sockaddr_in serv_addr;
char buffer[1024] = {0};
if ((client_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(atoi(argv[3]));
if (inet_pton(AF_INET, argv[2], &serv_addr.sin_addr) <= 0)
{
printf(
"\nInvalid address/ Address not supported \n");
return -1;
}
if ((status = connect(client_fd, (struct sockaddr *)&serv_addr,
sizeof(serv_addr))) < 0)
{
printf("\nConnection Failed \n");
return -1;
}
int nagle = 1;
int ret = setsockopt(client_fd, IPPROTO_TCP, TCP_NODELAY, &nagle, sizeof(nagle));
send(client_fd, argv[1], strlen(argv[1]), 0);
valread = recv(client_fd, buffer, 1024, 0);
std::string response(buffer);
if (string_contains(response, "failed"))
{
close(client_fd);
exit(0);
}
int fd_count = 2;
struct pollfd *pfds = (struct pollfd *)malloc(sizeof(struct pollfd) * 2);
pfds[0].fd = 0;
pfds[0].events = POLLIN;
pfds[1].fd = client_fd;
pfds[1].events = POLLIN;
while (true)
{
int poll_count = poll(pfds, fd_count, -1);
if (poll_count == -1)
{
perror("poll");
exit(1);
}
for (int i = 0; i < fd_count; i++)
{
if (pfds[i].revents & POLLIN)
{
if (pfds[i].fd == client_fd)
{
char *buff = new char[1024];
std::memset(buff, 0, 1024);
int n = recv(pfds[i].fd, buff, 1024, 0);
if (n < 0)
{
exit(0);
}
std::string command(buff);
if(string_contains(command, "exit"))
{
free(pfds);
close(client_fd);
exit(0);
}
else
{
std::cout << command << std::endl;
}
free(buff);
}
else
{
std::string command;
std::getline(std::cin, command);
if (string_contains(command, "unsubscribe"))
{
std::cout << "Unsubscribed from topic.\n";
}
else if (string_contains(command, "subscribe"))
{
command = "subscribe " + getSubString(command, ' ', ' ');
std::cout << "Subscribed to topic.\n";
int n = send(client_fd, command.c_str(), command.size(), 0);
}
else if (string_contains(command, "exit"))
{
std::string exit_msg = "exit";
send(client_fd, exit_msg.c_str(), exit_msg.size(), 0);
free(pfds);
close(client_fd);
exit(0);
}
}
}
}
}
return 0;
}