diff options
Diffstat (limited to 'src/Core/ConnectionManager.cpp')
-rw-r--r-- | src/Core/ConnectionManager.cpp | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/src/Core/ConnectionManager.cpp b/src/Core/ConnectionManager.cpp new file mode 100644 index 0000000..3b99875 --- /dev/null +++ b/src/Core/ConnectionManager.cpp @@ -0,0 +1,115 @@ +/* + * ConnectionManager.cpp + * + * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> + * + * This program 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 3 of the License, or + * (at your option) any later version. + * + * This program 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 program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "ConnectionManager.h" +#include <Net/ServerConnection.h> +#include <Net/Packet.h> +#include <Net/Listener.h> +#include <unistd.h> + +#include <iostream> + +namespace Mad { +namespace Core { + +void ConnectionManager::refreshPollfds() { + +} + +void ConnectionManager::daemonReceiveHandler(const Net::Connection*, const Net::Packet &packet) const { + std::cout << "Received daemon packet:" << std::endl; + std::cout << " Type: " << packet.getType() << std::endl; + std::cout << " Request ID: 0x" << std::hex << std::uppercase << packet.getRequestId() << std::dec << std::endl; + std::cout << " Length: " << packet.getLength() << std::endl; +} + +void ConnectionManager::clientReceiveHandler(const Net::Connection*, const Net::Packet &packet) const { + std::cout << "Received client packet:" << std::endl; + std::cout << " Type: " << packet.getType() << std::endl; + std::cout << " Request ID: 0x" << std::hex << std::uppercase << packet.getRequestId() << std::dec << std::endl; + std::cout << " Length: " << packet.getLength() << std::endl; +} + +ConnectionManager::ConnectionManager() { + try { + // TODO: Get listener addresses from config + listeners.push_back(new Net::Listener(Net::IPAddress("0.0.0.0", 6666))); + } + catch(Net::Exception &e) { + // TODO: Log error + } +} + +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::wait(int timeout) { + // TODO: wait() + + usleep(timeout); +} + +void ConnectionManager::run() { + // TODO: Logging + + for(std::list<Net::ServerConnection*>::iterator con = daemonConnections.begin(); con != daemonConnections.end();) { + if((*con)->isConnected()) { + (*con)->sendReceive(); + ++con; + } + else { + delete *con; + daemonConnections.erase(con++); + } + } + + for(std::list<Net::ServerConnection*>::iterator con = clientConnections.begin(); con != clientConnections.end();) { + if((*con)->isConnected()) { + (*con)->sendReceive(); + ++con; + } + else { + 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()) != 0) { + if(con->isDaemonConnection()) { + daemonConnections.push_back(con); + con->signalReceive().connect(sigc::mem_fun(this, &ConnectionManager::daemonReceiveHandler)); + } + else { + clientConnections.push_back(con); + con->signalReceive().connect(sigc::mem_fun(this, &ConnectionManager::clientReceiveHandler)); + } + } + } +} + +} +} |