core/AmSessionContainer.h
7c964b9b
 /*
  * Copyright (C) 2002-2003 Fhg Fokus
  *
7dcb7e2a
  * This file is part of SEMS, a free SIP media server.
7c964b9b
  *
7dcb7e2a
  * SEMS is free software; you can redistribute it and/or modify
7c964b9b
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
7dcb7e2a
  * (at your option) any later version. This program is released under
  * the GPL with the additional exemption that compiling, linking,
  * and/or using OpenSSL is allowed.
7c964b9b
  *
7dcb7e2a
  * For a license to use the SEMS software under conditions
7c964b9b
  * other than those described here, or to purchase support for this
  * software, please contact iptel.org by e-mail at the following addresses:
  *    info@iptel.org
  *
7dcb7e2a
  * SEMS is distributed in the hope that it will be useful,
7c964b9b
  * 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, write to the Free Software 
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
df041a16
 /** @file AmSessionContainer.h */
37abd537
 #ifndef AmSessionContainer_h
 #define AmSessionContainer_h
 
 #include "AmThread.h"
 #include "AmSession.h"
 
43a387c3
 #include "ampi/MonitoringAPI.h"
 
37abd537
 #include <string>
 #include <queue>
 #include <map>
 
 using std::string;
 
 /**
7c16a0f5
  * \brief Centralized session container.
  *
37abd537
  * This is the register for all active and dead sessions.
  * If has a deamon which wakes up only if it has work. 
  * Then, it kills all dead sessions and try to go to bed 
  * (it cannot sleep if one or more sessions are still alive).
  */
 class AmSessionContainer : public AmThread
 {
d9884e05
   static AmSessionContainer* _instance;
7c964b9b
 
9411ad3a
   typedef std::queue<AmSession*>      SessionQueue;
7c964b9b
 
   /** Container for dead sessions */
   SessionQueue d_sessions;
   /** Mutex to protect the dead session container */
   AmMutex      ds_mut;
 
d9884e05
   /** is container closed for new sessions? */
   AmCondition<bool> _container_closed;
 
7c964b9b
   /** the daemon only runs if this is true */
   AmCondition<bool> _run_cond;
 
   /** We are a Singleton ! Avoid people to have their own instance. */
   AmSessionContainer();
 
434cba1c
 
   bool enable_unclean_shutdown;
 
7c964b9b
   /**
    * Tries to stop the session and queue it destruction.
    */
   void stopAndQueue(AmSession* s);
 
   /** @see AmThread::run() */
   void run();
   /** @see AmThread::on_stop() */
   void on_stop();
37abd537
 
d9884e05
   bool clean_sessions();
 
37abd537
  public:
7c964b9b
   static AmSessionContainer* instance();
 
d9884e05
   static void dispose();
 
7545eb11
   enum AddSessionStatus {
     ShutDown,
     Inserted,
     AlreadyExist
   };
 
7c964b9b
   /**
    * Creates a new session.
    * @param req local request
    * @return a new session or NULL on error.
    */
a0ca3c54
   AmSession* createSession(AmSipRequest& req, 
 			   AmArg* session_params = NULL);
7c964b9b
 
   /**
    * Adds a session to the container (UAS only).
    * @return true if the session is new within the container.
    */
7545eb11
   AddSessionStatus addSession(const string& callid,
 			      const string& remote_tag,
 			      const string& local_tag,
d79f9edf
 			      const string& via_branch,
7545eb11
 			      AmSession* session);
7c964b9b
 
   /**
    * Adds a session to the container.
    * @return true if the session is new within the container.
    */
7545eb11
   AddSessionStatus addSession(const string& local_tag,
 			      AmSession* session);
7c964b9b
 
   /** 
    * Constructs a new session and adds it to the active session container. 
    * @param req client's request
    */
   void startSessionUAS(AmSipRequest& req);
 
   /** 
    * Constructs a new session and adds it to the active session container. 
    * @param req client's request
    */
7545eb11
   string startSessionUAC(AmSipRequest& req, 
 			 AmArg* session_params = NULL);
7c964b9b
 
   /**
    * Detroys a session.
    */
   void destroySession(AmSession* s);
 
   /**
    * post an event into the event queue of the identified dialog.
    * @return false if session doesn't exist 
    */
d79f9edf
   bool postEvent(const string& callid, 
 		 const string& remote_tag,
 		 const string& via_branch,
7c964b9b
 		 AmEvent* event);
 
   /**
    * post a generic event into the event queue of the identified dialog.
    * sess_key is local_tag (to_tag)
    * note: if hash_str is known, use 
    *          postGenericEvent(hash_str,sess_key,event);
    *       for better performance.
    * @return false if session doesn't exist 
    */
   bool postEvent(const string& local_tag, AmEvent* event);
43a387c3
 
f1b8362e
   /** 
    * broadcasts a server shutdown system event to all sessions
    */
   void  broadcastShutdown();
 
434cba1c
   /** enable unclean shutdown (will not broadcastShutdown event) */
   void enableUncleanShutdown();
 
43a387c3
   _MONITORING_DEFINE_INTERFACE;
 
37abd537
 };
 
 #endif