/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set et sw=2 ts=2: */ /*************************************************************************** * connectionhandler.cc * * Fri Mar 30 09:25:16 CEST 2012 * Copyright 2012 Bent Bisballe Nyeng * deva@aasimon.org ****************************************************************************/ /* * This file is part of Munia. * * Munia 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. * * Munia 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 Munia; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ #include "connectionhandler.h" #include #include "hugin.hpp" // Global ConnectionHandler. ConnectionHandler connection_handler; ConnectionHandler::ConnectionHandler() { } void ConnectionHandler::init(clientid_t clientid) { DEBUG(conn, "Adding client %p to connection list\n", clientid); connlist[clientid] = std::set(); DEBUG(conn, "Connections (%d):\n", (int)connlist.size()); ConnectionList::iterator it; for(it = connlist.begin(); it != connlist.end(); it++) { DEBUG(conn, "\t%p\n", it->first); } } void ConnectionHandler::close(clientid_t clientid) { connlist.erase(clientid); DEBUG(conn, "Removed connection\n"); } void ConnectionHandler::login(clientid_t clientid, std::string user, std::string password) { authlist[clientid] = (password == "hundemad"); DEBUG(conn, "Authentication %d\n", authlist[clientid]); } void ConnectionHandler::logout(clientid_t clientid) { authlist[clientid] = false; DEBUG(conn, "Authentication %d\n", authlist[clientid]); } bool ConnectionHandler::authenticated(clientid_t clientid) { return authlist.find(clientid) != authlist.end() && authlist[clientid]; } void ConnectionHandler::subscribe(clientid_t clientid, nodeid_t nodeid) { connlist[clientid].insert(nodeid); DEBUG(conn, "Added subscriber of %d\n", (int)nodeid); } bool ConnectionHandler::unsubscribe(clientid_t clientid, nodeid_t nodeid) { if(connlist[clientid].find(nodeid) == connlist[clientid].end()) { // Trying to unsubscribe from a node that is not subscribed. return false; } connlist[clientid].erase(nodeid); return true; } SubscriberList ConnectionHandler::subscriberlist(NodeIdList nodes) { DEBUG(conn, "Subscriberlist request (#nodes: %d)\n", (int)nodes.size()); SubscriberList clients; for(NodeIdList::iterator i = nodes.begin(); i != nodes.end(); i++) { nodeid_t tid = *i; DEBUG(conn, "Locating subscribers of node %d\n", (int)tid); for(ConnectionList::iterator ci = connlist.begin(); ci != connlist.end(); ci++) { std::set::iterator ti = ci->second.find(tid); if(ti != ci->second.end()) { std::pair m; m.first = ci->first; m.second = tid; clients.push_back(m); } } } return clients; }