summaryrefslogtreecommitdiffstats
path: root/src/Core
diff options
context:
space:
mode:
Diffstat (limited to 'src/Core')
-rw-r--r--src/Core/ConnectionManager.cpp63
-rw-r--r--src/Core/ConnectionManager.h19
2 files changed, 29 insertions, 53 deletions
diff --git a/src/Core/ConnectionManager.cpp b/src/Core/ConnectionManager.cpp
index 5ff5e29..020c8a7 100644
--- a/src/Core/ConnectionManager.cpp
+++ b/src/Core/ConnectionManager.cpp
@@ -23,58 +23,33 @@
#include <Net/Listener.h>
#include <unistd.h>
-#include <iostream>
-
namespace Mad {
namespace Core {
void ConnectionManager::refreshPollfds() {
pollfds.clear();
pollfdMap.clear();
-
+
for(std::list<Net::Listener*>::iterator listener = listeners.begin(); listener != listeners.end(); ++listener) {
std::vector<struct pollfd> fds = (*listener)->getPollfds();
-
+
for(std::vector<struct pollfd>::iterator fd = fds.begin(); fd != fds.end(); ++fd) {
pollfds.push_back(*fd);
pollfdMap.insert(std::make_pair(fd->fd, &pollfds.back().revents));
}
}
-
+
for(std::list<Net::ServerConnection*>::iterator con = daemonConnections.begin(); con != daemonConnections.end(); ++con) {
pollfds.push_back((*con)->getPollfd());
pollfdMap.insert(std::make_pair(pollfds.back().fd, &pollfds.back().revents));
}
-
+
for(std::list<Net::ServerConnection*>::iterator con = clientConnections.begin(); con != clientConnections.end(); ++con) {
pollfds.push_back((*con)->getPollfd());
pollfdMap.insert(std::make_pair(pollfds.back().fd, &pollfds.back().revents));
}
}
-void ConnectionManager::receiveHandler(Net::Connection *connection, const Net::Packet &packet) {
- switch(packet.getType()) {
- case Net::Packet::TYPE_UNKNOWN:
- break;
- case Net::Packet::TYPE_DEBUG:
- std::cout << "Received debug packet." << std::endl;
- std::cout << " Request ID: 0x" << std::hex << std::uppercase << packet.getRequestId() << std::dec << std::endl;
- break;
- case Net::Packet::TYPE_PING:
- connection->send(Net::Packet(Net::Packet::TYPE_PONG, packet.getRequestId(), packet.getData(), packet.getLength()));
- break;
- case Net::Packet::TYPE_PONG:
- // TODO: Pong!
- break;
- case Net::Packet::TYPE_DISCONNECT_REQ:
- connection->send(Net::Packet(Net::Packet::TYPE_DISCONNECT_REP, packet.getRequestId()));
- connection->disconnect();
- break;
- case Net::Packet::TYPE_DISCONNECT_REP:
- connection->disconnect();
- }
-}
-
ConnectionManager::ConnectionManager() {
try {
// TODO: Get listener addresses from config
@@ -83,72 +58,72 @@ ConnectionManager::ConnectionManager() {
catch(Net::Exception &e) {
// TODO: Log error
}
-
+
refreshPollfds();
}
ConnectionManager::~ConnectionManager() {
for(std::list<Net::ServerConnection*>::iterator con = daemonConnections.begin(); con != daemonConnections.end(); ++con)
delete *con;
-
+
for(std::list<Net::ServerConnection*>::iterator con = clientConnections.begin(); con != clientConnections.end(); ++con)
delete *con;
}
void ConnectionManager::run() {
// TODO: Logging
-
+
for(std::list<Net::ServerConnection*>::iterator con = daemonConnections.begin(); con != daemonConnections.end();) {
if((*con)->isConnected()) {
std::map<int,const short*>::iterator events = pollfdMap.find((*con)->getSocket());
-
+
if(events != pollfdMap.end())
(*con)->sendReceive(*events->second);
else
(*con)->sendReceive();
-
+
++con;
}
else {
- std::cout << "A daemon connection was dropped." << std::endl;
+ requestManager.unregisterConnection(*con);
delete *con;
daemonConnections.erase(con++);
}
}
-
+
for(std::list<Net::ServerConnection*>::iterator con = clientConnections.begin(); con != clientConnections.end();) {
if((*con)->isConnected()) {
std::map<int,const short*>::iterator events = pollfdMap.find((*con)->getSocket());
-
+
if(events != pollfdMap.end())
(*con)->sendReceive(*events->second);
else
(*con)->sendReceive();
-
+
++con;
}
else {
- std::cout << "A client connection was dropped." << std::endl;
+ requestManager.unregisterConnection(*con);
delete *con;
clientConnections.erase(con++);
}
}
-
+
for(std::list<Net::Listener*>::iterator listener = listeners.begin(); listener != listeners.end(); ++listener) {
Net::ServerConnection *con;
-
+
while((con = (*listener)->getConnection(pollfdMap)) != 0) {
if(con->isDaemonConnection()) {
daemonConnections.push_back(con);
- con->signalReceive().connect(sigc::mem_fun(this, &ConnectionManager::receiveHandler));
+ requestManager.registerConnection(con);
}
else {
clientConnections.push_back(con);
- con->signalReceive().connect(sigc::mem_fun(this, &ConnectionManager::receiveHandler));
+ requestManager.registerConnection(con);
}
}
}
-
+
refreshPollfds();
}
diff --git a/src/Core/ConnectionManager.h b/src/Core/ConnectionManager.h
index 608ec27..c7fe669 100644
--- a/src/Core/ConnectionManager.h
+++ b/src/Core/ConnectionManager.h
@@ -24,6 +24,7 @@
#include <vector>
#include <map>
#include <poll.h>
+#include <Common/RequestManager.h>
namespace Mad {
@@ -41,27 +42,27 @@ class ConnectionManager {
// Prevent shallow copy
ConnectionManager(const ConnectionManager &o);
ConnectionManager& operator=(const ConnectionManager &o);
-
+
+ Common::RequestManager requestManager;
+
std::list<Net::Listener*> listeners;
-
+
std::list<Net::ServerConnection*> daemonConnections;
std::list<Net::ServerConnection*> clientConnections;
-
+
std::vector<struct pollfd> pollfds;
std::map<int,const short*> pollfdMap;
-
+
void refreshPollfds();
-
- void receiveHandler(Net::Connection *connection, const Net::Packet &packet);
-
+
public:
ConnectionManager();
virtual ~ConnectionManager();
-
+
bool wait(int timeout) {
return (poll(pollfds.data(), pollfds.size(), timeout) > 0);
}
-
+
void run();
};