ADORe
ADORe is a modular open source software library and toolkit for decision making, planning, control and simulation of automated vehicles
zmqobjectprovider.h
Go to the documentation of this file.
1 /********************************************************************************
2  * Copyright (C) 2017-2020 German Aerospace Center (DLR).
3  * Eclipse ADORe, Automated Driving Open Research https://eclipse.org/adore
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License 2.0 which is available at
7  * http://www.eclipse.org/legal/epl-2.0.
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  * Daniel Heß - initial API and implementation
13  ********************************************************************************/
14 
15 #pragma once
16 #include <zmq.hpp>
17 #include <iostream>
18 
19 template<typename T>
21 {
22 private:
23  zmq::socket_t* m_socket;
24  zmq::context_t* m_context;
26  virtual void initialize(zmq::context_t& context, const char* target)
27  {
28  try
29  {
30  m_socket = new zmq::socket_t(context, ZMQ_PUSH);
31  m_socket->connect(target);
32  // Has to be int according to ZMQ docs
33  typedef int ZmqSndBufSizeType;
34  ZmqSndBufSizeType msgsize = sizeof(T);
35  //m_socket->setsockopt(ZMQ_SNDBUF,&msgsize,sizeof(ZmqSndBufSizeType));//hess_da, 01.08.2018: Some version of zmq seems to be incompatible with this command
36  m_initialized = true;
37  std::cout<<"connected to "<<target<<", with msg size="<<msgsize<<"\n";
38  }catch(zmq::error_t err)
39  {
40  std::cout<<"error in ZMQObjectProvider::init: "<<err.what()<<"\n";
41  m_initialized = false;
42  }
43  }
44 public:
45  ZMQObjectProvider(zmq::context_t& context, const char* target)
46  {
47  m_context = &context;
48  initialize(*m_context, target);
49  }
50  ZMQObjectProvider(const char* target)
51  {
52  m_context = new zmq::context_t(1);
53  initialize(*m_context, target);
54  }
55 
57  {
58  if(m_initialized)delete m_socket;
59  }
60  void send(T* value)
61  {
62  if(!m_initialized)return;
63  try
64  {
65  zmq::message_t message(sizeof(T));
66  memcpy(message.data(), value, sizeof(T));
67  m_socket->send(message);
68  int setting = 0;
69  }catch(zmq::error_t err)
70  {
71  std::cout<<"error in ZMQObjectProvider::send: "<<err.what()<<"\n";
72  }
73  }
74 
75 };
Definition: zmqobjectprovider.h:21
bool m_initialized
Definition: zmqobjectprovider.h:25
void send(T *value)
Definition: zmqobjectprovider.h:60
ZMQObjectProvider(const char *target)
Definition: zmqobjectprovider.h:50
zmq::socket_t * m_socket
Definition: zmqobjectprovider.h:23
virtual void initialize(zmq::context_t &context, const char *target)
Definition: zmqobjectprovider.h:26
virtual ~ZMQObjectProvider()
Definition: zmqobjectprovider.h:56
ZMQObjectProvider(zmq::context_t &context, const char *target)
Definition: zmqobjectprovider.h:45
zmq::context_t * m_context
Definition: zmqobjectprovider.h:24