More fixes for ZMQ_LAST_ENDPOINT. Added a test

This commit is contained in:
Mikko Koppanen 2012-02-18 20:44:41 +00:00
parent b0573486c7
commit d00d4843be
7 changed files with 84 additions and 22 deletions

View File

@ -95,32 +95,34 @@ void zmq::ipc_listener_t::in_event ()
send_attach (session, engine, false);
}
int zmq::ipc_listener_t::get_address (std::string *addr_)
int zmq::ipc_listener_t::get_address (std::string &addr_)
{
struct sockaddr_un saddr;
struct sockaddr_storage ss;
int rc;
// Get the details of the IPC socket
socklen_t sl = sizeof(sockaddr_un);
rc = getsockname (s, (sockaddr *)&saddr, &sl);
socklen_t sl = sizeof (ss);
rc = getsockname (s, (sockaddr *) &ss, &sl);
if (rc != 0) {
return rc;
}
// Store the address for retrieval by users using wildcards
*addr_ = std::string("ipc://") + std::string(saddr.sun_path);
addr_ = std::string ("ipc://");
struct sockaddr_un saddr;
memcpy (&saddr, &ss, sizeof (saddr));
addr_.append (saddr.sun_path);
return 0;
}
int zmq::ipc_listener_t::set_address (const char *addr_)
{
// Allow wildcard file
if(*addr_ == '*') {
if (*addr_ == '*') {
addr_ = tempnam(NULL, NULL);
}
// Get rid of the file associated with the UNIX domain socket that
// may have been left behind by the previous run of the application.
::unlink (addr_);
@ -148,8 +150,8 @@ int zmq::ipc_listener_t::set_address (const char *addr_)
rc = listen (s, options.backlog);
if (rc != 0)
return -1;
return 0;
return 0;
}
int zmq::ipc_listener_t::close ()

View File

@ -50,7 +50,7 @@ namespace zmq
int set_address (const char *addr_);
// Get the bound address for use with wildcards
int get_address(std::string *addr_);
int get_address (std::string &addr_);
private:

View File

@ -324,7 +324,7 @@ int zmq::socket_base_t::bind (const char *addr_)
// For convenience's sake, bind can be used interchageable with
// connect for PGM and EPGM transports.
return connect (addr_);
return connect (addr_);
}
// Remaining trasnports require to be run in an I/O thread, so at this
@ -344,8 +344,8 @@ int zmq::socket_base_t::bind (const char *addr_)
delete listener;
return -1;
}
rc = listener->get_address (&options.last_endpoint);
rc = listener->get_address (options.last_endpoint);
launch_child (listener);
return 0;
}
@ -360,8 +360,8 @@ int zmq::socket_base_t::bind (const char *addr_)
delete listener;
return -1;
}
rc = listener->get_address (&options.last_endpoint);
rc = listener->get_address (options.last_endpoint);
launch_child (listener);
return 0;
}

View File

@ -120,7 +120,7 @@ void zmq::tcp_listener_t::close ()
s = retired_fd;
}
int zmq::tcp_listener_t::get_address (std::string *addr_)
int zmq::tcp_listener_t::get_address (std::string &addr_)
{
struct sockaddr_storage ss;
char host [NI_MAXHOST];
@ -141,11 +141,17 @@ int zmq::tcp_listener_t::get_address (std::string *addr_)
}
if (ss.ss_family == AF_INET) {
address << "tcp://" << host << ":" << port;
struct sockaddr_in sa = {0};
memcpy (&sa, &ss, sizeof (sa));
address << "tcp://" << host << ":" << ntohs (sa.sin_port);
} else {
address << "tcp://[" << host << "]:" << port;
struct sockaddr_in6 sa = {0};
memcpy (&sa, &ss, sizeof (sa));
address << "tcp://[" << host << "]:" << ntohs (sa.sin6_port);
}
*addr_ = address.str ();
addr_ = address.str ();
return 0;
}

View File

@ -46,7 +46,7 @@ namespace zmq
int set_address (const char *addr_);
// Get the bound address for use with wildcard
int get_address(std::string *addr_);
int get_address (std::string &addr_);
private:

View File

@ -12,7 +12,8 @@ noinst_PROGRAMS = test_pair_inproc \
test_sub_forward \
test_invalid_rep \
test_msg_flags \
test_connect_resolve
test_connect_resolve \
test_last_endpoint
if !ON_MINGW
noinst_PROGRAMS += test_shutdown_stress \
@ -31,6 +32,7 @@ test_sub_forward_SOURCES = test_sub_forward.cpp
test_invalid_rep_SOURCES = test_invalid_rep.cpp
test_msg_flags_SOURCES = test_msg_flags.cpp
test_connect_resolve_SOURCES = test_connect_resolve.cpp
test_last_endpoint_SOURCES = test_last_endpoint.cpp
if !ON_MINGW
test_shutdown_stress_SOURCES = test_shutdown_stress.cpp

View File

@ -0,0 +1,52 @@
/*
Copyright (c) 2007-2012 iMatix Corporation
Copyright (c) 2011 250bpm s.r.o.
Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
This file is part of 0MQ.
0MQ is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
0MQ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <string.h>
#include "../include/zmq.h"
int main (int argc, char *argv [])
{
// Create the infrastructure
void *ctx = zmq_init (1);
assert (ctx);
void *sb = zmq_socket (ctx, ZMQ_XREP);
assert (sb);
int rc = zmq_bind (sb, "tcp://127.0.0.1:12345");
assert (rc == 0);
char test [255];
size_t siz = 255;
rc = zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, test, &siz);
assert (rc == 0 && strcmp (test, "tcp://127.0.0.1:12345") == 0);
rc = zmq_bind (sb, "tcp://127.0.0.1:54321");
assert (rc == 0);
siz = 255;
rc = zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, test, &siz);
assert (rc == 0 && strcmp (test, "tcp://127.0.0.1:54321") == 0);
return 0 ;
}