-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cpp
340 lines (301 loc) · 10.4 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#include <pthread.h>
#include <iostream>
#include <sstream>
#include <memory>
#include <set>
#include <vector>
#include <cctype>
#include <cassert>
#include "message.h"
#include "connection.h"
#include "user.h"
#include "room.h"
#include "guard.h"
#include "server.h"
using namespace std;
////////////////////////////////////////////////////////////////////////
// Server implementation data types
////////////////////////////////////////////////////////////////////////
// TODO: add any additional data types that might be helpful
// for implementing the Server member functions
class ConnectionException: public exception {
protected:
string msg; //error message
public:
ConnectionException(void) : msg("") {}
ConnectionException(const string &message) : msg(message) {}
const char *what(void) {return msg.c_str();}
};
// struct ConnInfo: represents a client connection
typedef struct ConnInfo{
Server* server;
Connection* newconn;
ConnInfo(Connection * connection, Server *server) { //constructor
this->newconn = connection;
this->server = server;
}
~ConnInfo(){
delete newconn;
}
} ConnInfo;
////////////////////////////////////////////////////////////////////////
// Client thread functions
////////////////////////////////////////////////////////////////////////
namespace {
/* check the status of a new connection
Parameter:
conn - new connection
*/
void connectionCheck(Connection* conn){ //check if there's error reveiving the message
if (conn->get_last_result() == Connection::EOF_OR_ERROR) {
throw ConnectionException();
}
}
/* send message of gor connection
Parameter:
conn - new connection
msg -error message
*/
void reportError(Connection* conn, string msg) {
Message MsgtoSend = {TAG_ERR,msg};
conn->send(MsgtoSend);
connectionCheck(conn); //check the connection status
}
/* send errpr message to the user
Parameter:
conn - new connection
msg -error message
*/
void reportGood(Connection* conn, string msg) {
Message MsgtoSend = {TAG_OK,msg};
conn->send(MsgtoSend);
connectionCheck(conn); //check the connection status
}
/* remove the trailing whitespace from the message
Parameter:
s - string
*/
string rtrim(const string &s){
const string WHITESPACE = " \n\r\t\f\v";
size_t end = s.find_last_not_of(WHITESPACE);
return (end == std::string::npos) ? "" : s.substr(0, end + 1);
}
/* check whether the message terminate with a new line character "\n"
Parameter:
conn - new connection
*/
bool msgHasNewLineTerminator(Message &msg) {
if (msg.data.find("\n") != string::npos) { //there is a ""\n"
return true;
} else { //there is no "\n" at the end
return false;
}
}
/* check whether the user name are in correct format
Parameter:
msg - the input message
*/
bool usernameCorrect(Message &msg) {
if (!msgHasNewLineTerminator(msg)) { //there is no new line terminator in message
return false;
} else {
if ( (rtrim(msg.data).find_first_not_of("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") == string::npos) && (msg.data.length() >= 1)) {
return true;
} else{ //incorrect format
return false;
}
}
}
/* chat with the receiver
Parameter:
conn - connection
server - server
user - user
*/
void chat_with_receiver(Server* server, Connection* conn, User* user) {
Message request;
//check if the connection receive the message request
if (!conn->receive(request)) { //if the connection does not receive message
reportError(conn, "receiver doesn't receive message");
throw ConnectionException();
}
//the message request is not in the correct format
if (!msgHasNewLineTerminator(request)|| request.tag != TAG_JOIN) {
reportError(conn, "receiver message request is in invalid format");
throw ConnectionException();
}
//creat chat room
Room *chatroom = server->find_or_create_room(rtrim(request.data));
chatroom->add_member(user); //add users
conn->send(Message(TAG_OK, ""));
if (conn->get_last_result() == Connection::EOF_OR_ERROR) { //connection error
chatroom->remove_member(user); //remove the suer from the chat room
throw ConnectionException();
}
//loop to retrieve message
while (true) {
Message* resmsg = user->mqueue.dequeue(); //get a message by dequeue from user queue
if (resmsg != nullptr) {
conn->send(*resmsg);
delete resmsg;
}
if (conn->get_last_result() == Connection::EOF_OR_ERROR) {
break;
}
}
chatroom->remove_member(user);
}
/* chat with the sender
Parameter:
conn - connection
server - server
username - username for sender
*/
void chat_with_sender( Server* thisServer, Connection* conn, string username) {
Message resmsg; //the response message
Room* chatroom = nullptr; //room to chat
//loop to chat with the sender
while (true) {
conn->receive(resmsg);
connectionCheck(conn); //check the connection
//tag can be quit, leave, sendall, join
if (conn->get_last_result() != Connection::INVALID_MSG) { //the message is valid:
if (resmsg.tag == TAG_QUIT) { //tag is quit
reportGood(conn,"");
throw ConnectionException();
} else if (resmsg.tag == TAG_JOIN) { //tag is join
if (! msgHasNewLineTerminator(resmsg)) { //if the message format is wrong
reportError(conn, "sender send invalid room name");
} else { //the room name format is right
chatroom = thisServer->find_or_create_room(rtrim(resmsg.data));
reportGood(conn,""); //report connection status
}
} else if (resmsg.tag == TAG_LEAVE) { //tag is to leava
if (chatroom != nullptr) {
chatroom = nullptr;
reportGood(conn,"");
} else {
reportError(conn, "sender is not in the room cannot leave");
}
} else if (resmsg.tag == TAG_SENDALL) { //tag is to send to sendall
if (chatroom != nullptr) {
if (!msgHasNewLineTerminator(resmsg)) {
reportError(conn, "invalid sendall message format");
} else {
chatroom->broadcast_message(username, rtrim(resmsg.data));
reportGood(conn,"");
}
} else {//the room is not exist, invalid
reportError(conn, "sender is not in the room cannot send message");
}
} else { //the tag is invalid
reportError(conn, "invalid tag");
}
} else { //the message is not valid, report error
reportError(conn, "invalid message format");
}
}
}
void *worker(void *arg) {
pthread_detach(pthread_self());
// TODO: use a static cast to convert arg from a void* to
// whatever pointer type describes the object(s) needed
// to communicate with a client (sender or receiver)
ConnInfo* info = static_cast<ConnInfo *>(arg); //get connection info
Connection* newConn = info->newconn; //new connection
Server* newServer = info->server; //new server
// TODO: read login message (should be tagged either with
// TAG_SLOGIN or TAG_RLOGIN), send resmsg
User* newUser = new User("");
Message inputMsg; //the message received by the server
try { //receive the message from connection
newConn->receive(inputMsg);
connectionCheck(newConn); //check the connection is not EOF
//check for invalid message
if (newConn->get_last_result() == Connection::INVALID_MSG) {
reportError(newConn, "MESSAGE INVALID");
}
//check the input username
if (!usernameCorrect(inputMsg)) { //the user name into does not have correct format
reportError(newConn, "USERNAME INVALID");
throw ConnectionException();
} else { //the user name is in correct format
reportGood(newConn,"");
newUser->username = rtrim(inputMsg.data); //remove the whitespace from the message
}
// TODO: depending on whether the client logged in as a sender or
// receiver, communicate with the client (implementing
// separate helper functions for each of these possibilities
// is a good idea)
//deal with sender and receiver login message
if (inputMsg.tag == TAG_RLOGIN) { //receiver login message
chat_with_receiver(newServer, newConn, newUser); //chat with receiver here
} else if (inputMsg.tag == TAG_SLOGIN) { //sender login message
chat_with_sender(newServer, newConn, newUser->username); //chat with sender here, not creating user for sender
} else { //login tag has problem
reportError(newConn, "LOGIN TAG INVALID");
throw ConnectionException();
}
} catch (ConnectionException& e) {
free(info);
return nullptr;
}
free(info);
return nullptr;
}
}
////////////////////////////////////////////////////////////////////////
// Server member function implementation
////////////////////////////////////////////////////////////////////////
Server::Server(int port)
: m_port(port)
, m_ssock(-1) {
// TODO: initialize mutex
pthread_mutex_init(&m_lock, NULL);
}
Server::~Server() {
// TODO: destroy mutex
close (m_ssock);
pthread_mutex_destroy(&m_lock);
}
bool Server::listen() {
// TODO: use open_listenfd to create the server socket, return true
// if successful, false if not
int server_fd = open_listenfd(std::to_string(m_port).c_str());
if (server_fd < 0) {
return false;
}else{
m_ssock = server_fd;
return true;
}
}
void Server::handle_client_requests() {
// TODO: infinite loop calling accept or Accept, starting a new
// pthread for each connected client
vector<ConnInfo*> threadArgs;
while(true){
int client_fd = Accept(m_ssock,NULL,NULL);
Connection* new_connection = new Connection(client_fd);
ConnInfo* new_info = new ConnInfo(new_connection, this);
threadArgs.push_back(new_info);
pthread_t thr_id;
if (pthread_create(&thr_id, NULL, worker, new_info) != 0) {
fprintf(stderr, "%s\n","Error occured while processing client request");
exit(-1);
}
}
}
Room *Server::find_or_create_room(const string &room_name) {
// TODO: return a pointer to the unique Room object representing
// the named chat room, creating a new one if necessary
Guard g(m_lock);
RoomMap::iterator target_room = m_rooms.find(room_name);
if (target_room == m_rooms.end()){
//create a new room
Room * new_room = new Room(room_name);
m_rooms.insert({room_name,new_room});
return new_room;
}else{
return target_room->second;
}
}