glooxd  0.3-svn
sm.h
1 /*
2  Copyright (c) 2008-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 #ifndef SM_H__
15 #define SM_H__
16 
17 #include "componentbase.h"
18 #include "glooxd.h"
19 
20 #include <gloox/gloox.h>
21 #include <gloox/jid.h>
22 #include <gloox/mutex.h>
23 
24 #include <list>
25 #include <map>
26 #include <string>
27 
28 namespace gloox
29 {
30  class LogSink;
31 }
32 
33 namespace glooxd
34 {
35 
36  class Client;
37  class Plugin;
38  class RosterProvider;
39 
45  class GLOOXD_API SM : public ComponentBase
46  {
47  public:
60  SM( Router& router, ConfigManager& cm, const gloox::LogSink& logInstance,
61  RosterProvider* rp, bool threading = false,
62  const gloox::JID& name = gloox::JID( "sm." + GLOOXD_DOMAIN ) );
63 
67  virtual ~SM();
68 
73  void run();
74 
85  bool createSession( gloox::JID& jid, const std::string& id );
86 
93  bool removeSession( const std::string& id );
94 
99  void addPlugin( Plugin* p )
100  {
101  if( p )
102  m_plugins.push_back( p );
103  }
104 
110  void dispatchPresence( const gloox::Tag* pres );
111 
118  bool dispatchMessage( const gloox::Tag* msg );
119 
120  // reimplemented from TagHandler
121  virtual void handleIncomingTag( gloox::Tag* tag );
122 
123  // reimplemented from TagHandler
124  virtual void handleOutgoingTag( gloox::Tag* ) {}
125 
126  // reimplemented from ConfigHandler
127  virtual void handleDomainAdded( const std::string& domain );
128 
129  // reimplemented from ConfigHandler
130  virtual void handleDomainRemoved( const std::string& domain );
131 
132  // reimplemented from ConfigHandler
133  virtual void handleShutdown();
134 
135  private:
136  struct Session
137  {
138  std::string id;
139  gloox::Tag* presence;
140  int priority;
141  };
142 
146  typedef std::map<std::string, Session> SessionList;
147 
148  typedef std::list<Plugin*> PluginList;
149 
150  void handleTag( gloox::Tag* tag );
151 
157  const SessionList findSessions( const gloox::JID& jid );
158 
159  SessionList m_sessions;
160  PluginList m_plugins;
161  RosterProvider* m_rosterProvider;
162 
163  gloox::util::Mutex m_sessionMutex;
164 
165  gloox::TagList m_queue;
166  glooxd::util::Semaphore m_queueSemaphore;
167  gloox::util::Mutex m_queueMutex;
168 
169  volatile bool m_threading;
170 
171  };
172 
173 }
174 
175 #endif // SM_H__