10 zmq::socket_t Ireceive (*
context, ZMQ_PUSH);
11 Ireceive.connect(
"inproc://ServicePublish");
13 boost::uuids::uuid m_UUID;
14 m_UUID = boost::uuids::random_generator()();
16 std::stringstream test;
17 test<<
"Add "<< ServiceName <<
" "<<m_UUID<<
" "<<port<<
" "<<((int)StatusQuery) ;
19 zmq::message_t send(test.str().length()+1);
20 snprintf ((
char *) send.data(), test.str().length()+1 ,
"%s" ,test.str().c_str()) ;
22 return Ireceive.send(send);
30 zmq::socket_t Ireceive (*
context, ZMQ_PUSH);
31 Ireceive.connect(
"inproc://ServicePublish");
33 std::stringstream test;
34 test<<
"Delete "<< ServiceName <<
" ";
35 zmq::message_t send(test.str().length()+1);
36 snprintf ((
char *) send.data(), test.str().length()+1 ,
"%s" ,test.str().c_str()) ;
38 return Ireceive.send(send);
44 boost::uuids::uuid m_UUID=boost::uuids::random_generator()();
47 zmq::socket_t Ireceive (*
context, ZMQ_DEALER);
48 Ireceive.connect(
"inproc://ServiceDiscovery");
51 zmq::message_t send(4);
52 snprintf ((
char *) send.data(), 4 ,
"%s" ,
"All") ;
57 zmq::message_t receive;
58 Ireceive.recv(&receive);
59 std::istringstream iss(static_cast<char*>(receive.data()));
64 for(
int i=0;i<size;i++){
66 Store *service =
new Store;
68 zmq::message_t servicem;
69 Ireceive.recv(&servicem);
71 std::istringstream ss(static_cast<char*>(servicem.data()));
72 service->JsonParser(ss.str());
78 service->Get(
"msg_value",type);
79 service->Get(
"uuid",uuid);
80 service->Get(
"ip",ip);
81 service->Get(
"remote_port",port);
82 std::string tmp=ip +
":" + port;
85 if(type == ServiceName && connections.count(tmp)==0){
86 connections[tmp]=service;
92 sock->connect(tmp.c_str());
102 return connections.size();
107 if(
Threads.count(ThreadName)==0){
133 if(
Threads.count(ThreadName)==0){
150 zmq::socket_t IThread(*(args->
context), ZMQ_PAIR);
152 std::stringstream tmp;
154 IThread.bind(tmp.str().c_str());
157 zmq::pollitem_t initems[] = {
158 {IThread, 0, ZMQ_POLLIN, 0}};
165 std::string command=
"";
167 zmq::poll(&initems[0], 1, 0);
169 if ((initems[0].revents & ZMQ_POLLIN)){
171 zmq::message_t message;
172 IThread.recv(&message);
173 command=std::string(static_cast<char *>(message.data()));
210 args->
sock =
new zmq::socket_t(*(args->
context), ZMQ_PAIR);
211 std::stringstream tmp;
213 args->
sock->connect(tmp.str().c_str());
217 zmq::message_t msg(Message.length()+1);
218 snprintf((
char *)msg.data(), Message.length()+1,
"%s", Message.c_str());
220 if(block) ret=args->
sock->send(msg);
221 else ret=args->
sock->send(msg, ZMQ_NOBLOCK);
243 pthread_join(args->
thread, NULL);
265 std::string filepath = pathname +
"/" + filename;
266 bool exists = access(filepath.c_str(), F_OK) != -1;
268 std::stringstream ss;
269 ss <<
"FATAL: " << filepath <<
" not found or inaccessible";
276 void util::Log(
const std::string & message,
const int message_level) {
277 std::stringstream tmp;
278 tmp <<
"[" << message_level <<
"] " << message;
279 std::cout << tmp.str() << std::endl;
282 void util::Log(std::stringstream & message,
const int message_level) {
283 Log(message.str(), message_level);
bool MessageThread(Thread_args *args, std::string Message, bool block=true)
Send simple string to String thread.
bool running
Bool flag to tell the thread to run (if not set thread goes into wait cycle.
bool AddService(std::string ServiceName, unsigned int port, bool StatusQuery=false)
Broadcasts an available service (only in remote mode)
bool kill
Bool flay used to kill the thread.
std::string ThreadName
name of thread (deffined at creation)
zmq::socket_t * sock
ZMQ socket pointer is assigned in string thread,but can be sued otherwise.
static void * String_Thread(void *arg)
Simpe string thread.
static void * Thread(void *arg)
Thread with args.
void(* func_with_string)(std::string)
function pointer to string thread
void(* func)(Thread_args *)
function pointer to thread with args
Thread_args * CreateThread(std::string ThreadName, void(*func)(std::string))
Utilities(zmq::context_t *zmqcontext)
Simple constructor.
std::map< std::string, Thread_args * > Threads
Map of threads managed by the utilities class.
pthread_t thread
Simple constructor underlying thread that interface is built ontop of.
zmq::context_t * context
ZMQ context used for ZMQ socket creation.
void Log(const std::string &message, const int message_level)
Format messages in the same way as for tools.
bool KillThread(Thread_args *&args)
Kill a thread assosiated to args.
bool RemoveService(std::string ServiceName)
Removes service broadcasts for a service.
bool FileExists(std::string pathname, std::string filename)
Check if a file exists.
zmq::context_t * context
ZMQ context pointer.
int UpdateConnections(std::string ServiceName, zmq::socket_t *sock, std::map< std::string, Store * > &connections)
Dynamically connects a socket tp services broadcast with a specific name.