Skip to content
Navigation Menu
{{ message }}
forked from insooth/libcppa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal_actor.cpp
More file actions
224 lines (191 loc) · 8.8 KB
/
Copy pathlocal_actor.cpp
File metadata and controls
224 lines (191 loc) · 8.8 KB
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
/******************************************************************************\
* ___ __ *
* /\_ \ __/\ \ *
* \//\ \ /\_\ \ \____ ___ _____ _____ __ *
* \ \ \ \/\ \ \ '__`\ /'___\/\ '__`\/\ '__`\ /'__`\ *
* \_\ \_\ \ \ \ \L\ \/\ \__/\ \ \L\ \ \ \L\ \/\ \L\.\_ *
* /\____\\ \_\ \_,__/\ \____\\ \ ,__/\ \ ,__/\ \__/.\_\ *
* \/____/ \/_/\/___/ \/____/ \ \ \/ \ \ \/ \/__/\/_/ *
* \ \_\ \ \_\ *
* \/_/ \/_/ *
* *
* Copyright (C) 2011-2013 *
* Dominik Charousset <dominik.charousset@haw-hamburg.de> *
* *
* This file is part of libcppa. *
* libcppa is free software: you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, *
* or (at your option) any later version. *
* *
* libcppa is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with libcppa. If not, see <http://www.gnu.org/licenses/>. *
\******************************************************************************/
#include <string>
#include "cppa/cppa.hpp"
#include "cppa/atom.hpp"
#include "cppa/logging.hpp"
#include "cppa/scheduler.hpp"
#include "cppa/local_actor.hpp"
#include "cppa/detail/raw_access.hpp"
namespace cppa {
namespace {
class down_observer : public attachable {
actor_addr m_observer;
actor_addr m_observed;
public:
down_observer(actor_addr observer, actor_addr observed)
: m_observer(std::move(observer)), m_observed(std::move(observed)) {
CPPA_REQUIRE(m_observer != nullptr);
CPPA_REQUIRE(m_observed != nullptr);
}
void actor_exited(std::uint32_t reason) {
auto ptr = detail::raw_access::get(m_observer);
message_header hdr{m_observed, ptr, message_id{}.with_high_priority()};
hdr.deliver(make_any_tuple(down_msg{m_observed, reason}));
}
bool matches(const attachable::token& match_token) {
if (match_token.subtype == typeid(down_observer)) {
auto ptr = reinterpret_cast<const local_actor*>(match_token.ptr);
return m_observer == ptr->address();
}
return false;
}
};
} // namespace <anonymous>
local_actor::local_actor()
: m_trap_exit(false)
, m_dummy_node(), m_current_node(&m_dummy_node)
, m_planned_exit_reason(exit_reason::not_exited)
, m_state(actor_state::ready) {
m_node = get_middleman()->node();
}
local_actor::~local_actor() { }
void local_actor::monitor(const actor_addr& whom) {
if (!whom) return;
auto ptr = detail::raw_access::get(whom);
ptr->attach(attachable_ptr{new down_observer(address(), whom)});
}
void local_actor::demonitor(const actor_addr& whom) {
if (!whom) return;
auto ptr = detail::raw_access::get(whom);
attachable::token mtoken{typeid(down_observer), this};
ptr->detach(mtoken);
}
void local_actor::on_exit() { }
void local_actor::join(const group& what) {
CPPA_LOG_TRACE(CPPA_TSARG(what));
if (what && m_subscriptions.count(what) == 0) {
CPPA_LOG_DEBUG("join group: " << to_string(what));
m_subscriptions.insert(std::make_pair(what, what->subscribe(this)));
}
}
void local_actor::leave(const group& what) {
if (what) m_subscriptions.erase(what);
}
std::vector<group> local_actor::joined_groups() const {
std::vector<group> result;
for (auto& kvp : m_subscriptions) {
result.emplace_back(kvp.first);
}
return result;
}
void local_actor::reply_message(any_tuple&& what) {
auto& whom = m_current_node->sender;
if (!whom) return;
auto& id = m_current_node->mid;
if (id.valid() == false || id.is_response()) {
send_tuple(detail::raw_access::get(whom), std::move(what));
}
else if (!id.is_answered()) {
auto ptr = detail::raw_access::get(whom);
ptr->enqueue({address(), ptr, id.response_id()}, std::move(what));
id.mark_as_answered();
}
}
void local_actor::forward_message(const actor& dest, message_priority p) {
if (!dest) return;
auto id = (p == message_priority::high)
? m_current_node->mid.with_high_priority()
: m_current_node->mid.with_normal_priority();
detail::raw_access::get(dest)->enqueue({m_current_node->sender, detail::raw_access::get(dest), id}, m_current_node->msg);
// treat this message as asynchronous message from now on
m_current_node->mid = message_id::invalid;
}
void local_actor::send_tuple(message_priority prio, const channel& dest, any_tuple what) {
if (!dest) return;
message_id id;
if (prio == message_priority::high) id = id.with_high_priority();
dest->enqueue({address(), dest, id}, std::move(what));
}
void local_actor::send_exit(const actor_addr& whom, std::uint32_t reason) {
send(detail::raw_access::get(whom), exit_msg{address(), reason});
}
void local_actor::delayed_send_tuple(message_priority prio,
const channel& dest,
const util::duration& rel_time,
cppa::any_tuple msg) {
message_id mid;
if (prio == message_priority::high) mid = mid.with_high_priority();
get_scheduler()->delayed_send({address(), dest, mid},
rel_time, std::move(msg));
}
response_promise local_actor::make_response_promise() {
auto n = m_current_node;
response_promise result{address(), n->sender, n->mid.response_id()};
n->mid.mark_as_answered();
return result;
}
void local_actor::cleanup(std::uint32_t reason) {
CPPA_LOG_TRACE(CPPA_ARG(reason));
m_subscriptions.clear();
super::cleanup(reason);
}
void local_actor::quit(std::uint32_t reason) {
CPPA_LOG_TRACE("reason = " << reason
<< ", class " << detail::demangle(typeid(*this)));
if (reason == exit_reason::unallowed_function_call) {
// this is the only reason that causes an exception
cleanup(reason);
CPPA_LOG_WARNING("actor tried to use a blocking function");
// when using receive(), the non-blocking nature of event-based
// actors breaks any assumption the user has about his code,
// in particular, receive_loop() is a deadlock when not throwing
// an exception here
aout(this) << "*** warning: event-based actor killed because it tried "
"to use receive()\n";
throw actor_exited(reason);
}
planned_exit_reason(reason);
}
message_id local_actor::timed_sync_send_tuple_impl(message_priority mp,
const actor& dest,
const util::duration& rtime,
any_tuple&& what) {
auto nri = new_request_id();
if (mp == message_priority::high) nri = nri.with_high_priority();
dest->enqueue({address(), dest, nri}, std::move(what));
auto rri = nri.response_id();
get_scheduler()->delayed_send({address(), this, rri}, rtime,
make_any_tuple(sync_timeout_msg{}));
return rri;
}
message_id local_actor::sync_send_tuple_impl(message_priority mp,
const actor& dest,
any_tuple&& what) {
auto nri = new_request_id();
if (mp == message_priority::high) nri = nri.with_high_priority();
dest->enqueue({address(), dest, nri}, std::move(what));
return nri.response_id();
}
void anon_send_exit(const actor_addr& whom, std::uint32_t reason) {
auto ptr = detail::raw_access::get(whom);
ptr->enqueue({invalid_actor_addr, ptr, message_id{}.with_high_priority()},
make_any_tuple(exit_msg{invalid_actor_addr, reason}));
}
} // namespace cppa
You can’t perform that action at this time.
