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) 2003-2008 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <IceUtil/DisableWarnings.h>
#include <IceStorm/TopicManagerI.h>
#include <IceStorm/TopicI.h>
#include <IceStorm/SubscriberPool.h>
#include <IceStorm/BatchFlusher.h>
#include <IceStorm/TraceLevels.h>
#include <IceStorm/Instance.h>
#include <Freeze/Initialize.h>
#include <Ice/SliceChecksums.h>
#include <functional>
#include <ctype.h>
using namespace IceStorm;
using namespace std;
namespace IceStorm
{
string
identityToTopicName(const Ice::Identity& id)
{
//
// Work out the topic name. If the category is empty then we're in
// backwards compatibility mode and the name is just
// identity.name. Otherwise identity.name is topic.<topicname>.
//
if(id.category.empty())
{
return id.name;
}
assert(id.name.length() > 6 && id.name.compare(0, 6, "topic.") == 0);
return id.name.substr(6);
}
}
TopicManagerI::TopicManagerI(
const InstancePtr& instance,
const Ice::ObjectAdapterPtr& topicAdapter,
const string& envName,
const string& dbName) :
_instance(instance),
_topicAdapter(topicAdapter),
_envName(envName),
_dbName(dbName),
_connection(Freeze::createConnection(instance->communicator(), envName)),
_topics(_connection, dbName)
{
//
// Recreate each of the topics in the persistent map
//
for(PersistentTopicMap::const_iterator p = _topics.begin(); p != _topics.end(); ++p)
{
installTopic(identityToTopicName(p->first), p->first, p->second, false);
}
}
TopicManagerI::~TopicManagerI()
{
}
TopicPrx
TopicManagerI::create(const string& name, const Ice::Current&)
{
IceUtil::Mutex::Lock sync(*this);
reap();
if(_topicIMap.find(name) != _topicIMap.end())
{
TopicExists ex;
ex.name = name;
throw ex;
}
// Identity is instanceName>/topic.<topicname>
Ice::Identity id;
id.category = _instance->instanceName();
id.name = "topic." + name;
_topics.put(PersistentTopicMap::value_type(id, LinkRecordSeq()));
return installTopic(name, id, LinkRecordSeq(), true);
}
TopicPrx
TopicManagerI::retrieve(const string& name, const Ice::Current&) const
{
IceUtil::Mutex::Lock sync(*this);
TopicManagerI* This = const_cast<TopicManagerI*>(this);
This->reap();
TopicIMap::const_iterator p = _topicIMap.find(name);
if(p == _topicIMap.end())
{
NoSuchTopic ex;
ex.name = name;
throw ex;
}
// Here we cannot just reconstruct the identity since the
// identity could be either instanceName/topic name, or if
// created with pre-3.2 IceStorm / topic name.
return TopicPrx::uncheckedCast(_topicAdapter->createProxy(p->second->id()));
}
TopicDict
TopicManagerI::retrieveAll(const Ice::Current&) const
{
IceUtil::Mutex::Lock sync(*this);
TopicManagerI* This = const_cast<TopicManagerI*>(this);
This->reap();
TopicDict all;
for(TopicIMap::const_iterator p = _topicIMap.begin(); p != _topicIMap.end(); ++p)
{
//
// Here we cannot just reconstruct the identity since the
// identity could be either "<instanceName>/topic.<topicname>"
// name, or if created with pre-3.2 IceStorm "/<topicname>".
//
all.insert(TopicDict::value_type(
p->first, TopicPrx::uncheckedCast(_topicAdapter->createProxy(p->second->id()))));
}
return all;
}
Ice::SliceChecksumDict
TopicManagerI::getSliceChecksums(const Ice::Current&) const
{
return Ice::sliceChecksums();
}
void
TopicManagerI::reap()
{
//
// Always called with mutex locked.
//
// IceUtil::Mutex::Lock sync(*this);
//
TopicIMap::iterator i = _topicIMap.begin();
while(i != _topicIMap.end())
{
if(i->second->destroyed())
{
Ice::Identity id = i->second->id();
TraceLevelsPtr traceLevels = _instance->traceLevels();
if(traceLevels->topicMgr > 0)
{
Ice::Trace out(traceLevels->logger, traceLevels->topicMgrCat);
out << "Reaping " << i->first;
}
_topics.erase(id);
try
{
_topicAdapter->remove(id);
}
catch(const Ice::ObjectAdapterDeactivatedException&)
{
// Ignore
}
_topicIMap.erase(i++);
}
else
{
++i;
}
}
}
void
TopicManagerI::shutdown()
{
IceUtil::Mutex::Lock sync(*this);
reap();
for(TopicIMap::const_iterator p = _topicIMap.begin(); p != _topicIMap.end(); ++p)
{
p->second->reap();
}
}
TopicPrx
TopicManagerI::installTopic(const string& name, const Ice::Identity& id, const LinkRecordSeq& rec, bool create)
{
//
// Called by constructor or with 'this' mutex locked.
//
TraceLevelsPtr traceLevels = _instance->traceLevels();
if(traceLevels->topicMgr > 0)
{
Ice::Trace out(traceLevels->logger, traceLevels->topicMgrCat);
if(create)
{
out << "creating new topic \"" << name << "\". id: "
<< _instance->communicator()->identityToString(id);
}
else
{
out << "loading topic \"" << name << "\" from database. id: "
<< _instance->communicator()->identityToString(id);
}
}
//
// Create topic implementation
//
TopicIPtr topicI = new TopicI(_instance, name, id, rec, _envName, _dbName);
//
// The identity is the name of the Topic.
//
TopicPrx prx = TopicPrx::uncheckedCast(_topicAdapter->add(topicI, id));
_topicIMap.insert(TopicIMap::value_type(name, topicI));
return prx;
}
|