我已经参考了一些示例,并且我正在根据服务器和客户端示例对我的系统进行建模,我觉得我非常接近。
StarBuffer.fbs:
table StarBuffer {
radius: double;
mass: double;
volume: double;
}
root_type StarBuffer;
订阅者.cpp:
//
// Durable subscriber
//
// Olivier Chamoux <olivier.chamoux@fr.thalesgroup.com>
#include "zhelpers.hpp"
#include <zmq.hpp>
#include "StarBuffer.h" // generated flat file from StarBuffer.fbs
struct Star {
double radius ;
double mass;
double volume;
};
int main (int argc, char *argv[])
{
zmq::context_t context = zmq::context_t(1);
// Connect our subscriber socket
zmq::socket_t subSocket = zmq::socket_t(context, ZMQ_SUB);
subSocket.setsockopt(ZMQ_IDENTITY, "Hello", 5);
subSocket.setsockopt(ZMQ_SUBSCRIBE, "", 0);
subSocket.connect("tcp://localhost:5565");
// Get updates, expect random Ctrl-C death
while (1) {
zmq::message_t receiveMessage;
subSocket.recv(&receiveMessage);
flatbuffers::FlatBufferBuilder fbb;
StarBufferBuilder builder(fbb);
auto star = GetStarBuffer(receiveMessage.data());
std::cout << "Received Star" << std::endl;
std::cout << "radius: " << star->radius() << std::endl;
std::cout << "mass: " << star->mass() << std::endl;
std::cout << "volume: " << star->volume() << std::endl;
}
return 0;
}
发布者.cpp:
//
// Publisher for durable subscriber
//
// Olivier Chamoux <olivier.chamoux@fr.thalesgroup.com>
#include "zhelpers.hpp"
#include <zmq.hpp>
#include <random>
#include "StarBuffer.h" // generated flat file from StarBuffer.fbs
struct Star {
double radius ;
double mass;
double volume;
};
template<typename Numeric, typename Generator = std::mt19937>
Numeric random(Numeric from, Numeric to)
{
thread_local static Generator gen(std::random_device{}());
using dist_type = typename std::conditional
<
std::is_integral<Numeric>::value
, std::uniform_int_distribution<Numeric>
, std::uniform_real_distribution<Numeric>
>::type;
thread_local static dist_type dist;
return dist(gen, typename dist_type::param_type{from, to});
}
Star getRandomStar() {
double lower_bound = 0;
double upper_bound = std::numeric_limits<double>::max();
double randomRadius = random(lower_bound, upper_bound);
double randomMass = random(lower_bound, upper_bound);
double randomVolume = random(lower_bound, upper_bound);
Star s = Star();
s.radius = randomRadius;
s.mass = randomMass;
s.volume = randomVolume;
return s;
}
int main () {
zmq::context_t context = zmq::context_t(1);
// We send updates via this socket
zmq::socket_t publishSocket = zmq::socket_t(context, ZMQ_PUB);
publishSocket.bind("tcp://*:5565");
// Now broadcast exactly 10 updates with pause
int update_nbr;
for (update_nbr = 0; update_nbr < 100; update_nbr++) {
Star randomStar = getRandomStar();
flatbuffers::FlatBufferBuilder fbb;
StarBufferBuilder builder(fbb);
builder.add_radius(randomStar.radius);
builder.add_mass(randomStar.mass);
builder.add_volume(randomStar.volume);
auto response = builder.Finish();
fbb.Finish(response);
std::cout << "Sending Star " << update_nbr << "…" << std::endl;
int buffersize = fbb.GetSize();
zmq::message_t request(buffersize);
memcpy((void *)request.data(), fbb.GetBufferPointer(), buffersize);
publishSocket.send(request);
std::cout << "Star sent!" << std::endl;
sleep(1);
}
return 0;
}
编辑:代码编译并工作!我在这里更新了代码,以反映我在本地机器上的工作代码,供人们继续使用,因为这花了我很多时间来完成。
因为我也花了一段时间才弄清楚这些东西,所以这里是构建过程:
flatc --cpp StarBuffer.fbs
mv StarBuffer_generated.h StarBuffer.h
g++ -std=c++11 publisher.cpp -lzmq -o ./bin/zmq_pub
g++ -std=c++11 subscriber.cpp -lzmq -o ./bin/zmq_sub
需要本地目录中的“zhelpers.cpp”和“flatbuffers/flatbuffers.h”。