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
|
// **********************************************************************
//
// Copyright (c) 2003-2006 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 <IceStorm/OnewaySubscriber.h>
#include <IceStorm/TraceLevels.h>
#include <IceStorm/Event.h>
#include <IceStorm/QueuedProxy.h>
#include <Ice/ObjectAdapter.h>
#include <Ice/LoggerUtil.h>
#include <Ice/Communicator.h>
#include <Ice/LocalException.h>
#ifdef __BCPLUSPLUS__
#include <IceStorm/SubscriberFactory.h>
#endif
using namespace IceStorm;
using namespace std;
namespace IceStorm
{
class PerSubscriberPublisherProxyI : public Ice::BlobjectArray
{
public:
PerSubscriberPublisherProxyI(const SubscriberPtr& subscriber) :
_subscriber(subscriber)
{
}
~PerSubscriberPublisherProxyI()
{
}
virtual bool
ice_invoke(const pair<const Ice::Byte*, const Ice::Byte*>& inParams, vector< Ice::Byte>& outParam,
const Ice::Current& current)
{
const Ice::Context& context = current.ctx;
EventPtr event = new Event;
event->forwarded = false;
Ice::Context::const_iterator p = context.find("cost");
if(p != context.end())
{
event->cost = atoi(p->second.c_str());
}
else
{
event->cost = 0; // TODO: Default comes from property?
}
event->op = current.operation;
event->mode = current.mode;
vector<Ice::Byte>(inParams.first, inParams.second).swap(event->data);
event->context = context;
_subscriber->publish(event);
return true;
}
private:
//
// Set of associated subscribers
//
const SubscriberPtr _subscriber;
};
}
OnewaySubscriber::OnewaySubscriber(const SubscriberFactoryPtr& factory, const Ice::CommunicatorPtr& communicator,
const TraceLevelsPtr& traceLevels, const Ice::ObjectAdapterPtr& adapter,
const QueuedProxyPtr& obj) :
Subscriber(factory, communicator, traceLevels, obj),
_adapter(adapter)
{
}
OnewaySubscriber::~OnewaySubscriber()
{
}
bool
OnewaySubscriber::persistent() const
{
return false;
}
void
OnewaySubscriber::activate()
{
_proxy = _adapter->addWithUUID(new PerSubscriberPublisherProxyI(this));
}
void
OnewaySubscriber::unsubscribe()
{
Subscriber::unsubscribe();
//
// Clear the per-subscriber object.
//
try
{
_adapter->remove(_proxy->ice_getIdentity());
}
catch(const Ice::NotRegisteredException&)
{
// Ignore
}
}
void
OnewaySubscriber::replace()
{
Subscriber::replace();
//
// Clear the per-subscriber object.
//
try
{
_adapter->remove(_proxy->ice_getIdentity());
}
catch(const Ice::NotRegisteredException&)
{
// Ignore
}
}
void
OnewaySubscriber::publish(const EventPtr& event)
{
try
{
_obj->publish(event);
}
catch(const Ice::LocalException& e)
{
IceUtil::Mutex::Lock sync(_stateMutex);
//
// It's possible that the subscriber was unsubscribed, or
// marked invalid by another thread. Don't display a
// diagnostic in this case.
//
if(_state == StateActive)
{
if(_traceLevels->subscriber > 0)
{
Ice::Trace out(_traceLevels->logger, _traceLevels->subscriberCat);
out << _desc << ": publish failed: " << e;
}
_state = StateError;
//
// Clear the per-subscriber object.
//
try
{
_adapter->remove(_proxy->ice_getIdentity());
}
catch(const Ice::NotRegisteredException&)
{
// Ignore
}
}
}
}
Ice::ObjectPrx
OnewaySubscriber::proxy() const
{
assert(_proxy);
return _proxy;
}
|