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
)
28 hostfd
= nextClient(fd
,client
);
31 log("New host connection: ");
32 if ( ! createHostByConnection(hostfd
,client
) )
34 log("Host connection denied!\n");
41 bool HostListener::createHostByConnection(int p_fd
, sockaddr_in
& client
)
43 mysqlpp::Connection
& sqlConn
= dbConn
.getConnection();
44 // look up the host in the database, get the host id
45 std::string
ip(inet_ntoa(client
.sin_addr
));
46 log("Looking up ip %s in host list\n",ip
.c_str());
49 mysqlpp::Query query
= sqlConn
.query();
50 query
<< "select id from host where ip='%0:ip'";
55 res
.disable_exceptions();
56 row
= res
.fetch_row();
57 if ( row
&& !row
.empty() )
59 dbkey_t host_id
= (dbkey_t
)(row
["id"]);
60 hostmapbykey_t::iterator i
= hosts
.find(host_id
);
63 new Host(p_fd
,host_id
,ip
,hosts
);
68 // only one connection per host, deny connection
74 // if the host was not found in the database, deny host connection
79 void HostListener::deleteAllHosts()
81 hostmapbykey_t::iterator hI
;
83 for ( hI
= hosts
.begin(); hI
!= hosts
.end(); hI
++ )
85 hI
->second
->destroy(true);