Skip to content

Commit

Permalink
Merge pull request #69 from mavam/topic/publish-addr
Browse files Browse the repository at this point in the history
Support for listenting on a specific IP address.
  • Loading branch information
Neverlord committed Sep 5, 2012
2 parents a173e00 + 2c84f93 commit 92a71cf
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 12 deletions.
4 changes: 3 additions & 1 deletion cppa/cppa.hpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -751,9 +751,11 @@ inline void await_all_others_done() {
* The connection is automatically closed if the lifetime of @p whom ends. * The connection is automatically closed if the lifetime of @p whom ends.
* @param whom Actor that should be published at @p port. * @param whom Actor that should be published at @p port.
* @param port Unused TCP port. * @param port Unused TCP port.
* @param addr The IP address to listen to, or @p INADDR_ANY if @p addr is
* @p nullptr.
* @throws bind_failure * @throws bind_failure
*/ */
void publish(actor_ptr whom, std::uint16_t port); void publish(actor_ptr whom, std::uint16_t port, const char* addr = nullptr);


/** /**
* @brief Publishes @p whom using @p acceptor to handle incoming connections. * @brief Publishes @p whom using @p acceptor to handle incoming connections.
Expand Down
3 changes: 2 additions & 1 deletion cppa/detail/ipv4_acceptor.hpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class ipv4_acceptor : public util::acceptor {


public: public:


static std::unique_ptr<util::acceptor> create(std::uint16_t port); static std::unique_ptr<util::acceptor> create(std::uint16_t port,
const char* addr);


~ipv4_acceptor(); ~ipv4_acceptor();


Expand Down
5 changes: 3 additions & 2 deletions cppa/group.hpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -195,11 +195,12 @@ class group : public channel {
typedef intrusive_ptr<group> group_ptr; typedef intrusive_ptr<group> group_ptr;


/** /**
* @brief Makes *all* local groups accessible via network on @p port. * @brief Makes *all* local groups accessible via network on address @p addr
* and @p port.
* @throws bind_failure * @throws bind_failure
* @throws network_error * @throws network_error
*/ */
void publish_local_groups_at(std::uint16_t port); void publish_local_groups_at(std::uint16_t port, const char* addr = nullptr);


} // namespace cppa } // namespace cppa


Expand Down
2 changes: 1 addition & 1 deletion examples/group_server.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int main(int argc, char** argv) {
return 2; return 2;
} }
if (!args_valid) return 1; if (!args_valid) return 1;
publish_local_groups_at(port); publish_local_groups_at(port, "127.0.0.1");
cout << "type 'quit' to shutdown the server" << endl; cout << "type 'quit' to shutdown the server" << endl;
string line; string line;
while (getline(cin, line)) { while (getline(cin, line)) {
Expand Down
4 changes: 2 additions & 2 deletions src/group.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ struct group_nameserver : event_based_actor {
} }
}; };


void publish_local_groups_at(std::uint16_t port) { void publish_local_groups_at(std::uint16_t port, const char* addr) {
auto gn = spawn_hidden<group_nameserver>(); auto gn = spawn_hidden<group_nameserver>();
try { try {
publish(gn, port); publish(gn, port, addr);
} }
catch (std::exception&) { catch (std::exception&) {
gn->enqueue(nullptr, make_any_tuple(atom("SHUTDOWN"))); gn->enqueue(nullptr, make_any_tuple(atom("SHUTDOWN")));
Expand Down
12 changes: 10 additions & 2 deletions src/ipv4_acceptor.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#else #else
# include <netdb.h> # include <netdb.h>
# include <unistd.h> # include <unistd.h>
# include <arpa/inet.h>
# include <sys/types.h> # include <sys/types.h>
# include <sys/socket.h> # include <sys/socket.h>
# include <netinet/in.h> # include <netinet/in.h>
Expand Down Expand Up @@ -97,7 +98,8 @@ bool accept_impl(util::io_stream_ptr_pair& result, native_socket_type fd, bool n
ipv4_acceptor::ipv4_acceptor(native_socket_type fd, bool nonblocking) ipv4_acceptor::ipv4_acceptor(native_socket_type fd, bool nonblocking)
: m_fd(fd), m_is_nonblocking(nonblocking) { } : m_fd(fd), m_is_nonblocking(nonblocking) { }


std::unique_ptr<util::acceptor> ipv4_acceptor::create(std::uint16_t port) { std::unique_ptr<util::acceptor> ipv4_acceptor::create(std::uint16_t port,
const char* addr) {
native_socket_type sockfd; native_socket_type sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0); sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd == invalid_socket) { if (sockfd == invalid_socket) {
Expand All @@ -112,7 +114,13 @@ std::unique_ptr<util::acceptor> ipv4_acceptor::create(std::uint16_t port) {
struct sockaddr_in serv_addr; struct sockaddr_in serv_addr;
memset((char*) &serv_addr, 0, sizeof(serv_addr)); memset((char*) &serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET; serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY; if (! addr) {
serv_addr.sin_addr.s_addr = INADDR_ANY;
}
else if (inet_pton(AF_INET, addr, &serv_addr.sin_addr) <= 0) {
throw network_error("invalid IPv4 address");
}

serv_addr.sin_port = htons(port); serv_addr.sin_port = htons(port);
if (bind(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0) { if (bind(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) < 0) {
throw bind_failure(errno); throw bind_failure(errno);
Expand Down
4 changes: 2 additions & 2 deletions src/unicast_network.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ actor_ptr remote_actor(util::io_stream_ptr_pair peer) {
pinfptr->node_id()); pinfptr->node_id());
} }


void publish(actor_ptr whom, std::uint16_t port) { void publish(actor_ptr whom, std::uint16_t port, const char* addr) {
if (whom) publish(whom, detail::ipv4_acceptor::create(port)); if (whom) publish(whom, detail::ipv4_acceptor::create(port, addr));
} }


actor_ptr remote_actor(const char* host, std::uint16_t port) { actor_ptr remote_actor(const char* host, std::uint16_t port) {
Expand Down
2 changes: 1 addition & 1 deletion unit_testing/test__remote_actor.cpp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ int main(int argc, char** argv) {
bool success = false; bool success = false;
do { do {
try { try {
publish(self, port); publish(self, port, "127.0.0.1");
success = true; success = true;
} }
catch (bind_failure&) { catch (bind_failure&) {
Expand Down

0 comments on commit 92a71cf

Please sign in to comment.