-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscriber.c
More file actions
215 lines (176 loc) · 3.98 KB
/
Copy pathsubscriber.c
File metadata and controls
215 lines (176 loc) · 3.98 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <math.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include "server.h"
#define INT 0
#define SHORT_REAL 1
#define FLOAT 2
#define STRING 3
const char id_type[4][11] = {"INT\0", "SHORT_REAL\0", "FLOAT\0", "STRING\0"};
// sends a message containing the id to the server
// when connecting for the first time
int send_id(int sockfd, char* id) {
int n = send(sockfd, id, MAX_ID_LEN, 0);
return n;
}
void print_int(char* content) {
uint8_t* sign = (uint8_t*) content;
uint32_t* number = (uint32_t*) (content + sizeof(uint8_t));
int n = ntohl(*number);
if (*sign == 1) {
n *= -1;
}
printf("%d\n", n);
}
void print_short_real(char* content) {
uint16_t* n = (uint16_t*) content;
float val = ((float) (ntohs(*n))) / 100;
printf("%.2f\n", val);
}
void print_float(char* content) {
uint8_t* sign = (uint8_t*) content;
uint32_t* number = (uint32_t*) (content + sizeof(uint8_t));
uint8_t* pw = (uint8_t*) (content + sizeof(uint8_t) + sizeof(uint32_t));
float val = ((float) (ntohl(*number))) / pow(10, *pw);
if (*sign == 1) {
val *= -1;
}
printf("%.*f\n", *pw, val);
}
void print_string(char* content) {
printf("%s\n", content);
}
void process_topic_news_msg(struct msg message) {
char* topic = message.payload.topic;
uint8_t type = message.payload.type;
char* content = message.payload.payload;
// print topics
if (topic[MAX_TOPIC_SIZE] >= 'A' && topic[MAX_TOPIC_SIZE] <= 'z') {
// topic has 50 characters so (printf("%s\n")) won't work
printf("%.*s - ", MAX_TOPIC_SIZE, topic);
} else {
printf("%s - ", topic);
}
// print type
printf("%s - ", id_type[type]);
// print message content
switch (type) {
case INT:
print_int(content);
break;
case SHORT_REAL:
print_short_real(content);
break;
case FLOAT:
print_float(content);
break;
case STRING:
print_string(content);
break;
}
}
int process_recv_msg(struct msg message) {
uint8_t type = message.type;
switch(type) {
case WRONG:
return WRONG;
case ACK:
printf("%s", ((char*) &message) + sizeof(uint8_t));
return ACK;
case TOPIC_NEWS:
process_topic_news_msg(message);
return TOPIC_NEWS;
case KILL:
return KILL;
}
return -1;
}
int main(int argc, char* argv[]) {
setvbuf(stdout, NULL, _IONBF, BUFSIZ);
if (argc != 4) {
return 1;
}
char* id = argv[1];
char* server_ip = argv[2];
int port = atoi(argv[3]);
int sockfd, n, ret;
struct sockaddr_in serv_addr;
char buffer[BUFLEN];
fd_set read_fds, tmp_fds;
int fdmax;
FD_ZERO(&read_fds);
FD_ZERO(&tmp_fds);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
return 1;
}
int res = disable_nagle(sockfd);
if (res < 0) {
return 1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
inet_aton(server_ip, &serv_addr.sin_addr);
ret = connect(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr));
if (ret < 0) {
return 1;
}
// sending id to the server
n = send_id(sockfd, id);
if (n < 0) {
close(sockfd);
return 1;
}
FD_SET(0, &read_fds);
FD_SET(sockfd, &read_fds);
fdmax = sockfd;
while (1) {
tmp_fds = read_fds;
ret = select(fdmax + 1, &tmp_fds, NULL, NULL, NULL);
if (ret < 0) {
return 1;
}
for (int i = 0; i <= fdmax; i++) {
if (FD_ISSET(i, &tmp_fds)) {
if (i == 0) {
// STDIN
memset(buffer, 0, BUFLEN);
read_buffer(buffer);
// exit command
if (!strcmp(buffer, "exit")) {
close(sockfd);
return 0;
}
// sub / unsub command
n = send(sockfd, buffer, BUFLEN, 0);
if (n < 0) {
continue;
}
continue;
}
if (i == sockfd) {
// read from socket
struct msg message;
n = recv(sockfd, &message, sizeof(message), 0);
if (n < 0) {
return 1;
}
res = process_recv_msg(message);
if (res == KILL) {
close(sockfd);
return 0;
}
}
}
}
}
return 0;
}