summaryrefslogtreecommitdiff
path: root/src/connectionhandler.cc
blob: 8b48c034a5bb00603418cd684749dd5502f5419f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* -*- 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 <stdio.h>

#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<nodeid_t>();

	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<nodeid_t>::iterator ti = ci->second.find(tid);
			if(ti != ci->second.end())
			{
				std::pair<clientid_t, nodeid_t> m;
				m.first = ci->first;
				m.second = tid;
				clients.push_back(m);
			}
		}
	}

	return clients;
}