| 1 | /* |
|---|
| 2 | * Copyright (c) 2010, |
|---|
| 3 | * Gavriloaie Eugen-Andrei (shiretu@gmail.com) |
|---|
| 4 | * |
|---|
| 5 | * This file is part of crtmpserver. |
|---|
| 6 | * crtmpserver is free software: you can redistribute it and/or modify |
|---|
| 7 | * it under the terms of the GNU General Public License as published by |
|---|
| 8 | * the Free Software Foundation, either version 3 of the License, or |
|---|
| 9 | * (at your option) any later version. |
|---|
| 10 | * |
|---|
| 11 | * crtmpserver is distributed in the hope that it will be useful, |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | * GNU General Public License for more details. |
|---|
| 15 | * |
|---|
| 16 | * You should have received a copy of the GNU General Public License |
|---|
| 17 | * along with crtmpserver. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | #include "protocols/protocolmanager.h" |
|---|
| 22 | #include "protocols/baseprotocol.h" |
|---|
| 23 | #include "netio/netio.h" |
|---|
| 24 | |
|---|
| 25 | map<uint32_t, BaseProtocol *> ProtocolManager::_activeProtocols; |
|---|
| 26 | map<uint32_t, BaseProtocol *> ProtocolManager::_deadProtocols; |
|---|
| 27 | |
|---|
| 28 | void ProtocolManager::RegisterProtocol(BaseProtocol *pProtocol) { |
|---|
| 29 | if (MAP_HAS1(_activeProtocols, pProtocol->GetId())) |
|---|
| 30 | return; |
|---|
| 31 | if (MAP_HAS1(_deadProtocols, pProtocol->GetId())) |
|---|
| 32 | return; |
|---|
| 33 | _activeProtocols[pProtocol->GetId()] = pProtocol; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | void ProtocolManager::UnRegisterProtocol(BaseProtocol *pProtocol) { |
|---|
| 37 | if (MAP_HAS1(_activeProtocols, pProtocol->GetId())) |
|---|
| 38 | _activeProtocols.erase(pProtocol->GetId()); |
|---|
| 39 | if (MAP_HAS1(_deadProtocols, pProtocol->GetId())) |
|---|
| 40 | _deadProtocols.erase(pProtocol->GetId()); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | void ProtocolManager::EnqueueForDelete(BaseProtocol *pProtocol) { |
|---|
| 44 | if (pProtocol->GetNearProtocol() == NULL) { |
|---|
| 45 | FINEST("Enqueue for delete for protocol %s", STR(*pProtocol)); |
|---|
| 46 | } |
|---|
| 47 | pProtocol->SetApplication(NULL); |
|---|
| 48 | if (MAP_HAS1(_activeProtocols, pProtocol->GetId())) |
|---|
| 49 | _activeProtocols.erase(pProtocol->GetId()); |
|---|
| 50 | if (!MAP_HAS1(_deadProtocols, pProtocol->GetId())) |
|---|
| 51 | _deadProtocols[pProtocol->GetId()] = pProtocol; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | uint32_t ProtocolManager::CleanupDeadProtocols() { |
|---|
| 55 | uint32_t result = 0; |
|---|
| 56 | while (_deadProtocols.size() > 0) { |
|---|
| 57 | BaseProtocol *pBaseProtocol = MAP_VAL(_deadProtocols.begin()); |
|---|
| 58 | delete pBaseProtocol; |
|---|
| 59 | result++; |
|---|
| 60 | } |
|---|
| 61 | return result; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | void ProtocolManager::Shutdown() { |
|---|
| 65 | while (_activeProtocols.size() > 0) { |
|---|
| 66 | EnqueueForDelete(MAP_VAL(_activeProtocols.begin())); |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | BaseProtocol * ProtocolManager::GetProtocol(uint32_t id, |
|---|
| 71 | bool includeDeadProtocols) { |
|---|
| 72 | if (!includeDeadProtocols && MAP_HAS1(_deadProtocols, id)) |
|---|
| 73 | return NULL; |
|---|
| 74 | if (MAP_HAS1(_activeProtocols, id)) |
|---|
| 75 | return _activeProtocols[id]; |
|---|
| 76 | if (MAP_HAS1(_deadProtocols, id)) |
|---|
| 77 | return _deadProtocols[id]; |
|---|
| 78 | return NULL; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | const map<uint32_t, BaseProtocol *> & ProtocolManager::GetActiveProtocols() { |
|---|
| 82 | return _activeProtocols; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | void ProtocolManager::GetActiveProtocols(map<uint32_t, BaseProtocol *> &result, |
|---|
| 86 | protocolManagerFilter_f filter) { |
|---|
| 87 | result.clear(); |
|---|
| 88 | if (filter == NULL) { |
|---|
| 89 | result = _activeProtocols; |
|---|
| 90 | return; |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | FOR_MAP(_activeProtocols, uint32_t, BaseProtocol *, i) { |
|---|
| 94 | if (!filter(MAP_VAL(i))) |
|---|
| 95 | continue; |
|---|
| 96 | result[MAP_VAL(i)->GetId()] = MAP_VAL(i); |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | bool protocolManagerNetworkedProtocolsFilter(BaseProtocol *pProtocol) { |
|---|
| 101 | IOHandler *pIOHandler = pProtocol->GetIOHandler(); |
|---|
| 102 | if ((pIOHandler == NULL) |
|---|
| 103 | || ((pIOHandler->GetType() != IOHT_TCP_CARRIER) |
|---|
| 104 | && (pIOHandler->GetType() != IOHT_UDP_CARRIER))) |
|---|
| 105 | return false; |
|---|
| 106 | return true; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | bool protocolManagerNearProtocolsFilter(BaseProtocol *pProtocol) { |
|---|
| 110 | return pProtocol->GetNearProtocol() == NULL; |
|---|
| 111 | } |
|---|