-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsessionMGR.cpp
More file actions
251 lines (217 loc) · 7.32 KB
/
sessionMGR.cpp
File metadata and controls
251 lines (217 loc) · 7.32 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
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
/******************************************************************************
* $Revision$ *
* *
* Copyright (C) 2007 Ignasi Barri Vilardell *
* Copyright (C) 2007 Alberto Montañola Lacort *
* Copyright (C) 2007 Josep Rius Torrento *
* See the file AUTHORS for more info *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA *
* 02110-1301,USA. *
* *
* Please see the file COPYING for the full license. *
* *
*******************************************************************************
* *
* Xarxes II *
* Màster en Enginyeria de Programari Lliure *
* *
* Pràctica I *
* *
******************************************************************************/
#include <iostream>
#include <string>
#include <exception>
#include <queue>
#include <map>
#include <sstream>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "sessionMGR.h"
#include "server.h"
//BEGIN clientSession
clientSession::clientSession(int socket,U32 addr) {
_socket=socket;
_addr=addr;
_status=kNew;
}
int clientSession::fileno() const {
return _socket;
}
const char * clientSession::getName() const {
if(strlen(_name.c_str())==0) {
return NULL;
}
return _name.c_str();
}
void clientSession::setName(const char * name) {
_name=name;
}
void clientSession::setPort(U16 port) {
_udpport=port;
}
U16 clientSession::getUdpPort() {
return _udpport;
}
const char * clientSession::getAddr() const {
in_addr cip;
cip.s_addr=htonl(_addr);
return inet_ntoa(cip);
}
U32 clientSession::getIp() const {
return _addr;
}
void clientSession::rcvHello() {
if(_status!=kNew) throw protocolViolation("Hello was already sent");
_status=kIdent;
}
void clientSession::rcvRegister() {
if(_status==kNew) throw protocolViolation("Hello was not sent");
_status=kRegister;
}
bool clientSession::isHallowed() const {
return _status==kIdent;
}
bool clientSession::isRegistered() const {
return _status==kRegister;
}
void clientSession::sendall(const char * msg) {
int size=strlen(msg);
int total=0;
int sn;
while(total < size) {
sn=send(_socket,msg+total,size-total,0);
if(sn==-1) throw errorException("send");
total += sn;
}
}
void clientSession::sendOk(const char * msg) {
std::string buf = protocol::ok;
//buf += msg;
buf += protocol::sep;
sendall(buf.c_str());
}
void clientSession::senderror(const char * msg) {
std::string buf = protocol::error;
//buf += msg;
buf += protocol::sep;
sendall(buf.c_str());
}
void clientSession::senddif(const char * msg) {
std::string buf = protocol::bcast;
buf += " ";
buf += msg;
buf += protocol::sep;
sendall(buf.c_str());
}
void clientSession::sendanswer(const char * msg) {
std::string buf = protocol::answer;
buf += " ";
buf += msg;
buf += protocol::sep;
sendall(buf.c_str());
}
//END clientSession
//BEGIN sessionMGR
sessionMGR::sessionMGR(server * parent) {
_parent=parent;
}
sessionMGR::~sessionMGR() {
std::map<int,clientSession *>::iterator iter;
while(!_sockets.empty()) {
iter = _sockets.begin();
remove(iter->second);
}
}
std::map<int,clientSession *> & sessionMGR::getAllClients() {
return _sockets;
}
void sessionMGR::add(int sock,U32 addr) {
clientSession * cli = new clientSession(sock,addr);
/*if(_clients.find(addr) != _clients.end()) {
printf("Found! deleting...\n");
try {
_clients[addr]->senddif("Sorry, only one connection per ip is accepted!");
} catch(errorException & e) {
std::cout<<"Exception sending data..."<<e.what()<<std::endl;
}
remove(_clients[addr]);
}*/
//_clients[addr]=cli;
if(_sockets.find(sock) != _sockets.end()) {
//This should not happen never!!!
std::cout<<"Something very strange and rare is happending!!!"<<std::endl;
remove(_sockets[sock]);
}
_sockets[sock]=cli;
_parent->_select.register2(sock);
}
void sessionMGR::remove(clientSession * client,bool closeme) {
printf("close %i\n",client->fileno());
if(closeme && close(client->fileno())!=0) throw errorException("close client");
std::cout<<"Client "<<client->getAddr()<<" disconnected"<<std::endl;
_parent->_select.unregister(client->fileno());
if(client->getName()!=NULL) {
std::string out=client->getName();
out+=" has left the chat";
_parent->broadcast(out.c_str(),client);
_nicks.erase(client->getName());
}
//_clients.erase(client->getIp());
_sockets.erase(client->fileno());
delete client;
}
void sessionMGR::remove(int sock) {
remove((clientSession *)find(sock));
}
clientSession * sessionMGR::find(int sock) {
return _sockets[sock];
}
void sessionMGR::register2(clientSession * client,const char * nick,int port) {
if(_nicks.find(nick) != _nicks.end()) {
throw protocolViolation("NickAlreadyExists!");
}
std::string msg;
if(client->getName()!=NULL && _nicks.find(client->getName()) != _nicks.end()) {
_nicks.erase(client->getName());
msg=client->getName();
msg+=" is now know as ";
msg+=nick;
_parent->broadcast(msg.c_str(),client);
} else {
msg=nick;
msg+=" has joined the chat";
_parent->broadcast(msg.c_str(),client);
}
client->rcvRegister();
client->setName(nick);
client->setPort(port);
_nicks[client->getName()]=client;
}
std::string & sessionMGR::findAddress(const char * nick) {
if(_nicks.find(nick) != _nicks.end()) {
_tmp_msg=_nicks[nick]->getAddr();
_tmp_msg+=" ";
std::ostringstream o;
if (!(o << _nicks[nick]->getUdpPort())) throw protocolViolation("Error converting an unsigned int to a string");
_tmp_msg+=o.str();
} else {
_tmp_msg="null 0";
}
return _tmp_msg;
}
//END sessionMGR