Commit 7f577f42 authored by Dominik Charousset's avatar Dominik Charousset

Add free functions to I/O module

parent 0b610ed5
......@@ -298,7 +298,7 @@ void run_server(actor_system& system, const config& cfg) {
auto calc = system.spawn(calculator_fun);
// try to publish math actor at given port
cout << "*** try publish at port " << cfg.port << endl;
auto expected_port = system.middleman().publish(calc, cfg.port);
auto expected_port = io::publish(calc, cfg.port);
if (!expected_port) {
std::cerr << "*** publish failed: "
<< system.render(expected_port.error()) << endl;
......
......@@ -20,10 +20,12 @@
#ifndef CAF_IO_ALL_HPP
#define CAF_IO_ALL_HPP
#include "caf/io/basp/all.hpp"
#include "caf/io/publish.hpp"
#include "caf/io/broker.hpp"
#include "caf/io/middleman.hpp"
#include "caf/io/unpublish.hpp"
#include "caf/io/basp_broker.hpp"
#include "caf/io/remote_actor.hpp"
#include "caf/io/typed_broker.hpp"
#include "caf/io/receive_policy.hpp"
#include "caf/io/middleman_actor.hpp"
......@@ -34,4 +36,6 @@
#include "caf/io/network/multiplexer.hpp"
#include "caf/io/network/test_multiplexer.hpp"
#include "caf/io/basp/all.hpp"
#endif // CAF_IO_ALL_HPP
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright (C) 2011 - 2017 *
* Dominik Charousset <dominik.charousset (at) haw-hamburg.de> *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#ifndef CAF_IO_CLOSE_HPP
#define CAF_IO_CLOSE_HPP
#include <cstdint>
#include "caf/actor_system.hpp"
#include "caf/io/middleman.hpp"
namespace caf {
namespace io {
/// Closes port `port` regardless of whether an actor is published to it.
inline expected<void> close(actor_system& sys, uint16_t port) {
return sys.middleman().close(port);
}
} // namespace io
} // namespace caf
#endif // CAF_IO_CLOSE_HPP
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright (C) 2011 - 2017 *
* Dominik Charousset <dominik.charousset (at) haw-hamburg.de> *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#ifndef CAF_IO_CONNECT_HPP
#define CAF_IO_CONNECT_HPP
#include <string>
#include <cstdint>
#include "caf/node_id.hpp"
#include "caf/actor_system.hpp"
#include "caf/io/middleman.hpp"
namespace caf {
namespace io {
/// Tries to connect to given node.
/// @experimental
inline expected<node_id> connect(actor_system& sys, std::string host,
uint16_t port) {
return sys.middleman().connect(std::move(host), port);
}
} // namespace io
} // namespace caf
#endif // CAF_IO_CONNECT_HPP
......@@ -51,8 +51,8 @@ public:
/// Tries to open a port for other CAF instances to connect to.
/// @experimental
expected<uint16_t> open(uint16_t port, const char* cstr = nullptr,
bool ru = false);
expected<uint16_t> open(uint16_t port, const char* in = nullptr,
bool reuse = false);
/// Closes port `port` regardless of whether an actor is published to it.
expected<void> close(uint16_t port);
......
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright (C) 2011 - 2017 *
* Dominik Charousset <dominik.charousset (at) haw-hamburg.de> *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#ifndef CAF_IO_OPEN_HPP
#define CAF_IO_OPEN_HPP
#include <cstdint>
#include "caf/actor_system.hpp"
#include "caf/io/middleman.hpp"
namespace caf {
namespace io {
/// Tries to open a port for other CAF instances to connect to.
/// @experimental
inline expected<uint16_t> open(actor_system& sys, uint16_t port,
const char* in = nullptr, bool reuse = false) {
return sys.middleman().open(port, in, reuse);
}
} // namespace io
} // namespace caf
#endif // CAF_IO_OPEN_HPP
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright (C) 2011 - 2017 *
* Dominik Charousset <dominik.charousset (at) haw-hamburg.de> *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#ifndef CAF_IO_PUBLISH_HPP
#define CAF_IO_PUBLISH_HPP
#include <set>
#include <string>
#include <cstdint>
#include "caf/actor_system.hpp"
#include "caf/io/middleman.hpp"
namespace caf {
namespace io {
/// Tries to publish `whom` at `port` and returns either an `error` or the
/// bound port.
/// @param whom Actor that should be published at `port`.
/// @param port Unused TCP port.
/// @param in The IP address to listen to or `INADDR_ANY` if `in == nullptr`.
/// @param reuse Create socket using `SO_REUSEADDR`.
/// @returns The actual port the OS uses after `bind()`. If `port == 0`
/// the OS chooses a random high-level port.
template <class Handle>
expected<uint16_t> publish(const Handle& whom, uint16_t port,
const char* in = nullptr, bool reuse = false) {
if (!whom)
return sec::cannot_publish_invalid_actor;
auto& sys = whom.home_system();
return sys.middleman().publish(whom, port, in, reuse);
}
} // namespace io
} // namespace caf
#endif // CAF_IO_PUBLISH_HPP
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright (C) 2011 - 2017 *
* Dominik Charousset <dominik.charousset (at) haw-hamburg.de> *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#ifndef CAF_IO_UNPUBLISH_HPP
#define CAF_IO_UNPUBLISH_HPP
#include <cstdint>
#include "caf/actor_system.hpp"
#include "caf/io/middleman.hpp"
namespace caf {
namespace io {
/// Unpublishes `whom` by closing `port` or all assigned ports if `port == 0`.
/// @param whom Actor that should be unpublished at `port`.
/// @param port TCP port.
template <class Handle>
expected<void> unpublish(const Handle& whom, uint16_t port = 0) {
if (!whom)
return sec::invalid_argument;
return whom.home_system().middleman().unpublish(whom, port);
}
} // namespace io
} // namespace caf
#endif // CAF_IO_UNPUBLISH_HPP
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright (C) 2011 - 2017 *
* Dominik Charousset <dominik.charousset (at) haw-hamburg.de> *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#ifndef CAF_IO_REMOTE_ACTOR_HPP
#define CAF_IO_REMOTE_ACTOR_HPP
#include <set>
#include <string>
#include <cstdint>
#include "caf/actor_system.hpp"
#include "caf/io/middleman.hpp"
namespace caf {
namespace io {
/// Establish a new connection to the actor at `host` on given `port`.
/// @param host Valid hostname or IP address.
/// @param port TCP port.
/// @returns An `actor` to the proxy instance representing
/// a remote actor or an `error`.
template <class ActorHandle = actor>
expected<ActorHandle> remote_actor(actor_system& sys, std::string host,
uint16_t port) {
detail::type_list<ActorHandle> tk;
return sys.middleman().remote_actor(sys, sys.message_types(tk),
std::move(host), port);
}
} // namespace io
} // namespace caf
#endif // CAF_IO_REMOTE_ACTOR_HPP
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright (C) 2011 - 2017 *
* Dominik Charousset <dominik.charousset (at) haw-hamburg.de> *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#ifndef CAF_IO_REMOTE_GROUP_HPP
#define CAF_IO_REMOTE_GROUP_HPP
#include <string>
#include <cstdint>
#include "caf/actor_system.hpp"
#include "caf/io/middleman.hpp"
namespace caf {
namespace io {
inline expected<group> remote_group(actor_system& sys,
const std::string& group_uri) {
return sys.middleman().remote_group(group_uri);
}
inline expected<group> remote_group(actor_system& sys,
const std::string& group_identifier,
const std::string& host, uint16_t port) {
return sys.middleman().remote_group(group_identifier, host, port);
}
} // namespace io
} // namespace caf
#endif // CAF_IO_REMOTE_GROUP_HPP
/******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright (C) 2011 - 2017 *
* Dominik Charousset <dominik.charousset (at) haw-hamburg.de> *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#ifndef CAF_IO_UNPUBLISH_HPP
#define CAF_IO_UNPUBLISH_HPP
#include <cstdint>
#include "caf/actor_system.hpp"
#include "caf/io/middleman.hpp"
namespace caf {
namespace io {
/// Unpublishes `whom` by closing `port` or all assigned ports if `port == 0`.
/// @param whom Actor that should be unpublished at `port`.
/// @param port TCP port.
template <class Handle>
expected<void> unpublish(const Handle& whom, uint16_t port = 0) {
if (!whom)
return sec::invalid_argument;
return whom.home_system().middleman().unpublish(whom, port);
}
} // namespace io
} // namespace caf
#endif // CAF_IO_UNPUBLISH_HPP
......@@ -116,12 +116,12 @@ expected<strong_actor_ptr> middleman::remote_spawn_impl(const node_id& nid,
std::move(args), std::move(s));
}
expected<uint16_t> middleman::open(uint16_t port, const char* cstr, bool ru) {
expected<uint16_t> middleman::open(uint16_t port, const char* in, bool reuse) {
std::string str;
if (cstr != nullptr)
str = cstr;
if (in != nullptr)
str = in;
auto f = make_function_view(actor_handle());
return f(open_atom::value, port, std::move(str), ru);
return f(open_atom::value, port, std::move(str), reuse);
}
expected<void> middleman::close(uint16_t port) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment