Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client/Server tweaks #226

Merged
merged 2 commits into from
Jul 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ ZeroEQ provides the following major features:
* Publish events using zeroeq::Publisher
* Subscribe to events using zeroeq::Subscriber
* Web service APIs using zeroeq::http::Server using REST and JSON
* Client-Server request-reply using zeroeq::Server and zeroeq::Client
* Asynchronous, reliable transport using [ZeroMQ](http://www.zeromq.org)
* Automatic publisher discovery using [Zeroconf](https://en.wikipedia.org/wiki/Zero-configuration_networking)
* Serialization of events using [ZeroBuf](https://github.com/HBPVIS/ZeroBuf)
Expand Down
2 changes: 2 additions & 0 deletions tests/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#ifdef _WIN32
#include <process.h>
#define getpid _getpid
#define setenv(name, value, overwrite) _putenv_s(name, value)
#define unsetenv(name) _putenv_s(name, nullptr)
#else
#include <sys/types.h>
#include <unistd.h>
Expand Down
5 changes: 0 additions & 5 deletions tests/publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@

#include <servus/servus.h>

#ifdef _MSC_VER
#define setenv(name, value, overwrite) _putenv_s(name, value)
#define unsetenv(name) _putenv_s(name, nullptr)
#endif

BOOST_AUTO_TEST_CASE(create_uri_publisher)
{
const zeroeq::Publisher publisher(zeroeq::URI(""));
Expand Down
40 changes: 40 additions & 0 deletions tests/reqRep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <servus/servus.h>
#include <servus/uri.h>

#include <atomic>
#include <chrono>
#include <thread>

Expand Down Expand Up @@ -320,6 +321,45 @@ BOOST_AUTO_TEST_CASE(two_servers)
BOOST_CHECK(serverHandled);
}

BOOST_AUTO_TEST_CASE(envconnect)
{
test::Echo echo("The quick brown fox");
const test::Echo reply("Jumped over the lazy dog");

zeroeq::Server server1(zeroeq::NULL_SESSION);
zeroeq::Server server2(zeroeq::NULL_SESSION);
std::string servers(server1.getURI().getHost() + ":" +
std::to_string(int(server1.getURI().getPort())) + "," +
server2.getURI().getHost() + ":" +
std::to_string(int(server2.getURI().getPort())));
setenv("ZEROEQ_SERVERS", servers.c_str(), 1);

zeroeq::Client client;

bool handled1 = false;
bool handled2 = false;
std::thread thread1([&] { handled1 = runOnce(server1, echo, reply); });
std::thread thread2([&] { handled2 = runOnce(server2, echo, reply); });

size_t handled = 0;
const auto func = [&](const zeroeq::uint128_t&, const void*, size_t) {
++handled;
};
client.request(echo, func);
client.request(echo, func);

BOOST_CHECK_EQUAL(handled, 0);
BOOST_CHECK(client.receive(TIMEOUT));
if (handled < 2)
BOOST_CHECK(client.receive(TIMEOUT));
BOOST_CHECK_EQUAL(handled, 2);

thread1.join();
thread2.join();
BOOST_CHECK(handled1);
BOOST_CHECK(handled2);
}

BOOST_AUTO_TEST_CASE(two_clients)
{
test::Echo echo("The quick brown fox");
Expand Down
20 changes: 18 additions & 2 deletions zeroeq/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ class Client::Impl : public detail::Receiver
, _servers(zmq_socket(getContext(), ZMQ_DEALER),
[](void* s) { ::zmq_close(s); })
{
const char* serversEnv = getenv("ZEROEQ_SERVERS");
if (!serversEnv)
return;

std::string servers(serversEnv);
while (!servers.empty())
{
const size_t pos = servers.find(',');
const std::string server = servers.substr(0, pos);
servers = pos == std::string::npos ? std::string()
: servers.substr(pos + 1);

const auto& zmqURI = buildZmqURI(URI(server));
if (!addConnection(zmqURI))
ZEROEQTHROW(std::runtime_error("Cannot connect client to " +
zmqURI + ": " +
zmq_strerror(zmq_errno())));
}
}

explicit Impl(const URIs& uris)
Expand All @@ -38,11 +56,9 @@ class Client::Impl : public detail::Receiver

const auto& zmqURI = buildZmqURI(uri);
if (!addConnection(zmqURI))
{
ZEROEQTHROW(std::runtime_error("Cannot connect client to " +
zmqURI + ": " +
zmq_strerror(zmq_errno())));
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions zeroeq/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class Client : public Receiver
* Create a default client.
*
* Postconditions:
* - connects to all servers set in the comma-separated environment variable
* ZEROEQ_SERVERS
* - discovers servers on _zeroeq_rep._tcp ZeroConf service
* - filters session \<username\> or ZEROEQ_SERVER_SESSION from environment
*
Expand All @@ -49,6 +51,8 @@ class Client : public Receiver
* Create a client which connects to servers from the given session.
*
* Postconditions:
* - connects to all servers set in the comma-separated environment variable
* ZEROEQ_SERVERS
* - discovers publishers on _zeroeq_rep._tcp ZeroConf service
* - filters for given session
*
Expand Down