Rev 1317 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1317 | Rev 1403 | ||
|---|---|---|---|
| Line 4... | Line 4... | ||
| 4 | * Created on: Jul 21, 2009 |
4 | * Created on: Jul 21, 2009 |
| 5 | * Author: Mattias Runge |
5 | * Author: Mattias Runge |
| 6 | */ |
6 | */ |
| 7 | 7 | ||
| 8 | #include "Subscriber.h" |
8 | #include "Subscriber.h" |
| - | 9 | ||
| - | 10 | #include "broker/Broker.h" |
|
| 9 | 11 | ||
| 10 | namespace atom { |
12 | namespace atom { |
| 11 | namespace broker { |
- | |
| 12 | 13 | ||
| 13 | Subscriber::Subscriber( |
14 | Subscriber::Subscriber(bool receive_from_self) |
| 14 | { |
15 | { |
| 15 | LOG.setName("Subscriber"); |
16 | LOG.setName("Subscriber"); |
| 16 | this->myBroker = broker; |
- | |
| 17 | this->myReceiveFromMyself = receiveFromMyself; |
- | |
| 18 | this->myBroker->connect(boost::bind(&Subscriber::newMessageHandler, this, _1)); |
- | |
| 19 | } |
- | |
| 20 | 17 | ||
| 21 |
|
18 | this->receive_from_self_ = receive_from_self; |
| 22 | { |
19 | |
| 23 |
|
20 | Broker::GetInstance()->Connect(boost::bind(&Subscriber::Slot_OnMessage, this, _1)); |
| 24 | } |
21 | } |
| 25 | 22 | ||
| 26 |
|
23 | Subscriber::~Subscriber() |
| 27 | { |
24 | { |
| 28 |
|
25 | this->Stop(); |
| - | 26 | } |
|
| - | 27 | ||
| - | 28 | void Subscriber::Put(Message::Pointer message) |
|
| - | 29 | { |
|
| 29 |
|
30 | Broker::GetInstance()->Put(message); |
| 30 | } |
31 | } |
| 31 | 32 | ||
| 32 |
|
33 | Message::Pointer CreateMessage() |
| 33 | { |
34 | { |
| - | 35 | return Message::Pointer(new Message(static_cast<void *>this)); |
|
| 34 | } |
36 | } |
| 35 | 37 | ||
| 36 | void Subscriber:: |
38 | void Subscriber::Slot_OnMessage(Message::Ă…ointer message) |
| 37 | { |
39 | { |
| 38 | if (this-> |
40 | if (this->receive_from_self_ || message->GetOrigin() != static_cast<void *>this) |
| 39 | { |
41 | { |
| 40 | this-> |
42 | this->queue_.push(message); |
| 41 | - | ||
| 42 | this-> |
43 | this->on_message_condition_.notify_all(); |
| 43 | } |
44 | } |
| 44 | } |
45 | } |
| 45 | 46 | ||
| 46 | void Subscriber:: |
47 | void Subscriber::Run() |
| 47 | { |
48 | { |
| 48 | while (true) |
49 | while (true) |
| 49 | { |
50 | { |
| 50 |
|
51 | boost::mutex::scoped_lock guard(this->guard_mutex_); |
| 51 | 52 | ||
| 52 | try |
53 | try |
| 53 | { |
54 | { |
| 54 | while (this-> |
55 | while (this->queue_.size() > 0) |
| 55 | { |
56 | { |
| 56 | this-> |
57 | this->OnMessage(this->queue_.pop()); |
| 57 | } |
58 | } |
| 58 | 59 | ||
| 59 | this-> |
60 | this->on_message_condition_.wait(guard); |
| 60 | } |
61 | } |
| 61 | catch (boost::thread_interrupted e) |
62 | catch (boost::thread_interrupted e) |
| 62 | { |
63 | { |
| 63 | guard.unlock(); |
64 | guard.unlock(); |
| 64 | LOG.warn("Thread interrupted"); |
65 | LOG.warn("Thread interrupted"); |
| 65 | break; |
66 | break; |
| 66 | } |
67 | } |
| 67 | } |
68 | } |
| 68 | } |
69 | } |
| 69 | 70 | ||
| 70 | } |
- | |
| 71 | } |
71 | } // namespace atom |