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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
|
// **********************************************************************
//
// 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/Subscribers.h>
#include <IceStorm/Instance.h>
#include <IceStorm/TraceLevels.h>
#include <IceStorm/BatchFlusher.h>
#include <IceStorm/SubscriberPool.h>
#include <Ice/ObjectAdapter.h>
#include <Ice/LoggerUtil.h>
#include <Ice/Communicator.h>
#include <Ice/LocalException.h>
#include <Ice/Connection.h>
using namespace std;
using namespace IceStorm;
//
// Per Subscriber object.
//
namespace IceStorm
{
class PerSubscriberPublisherProxyI : public Ice::BlobjectArray
{
public:
PerSubscriberPublisherProxyI(const InstancePtr& instance) :
_instance(instance)
{
}
~PerSubscriberPublisherProxyI()
{
}
void
setSubscriber(const SubscriberPtr& subscriber)
{
_subscriber = subscriber;
}
virtual bool
ice_invoke(const pair<const Ice::Byte*, const Ice::Byte*>& inParams,
vector<Ice::Byte>&,
const Ice::Current& current)
{
EventPtr event = new Event;
event->op = current.operation;
event->mode = current.mode;
vector<Ice::Byte>(inParams.first, inParams.second).swap(event->data);
event->context = current.ctx;
EventSeq e;
e.push_back(event);
Subscriber::QueueState state = _subscriber->queue(false, e);
if(state == Subscriber::QueueStateFlush)
{
list<SubscriberPtr> l;
l.push_back(_subscriber);
_instance->subscriberPool()->add(l);
}
return true;
}
private:
const InstancePtr _instance;
/*const*/ SubscriberPtr _subscriber;
};
typedef IceUtil::Handle<PerSubscriberPublisherProxyI> PerSubscriberPublisherProxyIPtr;
}
// Each of the various Subscriber types.
namespace IceStorm
{
class SubscriberOneway : public Subscriber
{
public:
SubscriberOneway(const InstancePtr&,
const Ice::ObjectPrx&,
const Ice::ObjectPrx&,
bool);
//
// Oneway
//
virtual bool flush();
virtual void destroy();
private:
const bool _batch;
const Ice::ObjectPrx _obj;
const Ice::ObjectPrx _objBatch;
};
class SubscriberTwoway : public Subscriber
{
public:
SubscriberTwoway(const InstancePtr&,
const Ice::ObjectPrx&,
const Ice::ObjectPrx&);
virtual bool flush();
private:
const Ice::ObjectPrx _obj;
};
//
// Twoway Ordered
//
class SubscriberTwowayOrdered : public Subscriber
{
public:
SubscriberTwowayOrdered(const InstancePtr&,
const Ice::ObjectPrx&,
const Ice::ObjectPrx&);
virtual bool flush();
void response();
private:
const Ice::ObjectPrx _obj;
};
typedef IceUtil::Handle<SubscriberTwowayOrdered> SubscriberTwowayOrderedPtr;
class SubscriberLink : public Subscriber
{
public:
SubscriberLink(const InstancePtr&,
const TopicLinkPrx&,
int);
virtual QueueState queue(bool, const std::vector<EventPtr>&);
virtual bool flush();
void response();
private:
const TopicLinkPrx _obj;
const int _cost;
};
typedef IceUtil::Handle<SubscriberLink> SubscriberLinkPtr;
}
SubscriberOneway::SubscriberOneway(
const InstancePtr& instance,
const Ice::ObjectPrx& proxy,
const Ice::ObjectPrx& obj,
bool batch) :
Subscriber(instance, proxy, false, obj->ice_getIdentity()),
_batch(batch),
_obj(obj),
_objBatch(obj->ice_isDatagram() ? obj->ice_batchDatagram() : obj->ice_batchOneway())
{
if(batch)
{
_instance->batchFlusher()->add(_obj);
}
}
bool
SubscriberOneway::flush()
{
IceUtil::Mutex::Lock sync(_mutex);
//
// If another thread is busy delivering events or we're no longer
// active then we have nothing left to do.
//
if(_state != StateActive || _events.empty())
{
_busy = false;
return false;
}
assert(_busy);
try
{
if(_state != StateActive)
{
assert(!_busy);
return false;
}
//
// Get the current set of events, but release the lock before
// attempting to deliver the events. This allows other threads
// to add events in case we block (such as during connection
// establishment).
//
EventSeq v;
v.swap(_events);
sync.release();
// XXX:
/*
TraceLevelsPtr traceLevels = _instance->traceLevels();
if(_obj->ice_getIdentity().name.substr(0, 4) == "slow")
{
Ice::Trace out(traceLevels->logger, traceLevels->subscriberCat);
out << "deliberately stalling";
sleep(1);
}
*/
//
// Deliver the events without holding the lock.
//
// If there are more than one event queued and we are not in
// batch sending mode then send the events as a batch and then
// flush immediately, otherwise send one at a time.
//
vector<Ice::Byte> dummy;
if(v.size() > 1 && !_batch)
{
for(EventSeq::const_iterator p = v.begin(); p != v.end(); ++p)
{
_objBatch->ice_invoke((*p)->op, (*p)->mode, (*p)->data, dummy, (*p)->context);
}
Ice::ConnectionPtr conn = _objBatch->ice_getCachedConnection();
assert(conn);
conn->flushBatchRequests();
}
else
{
for(EventSeq::const_iterator p = v.begin(); p != v.end(); ++p)
{
_obj->ice_invoke((*p)->op, (*p)->mode, (*p)->data, dummy, (*p)->context);
}
}
//
// Reacquire the lock before we check the queue again.
//
sync.acquire();
//
// If there have been more events queued in the meantime then
// we are still busy.
//
if(_events.empty())
{
_busy = false;
}
}
catch(const Ice::LocalException& ex)
{
assert(!sync.acquired());
// setError will re-acquire and release the lock.
setError(ex);
return false;
}
return _busy;
}
void
SubscriberOneway::destroy()
{
if(_batch)
{
_instance->batchFlusher()->remove(_obj);
}
Subscriber::destroy();
}
namespace IceStorm
{
class TwowayInvokeI : public Ice::AMI_Object_ice_invoke
{
public:
TwowayInvokeI(const SubscriberPtr& subscriber) :
_subscriber(subscriber)
{
}
virtual void
ice_response(bool, const std::vector<Ice::Byte>&)
{
}
virtual void
ice_exception(const Ice::Exception& e)
{
_subscriber->setError(e);
}
private:
const SubscriberPtr _subscriber;
};
}
SubscriberTwoway::SubscriberTwoway(
const InstancePtr& instance,
const Ice::ObjectPrx& proxy,
const Ice::ObjectPrx& obj) :
Subscriber(instance, proxy, false, obj->ice_getIdentity()),
_obj(obj)
{
}
bool
SubscriberTwoway::flush()
{
IceUtil::Mutex::Lock sync(_mutex);
//
// If another thread is busy delivering events or we're no longer
// active then we have nothing left to do.
//
if(_state != StateActive || _events.empty())
{
_busy = false;
return false;
}
assert(_busy);
if(_state != StateActive)
{
assert(!_busy);
return false;
}
//
// Get the current set of events, but release the lock before
// attempting to deliver the events. This allows other threads
// to add events in case we block (such as during connection
// establishment).
//
EventSeq v;
v.swap(_events);
sync.release();
//
// Deliver the events without holding the lock.
//
for(EventSeq::const_iterator p = v.begin(); p != v.end(); ++p)
{
_obj->ice_invoke_async(new TwowayInvokeI(this), (*p)->op, (*p)->mode, (*p)->data, (*p)->context);
}
//
// Reacquire the lock before we check the queue again.
//
sync.acquire();
//
// If there have been more events queued in the meantime then we
// are still busy.
//
if(_events.empty())
{
_busy = false;
}
return _busy;
}
namespace IceStorm
{
class TwowayOrderedInvokeI : public Ice::AMI_Object_ice_invoke
{
public:
TwowayOrderedInvokeI(const SubscriberTwowayOrderedPtr& subscriber) :
_subscriber(subscriber)
{
}
virtual void
ice_response(bool, const std::vector<Ice::Byte>&)
{
_subscriber->response();
}
virtual void
ice_exception(const Ice::Exception& ex)
{
_subscriber->setError(ex);
}
private:
const SubscriberTwowayOrderedPtr _subscriber;
};
}
SubscriberTwowayOrdered::SubscriberTwowayOrdered(
const InstancePtr& instance,
const Ice::ObjectPrx& proxy,
const Ice::ObjectPrx& obj) :
Subscriber(instance, proxy, false, obj->ice_getIdentity()),
_obj(obj)
{
}
bool
SubscriberTwowayOrdered::flush()
{
EventPtr e;
{
IceUtil::Mutex::Lock sync(_mutex);
//
// If another thread is busy delivering events or we're no longer
// active then we have nothing left to do.
//
if(_state != StateActive || _events.empty())
{
_busy = false;
return false;
}
assert(_busy);
e = _events.front();
_events.erase(_events.begin());
}
_obj->ice_invoke_async(new TwowayOrderedInvokeI(this), e->op, e->mode, e->data, e->context);
return false;
}
void
SubscriberTwowayOrdered::response()
{
EventPtr e;
{
IceUtil::Mutex::Lock sync(_mutex);
assert(_state == StateActive && _busy);
if(_events.empty())
{
_busy = false;
return;
}
e = _events.front();
_events.erase(_events.begin());
}
_obj->ice_invoke_async(new TwowayOrderedInvokeI(this), e->op, e->mode, e->data, e->context);
}
namespace IceStorm
{
class Topiclink_forwardI : public IceStorm::AMI_TopicLink_forward
{
public:
Topiclink_forwardI(const SubscriberLinkPtr& subscriber) : _subscriber(subscriber)
{
}
virtual void
ice_response()
{
_subscriber->response();
}
virtual void
ice_exception(const Ice::Exception& ex)
{
try
{
ex.ice_throw();
}
catch(const Ice::ObjectNotExistException& ex)
{
_subscriber->setError(ex);
}
catch(const Ice::LocalException& ex)
{
_subscriber->setUnreachable(ex);
}
}
private:
const SubscriberLinkPtr _subscriber;
};
}
SubscriberLink::SubscriberLink(
const InstancePtr& instance,
const TopicLinkPrx& obj,
int cost) :
Subscriber(instance, 0, true, obj->ice_getIdentity()),
_obj(TopicLinkPrx::uncheckedCast(obj->ice_collocationOptimized(false))),
_cost(cost)
{
}
Subscriber::QueueState
SubscriberLink::queue(bool forwarded, const EventSeq& events)
{
if(forwarded)
{
return QueueStateNoFlush;
}
//
// Don't propagate a message that has already been forwarded.
// Also, if this link has a non-zero cost, then don't propagate
// a message whose cost exceeds the link cost.
//
IceUtil::Mutex::Lock sync(_mutex);
if(_state != StateActive)
{
//
// Either the state is error here or the link is inactive.
//
return (_state == StateError) ? QueueStateError : QueueStateNoFlush;
}
size_t s = _events.size();
for(EventSeq::const_iterator p = events.begin(); p != events.end(); ++p)
{
if(_cost != 0)
{
//
// Note that we could calculate this cost once and
// cache it in a private form of the event to avoid
// this if this really is a performance problem.
//
int cost = 0;
Ice::Context::const_iterator q = (*p)->context.find("cost");
if(q != (*p)->context.end())
{
cost = atoi(q->second.c_str());
}
if(cost > _cost)
{
continue;
}
}
_events.push_back(*p);
}
//
// If no event was queued, or we're busy then the subscriber
// doesn't need to be flushed, otherwise it must be.
//
if(_busy || s == _events.size())
{
return QueueStateNoFlush;
}
_busy = true;
return QueueStateFlush;
}
bool
SubscriberLink::flush()
{
EventSeq v;
{
IceUtil::Mutex::Lock sync(_mutex);
//
// If another thread is busy delivering events or we're no longer
// active then we have nothing left to do.
//
if(_state != StateActive || _events.empty())
{
_busy = false;
return false;
}
assert(_busy);
v.swap(_events);
}
_obj->forward_async(new Topiclink_forwardI(this), v);
return false;
}
void
SubscriberLink::response()
{
EventSeq v;
{
IceUtil::Mutex::Lock sync(_mutex);
assert(_state == StateActive && _busy);
if(_events.empty())
{
_busy = false;
return;
}
v.swap(_events);
}
_obj->forward_async(new Topiclink_forwardI(this), v);
}
SubscriberPtr
Subscriber::create(
const InstancePtr& instance,
const Ice::ObjectPrx& obj,
const IceStorm::QoS& qos)
{
PerSubscriberPublisherProxyIPtr per = new PerSubscriberPublisherProxyI(instance);
Ice::ObjectPrx proxy = instance->objectAdapter()->addWithUUID(per);
TraceLevelsPtr traceLevels = instance->traceLevels();
SubscriberPtr subscriber;
try
{
string reliability = "oneway";
QoS::const_iterator p = qos.find("reliability");
if(p != qos.end())
{
reliability = p->second;
}
Ice::ObjectPrx newObj;
if(reliability == "batch")
{
if(obj->ice_isDatagram())
{
newObj = obj->ice_batchDatagram();
}
else
{
newObj = obj->ice_batchOneway();
}
subscriber = new SubscriberOneway(instance, proxy, newObj, true);
}
else if(reliability == "twoway")
{
newObj = obj->ice_twoway();
subscriber = new SubscriberTwoway(instance, proxy, newObj);
}
else if(reliability == "twoway ordered")
{
newObj = obj->ice_twoway();
subscriber = new SubscriberTwowayOrdered(instance, proxy, newObj);
}
else // reliability == "oneway"
{
if(reliability != "oneway" && traceLevels->subscriber > 0)
{
Ice::Trace out(traceLevels->logger, traceLevels->subscriberCat);
out << reliability <<" mode not understood.";
}
if(obj->ice_isDatagram())
{
newObj = obj;
}
else
{
newObj = obj->ice_oneway();
}
subscriber = new SubscriberOneway(instance, proxy, newObj, false);
}
per->setSubscriber(subscriber);
}
catch(const Ice::Exception&)
{
instance->objectAdapter()->remove(proxy->ice_getIdentity());
throw;
}
return subscriber;
}
SubscriberPtr
Subscriber::create(
const InstancePtr& instance,
const TopicLinkPrx& link,
int cost)
{
return new SubscriberLink(instance, link, cost);
}
Subscriber::~Subscriber()
{
}
void
Subscriber::reachable()
{
IceUtil::Mutex::Lock sync(_mutex);
if(_state == StateUnreachable)
{
_state = StateActive;
TraceLevelsPtr traceLevels = _instance->traceLevels();
if(traceLevels->subscriber > 0)
{
Ice::Trace out(traceLevels->logger, traceLevels->subscriberCat);
out << "Reachable " << _instance->communicator()->identityToString(id());
}
}
}
Ice::ObjectPrx
Subscriber::proxy() const
{
return _proxy;
}
Ice::Identity
Subscriber::id() const
{
return _id;
}
bool
Subscriber::persistent() const
{
return _persistent;
}
Subscriber::QueueState
Subscriber::queue(bool, const EventSeq& events)
{
IceUtil::Mutex::Lock sync(_mutex);
if(_state != StateActive)
{
return QueueStateError;
}
copy(events.begin(), events.end(), back_inserter(_events));
//
// If another thread is busy delivering events then the subscriber
// does not need to be flushed.
//
if(_busy)
{
return QueueStateNoFlush;
}
_busy = true;
return QueueStateFlush;
}
void
Subscriber::destroy()
{
//
// Clear the per-subscriber object if it exists.
//
if(_proxy)
{
try
{
_instance->objectAdapter()->remove(_proxy->ice_getIdentity());
}
catch(const Ice::NotRegisteredException&)
{
// Ignore
}
}
}
void
Subscriber::setError(const Ice::Exception& e)
{
IceUtil::Mutex::Lock sync(_mutex);
if(_state != StateError)
{
_state = StateError;
_busy = false;
TraceLevelsPtr traceLevels = _instance->traceLevels();
if(traceLevels->subscriber > 0)
{
Ice::Trace out(traceLevels->logger, traceLevels->subscriberCat);
out << _instance->communicator()->identityToString(_id) << ": topic publish failed: " << e;
}
}
}
void
Subscriber::setUnreachable(const Ice::Exception& e)
{
IceUtil::Mutex::Lock sync(_mutex);
if(_state != StateUnreachable)
{
_state = StateUnreachable;
_busy = false;
TraceLevelsPtr traceLevels = _instance->traceLevels();
if(traceLevels->subscriber > 0)
{
Ice::Trace out(traceLevels->logger, traceLevels->subscriberCat);
out << _instance->communicator()->identityToString(_id) << ": link publish unreachable: " << e;
}
}
}
Subscriber::Subscriber(
const InstancePtr& instance,
const Ice::ObjectPrx& proxy,
bool persistent,
const Ice::Identity& id) :
_instance(instance),
_id(id),
_persistent(persistent),
_proxy(proxy),
_state(StateActive),
_busy(false)
{
}
bool
IceStorm::operator==(const SubscriberPtr& subscriber, const Ice::Identity& id)
{
return subscriber->id() == id;
}
|