summaryrefslogtreecommitdiffstats
path: root/vespalib
diff options
context:
space:
mode:
authorHaavard <havardpe@yahoo-inc.com>2017-04-07 10:37:11 +0000
committerHaavard <havardpe@yahoo-inc.com>2017-04-07 13:44:37 +0000
commit9074fb3e8509d803dfcc4484c3dc1e9d6d39c909 (patch)
tree3c78f6e51a4cf023885cc434420f7d4bb26814c8 /vespalib
parentb680033c5ce26d1da2e7ef2d40aab1d23f99e921 (diff)
use SocketSpec for listen/connect
Diffstat (limited to 'vespalib')
-rw-r--r--vespalib/src/tests/net/socket/socket_client.cpp4
-rw-r--r--vespalib/src/tests/net/socket/socket_server.cpp4
-rw-r--r--vespalib/src/tests/net/socket/socket_test.cpp15
-rw-r--r--vespalib/src/vespa/vespalib/net/server_socket.cpp12
-rw-r--r--vespalib/src/vespa/vespalib/net/server_socket.h5
-rw-r--r--vespalib/src/vespa/vespalib/net/socket.cpp12
-rw-r--r--vespalib/src/vespa/vespalib/net/socket.h5
-rw-r--r--vespalib/src/vespa/vespalib/net/socket_spec.cpp43
-rw-r--r--vespalib/src/vespa/vespalib/net/socket_spec.h7
9 files changed, 43 insertions, 64 deletions
diff --git a/vespalib/src/tests/net/socket/socket_client.cpp b/vespalib/src/tests/net/socket/socket_client.cpp
index c46b4989954..e12fe5330d0 100644
--- a/vespalib/src/tests/net/socket/socket_client.cpp
+++ b/vespalib/src/tests/net/socket/socket_client.cpp
@@ -1,6 +1,6 @@
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/fastos/fastos.h>
-#include <vespa/vespalib/net/socket_address.h>
+#include <vespa/vespalib/net/socket_spec.h>
#include <vespa/vespalib/net/server_socket.h>
#include <vespa/vespalib/net/socket.h>
#include <vespa/vespalib/util/stringfmt.h>
@@ -53,7 +53,7 @@ int main(int argc, char **argv) {
fprintf(stderr, " %s\n", addr.spec().c_str());
}
}
- Socket::UP socket = Socket::connect(host, port);
+ Socket::UP socket = Socket::connect(SocketSpec::from_host_port(host, port));
if (!socket->valid()) {
fprintf(stderr, "connect failed\n");
return 1;
diff --git a/vespalib/src/tests/net/socket/socket_server.cpp b/vespalib/src/tests/net/socket/socket_server.cpp
index f079a57e6f2..ff8d77f317d 100644
--- a/vespalib/src/tests/net/socket/socket_server.cpp
+++ b/vespalib/src/tests/net/socket/socket_server.cpp
@@ -1,6 +1,6 @@
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/fastos/fastos.h>
-#include <vespa/vespalib/net/socket_address.h>
+#include <vespa/vespalib/net/socket_spec.h>
#include <vespa/vespalib/net/server_socket.h>
#include <vespa/vespalib/net/socket.h>
#include <vespa/vespalib/util/stringfmt.h>
@@ -39,7 +39,7 @@ void write_msg(Socket &socket, const vespalib::string &msg) {
}
int main(int, char **) {
- ServerSocket::UP server = ServerSocket::listen(0);
+ ServerSocket::UP server = ServerSocket::listen(SocketSpec::from_port(0));
if (!server->valid()) {
fprintf(stderr, "listen failed, exiting\n");
return 1;
diff --git a/vespalib/src/tests/net/socket/socket_test.cpp b/vespalib/src/tests/net/socket/socket_test.cpp
index 8f77f12876c..a5a97e7d1ae 100644
--- a/vespalib/src/tests/net/socket/socket_test.cpp
+++ b/vespalib/src/tests/net/socket/socket_test.cpp
@@ -1,6 +1,6 @@
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/testkit/test_kit.h>
-#include <vespa/vespalib/net/socket_address.h>
+#include <vespa/vespalib/net/socket_spec.h>
#include <vespa/vespalib/net/server_socket.h>
#include <vespa/vespalib/net/socket.h>
#include <vespa/vespalib/util/stringfmt.h>
@@ -44,12 +44,9 @@ Socket::UP connect_sockets(bool is_server, ServerSocket &server_socket) {
if (is_server) {
return server_socket.accept();
} else {
- SocketAddress server_address = server_socket.address();
- if (server_address.is_ipc()) {
- return Socket::connect(server_address.path());
- } else {
- return Socket::connect("localhost", server_address.port());
- }
+ vespalib::string spec = server_socket.address().spec();
+ fprintf(stderr, "connecting to: %s\n", spec.c_str());
+ return Socket::connect(SocketSpec(spec));
}
}
@@ -80,7 +77,7 @@ TEST("ipc address") {
}
struct ServerWrapper {
- ServerSocket::UP server = ServerSocket::listen(0);
+ ServerSocket::UP server = ServerSocket::listen(SocketSpec::from_port(0));
};
TEST_MT_F("require that basic socket io works", 2, ServerWrapper) {
@@ -110,7 +107,7 @@ struct IpcServerWrapper {
: server_path(server_path_in), server()
{
unlink(server_path.c_str());
- server = ServerSocket::listen(server_path);
+ server = ServerSocket::listen(SocketSpec::from_path(server_path));
}
~IpcServerWrapper() {
server.reset();
diff --git a/vespalib/src/vespa/vespalib/net/server_socket.cpp b/vespalib/src/vespa/vespalib/net/server_socket.cpp
index 1b026cfa7d4..5fe863b412f 100644
--- a/vespalib/src/vespa/vespalib/net/server_socket.cpp
+++ b/vespalib/src/vespa/vespalib/net/server_socket.cpp
@@ -2,6 +2,7 @@
#include "server_socket.h"
+#include "socket_spec.h"
namespace vespalib {
@@ -27,16 +28,9 @@ ServerSocket::accept()
}
ServerSocket::UP
-ServerSocket::listen(int port)
+ServerSocket::listen(const SocketSpec &spec)
{
- SocketHandle handle = SocketAddress::select_local(port).listen();
- return std::make_unique<ServerSocket>(std::move(handle));
-}
-
-ServerSocket::UP
-ServerSocket::listen(const vespalib::string &path)
-{
- SocketHandle handle = SocketAddress::from_path(path).listen();
+ SocketHandle handle = spec.server_address().listen();
return std::make_unique<ServerSocket>(std::move(handle));
}
diff --git a/vespalib/src/vespa/vespalib/net/server_socket.h b/vespalib/src/vespa/vespalib/net/server_socket.h
index cba03e0fb3c..ac6b504522a 100644
--- a/vespalib/src/vespa/vespalib/net/server_socket.h
+++ b/vespalib/src/vespa/vespalib/net/server_socket.h
@@ -9,6 +9,8 @@
namespace vespalib {
+class SocketSpec;
+
class ServerSocket
{
private:
@@ -23,8 +25,7 @@ public:
SocketAddress address() const;
void shutdown();
Socket::UP accept();
- static ServerSocket::UP listen(int port);
- static ServerSocket::UP listen(const vespalib::string &path);
+ static ServerSocket::UP listen(const SocketSpec &spec);
};
} // namespace vespalib
diff --git a/vespalib/src/vespa/vespalib/net/socket.cpp b/vespalib/src/vespa/vespalib/net/socket.cpp
index 23a4e5f9765..8c016df3d6d 100644
--- a/vespalib/src/vespa/vespalib/net/socket.cpp
+++ b/vespalib/src/vespa/vespalib/net/socket.cpp
@@ -1,6 +1,7 @@
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "socket.h"
+#include "socket_spec.h"
namespace vespalib {
@@ -47,16 +48,9 @@ Socket::write(const char *buf, size_t len)
}
Socket::UP
-Socket::connect(const vespalib::string &host, int port)
+Socket::connect(const SocketSpec &spec)
{
- SocketHandle handle = SocketAddress::select_remote(port, host.c_str()).connect();
- return std::make_unique<Socket>(std::move(handle));
-}
-
-Socket::UP
-Socket::connect(const vespalib::string &path)
-{
- SocketHandle handle = SocketAddress::from_path(path).connect();
+ SocketHandle handle = spec.client_address().connect();
return std::make_unique<Socket>(std::move(handle));
}
diff --git a/vespalib/src/vespa/vespalib/net/socket.h b/vespalib/src/vespa/vespalib/net/socket.h
index 7298c38946c..b83faf0a67c 100644
--- a/vespalib/src/vespa/vespalib/net/socket.h
+++ b/vespalib/src/vespa/vespalib/net/socket.h
@@ -9,6 +9,8 @@
namespace vespalib {
+class SocketSpec;
+
class Socket
{
private:
@@ -25,8 +27,7 @@ public:
void shutdown();
ssize_t read(char *buf, size_t len);
ssize_t write(const char *buf, size_t len);
- static Socket::UP connect(const vespalib::string &host, int port);
- static Socket::UP connect(const vespalib::string &path);
+ static Socket::UP connect(const SocketSpec &spec);
};
} // namespace vespalib
diff --git a/vespalib/src/vespa/vespalib/net/socket_spec.cpp b/vespalib/src/vespa/vespalib/net/socket_spec.cpp
index 09d26417948..6427225dbae 100644
--- a/vespalib/src/vespa/vespalib/net/socket_spec.cpp
+++ b/vespalib/src/vespa/vespalib/net/socket_spec.cpp
@@ -11,6 +11,23 @@ const vespalib::string ipc_prefix("ipc/file:");
} // namespace vespalib::<unnamed>
+SocketAddress
+SocketSpec::address(bool server) const
+{
+ if (!valid()) {
+ return SocketAddress();
+ }
+ if (!_path.empty()) {
+ return SocketAddress::from_path(_path);
+ }
+ const char *node = _host.empty() ? nullptr : _host.c_str();
+ if (server) {
+ return SocketAddress::select_local(_port, node);
+ } else {
+ return SocketAddress::select_remote(_port, node);
+ }
+}
+
SocketSpec::SocketSpec(const vespalib::string &spec)
: SocketSpec()
{
@@ -40,30 +57,4 @@ SocketSpec::SocketSpec(const vespalib::string &spec)
}
}
-SocketAddress
-SocketSpec::client_address() const
-{
- if (!valid()) {
- return SocketAddress();
- }
- if (!_path.empty()) {
- return SocketAddress::from_path(_path);
- }
- const char *node = _host.empty() ? "localhost" : _host.c_str();
- return SocketAddress::select_remote(_port, node);
-}
-
-SocketAddress
-SocketSpec::server_address() const
-{
- if (!valid()) {
- return SocketAddress();
- }
- if (!_path.empty()) {
- return SocketAddress::from_path(_path);
- }
- const char *node = _host.empty() ? nullptr : _host.c_str();
- return SocketAddress::select_local(_port, node);
-}
-
} // namespace vespalib
diff --git a/vespalib/src/vespa/vespalib/net/socket_spec.h b/vespalib/src/vespa/vespalib/net/socket_spec.h
index cd8a546a9fd..43e7cb8c4b9 100644
--- a/vespalib/src/vespa/vespalib/net/socket_spec.h
+++ b/vespalib/src/vespa/vespalib/net/socket_spec.h
@@ -20,8 +20,9 @@ private:
SocketSpec() : _path(), _host(), _port(-1) {}
SocketSpec(const vespalib::string &path, const vespalib::string &host, int port)
: _path(path), _host(host), _port(port) {}
+ SocketAddress address(bool server) const;
public:
- SocketSpec(const vespalib::string &spec);
+ explicit SocketSpec(const vespalib::string &spec);
static SocketSpec from_path(const vespalib::string &path) {
return SocketSpec(path, "", -1);
}
@@ -35,8 +36,8 @@ public:
const vespalib::string &path() const { return _path; }
const vespalib::string &host() const { return _host; }
int port() const { return _port; }
- SocketAddress client_address() const;
- SocketAddress server_address() const;
+ SocketAddress client_address() const { return address(false); }
+ SocketAddress server_address() const { return address(true); }
};
} // namespace vespalib