File: Dispatcher.cpp

package info (click to toggle)
eris 1.2.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,360 kB
  • ctags: 1,348
  • sloc: sh: 8,289; cpp: 7,576; perl: 287; ansic: 172; makefile: 143
file content (310 lines) | stat: -rw-r--r-- 7,641 bytes parent folder | download
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#ifdef HAVE_CONFIG_H
	#include "config.h"
#endif

#include <Eris/Utils.h>
#include <Eris/ClassDispatcher.h>
#include <Eris/TypeDispatcher.h>
#include <Eris/OpDispatcher.h>
#include <Eris/EncapDispatcher.h>
#include <Eris/IdDispatcher.h>
#include <Eris/Log.h>
#include <Eris/Exceptions.h>

#include <Atlas/Message/Element.h>

#include <stdio.h>
#include <cassert>

using namespace Atlas::Message;

namespace Eris
{

Dispatcher::Dispatcher(const std::string &nm) :
	_name((nm[0] == '_') ? nm + getAnonymousSuffix(this) : nm),
	_refcount(0)
{
}

Dispatcher::~Dispatcher()
{
	assert(_refcount == 0);
}

std::string Dispatcher::getAnonymousSuffix(Dispatcher *d)
{
	static char buf[32];
	snprintf(buf, 32, "_%p", d);
	return buf;
}

void Dispatcher::enter()
{
    assert(global_inDispatch == false);
    global_inDispatch = true;
}

void Dispatcher::exit()
{
    assert(global_inDispatch == true);
    global_inDispatch = false;
    
    while (!global_needsPurging.empty()) {
	global_needsPurging.front()->purge();
	global_needsPurging.pop_front();
    }
}

bool Dispatcher::global_inDispatch = false;
std::list<Dispatcher*> Dispatcher::global_needsPurging;

//////////////////////////////////////////////////////////////////////////////////

StdBranchDispatcher::StdBranchDispatcher(const std::string nm) :
	Dispatcher(nm)
{
	
}

StdBranchDispatcher::~StdBranchDispatcher()
{
	for (DispatcherDict::iterator d = _subs.begin(); d!=_subs.end(); ++d)
		d->second->decRef();
}

Dispatcher* StdBranchDispatcher::addSubdispatch(Dispatcher *d, const std::string /*data*/)
{
	if (!d)
		throw InvalidOperation("NULL dispatcher passed to addSubdispatch");
	std::string nm = d->getName();
	DispatcherDict::iterator di = _subs.find(nm);
	if (di != _subs.end() && (di->second != NULL))
		throw InvalidOperation("Duplicate dispatcher <" + nm + "> added");
	
	d->addRef();
	
	if (di != _subs.end()) {
	    /* entry has been removed inside a dispatch, so the map entry still exists. Instead of
	    inserting, we just update the current one and we're done. */
	    di->second = d;
	} else
	    _subs.insert(di, DispatcherDict::value_type(nm, d));
	return d;
}

void StdBranchDispatcher::rmvSubdispatch(Dispatcher *d)
{
	if (!d)
		throw InvalidOperation("NULL dispatcher passed to rmvSubdispatch");
	std::string nm = d->getName();
	DispatcherDict::iterator di = _subs.find(nm);
	
	if (di != _subs.end()) {
	    if (di->second == NULL) // might happen with delayed deleteion
		throw InvalidOperation("duplicate remove of dispatcher " + di->first);
	    
	    safeSubErase(di);
	    d->decRef();
	    return;
	}
	
	// check for anonymous
	for (di=_subs.begin(); di!=_subs.end(); ++di)
	    if (di->second && (di->second->_name[0] == '_')) {
		Dispatcher *ds = di->second->getSubdispatch(nm);
		if (ds) {
		    di->second->rmvSubdispatch(d);
		    // clean up empty anonymous nodes automatically
		    if (di->second->empty()) {
			di->second->decRef();
			safeSubErase(di);
		    }
		    return;
	    }
	}
}

Dispatcher* StdBranchDispatcher::getSubdispatch(const std::string &nm)
{
    DispatcherDict::iterator D = _subs.find(nm);
    if (D != _subs.end())
		return D->second;
	
    // deal with annonymous ones
    for (D=_subs.begin(); D!=_subs.end(); ++D)
	if (D->second && (D->second->_name[0] == '_')) {
	    Dispatcher *ds = D->second->getSubdispatch(nm);
	    if (ds)
		return ds;
	}
	
    return NULL;
}

bool StdBranchDispatcher::subdispatch(DispatchContextDeque &dq)
{
    addRef();
    for (DispatcherDict::const_iterator d = _subs.begin(); d!=_subs.end(); ++d)
	if (d->second) {
           if (d->second->dispatch(dq)) {
               decRef();
               return true;
           }
        }
    decRef();
	
    return false;		
}

void StdBranchDispatcher::safeSubErase(const DispatcherDict::iterator &D)
{
    if (Dispatcher::global_inDispatch) {
	global_needsPurging.push_back(this); // make sure it gets cleaned up ASAP
	D->second = NULL;	// remove the entry
    } else
	_subs.erase(D);
}

void StdBranchDispatcher::purge()
{
    for (DispatcherDict::iterator d = _subs.begin(); d!=_subs.end();) {
	DispatcherDict::iterator cur(d++);
	if (!cur->second)
	    _subs.erase(cur);
    }
}

/////////////////////////////////////////////////////////////////////////////////////////

LeafDispatcher::LeafDispatcher(const std::string &nm) :
	Dispatcher(nm)
{
	;
}

bool LeafDispatcher::dispatch(DispatchContextDeque &dq)
{
	Element::MapType &o = dq.back().asMap();
	o["__DISPATCHED__"] = "1";
	return false;
}

Dispatcher* LeafDispatcher::addSubdispatch(Dispatcher*, const std::string)
{
    throw InvalidOperation("called addSubdispatch on LeafDispatcher " + _name);
}
	
void LeafDispatcher::rmvSubdispatch(Dispatcher*)
{
    throw InvalidOperation("called rmvSubdispatch on LeafDispatcher " + _name);
}


Dispatcher* LeafDispatcher::getSubdispatch(const std::string &nm)
{
	Eris::log(LOG_ERROR, "looking for child %s in LeafDispatcher %s", 
		nm.c_str(), _name.c_str());
	return NULL;
}

bool LeafDispatcher::empty()
{
	throw InvalidOperation("called empty() on LeafDispatcher " + _name);
}


void LeafDispatcher::purge()
{
    throw InvalidOperation("called purge() on LeafDispatcher " + _name);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool IdDispatcher::dispatch(DispatchContextDeque &dq)
{
	if (getMember(dq.front(), "id").asString() != _id)
		return false;
	return StdBranchDispatcher::subdispatch(dq);
}

bool TypeDispatcher::dispatch(DispatchContextDeque &dq)
{
	if (!hasMember(dq.front(), "objtype"))
		return false;
	
	if (getMember(dq.front(), "objtype").asString() != _type)
		return false;
	return StdBranchDispatcher::subdispatch(dq);
}

bool ObjectDispatcher::dispatch(DispatchContextDeque &dq)
{
	if (!hasMember(dq.front(), "objtype"))
		return false;
	
	const std::string & otype = getMember(dq.front(), "objtype").asString();
	if ((otype != "object") && (otype != "obj"))
		return false;
	return StdBranchDispatcher::subdispatch(dq);
}

bool OpFromDispatcher::dispatch(DispatchContextDeque &dq)
{
	if (getMember(dq.front(), "from").asString() != _id)
		return false;
	return StdBranchDispatcher::subdispatch(dq);	
}

bool OpToDispatcher::dispatch(DispatchContextDeque &dq)
{
	if (!hasMember(dq.front(), "to")) return false;
	if (getMember(dq.front(), "to").asString() != _id)
		return false;
	return StdBranchDispatcher::subdispatch(dq);	
}

bool OpRefnoDispatcher::dispatch(DispatchContextDeque &dq)
{
	DispatchContextDeque::const_iterator item = dq.begin();
	for(unsigned i = 0; i < _depth; ++i)
		if(++item == dq.end())
			return false;

	assert((*item).isMap());
	const Atlas::Message::Element::MapType &map = (*item).asMap();
	Atlas::Message::Element::MapType::const_iterator I = map.find("refno");
	if(I == map.end()) {
		std::string warning = "Op without a refno, keys are:";
		for(I = map.begin(); I != map.end(); ++I)
			warning += " " + I->first;
		log(LOG_DEBUG, warning.c_str());
		return false;
	}
	assert(I->second.isInt());
	if (I->second.asInt() != _refno)
		return false;
	return StdBranchDispatcher::subdispatch(dq);	
}

bool EncapDispatcher::dispatch(DispatchContextDeque &dq)
{
	const Atlas::Message::Element::ListType &args = 
		getMember(dq.front(), "args").asList();
	if (args.size() < _position)
		return false;
	
	dq.push_front(args[_position]);
	bool ret = StdBranchDispatcher::subdispatch(dq);	
	
	// we need to restore the context stack, otherwise the next dispatcher along gets very confused
	dq.pop_front();
	return ret;
}

Dispatcher* EncapDispatcher::newAnonymous()
{
	return new EncapDispatcher("_encap");
}

}