-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.cpp
315 lines (219 loc) · 9.08 KB
/
server.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
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
/*
* Author: David Strauß, Mathias Paumgarten
*/
#include <sys/types.h>
#include "server.h"
#include <pthread.h>
#include <iostream>
#include <cerrno>
#include <string>
#include <sstream>
#include "client.h"
#include "info.h"
#pragma comment(lib, "ws2_32.lib")
using namespace std;
/*
* Server is initialized with IP Adress and Port.
*/
Server::Server(char* _ip, unsigned short _port):ip(_ip), port(_port) {
this->serverAddress.sin_family = AF_INET;
this->serverAddress.sin_port = htons(port);
this->serverAddress.sin_addr.s_addr = inet_addr(ip);
data = new Data();
}
Server::~Server() {
delete data;
}
/*
* Walkes through the socket initializing process by calling several subroutines.
*/
void Server::start() {
initializeWindowsSockets();
openSocket();
bindSocket();
listenSocket();
highestSocketFileDescriptor = serverSocketFileDescriptor;
isAlive = true;
}
/*
* Parameters to be passed onto the thread.
*/
struct threadParameter {
int clientAccept;
fd_set *readFileDescriptorSet;
Data *data;
char* message;
};
/*
* This function is launched as thread an handles the incomin request from each client.
* One thread per message.
*/
void *handleIncomeMessage(void *params) {
threadParameter* args;
args = (threadParameter*) params;
int socket = args->clientAccept;
char order = args->message[0];
string message(args->message);
message.erase(0, 1);
string forwardMessage;
string logInfo;
stringstream stream;
set<string> followersList;
set<string>::iterator m;
list<string>::iterator u;
list<string>* tweets;
switch (order) {
case 'n': // command for client login.
if (args->data->isClient(message)) {
if (args->data->isOnline(message)) { // if the client is already logged in.
send(args->clientAccept, "0", sizeof(char), 0);
stream << "LOGIN Error. User " << message << " is alread logged in";
Info::log(stream.str());
} else { // if the client returns but already is in the data structure.
send(args->clientAccept, "1", sizeof(char), 0);
stream << "LOGIN User " << message << " came back. Now as client " << args->clientAccept;
Info::log(stream.str());
args->data->setClientDescriptor(message, args->clientAccept);
args->data->setClientOnline(message, true);
tweets = args->data->getTweetsForClient(message);
stream.str("");
stream << "================ OLD TWEETS ===============" << endl;
u = tweets->begin();
while (u != tweets->end()) {
stream << (*u) << endl << endl;
u++;
}
stream << "=============================================";
send(args->clientAccept, stream.str().c_str(), sizeof(char) * stream.str().length(), 0);
stream.str("");
stream << "REFRESH Sending all related tweets to client " << args->clientAccept;
Info::log(stream.str());
}
} else { // a new client logged in.
args->data->addClient(message, args->clientAccept);
send(args->clientAccept, "1", sizeof(char), 0);
stream << "LOGIN client " << args->clientAccept << " logged in as " << message;
Info::log(stream.str());
}
break;
case 't': // command for a new tweet
stream << "TWEET client " << args->clientAccept << " tweets: " << message;
Info::log(stream.str());
args->data->addTweet(args->data->getNameByDescriptor(args->clientAccept), message);
forwardMessage.append(args->data->getNameByDescriptor(args->clientAccept));
forwardMessage.append(": ");
forwardMessage.append(message);
followersList = args->data->getFollowers(args->data->getNameByDescriptor(args->clientAccept));
m = followersList.begin();
while (m != followersList.end()) { // forward this tweet to all clients who follow the current client.
send(args->data->getDescriptorByName(*m), forwardMessage.c_str(), Client::TWEET_LENGTH, 0);
stream.str("");
stream << "SEND TWEET " << "forward tweet to " << (*m);
Info::log(stream.str());
m++;
}
break;
case 'f': // command for a follow
stream << "FOLLOW client " << args->clientAccept << " now follows " << message;
Info::log(stream.str());
args->data->addFollower(args->data->getNameByDescriptor(args->clientAccept), message);
break;
case 'u': // command for unfollow
stream << "UNFOLLOW client " << args->clientAccept << " stopped following " << message;
Info::log(stream.str());
args->data->removeFollower(args->data->getNameByDescriptor(args->clientAccept), message);
break;
default: // in case a unknown command is sent.
stream << "UNKNOWKN TOKEN recieved order '" << order << "' with message: " << message;
Info::log(stream.str());
break;
}
std::free(args);
return 0;
}
/*
* This function keeps tracking incoming messages via selecting over a certain amount
* of ports.
*/
void Server::selectSockets() {
int selectResult;
fd_set currentReadSet;
fd_set masterReadSet;
FD_ZERO(&masterReadSet);
FD_SET(serverSocketFileDescriptor, &masterReadSet);
sizeOfClient = sizeof(clientAddress);
while (isAlive) {
stringstream stream;
memcpy(¤tReadSet, &masterReadSet, sizeof(masterReadSet));
selectResult = select(highestSocketFileDescriptor + 1, ¤tReadSet, NULL, NULL, NULL);
if (selectResult < 0) {
cout << "Error with select!\nProgram is canceled" << endl;
closesocket(serverSocketFileDescriptor);
exit(1);
}
if (selectResult > 0) {
// Looping over all sockets
for (int i = 4; i < highestSocketFileDescriptor + 1; i++) {
// Sockets that were already accepted.
if (FD_ISSET(i, ¤tReadSet)) {
if (i != serverSocketFileDescriptor) {
threadParameter* params;
params = (threadParameter *) malloc(sizeof(threadParameter));
char buffer[Client::TWEET_LENGTH + 1];
memset(buffer, 0, sizeof(buffer));
int response = recv(i, buffer, sizeof(buffer), 0);
if (response == -1) {
closesocket(i);
FD_CLR(i, &masterReadSet);
data->removeClient(i);
stream << "DISCONNECT client " << i << " left";
Info::log(stream.str());
continue;
}
params->clientAccept = i;
params->data = data;
params->message = buffer;
// Starting the thread to handel the incomeing request.
pthread_t receiveID;
pthread_create(&receiveID, NULL, &handleIncomeMessage, params);
} else if (i == serverSocketFileDescriptor) { // Accepting new clients
int socket = accept(serverSocketFileDescriptor, (sockaddr *)&clientAddress, &sizeOfClient);
FD_SET(socket, &masterReadSet);
stream << "CONNECTION client connected on: " << socket;
Info::log(stream.str());
if (socket > highestSocketFileDescriptor) {
highestSocketFileDescriptor = socket;
}
}
}
}
}
}
}
/*
* Subroutines for sockets to work.
*/
void Server::openSocket() {
serverSocketFileDescriptor = socket(PF_INET, SOCK_STREAM, 0);
if (serverSocketFileDescriptor < 0) {
throw "Couldn't open socket";
}
}
void Server::bindSocket() {
int bindResult = bind(serverSocketFileDescriptor, (sockaddr *)&serverAddress, sizeof(serverAddress));
if (bindResult < 0) {
throw "Couldn't bind socket";
}
}
void Server::listenSocket() {
int listenResult = listen(serverSocketFileDescriptor, 3);
if (listenResult < 0) {
throw "Couldn't listen to Socket";
}
}
void Server::initializeWindowsSockets() {
WSADATA wsa;
if (WSAStartup(MAKEWORD(1, 1), &wsa)) {
throw "Couldn't initialize Windows Sockets (WSAStartup failed)";
}
}