1 #include "HostListener.h"
3 namespace remote
{ namespace diku_mcs
{
5 HostListener::HostListener(unsigned int port
)
7 openServerSocket( server
,
13 log("Listening for host connections on port %u.\n",port
);
16 HostListener::~HostListener()
21 void HostListener::handleEvent(short events
)
23 struct sockaddr_in client
;
26 if (events
& POLLIN
|| events
& POLLPRI
) {
27 hostfd
= nextClient(fd
, client
);
29 log("New host connection\n");
30 if (!createHostByConnection(hostfd
,client
)) {
31 log("Host connection denied!\n");
38 bool HostListener::createHostByConnection(int p_fd
, sockaddr_in
& client
)
40 mysqlpp::Connection
& sqlConn
= dbConn
.getConnection();
41 // look up the host in the database, get the host id
42 std::string
ip(inet_ntoa(client
.sin_addr
));
43 log("Looking up ip %s in host list\n", ip
.c_str());
46 mysqlpp::Query query
= sqlConn
.query();
48 query
<< "select id from host where ip = " << mysqlpp::quote
<< ip
;
51 res
.disable_exceptions();
52 row
= res
.fetch_row();
53 if (row
&& !row
.empty()) {
54 dbkey_t host_id
= (dbkey_t
) (row
["id"]);
55 hostmapbykey_t::iterator i
= hosts
.find(host_id
);
56 if (i
== hosts
.end()) {
57 new Host(p_fd
, host_id
, ip
, hosts
);
61 // only one connection per host, deny connection
66 // if the host was not found in the database, deny host connection
71 void HostListener::deleteAllHosts()
73 hostmapbykey_t::iterator hI
;
75 for (hI
= hosts
.begin(); hI
!= hosts
.end(); hI
++) {
76 hI
->second
->destroy(true);