-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Protobuf + ZeroMQ Networking Example
See #12, #13. References: - http://api.zeromq.org/2-1:zmq-cpp
- Loading branch information
1 parent
4af23e5
commit 1a9bc4e
Showing
5 changed files
with
157 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#include <cstdlib> | ||
#include <cstring> | ||
|
||
#include <iostream> | ||
#include <iterator> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include <google/protobuf/stubs/common.h> | ||
#include <sweep/sweep.hpp> | ||
#include <zmq.hpp> | ||
|
||
#include "net.pb.h" | ||
|
||
static void usage() { | ||
std::cout << "Usage: example-net [ publisher | subscriber ]\n"; | ||
std::exit(EXIT_SUCCESS); | ||
} | ||
|
||
void subscriber() { | ||
zmq::context_t ctx{/*io_threads=*/1}; | ||
zmq::socket_t sub{ctx, ZMQ_SUB}; | ||
|
||
sub.connect("tcp://127.0.0.1:5555"); | ||
sub.setsockopt(ZMQ_SUBSCRIBE, "", 0); | ||
|
||
std::cout << "Subscribing." << std::endl; | ||
|
||
for (;;) { | ||
zmq::message_t msg; | ||
|
||
if (!sub.recv(&msg)) | ||
continue; | ||
|
||
sweep::proto::scan in; | ||
in.ParseFromArray(msg.data(), msg.size()); | ||
|
||
const auto n = in.angle_size(); | ||
|
||
for (auto i = 0; i < n; ++i) { | ||
std::cout << "Angle: " << in.angle(i) // | ||
<< " Distance: " << in.distance(i) // | ||
<< " Signal strength: " << in.signal_strength(i) // | ||
<< std::endl; | ||
} | ||
} | ||
} | ||
|
||
void publisher() try { | ||
zmq::context_t ctx{/*io_threads=*/1}; | ||
zmq::socket_t pub{ctx, ZMQ_PUB}; | ||
|
||
pub.bind("tcp://127.0.0.1:5555"); | ||
|
||
sweep::sweep device; | ||
device.start_scanning(); | ||
|
||
std::cout << "Publishing. Each dot is a full 360 degree scan." << std::endl; | ||
|
||
for (;;) { | ||
const sweep::scan scan = device.get_scan(); | ||
|
||
sweep::proto::scan out; | ||
|
||
for (const sweep::sample& sample : scan.samples) { | ||
out.add_angle(sample.angle); | ||
out.add_distance(sample.distance); | ||
out.add_signal_strength(sample.signal_strength); | ||
} | ||
|
||
auto encoded = out.SerializeAsString(); | ||
|
||
zmq::message_t msg{encoded.size()}; | ||
std::memcpy(msg.data(), encoded.data(), encoded.size()); | ||
|
||
const auto ok = pub.send(msg); | ||
|
||
if (ok) | ||
std::cout << "." << std::flush; | ||
} | ||
|
||
device.stop_scanning(); | ||
|
||
} catch (const sweep::device_error& e) { | ||
std::cerr << "Error: " << e.what() << '\n'; | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
std::vector<std::string> args{argv, argv + argc}; | ||
|
||
if (args.size() != 2) | ||
usage(); | ||
|
||
const auto isPublisher = args[1] == "publisher"; | ||
const auto isSubscriber = args[1] == "subscriber"; | ||
|
||
if (!isPublisher && !isSubscriber) | ||
usage(); | ||
|
||
GOOGLE_PROTOBUF_VERIFY_VERSION; | ||
|
||
struct AtExit { | ||
~AtExit() { ::google::protobuf::ShutdownProtobufLibrary(); } | ||
} sentry; | ||
|
||
if (isPublisher) | ||
publisher(); | ||
|
||
if (isSubscriber) | ||
subscriber(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package sweep.proto; | ||
|
||
option optimize_for = LITE_RUNTIME; | ||
|
||
message scan { | ||
repeated int32 angle = 1 [packed=true]; | ||
repeated int32 distance = 2 [packed=true]; | ||
repeated int32 signal_strength = 3 [packed=true]; | ||
} |