File: broker.cpp

package info (click to toggle)
actor-framework 0.18.7-1~exp1
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 8,740 kB
  • sloc: cpp: 85,162; sh: 491; python: 187; makefile: 11
file content (168 lines) | stat: -rw-r--r-- 4,786 bytes parent folder | download
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
// This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
// the main distribution directory for license terms and copyright or visit
// https://github.com/actor-framework/actor-framework/blob/master/LICENSE.

#define CAF_SUITE io.broker

#include "caf/io/broker.hpp"

#include "io-test.hpp"

#include <cstdint>
#include <iostream>
#include <memory>

#include "caf/all.hpp"
#include "caf/io/all.hpp"

using namespace caf;
using namespace caf::io;

namespace {

struct suite_state {
  int pings = 0;
  int pongs = 0;
  suite_state() = default;
};

using suite_state_ptr = std::shared_ptr<suite_state>;

behavior ping(event_based_actor* self, suite_state_ptr ssp) {
  return {
    [=](ok_atom, const actor& pong) {
      MESSAGE("received `ok_atom`");
      ++ssp->pings;
      self->send(pong, ping_atom_v);
      self->become({
        [=](pong_atom) {
          MESSAGE("ping: received pong");
          self->send(pong, ping_atom_v);
          if (++ssp->pings == 10) {
            self->quit();
            MESSAGE("ping is done");
          }
        },
        [=](ping_atom) { CAF_FAIL("ping received a ping message"); },
      });
    },
  };
}

behavior pong(event_based_actor* self, suite_state_ptr ssp) {
  return {
    [=](ping_atom) -> pong_atom {
      MESSAGE("pong: received ping");
      if (++ssp->pongs == 10) {
        self->quit();
        MESSAGE("pong is done");
      }
      return pong_atom_v;
    },
  };
}

behavior peer_fun(broker* self, connection_handle hdl, const actor& buddy) {
  MESSAGE("peer_fun called");
  CAF_REQUIRE(self != nullptr);
  CAF_REQUIRE(self->subtype() == resumable::io_actor);
  self->monitor(buddy);
  self->set_down_handler([self](down_msg& dm) {
    // Stop if buddy is done.
    self->quit(std::move(dm.reason));
  });
  // Assume exactly one connection.
  CAF_REQUIRE(self->connections().size() == 1);
  self->configure_read(hdl, receive_policy::exactly(sizeof(type_id_t)));
  auto write = [=](type_id_t type) {
    auto& buf = self->wr_buf(hdl);
    auto first = reinterpret_cast<byte*>(&type);
    buf.insert(buf.end(), first, first + sizeof(type_id_t));
    self->flush(hdl);
  };
  return {
    [=](const connection_closed_msg&) {
      MESSAGE("received connection_closed_msg");
      self->quit();
    },
    [=](const new_data_msg& msg) {
      MESSAGE("received new_data_msg");
      CAF_REQUIRE_EQUAL(msg.buf.size(), sizeof(type_id_t));
      type_id_t type = 0;
      memcpy(&type, msg.buf.data(), sizeof(type_id_t));
      static_assert(type_id_v<ping_atom> != type_id_v<pong_atom>);
      if (type == type_id_v<ping_atom>) {
        self->send(buddy, ping_atom_v);
      } else if (type == type_id_v<pong_atom>) {
        self->send(buddy, pong_atom_v);
      } else {
        CAF_FAIL("unexpected message type");
      }
    },
    [=](ping_atom) { write(type_id_v<ping_atom>); },
    [=](pong_atom) { write(type_id_v<pong_atom>); },
  };
}

behavior peer_acceptor_fun(broker* self, const actor& buddy) {
  MESSAGE("peer_acceptor_fun");
  return {
    [=](const new_connection_msg& msg) {
      MESSAGE("received `new_connection_msg`");
      self->fork(peer_fun, msg.handle, buddy);
      self->quit();
    },
    [=](publish_atom) -> result<uint16_t> {
      if (auto res = self->add_tcp_doorman(8080))
        return res->second;
      else
        return std::move(res.error());
    },
  };
}

using int_peer = connection_handler::extend<replies_to<int>::with<int>>;

int_peer::behavior_type int_peer_fun(int_peer::broker_pointer) {
  return {
    [=](const connection_closed_msg&) {
      CAF_FAIL("received connection_closed_msg");
    },
    [=](const new_data_msg&) { CAF_FAIL("received new_data_msg"); },
    [=](int value) {
      MESSAGE("received: " << value);
      return value;
    },
  };
}

} // namespace

BEGIN_FIXTURE_SCOPE(point_to_point_fixture<>)

CAF_TEST(test broker to broker communication) {
  prepare_connection(mars, earth, "mars", 8080);
  MESSAGE("spawn peer acceptor on mars");
  auto ssp = std::make_shared<suite_state>();
  auto server = mars.mm.spawn_broker(peer_acceptor_fun,
                                     mars.sys.spawn(pong, ssp));
  mars.self->send(server, publish_atom_v);
  run();
  expect_on(mars, (uint16_t), from(server).to(mars.self).with(8080));
  MESSAGE("spawn ping and client on earth");
  auto pinger = earth.sys.spawn(ping, ssp);
  auto client = unbox(earth.mm.spawn_client(peer_fun, "mars", 8080, pinger));
  anon_send(pinger, ok_atom_v, client);
  run();
  CHECK_EQ(ssp->pings, 10);
  CHECK_EQ(ssp->pongs, 10);
}

CAF_TEST(test whether we can spawn typed broker) {
  auto peer = mars.mm.spawn_broker(int_peer_fun);
  mars.self->send(peer, 42);
  run();
  expect_on(mars, (int), from(peer).to(mars.self).with(42));
}

END_FIXTURE_SCOPE()