File: protocolfactorymanager.cpp

package info (click to toggle)
crtmpserver 1.0~dfsg-5.3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 5,424 kB
  • sloc: cpp: 56,355; sh: 411; makefile: 21
file content (201 lines) | stat: -rw-r--r-- 5,592 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* 
 *  Copyright (c) 2010,
 *  Gavriloaie Eugen-Andrei (shiretu@gmail.com)
 *
 *  This file is part of crtmpserver.
 *  crtmpserver is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  crtmpserver is distributed in the hope that it will be useful,
 *  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 crtmpserver.  If not, see <http://www.gnu.org/licenses/>.
 */




#include "protocols/protocolfactorymanager.h"
#include "protocols/baseprotocolfactory.h"
#include "protocols/baseprotocol.h"

map<uint32_t, BaseProtocolFactory *> ProtocolFactoryManager::_factoriesById;
map<uint64_t, BaseProtocolFactory *> ProtocolFactoryManager::_factoriesByProtocolId;
map<string, BaseProtocolFactory *> ProtocolFactoryManager::_factoriesByChainName;

bool ProtocolFactoryManager::RegisterProtocolFactory(BaseProtocolFactory *pFactory) {

	//1. Test to see if this factory is already registered
	if (MAP_HAS1(_factoriesById, pFactory->GetId())) {
		FATAL("Factory id %u already registered", pFactory->GetId());
		return false;
	}

	//2. Test to see if the protocol chains exported by this factory are already in use
	vector<string> protocolChains = pFactory->HandledProtocolChains();

	FOR_VECTOR(protocolChains, i) {
		if (MAP_HAS1(_factoriesByChainName, protocolChains[i])) {
			FATAL("protocol chain %s already handled by factory %u",
					STR(protocolChains[i]),
					_factoriesByChainName[protocolChains[i]]->GetId());
			return false;
		}
	}

	//3. Test to see if the protocols exported by this factory are already in use
	vector<uint64_t> protocols = pFactory->HandledProtocols();

	FOR_VECTOR(protocols, i) {
		if (MAP_HAS1(_factoriesByProtocolId, protocols[i])) {
			FATAL("protocol %" PRIx64" already handled by factory %u", protocols[i],
					_factoriesByProtocolId[protocols[i]]->GetId());
			return false;
		}
	}

	//4. Register everything

	FOR_VECTOR(protocolChains, i) {
		_factoriesByChainName[protocolChains[i]] = pFactory;
	}

	FOR_VECTOR(protocols, i) {
		_factoriesByProtocolId[protocols[i]] = pFactory;
	}

	_factoriesById[pFactory->GetId()] = pFactory;

	return true;
}

bool ProtocolFactoryManager::UnRegisterProtocolFactory(uint32_t factoryId) {
	if (!MAP_HAS1(_factoriesById, factoryId)) {
		WARN("Factory id not found: %u", factoryId);
		return true;
	}
	return UnRegisterProtocolFactory(_factoriesById[factoryId]);
}

bool ProtocolFactoryManager::UnRegisterProtocolFactory(BaseProtocolFactory *pFactory) {
	if (pFactory == NULL) {
		WARN("pFactory is NULL");
		return true;
	}

	if (!MAP_HAS1(_factoriesById, pFactory->GetId())) {
		WARN("Factory id not found: %u", pFactory->GetId());
		return true;
	}

	vector<string> protocolChains = pFactory->HandledProtocolChains();
	vector<uint64_t> protocols = pFactory->HandledProtocols();

	FOR_VECTOR(protocolChains, i) {
		_factoriesByChainName.erase(protocolChains[i]);
	}

	FOR_VECTOR(protocols, i) {
		_factoriesByProtocolId.erase(protocols[i]);
	}

	_factoriesById.erase(pFactory->GetId());

	return true;
}

vector<uint64_t> ProtocolFactoryManager::ResolveProtocolChain(string name) {
	if (!MAP_HAS1(_factoriesByChainName, name)) {
		FATAL("chain %s not registered by any protocol factory", STR(name));
		return vector<uint64_t > ();
	}

	return _factoriesByChainName[name]->ResolveProtocolChain(name);
}

BaseProtocol *ProtocolFactoryManager::CreateProtocolChain(string name,
		Variant &parameters) {
	vector<uint64_t> chain = ResolveProtocolChain(name);
	if (chain.size() == 0) {
		FATAL("Unable to create protocol chain");
		return NULL;
	}

	return CreateProtocolChain(chain, parameters);
}

BaseProtocol *ProtocolFactoryManager::CreateProtocolChain(vector<uint64_t> &chain,
		Variant &parameters) {
	BaseProtocol *pResult = NULL;

	//1. Check and see if all the protocols are handled by a factory

	FOR_VECTOR(chain, i) {
		if (!MAP_HAS1(_factoriesByProtocolId, chain[i])) {
			FATAL("protocol %" PRIx64" not handled by anyone", chain[i]);
			return NULL;
		}
	}

	//2. Spawn the protocols

	bool failed = false;

	FOR_VECTOR(chain, i) {
		BaseProtocol *pProtocol = _factoriesByProtocolId[chain[i]]->SpawnProtocol(
				chain[i], parameters);
		if (pProtocol == NULL) {
			FATAL("Unable to spawn protocol %s handled by factory %u",
					STR(tagToString(chain[i])),
					_factoriesByProtocolId[chain[i]]->GetId());
			failed = true;
			break;
		}
		if (pResult != NULL)
			pResult->SetNearProtocol(pProtocol);

		pResult = pProtocol;
	}

	if (failed) {
		if (pResult != NULL)
			delete pResult->GetFarEndpoint();
		pResult = NULL;
	} else {
		pResult = pResult->GetNearEndpoint();
	}

	return pResult;
}

string ProtocolFactoryManager::Dump() {
	string result = "Factories by id\n";

	FOR_MAP(_factoriesById, uint32_t, BaseProtocolFactory *, i) {
		result += format("\t%u\t%p\n", MAP_KEY(i), MAP_VAL(i));
	}

	result += "Factories by protocol id\n";

	FOR_MAP(_factoriesByProtocolId, uint64_t, BaseProtocolFactory *, i) {
		result += format("\t%s\t%p\n",
				STR(tagToString(MAP_KEY(i))),
				MAP_VAL(i));
	}

	result += "Factories by chain name\n";

	FOR_MAP(_factoriesByChainName, string, BaseProtocolFactory *, i) {
		result += format("\t%s\t%p\n",
				STR(MAP_KEY(i)),
				MAP_VAL(i));
	}

	return result;
}