finally added callIdQueue (sorry)
[anytun.git] / rtpSessionTable.cpp
blob5beb3d65e8d51fdb2da16f56493280707f7af75c
1 /*
2 * anytun
4 * The secure anycast tunneling protocol (satp) defines a protocol used
5 * for communication between any combination of unicast and anycast
6 * tunnel endpoints. It has less protocol overhead than IPSec in Tunnel
7 * mode and allows tunneling of every ETHER TYPE protocol (e.g.
8 * ethernet, ip, arp ...). satp directly includes cryptography and
9 * message authentication based on the methodes used by SRTP. It is
10 * intended to deliver a generic, scaleable and secure solution for
11 * tunneling and relaying of packets of any protocol.
14 * Copyright (C) 2007 anytun.org <satp@wirdorange.org>
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2
18 * as published by the Free Software Foundation.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program (see the file COPYING included with this
26 * distribution); if not, write to the Free Software Foundation, Inc.,
27 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "threadUtils.hpp"
30 #include "datatypes.h"
32 #include "rtpSessionTable.h"
34 RtpSessionTable* RtpSessionTable::inst = NULL;
35 Mutex RtpSessionTable::instMutex;
36 RtpSessionTable& gRtpSessionTable = RtpSessionTable::instance();
39 RtpSessionTable& RtpSessionTable::instance()
41 Lock lock(instMutex);
42 static instanceCleaner c;
43 if(!inst)
44 inst = new RtpSessionTable();
46 return *inst;
49 RtpSessionTable::RtpSessionTable()
53 RtpSessionTable::~RtpSessionTable()
57 void RtpSessionTable::delSession(const std::string & call_id)
59 Lock lock(mutex_);
61 RtpSessionMap::iterator it = map_.find(call_id);
62 if(it!=map_.end())
63 delete it->second;
65 map_.erase(it);
68 RtpSession& RtpSessionTable::getOrNewSession(const std::string & call_id, bool& is_new)
70 Lock lock(mutex_);
71 return getOrNewSessionUnlocked(call_id, is_new);
74 RtpSession& RtpSessionTable::getOrNewSessionUnlocked(const std::string & call_id, bool& is_new)
76 is_new = false;
77 RtpSessionMap::iterator it = map_.find(call_id);
78 if(it!=map_.end())
79 return *(it->second);
81 is_new = true;
82 std::pair<RtpSessionMap::iterator, bool> ret = map_.insert(RtpSessionMap::value_type(call_id, NULL));
83 ret.first->second = new RtpSession(ret.first->first);
84 return *(ret.first->second);
87 RtpSession& RtpSessionTable::getSession(const std::string & call_id)
89 RtpSessionMap::iterator it = map_.find(call_id);
90 if(it!=map_.end())
91 return *(it->second);
93 throw std::runtime_error("session not found");
96 RtpSessionMap::iterator RtpSessionTable::getBeginUnlocked()
98 return map_.begin();
101 RtpSessionMap::iterator RtpSessionTable::getEndUnlocked()
103 return map_.end();
106 void RtpSessionTable::clear()
108 Lock lock(mutex_);
109 map_.clear();
112 bool RtpSessionTable::empty()
114 Lock lock(mutex_);
115 return map_.empty();
118 Mutex& RtpSessionTable::getMutex()
120 return mutex_;