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
|
// **********************************************************************
//
// Copyright (c) 2001
// MutableRealms, Inc.
// Huntsville, AL, USA
//
// All Rights Reserved
//
// **********************************************************************
#include <Ice/Ice.h>
#include <Ice/Functional.h>
#include <IceStorm/TopicI.h>
#include <IceStorm/Flusher.h>
#include <IceStorm/Subscriber.h>
#include <IceStorm/TraceLevels.h>
#include <algorithm>
using namespace IceStorm;
using namespace std;
namespace IceStorm
{
class BlobjectI : public Ice::Blobject
{
public:
BlobjectI(const IceStorm::TopicSubscribersPtr& s) :
_subscribers(s)
{
}
~BlobjectI()
{
}
virtual void ice_invokeIn(const std::string&, const std::string&, const std::string&,
const std::vector< ::Ice::Byte>&);
private:
IceStorm::TopicSubscribersPtr _subscribers;
};
class TopicSubscribers : public IceUtil::Shared, public JTCMutex
{
public:
TopicSubscribers(const TraceLevelsPtr& traceLevels, const Ice::LoggerPtr& logger, const string& topic,
const FlusherPtr& flusher) :
_traceLevels(traceLevels),
_logger(logger),
_topic(topic),
_flusher(flusher)
{
}
~TopicSubscribers()
{
}
void
add(const Ice::ObjectPrx& s, const string& idPrefix, const QoS& qos)
{
//
// Create the full topic subscriber id (<prefix>#<topic>)
//
string id = idPrefix;
id += '#';
id += _topic;
//
// Change the identity of the proxy
//
Ice::ObjectPrx obj = s->ice_newIdentity(id);
SubscriberPtr subscriber = new Subscriber(_logger, _traceLevels, _flusher, qos, obj);
JTCSyncT<JTCMutex> sync(*this);
//
// Add to the set of subscribers
//
_subscribers.push_back(subscriber);
}
void
remove(const string& idPrefix)
{
JTCSyncT<JTCMutex> sync(*this);
//
// Create the full topic subscriber id (<prefix>#<topic>)
//
string id = idPrefix;
id += '#';
id += _topic;
SubscriberList::iterator i;
for (i = _subscribers.begin() ; i != _subscribers.end(); ++i)
{
if ((*i)->id() == id)
{
//
// This marks the subscriber as invalid. It will be
// removed on the next event publish.
//
(*i)->unsubscribe();
break;
}
}
//
// If the subscriber was not found then display a diagnostic
//
if (i == _subscribers.end())
{
if (_traceLevels->topic > 0)
{
ostringstream s;
s << _topic << ": " << id << "not subscribed.";
_logger->trace(_traceLevels->topicCat, s.str());
}
}
}
void
publish(const string& op, const std::vector< ::Ice::Byte>& blob)
{
JTCSyncT<JTCMutex> sync(*this);
//
// Using standard algorithms I don't think there is a way to
// do this in one pass. For instance, I thought about using
// remove_if - but the predicate needs to be a pure function
// (see Meyers for details). If this is fixed then fix Flusher
// also.
//
_subscribers.remove_if(::Ice::constMemFun(&Subscriber::invalid));
for (SubscriberList::iterator i = _subscribers.begin(); i != _subscribers.end(); ++i)
{
(*i)->publish(op, blob);
}
//for_each(_subscribers.begin(), _subscribers.end(), Ice::memFun(&Subscriber::publish));
}
private:
TraceLevelsPtr _traceLevels;
Ice::LoggerPtr _logger;
string _topic;
FlusherPtr _flusher;
SubscriberList _subscribers;
};
} // End namespace IceStorm
void
BlobjectI::ice_invokeIn(const string&, const string&, const string& op,
const std::vector< ::Ice::Byte>& blob)
{
cerr << "ice_invokeIn" << endl;
_subscribers->publish(op, blob);
}
TopicI::TopicI(const Ice::ObjectAdapterPtr& adapter, const TraceLevelsPtr& traceLevels, const Ice::LoggerPtr& logger,
const string& name, const FlusherPtr& flusher) :
_adapter(adapter),
_traceLevels(traceLevels),
_logger(logger),
_name(name),
_flusher(flusher),
_destroyed(false)
{
_subscribers = new TopicSubscribers(_traceLevels, _logger, _name, _flusher);
_publisher = new BlobjectI(_subscribers);
string id = name;
id += '#';
id += "publish";
_adapter->add(_publisher, id);
_obj = adapter->createProxy(id);
}
TopicI::~TopicI()
{
}
string
TopicI::getName()
{
return _name;
}
Ice::ObjectPrx
TopicI::getPublisher()
{
return _obj;
}
void
TopicI::destroy()
{
JTCSyncT<JTCMutex> sync(_destroyedMutex);
_adapter->remove(_name);
_destroyed = true;
}
bool
TopicI::destroyed() const
{
JTCSyncT<JTCMutex> sync(_destroyedMutex);
return _destroyed;
}
void
TopicI::subscribe(const Ice::ObjectPrx& tmpl, const string& id, const QoS& qos)
{
_subscribers->add(tmpl, id, qos);
}
void
TopicI::unsubscribe(const string& id)
{
_subscribers->remove(id);
}
|