Commit dde41c25 authored by Joseph Noir's avatar Joseph Noir

Align port and addr functions in network managers

parent 845b9831
......@@ -18,24 +18,24 @@
#pragma once
#include <vector>
#include <unordered_map>
#include <vector>
#include "caf/scheduled_actor.hpp"
#include "caf/prohibit_top_level_spawn_marker.hpp"
#include "caf/scheduled_actor.hpp"
#include "caf/io/fwd.hpp"
#include "caf/io/accept_handle.hpp"
#include "caf/io/receive_policy.hpp"
#include "caf/io/connection_handle.hpp"
#include "caf/io/datagram_handle.hpp"
#include "caf/io/fwd.hpp"
#include "caf/io/receive_policy.hpp"
#include "caf/io/system_messages.hpp"
#include "caf/io/connection_handle.hpp"
#include "caf/io/network/acceptor_manager.hpp"
#include "caf/io/network/datagram_manager.hpp"
#include "caf/io/network/ip_endpoint.hpp"
#include "caf/io/network/native_socket.hpp"
#include "caf/io/network/stream_manager.hpp"
#include "caf/io/network/acceptor_manager.hpp"
#include "caf/io/network/datagram_manager.hpp"
namespace caf {
namespace io {
......@@ -223,10 +223,11 @@ public:
add_datagram_servant_for_endpoint(network::native_socket fd,
const network::ip_endpoint& ep);
/// Creates a new `datagram_servant` for the remote endpoint `host` and `port`.
/// Creates a new `datagram_servant` for the remote endpoint `host` and
/// `port`.
/// @returns The handle to the new `datagram_servant`.
expected<datagram_handle>
add_udp_datagram_servant(const std::string& host, uint16_t port);
expected<datagram_handle> add_udp_datagram_servant(const std::string& host,
uint16_t port);
/// Tries to open a local port and creates a `datagram_servant` managing it on
/// success. If `port == 0`, then the broker will ask the operating system to
......@@ -265,6 +266,10 @@ public:
/// or an empty string if `hdl` is invalid.
std::string remote_addr(datagram_handle hdl);
/// Returns the local address associated with `hdl`
/// or empty string if `hdl` is invalid.
std::string local_addr(datagram_handle hdl);
/// Returns the remote port associated with `hdl`
/// or `0` if `hdl` is invalid.
uint16_t remote_port(datagram_handle hdl);
......@@ -358,8 +363,8 @@ protected:
using doorman_map = std::unordered_map<accept_handle, intrusive_ptr<doorman>>;
using scribe_map = std::unordered_map<connection_handle,
intrusive_ptr<scribe>>;
using scribe_map
= std::unordered_map<connection_handle, intrusive_ptr<scribe>>;
using datagram_servant_map
= std::unordered_map<datagram_handle, intrusive_ptr<datagram_servant>>;
......@@ -428,4 +433,3 @@ private:
} // namespace io
} // namespace caf
......@@ -63,7 +63,7 @@ public:
virtual uint16_t local_port() const = 0;
/// Returns the address of the remote endpoint for the handle.
virtual std::string remote_addr(datagram_handle) const = 0;
virtual std::string local_addr() const = 0;
/// Returns all the handles associated with this servant
virtual std::vector<datagram_handle> hdls() const = 0;
......
......@@ -38,9 +38,11 @@ public:
/// Get the port of the underlying I/O device.
virtual uint16_t port() const = 0;
/// Get the address of the underlying I/O device.
virtual std::string addr() const = 0;
};
} // namespace network
} // namespace io
} // namespace caf
......@@ -34,12 +34,14 @@ public:
/// Called by the underlying I/O device whenever it received data.
/// @returns `true` if the manager accepts further reads, otherwise `false`.
virtual bool consume(execution_unit*, datagram_handle hdl,
receive_buffer& buf) = 0;
virtual bool
consume(execution_unit*, datagram_handle hdl, receive_buffer& buf)
= 0;
/// Called by the underlying I/O device whenever it sent data.
virtual void datagram_sent(execution_unit*, datagram_handle hdl, size_t,
std::vector<char> buffer) = 0;
std::vector<char> buffer)
= 0;
/// Called by the underlying I/O device to indicate that a new remote
/// endpoint has been detected, passing in the received datagram.
......@@ -49,9 +51,11 @@ public:
/// Get the port of the underlying I/O device.
virtual uint16_t port(datagram_handle) const = 0;
/// Get the port of the underlying I/O device.
virtual std::string addr(datagram_handle) const = 0;
};
} // namespace network
} // namespace io
} // namespace caf
......@@ -52,9 +52,9 @@ public:
void flush() override;
std::string remote_addr(datagram_handle hdl) const override;
std::string addr(datagram_handle hdl) const override;
std::string addr() const override;
std::string local_addr() const override;
uint16_t port(datagram_handle hdl) const override;
......
......@@ -18,9 +18,9 @@
#pragma once
#include "caf/intrusive_ptr.hpp"
#include "caf/message.hpp"
#include "caf/ref_counted.hpp"
#include "caf/intrusive_ptr.hpp"
#include "caf/io/fwd.hpp"
#include "caf/io/network/operation.hpp"
......@@ -65,9 +65,6 @@ public:
/// Detaches this manager from its parent in case of an error.
void io_failure(execution_unit* ctx, operation op);
/// Get the address of the underlying I/O device.
virtual std::string addr() const = 0;
protected:
/// Creates a message signalizing a disconnect to the parent.
virtual message detach_message() = 0;
......@@ -81,4 +78,3 @@ protected:
} // namespace network
} // namespace io
} // namespace caf
......@@ -38,13 +38,16 @@ public:
/// Called by the underlying I/O device whenever it sent data.
virtual void data_transferred(execution_unit* ctx, size_t num_bytes,
size_t remaining_bytes) = 0;
size_t remaining_bytes)
= 0;
/// Get the port of the underlying I/O device.
virtual uint16_t port() const = 0;
/// Get the address of the underlying I/O device.
virtual std::string addr() const = 0;
};
} // namespace network
} // namespace io
} // namespace caf
......@@ -320,8 +320,12 @@ datagram_handle abstract_broker::datagram_hdl_by_port(uint16_t port) {
std::string abstract_broker::remote_addr(datagram_handle hdl) {
auto i = datagram_servants_.find(hdl);
return i != datagram_servants_.end() ? i->second->remote_addr(hdl)
: std::string{};
return i != datagram_servants_.end() ? i->second->addr(hdl) : std::string{};
}
std::string abstract_broker::local_addr(datagram_handle hdl) {
auto i = datagram_servants_.find(hdl);
return i != datagram_servants_.end() ? i->second->local_addr() : 0;
}
uint16_t abstract_broker::remote_port(datagram_handle hdl) {
......
......@@ -87,14 +87,7 @@ void datagram_servant_impl::flush() {
handler_.flush(this);
}
std::string datagram_servant_impl::addr() const {
auto x = remote_addr_of_fd(handler_.fd());
if (!x)
return "";
return *x;
}
std::string datagram_servant_impl::remote_addr(datagram_handle hdl) const {
std::string datagram_servant_impl::addr(datagram_handle hdl) const {
auto& eps = handler_.endpoints();
auto itr = eps.find(hdl);
if (itr == eps.end())
......@@ -102,6 +95,13 @@ std::string datagram_servant_impl::remote_addr(datagram_handle hdl) const {
return network::host(itr->second);
}
std::string datagram_servant_impl::local_addr() const {
auto x = local_addr_of_fd(handler_.fd());
if (!x)
return 0;
return *x;
}
uint16_t datagram_servant_impl::port(datagram_handle hdl) const {
auto& eps = handler_.endpoints();
auto itr = eps.find(hdl);
......
......@@ -373,10 +373,10 @@ datagram_servant_ptr test_multiplexer::new_datagram_servant(datagram_handle hdl,
void flush() override {
// nop
}
std::string remote_addr(datagram_handle) const override {
std::string addr(datagram_handle) const override {
return "test";
}
std::string addr() const override {
std::string local_addr() const override {
return "test";
}
uint16_t port(datagram_handle dh) const override {
......
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