glooxd  0.3-svn
configmanager.cpp
1 /*
2  Copyright (c) 2009 by Jakob Schroeter <js@camaya.net>
3  This file is part of the glooxd library. http://camaya.net/glooxd
4 
5  This software is distributed under a license. The full license
6  agreement can be found in the file LICENSE in this distribution.
7  This software may not be copied, modified, sold or distributed
8  other than expressed in the named license agreement.
9 
10  This software is distributed without any warranty.
11 */
12 
13 
14 #include "confighandler.h"
15 #include "configmanager.h"
16 #include "servereventhandler.h"
17 
18 #include <gloox/logsink.h>
19 #include <gloox/mutexguard.h>
20 #include <gloox/util.h>
21 
22 #include <cstdio>
23 
24 namespace glooxd
25 {
26 
27  // --- ConfigManager::Interface ---
28  bool ConfigManager::Interface::hasDomain( const std::string& domain )
29  {
30  gloox::StringList::const_iterator it = m_domains.begin();
31  for( ; it != m_domains.end() && (*it) != domain; ++it )
32  ;
33  return it != m_domains.end();
34  }
35 // --- ~ConfigManager::Interface ---
36 
37  ConfigManager::ConfigManager( const gloox::LogSink& logInstance )
38  : m_logInstance( logInstance )
39  {
40  }
41 
43  {
44  gloox::util::clearList( m_c2sInterfaces );
45  }
46 
48  {
49  if( !ch )
50  return;
51 
52  m_configHandlersMutex.lock();
53  m_configHandlers.push_back( ch );
54  m_configHandlersMutex.unlock();
55  }
56 
58  {
59  if( !seh )
60  return;
61 
62  m_eventHandlersMutex.lock();
63  m_eventHandlers.push_back( seh );
64  m_eventHandlersMutex.unlock();
65  }
66 
67  void ConfigManager::bindDomain( const std::string& domain, const std::string& interface,
68  unsigned short port )
69  {
70  if( domain.empty() )
71  return;
72 
73  m_configHandlersMutex.lock();
74  gloox::util::ForEach( m_configHandlers, &ConfigHandler::handleDomainAdded, domain );
75  m_configHandlersMutex.unlock();
76 
77  gloox::util::MutexGuard mg( m_c2sInterfacesMutex );
78  InterfaceList::const_iterator it = m_c2sInterfaces.begin();
79  for( ; it != m_c2sInterfaces.end(); ++it )
80  {
81  if( (*it)->interface() == interface && (*it)->port() == port )
82  {
83  (*it)->addDomain( domain );
84  return;
85  }
86  }
87  }
88 
89  void ConfigManager::unbindDomain( const std::string& domain, const std::string& interface,
90  unsigned short port )
91  {
92  if( domain.empty() )
93  return;
94 
95  m_configHandlersMutex.lock();
96  gloox::util::ForEach( m_configHandlers, &ConfigHandler::handleDomainRemoved, domain );
97  m_configHandlersMutex.unlock();
98 
99  gloox::util::MutexGuard mg( m_c2sInterfacesMutex );
100  InterfaceList::const_iterator it = m_c2sInterfaces.begin();
101  for( ; it != m_c2sInterfaces.end(); ++it )
102  {
103  if( (*it)->interface() == interface && (*it)->port() == port )
104  {
105  (*it)->removeDomain( domain );
106  return;
107  }
108  }
109  }
110 
111  bool ConfigManager::addC2SInterface( const std::string& interface,
112  unsigned short port )
113  {
114  if( findInterface( interface, port ) )
115  return false;
116 
117  m_c2sInterfacesMutex.lock();
118  m_c2sInterfaces.push_back( new Interface( interface, port ) );
119  m_c2sInterfacesMutex.unlock();
120 
121  m_configHandlersMutex.lock();
122  gloox::util::ForEach( m_configHandlers, &ConfigHandler::handleC2SInterfaceAdded, interface, port );
123  m_configHandlersMutex.unlock();
124  return true;
125  }
126 
127  bool ConfigManager::removeC2SInterface( const std::string& interface,
128  unsigned short port )
129  {
130  gloox::util::MutexGuard mg( m_c2sInterfacesMutex );
131  InterfaceList::iterator it = m_c2sInterfaces.begin();
132  for( ; it != m_c2sInterfaces.end(); ++it )
133  {
134  if( (*it)->interface() == interface && (*it)->port() == port )
135  {
136  delete (*it);
137  m_c2sInterfaces.erase( it );
138  gloox::util::ForEach( m_configHandlers, &ConfigHandler::handleC2SInterfaceRemoved, interface, port );
139  return true;
140  }
141  }
142 
143  return false;
144  }
145 
146  bool ConfigManager::findInterface( const std::string& interface,
147  unsigned short port )
148  {
149  gloox::util::MutexGuard mg( m_c2sInterfacesMutex );
150  InterfaceList::const_iterator it = m_c2sInterfaces.begin();
151  for( ; it != m_c2sInterfaces.end(); ++it )
152  {
153  if( (*it)->interface() == interface && (*it)->port() == port )
154  return true;
155  }
156  return false;
157  }
158 
160  {
161  gloox::util::MutexGuard mg( m_configHandlersMutex );
162  InterfaceList::const_iterator it = m_c2sInterfaces.begin();
163  for( ; it != m_c2sInterfaces.end(); ++it )
164  {
165  const std::string& interface = (*it)->interface();
166  int port = (*it)->port();
167  gloox::util::ForEach( m_configHandlers, &ConfigHandler::handleC2SInterfaceRemoved, interface, port );
168  gloox::util::ForEach( m_configHandlers, &ConfigHandler::handleC2SInterfaceAdded, interface, port );
169  }
170  }
171 
173  {
174  gloox::util::MutexGuard mg( m_configHandlersMutex );
175  gloox::util::ForEach( m_configHandlers, &ConfigHandler::handleShutdown );
176  }
177 
178  bool ConfigManager::checkDomain( const std::string& domain, const std::string& ip,
179  int port )
180  {
181 #ifdef DEBUG
182  printf( "checking %s on %s:%d\n", domain.c_str(), ip.c_str(), port );
183 #endif
184  gloox::util::MutexGuard mg( m_c2sInterfacesMutex );
185  InterfaceList::const_iterator it = m_c2sInterfaces.begin();
186  for( ; it != m_c2sInterfaces.end(); ++it )
187  {
188  if( ( (*it)->interface() == gloox::EmptyString
189  || (*it)->interface() == ip )
190  && (*it)->port() == port
191  && (*it)->hasDomain( domain ) )
192  return true;
193  }
194  return false;
195  }
196 
198  {
199  gloox::util::MutexGuard mg( m_eventHandlersMutex );
200  gloox::util::ForEach( m_eventHandlers, &ServerEventHandler::handleC2SListening, yes );
201  }
202 
204  {
205  gloox::util::MutexGuard mg( m_eventHandlersMutex );
206  gloox::util::ForEach( m_eventHandlers, &ServerEventHandler::handleS2SListening, yes );
207  }
208 
209  void ConfigManager::handleClientConnected( const gloox::JID jid, const std::string localIP, int localPort,
210  const std::string remoteIP, int remotePort )
211  {
212  gloox::util::MutexGuard mg( m_eventHandlersMutex );
213  ServerEventHandlerList::const_iterator it = m_eventHandlers.begin();
214  for( ; it != m_eventHandlers.end(); ++it )
215  (*it)->handleClientConnected( jid, localIP, localPort, remoteIP, remotePort );
216  }
217 
218  void ConfigManager::handleClientDisconnected( const gloox::JID jid, gloox::ConnectionError e,
219  gloox::StreamError se )
220  {
221  gloox::util::MutexGuard mg( m_eventHandlersMutex );
222  gloox::util::ForEach( m_eventHandlers, &ServerEventHandler::handleClientDisconnected, jid, e, se );
223  }
224 
225 }