Skip to content
Navigation Menu
{{ message }}
forked from actor-framework/actor-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefault_protocol.cpp
More file actions
248 lines (223 loc) · 10.3 KB
/
Copy pathdefault_protocol.cpp
File metadata and controls
248 lines (223 loc) · 10.3 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/******************************************************************************\
* ___ __ *
* /\_ \ __/\ \ *
* \//\ \ /\_\ \ \____ ___ _____ _____ __ *
* \ \ \ \/\ \ \ '__`\ /'___\/\ '__`\/\ '__`\ /'__`\ *
* \_\ \_\ \ \ \ \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 3 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 <future>
#include <cstdint>
#include <iostream>
#include "cppa/logging.hpp"
#include "cppa/to_string.hpp"
#include "cppa/network/middleman.hpp"
#include "cppa/network/default_peer.hpp"
#include "cppa/network/ipv4_acceptor.hpp"
#include "cppa/network/ipv4_io_stream.hpp"
#include "cppa/network/default_protocol.hpp"
#include "cppa/network/default_peer_acceptor.hpp"
#include "cppa/detail/actor_registry.hpp"
#include "cppa/detail/singleton_manager.hpp"
#include "cppa/intrusive/blocking_single_reader_queue.hpp"
using namespace std;
using namespace cppa::detail;
namespace cppa { namespace network {
default_protocol::default_protocol(abstract_middleman* parent)
: super(parent), m_addressing(this) { }
atom_value default_protocol::identifier() const {
return atom("DEFAULT");
}
void default_protocol::publish(const actor_ptr& whom, variant_args args) {
CPPA_LOG_TRACE(CPPA_TARG(whom, to_string)
<< ", args.size() = " << args.size());
if (!whom) return;
CPPA_REQUIRE(args.size() == 2 || args.size() == 1);
auto i = args.begin();
if (args.size() == 1) {
auto port = get<uint16_t>(*i);
CPPA_LOG_INFO("publish " << to_string(whom) << " on port " << port);
publish(whom, ipv4_acceptor::create(port), {});
}
else if (args.size() == 2) {
auto port = get<uint16_t>(*i++);
auto& addr = get<string>(*i);
CPPA_LOG_INFO("publish " << to_string(whom) << " on port " << port
<< " with addr = " << addr);
publish(whom, ipv4_acceptor::create(port, addr.c_str()), {});
}
else throw logic_error("wrong number of arguments, expected one or two");
}
void default_protocol::publish(const actor_ptr& whom,
std::unique_ptr<acceptor> ptr,
variant_args args ) {
CPPA_LOG_TRACE(CPPA_TARG(whom, to_string) << ", " << CPPA_MARG(ptr, get)
<< ", args.size() = " << args.size());
if (!whom) return;
CPPA_REQUIRE(args.size() == 0);
static_cast<void>(args); // keep compiler happy
singleton_manager::get_actor_registry()->put(whom->id(), whom);
default_protocol_ptr proto = this;
auto impl = make_counted<default_peer_acceptor>(this, move(ptr), whom);
run_later([=] {
CPPA_LOGF_TRACE("lambda from default_protocol::publish");
proto->m_acceptors[whom].push_back(impl);
proto->continue_reader(impl.get());
});
}
void default_protocol::unpublish(const actor_ptr& whom) {
CPPA_LOG_TRACE("whom = " << to_string(whom));
default_protocol_ptr proto = this;
run_later([=] {
CPPA_LOGF_TRACE("lambda from default_protocol::unpublish");
auto& acceptors = m_acceptors[whom];
for (auto& ptr : acceptors) {
proto->stop_reader(ptr.get());
}
m_acceptors.erase(whom);
});
}
void default_protocol::register_peer(const process_information& node,
default_peer* ptr) {
CPPA_LOG_TRACE("node = " << to_string(node) << ", ptr = " << ptr);
auto& entry = m_peers[node];
if (entry.impl == nullptr) {
if (entry.queue == nullptr) entry.queue.emplace();
ptr->set_queue(entry.queue);
entry.impl.reset(ptr);
if (!entry.queue->empty()) {
auto tmp = entry.queue->pop();
ptr->enqueue(tmp.first, tmp.second);
}
}
else { CPPA_LOG_ERROR("peer " << to_string(node) << " already defined"); }
}
default_peer_ptr default_protocol::get_peer(const process_information& n) {
CPPA_LOG_TRACE("n = " << to_string(n));
auto i = m_peers.find(n);
if (i != m_peers.end()) {
CPPA_LOG_DEBUG("result = " << i->second.impl.get());
return i->second.impl;
}
CPPA_LOG_DEBUG("result = nullptr");
return nullptr;
}
void default_protocol::enqueue(const process_information& node,
const message_header& hdr,
any_tuple msg) {
auto& entry = m_peers[node];
if (entry.impl) {
CPPA_REQUIRE(entry.queue != nullptr);
if (!entry.impl->has_unwritten_data()) {
CPPA_REQUIRE(entry.queue->empty());
entry.impl->enqueue(hdr, msg);
return;
}
}
if (entry.queue == nullptr) entry.queue.emplace();
entry.queue->emplace(hdr, msg);
}
actor_ptr default_protocol::remote_actor(variant_args args) {
CPPA_LOG_TRACE("args.size() = " << args.size());
CPPA_REQUIRE(args.size() == 2);
auto i = args.begin();
auto port = get<uint16_t>(*i++);
auto& host = get<string>(*i);
auto io = ipv4_io_stream::connect_to(host.c_str(), port);
return remote_actor(io_stream_ptr_pair(io, io), {});
}
struct remote_actor_result { remote_actor_result* next; actor_ptr value; };
actor_ptr default_protocol::remote_actor(io_stream_ptr_pair io,
variant_args args ) {
CPPA_LOG_TRACE("io = {" << io.first.get() << ", " << io.second.get() << "}, "
<< "args.size() = " << args.size());
CPPA_REQUIRE(args.size() == 0);
static_cast<void>(args); // keep compiler happy when compiling w/o debug
auto pinf = process_information::get();
std::uint32_t process_id = pinf->process_id();
// throws on error
io.second->write(&process_id, sizeof(std::uint32_t));
io.second->write(pinf->node_id().data(), pinf->node_id().size());
actor_id remote_aid;
std::uint32_t peer_pid;
process_information::node_id_type peer_node_id;
io.first->read(&remote_aid, sizeof(actor_id));
io.first->read(&peer_pid, sizeof(std::uint32_t));
io.first->read(peer_node_id.data(), peer_node_id.size());
auto pinfptr = make_counted<process_information>(peer_pid, peer_node_id);
if (*pinf == *pinfptr) {
// dude, this is not a remote actor, it's a local actor!
CPPA_LOG_ERROR("remote_actor() called to access a local actor");
# ifndef CPPA_DEBUG
std::cerr << "*** warning: remote_actor() called to access a local actor\n"
<< std::flush;
# endif
return singleton_manager::get_actor_registry()->get(remote_aid);
}
default_protocol_ptr proto = this;
intrusive::blocking_single_reader_queue<remote_actor_result> q;
run_later([proto, io, pinfptr, remote_aid, &q] {
CPPA_LOGF_TRACE("lambda from default_protocol::remote_actor");
auto pp = proto->get_peer(*pinfptr);
CPPA_LOGF_INFO_IF(pp, "connection already exists (re-use old one)");
if (!pp) proto->new_peer(io.first, io.second, pinfptr);
auto res = proto->addressing()->get_or_put(*pinfptr, remote_aid);
q.push_back(new remote_actor_result{0, res});
});
unique_ptr<remote_actor_result> result(q.pop());
CPPA_LOGF_DEBUG("result = " << result->value.get());
return result->value;
}
void default_protocol::last_proxy_exited(const default_peer_ptr& pptr) {
CPPA_REQUIRE(pptr != nullptr);
CPPA_LOG_TRACE("pptr = " << pptr.get()
<< ", pptr->node() = " << to_string(pptr->node()));
if (pptr->erase_on_last_proxy_exited() && pptr->queue().empty()) {
stop_reader(pptr.get());
auto i = m_peers.find(pptr->node());
if (i != m_peers.end()) {
CPPA_LOG_DEBUG_IF(i->second.impl != pptr,
"node " << to_string(pptr->node())
<< " does not exist in m_peers");
if (i->second.impl == pptr) {
m_peers.erase(i);
}
}
}
}
void default_protocol::new_peer(const input_stream_ptr& in,
const output_stream_ptr& out,
const process_information_ptr& node) {
CPPA_LOG_TRACE("");
auto ptr = make_counted<default_peer>(this, in, out, node);
continue_reader(ptr.get());
if (node) register_peer(*node, ptr.get());
}
void default_protocol::continue_writer(const default_peer_ptr& pptr) {
CPPA_LOG_TRACE(CPPA_MARG(pptr, get));
super::continue_writer(pptr.get());
}
default_actor_addressing* default_protocol::addressing() {
return &m_addressing;
}
} } // namespace cppa::network
You can’t perform that action at this time.
