Created
April 10, 2018 16:50
-
-
Save SysOverdrive/a4b509dd5d19de85c791a32fab9224c8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// ClientForZmq.cpp : Defines the entry point for the console application. | |
// | |
#include "stdafx.h" | |
#include <zmq.hpp> | |
#include <string.h> | |
#include <stdio.h> | |
#include "stdlib.h" | |
#include "string.h" | |
#include <msgpack.hpp> | |
#include <iostream> | |
//#include "zhelpers.h" | |
struct center { | |
center() : x(0), y(0), z(0) {} | |
int x; | |
int y; | |
int z; | |
MSGPACK_DEFINE_ARRAY(x, y, z); | |
}center; | |
struct normal { | |
normal() : x(0), y(0), z(0) {} | |
int x; | |
int y; | |
int z; | |
MSGPACK_DEFINE(x, y, z); | |
}normal; | |
struct circle_3d { | |
circle_3d() : radius(0) {} | |
int radius; | |
MSGPACK_DEFINE(MSGPACK_DEFINE_ARRAY(center), MSGPACK_DEFINE(normal), radius); | |
}; | |
typedef struct PupilTopStruct: circle_3d, normal, center { | |
PupilTopStruct() : confidence(0), timestamp(0) {} //Default Values | |
std::string topic; | |
double confidence; | |
int timestamp; | |
MSGPACK_DEFINE(topic, MSGPACK_DEFINE(circle_3d), confidence, timestamp); | |
}; | |
// struct norm_pos { | |
// double x; | |
// double y; | |
// MSGPACK_DEFINE_ARRAY(x, y); | |
//}; | |
// | |
// struct gaze :norm_pos{ | |
//std::string topic; | |
//MSGPACK_DEFINE_MAP(topic, MSGPACK_BASE_ARRAY(norm_pos)); | |
//}; | |
int main(void) | |
{ | |
//Variables | |
zmq::context_t *ctx; | |
zmq::socket_t *subSocket; | |
//void *requester; | |
//Initialise context and connect | |
ctx = new zmq::context_t(1); | |
std::string addr = "tcp://127.0.0.1:"; | |
std::string req_port = "50020"; | |
std::string con_addr = addr + req_port; | |
zmq::socket_t s(*ctx, ZMQ_REQ); | |
s.connect(con_addr); | |
// Send Request for Sub Port | |
std::string sendSubPort = u8"SUB_PORT"; | |
zmq::message_t subport_request(8); | |
memcpy(subport_request.data(), sendSubPort.c_str(), sendSubPort.length()); | |
s.send(subport_request); | |
//Receive Sub Port | |
zmq::message_t reply; | |
s.recv(&reply); //type message_t | |
//Log Subport | |
std::string rpl = std::string(static_cast<char*>(reply.data()), reply.size()); | |
printf("ZMQ >>>>Sub Port Response %s", rpl.c_str()); | |
/// SUBSCRIBER SOCKET | |
std::string subPortAddr = addr + rpl; | |
subSocket = new zmq::socket_t(*ctx, ZMQ_SUB); | |
subSocket->connect(subPortAddr); | |
//Subscribe to topic | |
std::string filter = u8"gaze"; | |
subSocket->setsockopt(ZMQ_SUBSCRIBE, filter.c_str(), filter.length()); | |
int i = 0; | |
while (i < 100) | |
{ | |
zmq::message_t topic; | |
subSocket->recv(&topic); | |
std::string rpl = std::string(static_cast<char*>(topic.data()), topic.size()); | |
printf("ZMQ >>>>Topic %s \n", rpl.c_str()); | |
//FString PortRequest(rpl.c_str()); | |
// UE_LOG(LogTemp, Warning, TEXT("ZMQ >>>>Topic %s"), *PortRequest); | |
zmq::message_t info; | |
subSocket->recv(&info); | |
char* payload = static_cast<char*>(info.data()); | |
std::string rplInfo = std::string(payload, info.size()); | |
msgpack::object_handle result; | |
result = msgpack::unpack(payload, info.size()); | |
msgpack::object obj(result.get()); | |
printf(" \nINFO.Data : \n"); | |
msgpack::object_handle oh = msgpack::unpack(payload, info.size()); | |
msgpack::object deserialized = oh.get(); | |
std::cout << deserialized << std::endl; | |
//PupilStruct rvec; | |
// PupilClass rvec; | |
// deserialized.convert(rvec); | |
i++; | |
} | |
zmq_ctx_destroy(ctx); | |
getchar(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment