Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 366 lines (347 sloc) 11.289 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 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
#include <thread>
#include <string>
#include <cstring>
#include <sstream>
#include <iostream>

#include "test.hpp"
#include "ping_pong.hpp"
#include "cppa/cppa.hpp"
#include "cppa/logging.hpp"
#include "cppa/exception.hpp"

using namespace std;
using namespace cppa;

namespace {

typedef vector<actor_ptr> actor_vector;

vector<string_pair> get_kv_pairs(int argc, char** argv, int begin = 1) {
    vector<string_pair> result;
    for (int i = begin; i < argc; ++i) {
        auto vec = split(argv[i], '=');
        if (vec.size() != 2) {
            cerr << "\"" << argv[i] << "\" is not a key-value pair" << endl;
        }
        else if (any_of(result.begin(), result.end(),
                             [&](const string_pair& p) { return p.first == vec[0]; })) {
            cerr << "key \"" << vec[0] << "\" is already defined" << endl;
        }
        else {
            result.emplace_back(vec[0], vec[1]);
        }
    }
    return result;
}

struct reflector : public event_based_actor {
    void init() {
        become (
            others() >> [=] {
                reply_tuple(last_dequeued());
                quit();
            }
        );
    }
};

struct replier : public event_based_actor {
    void init() {
        become (
            others() >> [=] {
                reply(42);
                quit();
            }
        );
    }
};

// receive seven reply messages (2 local, 5 remote)
void spawn5_server(actor_ptr client, bool inverted) {
    auto default_case = others() >> [] {
        cout << "unexpected message; "
             << __FILE__ << " line " << __LINE__ << ": "
             << to_string(self->last_dequeued())
             << endl;
    };
    group_ptr grp;
    if (!inverted) {
        grp = group::get("local", "foobar");
    }
    else {
        send(client, atom("GetGroup"));
        receive (
            on_arg_match >> [&](const group_ptr& remote_group) {
                grp = remote_group;
            }
        );
    }
    spawn_in_group<reflector>(grp);
    spawn_in_group<reflector>(grp);
    receive_response (sync_send(client, atom("Spawn5"), grp)) (
        on(atom("ok"), arg_match) >> [&](const actor_vector& vec) {
            send(grp, "Hello reflectors!", 5.0);
            if (vec.size() != 5) {
                cout << "remote client did not spawn five reflectors!\n";
            }
            for (auto& a : vec) {
                self->monitor(a);
            }
        },
        default_case,
        after(chrono::seconds(10)) >> [&] {
            throw runtime_error("timeout");
        }
    );
    cout << "wait for reflected messages\n";
    // receive seven reply messages (2 local, 5 remote)
    int x = 0;
    receive_for(x, 7) (
        on("Hello reflectors!", 5.0) >> [] { }
    );
    cout << "wait for DOWN messages\n";
    // wait for DOWN messages
    {int i = 0; receive_for(i, 5) (
        on(atom("DOWN"), arg_match) >> [](std::uint32_t reason) {
            if (reason != exit_reason::normal) {
                cout << "reflector exited for non-normal exit reason!" << endl;
            }
        },
        default_case,
        after(chrono::seconds(2)) >> [&] {
            i = 4;
            cout << "timeout while waiting for DOWN messages!\n";
        }
    );}
    // wait for locally spawned reflectors
    await_all_others_done();
    send(client, atom("Spawn5Done"));
}

void spawn5_client() {
    bool spawned_reflectors = false;
    do_receive (
        on(atom("Spawn5"), arg_match) >> [&](const group_ptr& grp) {
            actor_vector vec;
            for (int i = 0; i < 5; ++i) {
                vec.push_back(spawn_in_group<reflector>(grp));
            }
            reply(atom("ok"), std::move(vec));
            spawned_reflectors = true;
        },
        on(atom("GetGroup")) >> [] {
            reply(group::get("local", "foobar"));
        }
    ).until(gref(spawned_reflectors));
    await_all_others_done();
    // wait for server
    receive (
        on(atom("Spawn5Done")) >> [] { }
    );
}

int client_part(const vector<string_pair>& args) {
    CPPA_TEST(test__remote_actor_client_part);
    auto i = find_if(args.begin(), args.end(),
                          [](const string_pair& p) { return p.first == "port"; });
    if (i == args.end()) {
        throw runtime_error("no port specified");
    }
    auto port = static_cast<uint16_t>(stoi(i->second));
    auto server = remote_actor("localhost", port);
    // remote_actor is supposed to return the same server when connecting to
    // the same host again
    for (int i = 0; i < 5; ++i) {
        auto server2 = remote_actor("localhost", port);
        CPPA_CHECK(server == server2);
    }
    send(server, atom("SpawnPing"));
    receive (
        on(atom("PingPtr"), arg_match) >> [](actor_ptr ping_actor) {
            spawn<detached>(pong, ping_actor);
        }
    );
    await_all_others_done();
    receive_response (sync_send(server, atom("SyncMsg"))) (
        others() >> [&] {
            if (self->last_dequeued() != make_cow_tuple(atom("SyncReply"))) {
                ostringstream oss;
                oss << "unexpected message; "
                    << __FILE__ << " line " << __LINE__ << ": "
                    << to_string(self->last_dequeued()) << endl;
                send(server, atom("Failure"), oss.str());
            }
            else {
                send(server, atom("Done"));
            }
        },
        after(chrono::seconds(5)) >> [&] {
            cerr << "sync_send timed out!" << endl;
            send(server, atom("Timeout"));
        }
    );
    receive (
        others() >> [&] {
            CPPA_ERROR("unexpected message; "
                       << __FILE__ << " line " << __LINE__ << ": "
                       << to_string(self->last_dequeued()));
        },
        after(chrono::seconds(0)) >> [&] { }
    );
    // test 100 sync_messages
    for (int i = 0; i < 100; ++i) {
        receive_response (sync_send(server, atom("foo"), atom("bar"), i)) (
            on(atom("foo"), atom("bar"), i) >> [] {

            },
            others() >> [&] {
                CPPA_ERROR("unexpected message; "
                           << __FILE__ << " line " << __LINE__ << ": "
                           << to_string(self->last_dequeued()));
            },
            after(chrono::seconds(10)) >> [&] {
                CPPA_ERROR("unexpected timeout!");
            }
        );
    }
    spawn5_server(server, false);
    spawn5_client();
    // wait for locally spawned reflectors
    await_all_others_done();

    receive (
        on(atom("fwd"), arg_match) >> [&](const actor_ptr& fwd, const string&) {
            forward_to(fwd);
        }
    );

    send(server, atom("farewell"));
    shutdown();
    return CPPA_TEST_RESULT;
}

} // namespace <anonymous>

void verbose_terminate() {
    try { if (std::uncaught_exception()) throw; }
    catch (std::exception& e) {
        cerr << "terminate called after throwing "
             << to_verbose_string(e) << endl;
    }
    catch (...) {
        cerr << "terminate called after throwing an unknown exception" << endl;
    }

    abort();
}

int main(int argc, char** argv) {
    set_terminate(verbose_terminate);
    announce<actor_vector>();
    cout.unsetf(ios_base::unitbuf);
    string app_path = argv[0];
    bool run_remote_actor = true;
    if (argc > 1) {
        if (strcmp(argv[1], "run_remote_actor=false") == 0) {
            run_remote_actor = false;
        }
        else {
            auto args = get_kv_pairs(argc, argv);
            return client_part(args);
        }
    }
    CPPA_TEST(test__remote_actor);
    CPPA_LOGF_TRACE("");
    //auto ping_actor = spawn(ping, 10);
    uint16_t port = 4242;
    bool success = false;
    do {
        try {
            publish(self, port, "127.0.0.1");
            success = true;
            CPPA_LOGF_DEBUG("running on port " << port);
        }
        catch (bind_failure&) {
            // try next port
            ++port;
        }
    }
    while (!success);
    thread child;
    ostringstream oss;
    if (run_remote_actor) {
        oss << app_path << " run=remote_actor port=" << port;// << " &>client.txt";
        // execute client_part() in a separate process,
        // connected via localhost socket
        child = thread([&oss]() {
            CPPA_LOGF_TRACE("lambda expression calling system()");
            string cmdstr = oss.str();
            if (system(cmdstr.c_str()) != 0) {
                cerr << "FATAL: command \"" << cmdstr << "\" failed!" << endl;
                abort();
            }
        });
    }
    else {
        cout << "actor published at port " << port << endl;
    }
    //cout << "await SpawnPing message" << endl;
    actor_ptr remote_client;
    CPPA_LOGF_DEBUG("send 'SpawnPing', expect 'PingPtr'");
    receive (
        on(atom("SpawnPing")) >> [&]() {
            remote_client = self->last_sender();
            CPPA_LOGF_ERROR_IF(!remote_client, "last_sender() == nullptr");
            CPPA_LOGF_DEBUG("spawn 10 event-based ping actors");
            reply(atom("PingPtr"), spawn_event_based_ping(10));
        }
    );
    CPPA_LOGF_DEBUG("wait until spawned ping actors are done");
    await_all_others_done();
    CPPA_CHECK_EQUAL(10, pongs());
    cout << "test remote sync_send" << endl;
    receive (
        on(atom("SyncMsg")) >> [] {
            reply(atom("SyncReply"));
        }
    );
    receive (
        on(atom("Done")) >> [] {
            // everything's fine
        },
        on(atom("Failure"), arg_match) >> [&](const string& str) {
            CPPA_ERROR(str);
        },
        on(atom("Timeout")) >> [&] {
            CPPA_ERROR("sync_send timed out");
        }
    );
    // test 100 sync messages
    cout << "test 100 synchronous messages" << endl;
    int i = 0;
    receive_for(i, 100) (
        others() >> [] {
            reply_tuple(self->last_dequeued());
        }
    );
    cout << "test group communication via network" << endl;
    // group test
    spawn5_client();
    cout << "test group communication via network (inverted setup)" << endl;
    spawn5_server(remote_client, true);

    // test forward_to "over network and back"
    cout << "test forwarding over network 'and back'" << endl;
    auto ra = spawn<replier>();
    sync_send(remote_client, atom("fwd"), ra, "hello replier!").await(
        on(42) >> [&] {
            auto from = self->last_sender();
            if (!from) {
                CPPA_ERROR("from == nullptr");
            }
            else if (from != ra) {
                CPPA_ERROR("response came from wrong actor");
                if (from->is_proxy()) {
                    CPPA_ERROR("received response from a remote actor");
                }
            }
        },
        others() >> [&] {
            CPPA_ERROR("unexpected: " << to_string(self->last_dequeued()));
        },
        after(chrono::seconds(5)) >> [&] {
            CPPA_ERROR("fowarding failed; no message received within 5s");
        }
    );

    cout << "wait for a last goodbye" << endl;
    receive (
        on(atom("farewell")) >> [] { }
    );
    // wait until separate process (in sep. thread) finished execution
    if (run_remote_actor) child.join();
    shutdown();
    return CPPA_TEST_RESULT;
}
Something went wrong with that request. Please try again.