-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkClient.cpp
executable file
·348 lines (320 loc) · 11.3 KB
/
NetworkClient.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
341
342
343
344
345
346
347
348
/*
* File: NetworkClient.cpp
* Author: scribble
*
* Created on November 24, 2012, 3:51 PM
*
* Parts of this code has been taken from boost.org under the Boost Software License Version 1.0:
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#include "NetworkClient.h"
#include "Sender.h"
NetworkClient::NetworkClient(boost::asio::io_service& io_service, tcp::resolver::iterator endpoint_iterator, ScribbleArea* scribbleArea) : io_service_(io_service),
socket_(io_service), scribbleArea(scribbleArea), connected(false), connectionFailed(false), timer_(io_service)
{
tcp::endpoint endpoint = *endpoint_iterator;
socket_.async_connect(endpoint, boost::bind(&NetworkClient::handle_connect, this, boost::asio::placeholders::error, ++endpoint_iterator));
//Closing the connection if not connected within 5 seconds
timer_.expires_from_now(boost::posix_time::seconds(5));
timer_.async_wait(boost::bind(&NetworkClient::ServerConnectionFailed, this));
}
void NetworkClient::ServerConnectionFailed()
{
if ( !connected )
{
close();
}
}
void NetworkClient::write(const RequestMessage& msg)
{
//RequestMessage encodeMessage(std::string line)
io_service_.post(boost::bind(&NetworkClient::do_write, this, msg));
}
void NetworkClient::close()
{
io_service_.post(boost::bind(&NetworkClient::do_close, this));
}
void NetworkClient::handle_connect(const boost::system::error_code& error, tcp::resolver::iterator endpoint_iterator)
{
if ( !error )
{
connected = true;
connectionFailed = false;
boost::asio::async_read(socket_, boost::asio::buffer(read_msg_.data(), RequestMessage::header_length), boost::bind(&NetworkClient::handle_read_header, this, boost::asio::placeholders::error));
}
else if ( endpoint_iterator != tcp::resolver::iterator() )
{
connected = false;
connectionFailed = true;
std::cout << "Fail connecting to server... Try again later" << std::endl;
socket_.close();
tcp::endpoint endpoint = *endpoint_iterator;
socket_.async_connect(endpoint, boost::bind(&NetworkClient::handle_connect, this, boost::asio::placeholders::error, ++endpoint_iterator));
}
else
{
std::cout << "Fail connecting to server... Server is not available" << std::endl;
connectionFailed = true;
}
}
void NetworkClient::handle_read_header(const boost::system::error_code& error)
{
if ( !error && read_msg_.decode_header() )
{
boost::asio::async_read(socket_, boost::asio::buffer(read_msg_.body(), read_msg_.body_length()), boost::bind(&NetworkClient::handle_read_body, this, boost::asio::placeholders::error));
}
else
{
do_close();
}
}
void NetworkClient::handle_read_body(const boost::system::error_code& error)
{
if ( !error )
{
//std::cout.write(read_msg_.body(), read_msg_.body_length());
std::string msg(read_msg_.body(), read_msg_.body_length());
decodeRequest(msg);
boost::asio::async_read(socket_, boost::asio::buffer(read_msg_.data(), RequestMessage::header_length), boost::bind(&NetworkClient::handle_read_header, this, boost::asio::placeholders::error));
}
else
{
do_close();
}
}
void NetworkClient::do_write(RequestMessage msg)
{
bool write_in_progress = !write_msgs_.empty();
write_msgs_.push_back(msg);
if ( !write_in_progress )
{
boost::asio::async_write(socket_, boost::asio::buffer(write_msgs_.front().data(), write_msgs_.front().length()), boost::bind(&NetworkClient::handle_write, this, boost::asio::placeholders::error));
}
}
void NetworkClient::handle_write(const boost::system::error_code& error)
{
if ( !error )
{
write_msgs_.pop_front();
if ( !write_msgs_.empty() )
{
boost::asio::async_write(socket_, boost::asio::buffer(write_msgs_.front().data(), write_msgs_.front().length()), boost::bind(&NetworkClient::handle_write, this, boost::asio::placeholders::error));
}
}
else
{
do_close();
}
}
tcp::socket& NetworkClient::getSocket()
{
return socket_;
}
void NetworkClient::do_close()
{
scribbleArea->setNetworkActivity(ScribbleArea::NetworkActivity::LOST_NETWORK_CONNECTION);
std::cout << "Socket closed" << std::endl;
socket_.close();
}
void NetworkClient::decodeRequest(std::string msg)
{
//Removing the leading & and the lagging \n
std::string received(msg, 1, msg.size() - 2);
//Separating the received message
std::vector<std::string> info;
boost::split(info, received, boost::is_any_of(Sender::separator));
int choice = atoi(info[0].c_str());
//std::cout << "Choice: " << choice << std::endl;
switch ( choice )
{
case Sender::REQUEST_OWNERSHIP:
{
//We have received a message with who is the current owner
//std::cout << "REQUEST_OWNERSHIP" << std::endl;
if ( info.size() > 1 && info[1] == scribbleArea->getSender()->getUsername() )
{
std::cout << "I got ownership" << std::endl;
scribbleArea->setOwnershipMe();
}
else
{
std::cout << "I didn't get the ownership, somebody else got it" << std::endl;
scribbleArea->setOwnershipTaken();
}
break;
}
case Sender::RELEASE_OWNERSHIP:
{
//File ownership is available
std::cout << "RELEASE_OWNERSHIP, ownership is free fro grabs" << std::endl;
scribbleArea->setOwnershipFree();
break;
}
case Sender::NEW_PATH:
{
//Create new path
//std::cout << "NEW_PATH" << std::endl;
int pathID = atoi(info[1].c_str());
bool mode = ( info[2] == "1" ) ? true : false;
int colorInt = atoi(info[3].c_str());
int page = atoi(info[4].c_str());
int width = atoi(info[5].c_str());
int colorR = ( ( colorInt >> 16 ) & 0xFF );
int colorG = ( ( colorInt >> 8 ) & 0xFF );
int colorB = ( ( colorInt ) & 0xFF );
Color color(colorR, colorG, colorB);
Path* path = new Path(mode, color, width, pathID);
scribbleArea->setNetworkPage(page);
scribbleArea->setNetworkPath(path);
break;
}
case Sender::ADD_POINTS:
{
//Add points to the current path
//std::cout << "ADD_POINTS" << std::endl;
std::vector<std::string> p;
boost::split(p, info[1], boost::is_any_of(Sender::separatorPoints));
for ( int i = 0; i < p.size(); )
{
int x = atoi(p[i++].c_str());
int y = atoi(p[i++].c_str());
Point* point = new Point(x, y);
scribbleArea->addNetworkPoint(point);
}
break;
}
case Sender::END_PATH:
{
//End current network path
//std::cout << "END_PATH" << std::endl;
scribbleArea->endNetworkPath();
break;
}
case Sender::UNDO:
{
//Undo last action, this will simply call the UNDO function of the ScribbleArea
//std::cout << "UNDO" << std::endl;
int page = atoi(info[1].c_str());
scribbleArea->undo(page);
break;
}
case Sender::REDO:
{
//std::cout << "REDO" << std::endl;
int page = atoi(info[1].c_str());
scribbleArea->redo(page);
break;
}
case Sender::DELETE_PATH:
{
//Delete path, this will find the path to be deleted and completely remove it from the drawings
std::cout << "DELETE_PATH, not implemented" << std::endl;
//TODO call the delete function of the scribble area... For now we use white color as eraser, but keeping this if that would change
break;
}
case Sender::LOGIN:
{
if ( info.size() > 1 && info[1] == "1" )
{
std::cout << "LOGIN FINE!!!!!! NEED TO SET some flag" << std::endl;
scribbleArea->getSender()->setLogin(true);
scribbleArea->setNetworkActivity(ScribbleArea::NetworkActivity::LOGIN_OK);
}
else
{
std::cout << "LOGIN FAILED!!!!!! NEED TO DISPLAY AN ERROR MESSAGE" << std::endl;
scribbleArea->getSender()->setLogin(false);
scribbleArea->setNetworkActivity(ScribbleArea::NetworkActivity::LOGIN_FAILED);
}
break;
}
case Sender::LOGOUT:
{
scribbleArea->getSender()->setLogin(false);
//std::cout << "LOGOUT" << std::endl;
break;
}
case Sender::GET_FILES_LIST:
{
std::string file = info[1];
scribbleArea->addFileOnServer(file);
std::cout << "File: " << file << "\n";
break;
}
case Sender::GET_FILE_LIST_CLEAR:
{
scribbleArea->clearFilesOnServer();
break;
}
case Sender::GET_FILE_LIST_COMPLETE:
{
scribbleArea->setNetworkActivity(ScribbleArea::NetworkActivity::FILES_LIST_AVAILABLE);
break;
}
case Sender::DOWNLOAD_FILE:
{
//TOCONF Not in use, file download is done through other threads.
std::cout << "DOWNLOAD_FILE, not implemented!!!" << std::endl;
break;
}
case Sender::CLEAR_ALL:
{
int page = atoi(info[1].c_str());
scribbleArea->clearAll(page);
break;
}
case Sender::DOWNLOAD_FILE_DONE:
{
std::cout << "DOWNLOAD_FILE COMPLETED" << std::endl;
break;
}
case Sender::CREATE_NEW_FILE:
{
int result = atoi(info[1].c_str());
if ( result == FILE_WAS_CREATED )
{
scribbleArea->getSender()->sendDownloadFile(info[2]);
}
else if ( result == FILE_EXISTS )
{
//TOCONF should we download the file that exists on the server?
scribbleArea->setNetworkActivity(ScribbleArea::NetworkActivity::NEW_FILE_ALREADY_EXISTS);
}
else if ( result == FILE_CREATION_FAILED )
{
scribbleArea->setNetworkActivity(ScribbleArea::NetworkActivity::NEW_FILE_CREATION_FAILED);
}
else
{
std::cerr << "Unknown Create new file code... was will not be created in the server\n";
}
break;
}
default:
std::cout << "Should not be here " << choice << std::endl;
}
}
void NetworkClient::sendMessage(std::string line)
{
//We need to add an end of line to the message
line += '\n';
RequestMessage msg;
msg.body_length(std::strlen(line.c_str()));
std::memcpy(msg.body(), line.c_str(), msg.body_length());
msg.encode_header();
write(msg);
}
ScribbleArea* NetworkClient::getScribbleArea()
{
return scribbleArea;
}
bool NetworkClient::isConnected()
{
return connected;
}
bool NetworkClient::failedConnecting()
{
return connectionFailed;
}