1 |
#include "Server.h"
|
2 |
#include "../Program/WebServer/WebServer.h"
|
3 |
#include "../String/String.h"
|
4 |
#include "../System/SystemDefine.h"
|
5 |
#include "../String/ConfigInput.h"
|
6 |
#include "../System/System.h"
|
7 |
#include "../Array/ObjectArray.h"
|
8 |
#include "../NetWork/MutexHandler.h"
|
9 |
#include "../LinkedList/IntLinkedList.h"
|
10 |
#include "../Binary/BinaryInput.h"
|
11 |
#include "../Files/FileTools.h"
|
12 |
#include "NetWorkTools.h"
|
13 |
#include <iostream>
|
14 |
|
15 |
Server::Server(){}
|
16 |
Server::~Server(){}
|
17 |
|
18 |
bool quit=false;
|
19 |
|
20 |
#define SD_SEND 1
|
21 |
|
22 |
#ifdef sun
|
23 |
#define INADDR_NONE -1
|
24 |
#define INVALID_SOCKET -1
|
25 |
void *echoHandler(void *iSd);
|
26 |
#endif
|
27 |
|
28 |
#ifdef linux
|
29 |
#define INVALID_SOCKET -1
|
30 |
void *echoHandler(void *iSd);
|
31 |
#endif
|
32 |
|
33 |
#ifdef WIN32
|
34 |
DWORD WINAPI echoHandler(void* iSd);
|
35 |
DWORD nThreadID=0;
|
36 |
#endif
|
37 |
|
38 |
bool closeConnection(int sd);
|
39 |
bool echoIncomingPackets(PacketInfo *packetInfo);
|
40 |
void acceptConnections(int listeningSocket);
|
41 |
int kDefaultServerPort = 80;
|
42 |
String *ipNumber=null;
|
43 |
bool inputHttpProtocol(PacketInfo *packetInfo, char *ip, int port, int socket);
|
44 |
int handleConnection(void *iSd);
|
45 |
ObjectArray<PacketInfo> *packetInfoArray = new ObjectArray<PacketInfo>(4);
|
46 |
void *socketHandler(void *arg);
|
47 |
StringInput *stringInput=null;
|
48 |
ConfigInput *configInput=null;
|
49 |
sockaddr_in sinRemote;
|
50 |
int nAddrSize = sizeof(sinRemote);
|
51 |
|
52 |
void Server::start()
|
53 |
{
|
54 |
|
55 |
configInput = new ConfigInput();
|
56 |
configInput->readFile("data/webServerConfig.txt");
|
57 |
|
58 |
int size=packetInfoArray->size;
|
59 |
|
60 |
for ( int i=0; i<size ; i++ )
|
61 |
{
|
62 |
PacketInfo *packetInfo=new PacketInfo();
|
63 |
packetInfoArray->setObject(packetInfo,i);
|
64 |
|
65 |
pthread_t *thread = new pthread_t();
|
66 |
|
67 |
if(pthread_create(thread, null, &echoHandler, packetInfo)>=0)
|
68 |
{
|
69 |
System::println("Sucessful creation of thread...");
|
70 |
}
|
71 |
else
|
72 |
{
|
73 |
System::println("Failed to create a thread...");
|
74 |
}
|
75 |
}
|
76 |
}
|
77 |
|
78 |
//// DoWinsock /////////////////////////////////////////////////////////
|
79 |
// The module's driver function -- we just call other functions and
|
80 |
// interpret their results.
|
81 |
|
82 |
int Server::DoWinsock()
|
83 |
{
|
84 |
stringInput = new StringInput();
|
85 |
|
86 |
System::println("Establishing the listener...");
|
87 |
|
88 |
int listeningSocket = SetUpListener("194.47.143.2", htons(6666));
|
89 |
|
90 |
if (listeningSocket == INVALID_SOCKET)
|
91 |
{
|
92 |
System::println("Faild to establishing the listener...");
|
93 |
return 3;
|
94 |
}
|
95 |
|
96 |
System::println("Waiting for connections...");
|
97 |
|
98 |
// while (!quit)
|
99 |
// {
|
100 |
acceptConnections(listeningSocket);
|
101 |
// System::println("Acceptor restarting...");
|
102 |
// }
|
103 |
|
104 |
if (stringInput!=null)
|
105 |
{
|
106 |
delete stringInput;
|
107 |
}
|
108 |
|
109 |
if (configInput!=null)
|
110 |
{
|
111 |
delete configInput;
|
112 |
}
|
113 |
|
114 |
if (packetInfoArray!=null)
|
115 |
{
|
116 |
delete packetInfoArray;
|
117 |
}
|
118 |
|
119 |
return null; // warning eater
|
120 |
}
|
121 |
|
122 |
|
123 |
//// SetUpListener /////////////////////////////////////////////////////
|
124 |
// Sets up a listener on the given interface and port, returning the
|
125 |
// listening socket if successful; if not, returns INVALID_SOCKET.
|
126 |
|
127 |
int Server::SetUpListener(const char* pcAddress, int nPort)
|
128 |
{
|
129 |
int recSize=16384;
|
130 |
int sd = socket(AF_INET, SOCK_STREAM, 0);
|
131 |
|
132 |
if (sd != INVALID_SOCKET)
|
133 |
{
|
134 |
|
135 |
if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char*)&recSize, sizeof(recSize)))
|
136 |
{
|
137 |
//cerr<<"Error when calling setsockopt"<<endl;
|
138 |
//exit(-1);
|
139 |
}
|
140 |
|
141 |
sockaddr_in sinInterface;
|
142 |
sinInterface.sin_family = AF_INET;
|
143 |
sinInterface.sin_addr.s_addr = htonl(INADDR_ANY);
|
144 |
sinInterface.sin_port = nPort;
|
145 |
|
146 |
if (bind(sd, (sockaddr*)&sinInterface, sizeof(sockaddr_in)) != SOCKET_ERROR)
|
147 |
{
|
148 |
listen(sd, SOMAXCONN);
|
149 |
return sd;
|
150 |
}
|
151 |
else
|
152 |
{
|
153 |
System::println("Error binding socket");
|
154 |
}
|
155 |
}
|
156 |
|
157 |
return INVALID_SOCKET;
|
158 |
}
|
159 |
|
160 |
void acceptConnections(int listeningSocket)
|
161 |
{
|
162 |
pthread_mutex_t mut=MutexHandler::getMutex();
|
163 |
|
164 |
while (true)
|
165 |
{
|
166 |
pthread_mutex_lock(&mut);
|
167 |
|
168 |
PacketInfo *freePacketInfo=null;
|
169 |
int size=packetInfoArray->size;
|
170 |
|
171 |
for ( int i=0; i<size ; i++ )
|
172 |
{
|
173 |
PacketInfo *packetInfo=packetInfoArray->getObject(i);
|
174 |
|
175 |
if (!packetInfo->inUse)
|
176 |
{
|
177 |
freePacketInfo=packetInfo;
|
178 |
i=size;
|
179 |
}
|
180 |
}
|
181 |
|
182 |
pthread_mutex_unlock(&mut);
|
183 |
|
184 |
if (freePacketInfo!=null)
|
185 |
{
|
186 |
int so = accept(listeningSocket, (sockaddr*)&sinRemote,(socklen_t*)&nAddrSize);
|
187 |
|
188 |
if (so != 0)
|
189 |
{
|
190 |
if (inputHttpProtocol(freePacketInfo, inet_ntoa(sinRemote.sin_addr), ntohs(sinRemote.sin_port), so))
|
191 |
{
|
192 |
System::println("sucessful connections....");
|
193 |
}
|
194 |
}
|
195 |
}
|
196 |
|
197 |
}
|
198 |
}
|
199 |
|
200 |
void* echoHandler(void *iSd)
|
201 |
{
|
202 |
// pthread_mutex_t mut=MutexHandler::getMutex();
|
203 |
|
204 |
PacketInfo *packetInfo=(PacketInfo*)iSd;
|
205 |
WebServer *webServer = new WebServer(configInput);
|
206 |
|
207 |
while (true)
|
208 |
{
|
209 |
if (packetInfo->inUse)
|
210 |
{
|
211 |
webServer->init(packetInfo);
|
212 |
|
213 |
// pthread_mutex_lock(&mut);
|
214 |
|
215 |
if (close(packetInfo->socket)!=-1)
|
216 |
{
|
217 |
String socketInfo="close Socket";
|
218 |
System::println(socketInfo + packetInfo->socket);
|
219 |
}
|
220 |
|
221 |
packetInfo->inUse=false;
|
222 |
|
223 |
// pthread_mutex_unlock(&mut);
|
224 |
|
225 |
}
|
226 |
}
|
227 |
|
228 |
if (webServer!=null)
|
229 |
{
|
230 |
delete webServer;
|
231 |
}
|
232 |
|
233 |
pthread_exit((void *)0);
|
234 |
}
|
235 |
|
236 |
|
237 |
|
238 |
bool inputHttpProtocol(PacketInfo *packetInfo, char *ip, int port, int socket)
|
239 |
{
|
240 |
if (!stringInput->socketGetHttp(socket))
|
241 |
{
|
242 |
if (close(socket)!=-1)
|
243 |
{
|
244 |
System::println("closed Socket");
|
245 |
}
|
246 |
|
247 |
System::println("socketErrorInBegining");
|
248 |
return false;
|
249 |
}
|
250 |
else
|
251 |
{
|
252 |
String request=stringInput->scanWord();
|
253 |
|
254 |
String filePath=stringInput->scanWord();
|
255 |
|
256 |
String protocolType=stringInput->scanWord();
|
257 |
|
258 |
if (filePath.equals("/quit"))
|
259 |
{
|
260 |
quit=true;
|
261 |
}
|
262 |
|
263 |
packetInfo->ip->setString(ip);
|
264 |
packetInfo->port->setString(port);
|
265 |
packetInfo->socket=socket;;
|
266 |
packetInfo->request->setString(request);
|
267 |
packetInfo->filePath->setString(filePath);
|
268 |
packetInfo->protocolType->setString(protocolType);
|
269 |
|
270 |
String acceptInfo="Accepted connection from ";
|
271 |
String date=DateTime(date_);
|
272 |
String time=DateTime(time_);
|
273 |
String requestInfo="The request was ";
|
274 |
|
275 |
System::println(acceptInfo + ip + ":" + port);
|
276 |
|
277 |
String logFileOutPut=acceptInfo + packetInfo->ip + ":" + packetInfo->port + " at the time " + time + NewRow() + requestInfo + request + " " + filePath + " " + protocolType + NewRow();
|
278 |
String logPath=configInput->getData("log","logPath");
|
279 |
|
280 |
// if (logFileOutPut.writelnToFileEnd(logPath + date + ".txt")==FileExist)
|
281 |
// {
|
282 |
// System::println("logged to file..");
|
283 |
// }
|
284 |
|
285 |
|
286 |
packetInfo->inUse=true;
|
287 |
}
|
288 |
|
289 |
return true;
|
290 |
}
|
291 |
|
292 |
|
293 |
|
294 |
//// AcceptConnections /////////////////////////////////////////////////
|
295 |
// Spins forever waiting for connections. For each one that comes in,
|
296 |
// we create a thread to handle it and go back to waiting for
|
297 |
// connections. If an error occurs, we return.
|
298 |
// PacketInfo *packetInfo=inputHttpProtocol(inet_ntoa(sinRemote.sin_addr), ntohs(sinRemote.sin_port), sd);
|
299 |
// echoHandler((void*)packetInfo);
|
300 |
|
301 |
// pthread_attr_t *attr=(pthread_attr_t*)malloc(sizeof(pthread_attr_t));
|
302 |
|
303 |
// pthread_attr_setdetachstate(attr,PTHREAD_CREATE_JOINABLE);
|
304 |
|
305 |
/*
|
306 |
|
307 |
|
308 |
pthread_t *thread = new pthread_t();
|
309 |
|
310 |
if(pthread_create(thread, null, &echoHandler, packetInfo)>=0)
|
311 |
{
|
312 |
System::println("Sucessful creation of thread...");
|
313 |
}
|
314 |
else
|
315 |
{
|
316 |
System::println("Failed to create a thread...");
|
317 |
}
|
318 |
*/
|
319 |
/*
|
320 |
#ifndef linux
|
321 |
|
322 |
int socket = accept(listeningSocket, (sockaddr*)&sinRemote,&nAddrSize);
|
323 |
#endif
|
324 |
|
325 |
#ifdef WIN32
|
326 |
|
327 |
|
328 |
echoHandler((void*)packetInfo);
|
329 |
|
330 |
// LPSECURITY_ATTRIBUTES *pThreadAttributes =(LPSECURITY_ATTRIBUTES*)malloc(sizeof(LPSECURITY_ATTRIBUTES));
|
331 |
|
332 |
// HANDLE handle=CreateThread(0, 0,echoHandler, (void*)packetInfo, 0, &nThreadID);
|
333 |
|
334 |
// if (handle>=0)
|
335 |
// {
|
336 |
// // SetThreadPriority(handle,0);
|
337 |
// System::println("Sucessful creation of thread...");
|
338 |
// }
|
339 |
// else
|
340 |
// {
|
341 |
// System::println("Failed to create a thread...");
|
342 |
// }
|
343 |
|
344 |
|
345 |
//#endif
|
346 |
*/
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
// pthread_mutex_t mut=MutexHandler::getMutex();
|
352 |
|
353 |
/* PacketInfo *freePacketInfo=null;
|
354 |
|
355 |
// mainLoopSemaphore->wait();
|
356 |
|
357 |
pthread_mutex_lock(&mut);
|
358 |
|
359 |
int size=packetInfoArray->size;
|
360 |
|
361 |
for ( int i=0; i<size ; i++ )
|
362 |
{
|
363 |
PacketInfo *packetInfo=packetInfoArray->getObject(i);
|
364 |
|
365 |
if (!packetInfo->inUse)
|
366 |
{
|
367 |
freePacketInfo=packetInfo;
|
368 |
i=size;
|
369 |
}
|
370 |
}
|
371 |
|
372 |
// mainLoopSemaphore->signal();
|
373 |
*/
|
374 |
/*
|
375 |
if (freePacketInfo!=null)
|
376 |
{
|
377 |
//System::println("1");
|
378 |
*/
|
379 |
|
380 |
/* String socketInfo="socket ";
|
381 |
String kalle=socketInfo + so;
|
382 |
|
383 |
System::println(kalle);
|
384 |
|
385 |
{
|
386 |
|
387 |
WebServer *webServer = new WebServer(configInput);
|
388 |
|
389 |
PacketInfo *packetInfo=inputHttpProtocol(inet_ntoa(sinRemote.sin_addr), ntohs(sinRemote.sin_port),so);
|
390 |
|
391 |
webServer->init(packetInfo);
|
392 |
|
393 |
delete webServer;
|
394 |
*/
|
395 |
|
396 |
|
397 |
/*
|
398 |
// System::println("3");
|
399 |
// mainLoopSemaphore->wait();
|
400 |
// System::println("4");
|
401 |
if (inputHttpProtocol(freePacketInfo, inet_ntoa(sinRemote.sin_addr), ntohs(sinRemote.sin_port), so))
|
402 |
{
|
403 |
freePacketInfo->inUse=true;
|
404 |
}
|
405 |
|
406 |
// System::println("5");
|
407 |
// mainLoopSemaphore->signal();
|
408 |
// }
|
409 |
|
410 |
//System::println("6");
|
411 |
|
412 |
|
413 |
|
414 |
else
|
415 |
{
|
416 |
// System::println("accept failed");
|
417 |
// return;
|
418 |
}
|
419 |
*/
|
420 |
/*
|
421 |
}
|
422 |
// pthread_mutex_unlock(&mut);
|
423 |
|
424 |
}
|
425 |
*/
|
426 |
|
427 |
//// EchoHandler ///////////////////////////////////////////////////////
|
428 |
// Handles the incoming data by reflecting it back to the sender.
|
429 |
|
430 |
//int handleConnection(void *iSd)
|
431 |
/*
|
432 |
void* echoHandler(void *iSd)
|
433 |
{
|
434 |
// Semaphore *threadSemaphore = new Semaphore();
|
435 |
|
436 |
// threadSemaphore->attach("mainKey", 'a', 0666);
|
437 |
|
438 |
int status=1;
|
439 |
PacketInfo *packetInfo=(PacketInfo*)iSd;
|
440 |
|
441 |
WebServer *webServer=new WebServer(configInput);
|
442 |
|
443 |
pthread_mutex_t mut=MutexHandler::getMutex();
|
444 |
|
445 |
while(!quit)
|
446 |
{
|
447 |
// threadSemaphore->wait();
|
448 |
|
449 |
if (packetInfo->inUse)
|
450 |
{
|
451 |
int sd=packetInfo->socket;
|
452 |
|
453 |
// threadSemaphore->signal();
|
454 |
|
455 |
if (webServer->init(packetInfo))
|
456 |
{
|
457 |
status=0;
|
458 |
}
|
459 |
*/
|
460 |
// System::println("Shutting connection down");
|
461 |
/*
|
462 |
if (closeConnection(sd))
|
463 |
{
|
464 |
// System::println("Connection is down.");
|
465 |
}
|
466 |
else
|
467 |
{
|
468 |
status=0;
|
469 |
}
|
470 |
*/
|
471 |
/*
|
472 |
pthread_mutex_lock(&mut);
|
473 |
|
474 |
if (close(sd) == SOCKET_ERROR)
|
475 |
{
|
476 |
// System::println("shutdown failed 3...");
|
477 |
// return false;
|
478 |
}
|
479 |
else
|
480 |
{
|
481 |
String info="closeSocketFinal ";
|
482 |
System::println(info + sd);
|
483 |
}
|
484 |
|
485 |
pthread_mutex_unlock(&mut);
|
486 |
|
487 |
// threadSemaphore->wait();
|
488 |
packetInfo->inUse=false;
|
489 |
// threadSemaphore->signal();
|
490 |
}
|
491 |
else
|
492 |
{
|
493 |
// threadSemaphore->signal();
|
494 |
}
|
495 |
}
|
496 |
|
497 |
if (webServer!=null)
|
498 |
{
|
499 |
delete webServer;
|
500 |
}
|
501 |
|
502 |
pthread_exit((void *)0);
|
503 |
|
504 |
// return status;
|
505 |
}
|
506 |
*/
|
507 |
|
508 |
/*
|
509 |
#ifdef WIN32
|
510 |
|
511 |
DWORD WINAPI echoHandler(void *iSd)
|
512 |
{
|
513 |
return handleConnection(iSd);
|
514 |
}
|
515 |
|
516 |
#endif
|
517 |
|
518 |
#ifndef WIN32
|
519 |
|
520 |
|
521 |
void* echoHandler(void *iSd)
|
522 |
{
|
523 |
handleConnection(iSd);
|
524 |
pthread_exit((void *)0);
|
525 |
}
|
526 |
|
527 |
#endif
|
528 |
*/
|
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 |
|
536 |
/*
|
537 |
|
538 |
//// ShutdownConnection ////////////////////////////////////////////////
|
539 |
// Gracefully shuts the connection sd down. Returns true if we're
|
540 |
// successful, false otherwise.
|
541 |
|
542 |
bool closeConnection(int sd)
|
543 |
{
|
544 |
*/
|
545 |
// Disallow any further data sends. This will tell the other side
|
546 |
// that we want to go away now. If we skip this step, we don't
|
547 |
// shut the connection down nicely.
|
548 |
/* if (shutdown(sd, SD_SEND) == SOCKET_ERROR)
|
549 |
{
|
550 |
if (close(sd) == SOCKET_ERROR)
|
551 |
{
|
552 |
return false;
|
553 |
}
|
554 |
else
|
555 |
{
|
556 |
String info="closeSocketIN ";
|
557 |
System::println(info + sd);
|
558 |
}
|
559 |
|
560 |
return false;
|
561 |
}
|
562 |
else
|
563 |
{
|
564 |
String info="ShutDown ";
|
565 |
System::println(info + sd);
|
566 |
}
|
567 |
|
568 |
// Receive any extra data still sitting on the socket. After all
|
569 |
// data is received, this call will block until the remote host
|
570 |
// acknowledges the TCP control packet sent by the shutdown above.
|
571 |
// Then we'll get a 0 back from recv, signalling that the remote
|
572 |
// host has closed its side of the connection.
|
573 |
const int kBufferSize = 4097;
|
574 |
char acReadBuffer[kBufferSize];
|
575 |
|
576 |
while (true)
|
577 |
{
|
578 |
int nNewBytes = recv(sd, acReadBuffer, kBufferSize, 0);
|
579 |
if (nNewBytes == SOCKET_ERROR)
|
580 |
{
|
581 |
System::println("newBytesError");
|
582 |
return false;
|
583 |
}
|
584 |
else if (nNewBytes != 0)
|
585 |
{
|
586 |
String errorMessage="FYI, received ";
|
587 |
System::println(errorMessage + nNewBytes + " unexpected bytes during shutdown.");
|
588 |
}
|
589 |
else
|
590 |
{
|
591 |
System::println("Okay, we're done!");
|
592 |
break;
|
593 |
}
|
594 |
}
|
595 |
*/
|
596 |
/*
|
597 |
// Close the socket.
|
598 |
|
599 |
#ifdef WIN32
|
600 |
|
601 |
if (closesocket(sd) == SOCKET_ERROR)
|
602 |
{
|
603 |
return false;
|
604 |
}
|
605 |
|
606 |
#endif
|
607 |
|
608 |
|
609 |
#ifndef WIN32
|
610 |
|
611 |
if (close(sd) == SOCKET_ERROR)
|
612 |
{
|
613 |
// System::println("shutdown failed 3...");
|
614 |
|
615 |
return false;
|
616 |
}
|
617 |
else
|
618 |
{
|
619 |
String info="closeSocketFinal ";
|
620 |
System::println(info + sd);
|
621 |
}
|
622 |
|
623 |
#endif
|
624 |
|
625 |
return true;
|
626 |
}
|
627 |
|
628 |
*/
|
629 |
|
630 |
|
631 |
|
632 |
|