-
Notifications
You must be signed in to change notification settings - Fork 12
/
ListeningSocket.cc
123 lines (94 loc) · 3.57 KB
/
ListeningSocket.cc
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
/*
(C) Copyright 2005, ActivMedia Robotics LLC <http://www.activmedia.com>
(C) Copyright 2006-2010 MobileRobots, Inc. <http://www.mobilerobots.com>
(C) Copyright 2011-2015 Adept MobileRobots <http://www.mobilerobots.com>
(C) Copyright 2016-2017 Omron Adept Technologies
This 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 software 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 software; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "ListeningSocket.hh"
#include "MobileSim.hh"
#include "RobotFactory.hh"
ListeningSocket::ListeningSocket()
{
//ArLog::log(ArLog::Normal, "ListeningSocket ctor");
}
ListeningSocket::~ListeningSocket()
{
//ArLog::log(ArLog::Normal, "ListeningSocket dtor");
}
ArSocket *ListeningSocket::init(int port, RobotFactory *parentFactory, const char *listenAddress/* = NULL*/)
{
myPort = port;
myFactory = parentFactory;
if(mySocket.open(myPort, ArSocket::TCP, listenAddress))
{
mySocket.setNonBlock();
mySocket.setReuseAddress();
//ArLog::log(ArLog::Normal, "ListeningSocket: Opened socket for RobotFactory(%s)", myFactory->getModelName().c_str());
return &mySocket;
}
else
{
ArLog::log(ArLog::Normal, "ListeningSocket: Error %d opening socket on port %d for RobotFactory(%s): %s\n",
mySocket.getError(), myPort, myFactory->getModelName().c_str(), mySocket.getErrorStr().c_str());
return NULL;
}
}
void * ListeningSocket::runThread(void *arg)
{
threadStarted();
while (myRunning) {
runLoop();
//ArUtil::sleep(1); // TODO: For how long?
}
threadFinished();
return NULL;
} // end method runThread
void ListeningSocket::runLoop()
{
//ArLog::log(ArLog::Normal, "ListeningSocket::runLoop()");
//ArLog::log(ArLog::Normal, "ListeningSocket: listening on port (%d), fd = %d", myPort, mySocket.getFD());
//ArLog::log(ArLog::Normal, "ListeningSocket: about to 'select', blocking");
fd_set readfds;
FD_SET( mySocket.getFD(), &readfds );
int maxfd = mySocket.getFD();
int activity = select( maxfd + 1, &readfds, NULL, NULL, NULL );
if (activity < 0)
ArLog::log(ArLog::Normal, "ListeningSocket: select error");
if (activity == 0)
return;
if( FD_ISSET( mySocket.getFD(), &readfds ) )
{
//ArLog::log(ArLog::Normal, "ListeningSocket: Activity on ListeningSocket. Creating new connection", mySocket.getFD());
ArSocket *clientSocket = new ArSocket;
if(!mySocket.accept(clientSocket))
{
ArLog::log(ArLog::Normal, "ListeningSocket: error accepting client:");
ArLog::log(ArLog::Normal, mySocket.getErrorStr().c_str());
delete clientSocket;
return;
}
if(!clientSocket->isOpen())
{
ArLog::log(ArLog::Normal, "ListeningSocket: error accepting client: new client socket not open.");
delete clientSocket;
return;
}
//ArLog::log(ArLog::Normal, "ListeningSocket: Accepted client. Sending to RobotFactory...");
myFactory->acceptNewClientFromListenerThread(clientSocket);
}
else
{
//ArLog::log(ArLog::Normal, "ListeningSocket: No activity on ListeningSocket");
}
}