EtherCAT Client Library Code 0.0.1
EtherCAT Client library that uses different communication protocols with EtherCAT Master server.
Loading...
Searching...
No Matches
ec_zmq_context.h
1#ifndef __EC_ZMQ_CONTEXT__
2#define __EC_ZMQ_CONTEXT__
3
4#include <protobuf/repl_cmd.pb.h>
5#include <protobuf/ecat_pdo.pb.h>
6
7#include "zmq.hpp"
8#include <zmq_addon.hpp>
9#include <google/protobuf/io/coded_stream.h>
10#include <google/protobuf/io/zero_copy_stream_impl.h>
11
12namespace EcZmqPdoContext{
13 extern std::unique_ptr<zmq::context_t> pdo_context;
14 inline static void start_context(){
15 if(pdo_context==nullptr){
16 try{
17 pdo_context=std::make_unique<zmq::context_t>(1);
18 //std::cout << "starting of zmq pdo context" << std::endl;
19 }catch (const zmq::error_t &err){
20 //std::cout << "fatal error on starting zmq pdo context: " << err.what() << std::endl;
21 }
22 }
23 }
24 inline static void stop_context(){
25 if(pdo_context!=nullptr){
26 try{
27 pdo_context->shutdown();
28 //std::cout << "shutdowning of zmq pdo context" << std::endl;
29 pdo_context.reset();
30 }catch (const zmq::error_t &err){
31 //std::cout << "fatal error on shutdowning zmq pdo context: " << err.what() << std::endl;
32 }
33 }
34 }
35}
36
37namespace EcZmqCmdContext{
38 extern std::unique_ptr<zmq::context_t> cmd_context;
39 inline static void start_context(){
40 if(cmd_context==nullptr){
41 try{
42 cmd_context=std::make_unique<zmq::context_t>(1);
43 //std::cout << "starting of zmq command context" << std::endl;
44 }catch (const zmq::error_t &err){
45 //std::cout << "fatal error on starting zmq command context: " << err.what() << std::endl;
46 }
47 }
48 }
49 inline static void stop_context(){
50 if(cmd_context!=nullptr){
51 try{
52 cmd_context->shutdown();
53 //std::cout << "shutdowning of zmq command context" << std::endl;
54 cmd_context.reset();
55 }catch (const zmq::error_t &err){
56 //std::cout << "fatal error on shutdowning zmq command context: " << err.what() << std::endl;
57 }
58 }
59 }
60}
61#endif
62
63
64/******************** SINGLETON TEST */
65/*
66class EcZmqPdoContext {
67public:
68 EcZmqPdoContext &operator=(EcZmqPdoContext &&) = default;
69 static EcZmqPdoContext& getInstance()
70 {
71 static EcZmqPdoContext instance;
72 if(_pdo_context==nullptr){
73 _pdo_context=std::make_unique<zmq::context_t>(1);
74 std::cout << "new context" << std::endl;
75 }
76 return instance;
77 }
78
79 static zmq::context_t* context(){
80 return _pdo_context.get();
81 }
82
83 static void clear_context() {
84 getInstance() = EcZmqPdoContext{};
85 if(_pdo_context!=nullptr){
86 _pdo_context->shutdown();
87 std::cout << "shutdown context" << std::endl;
88 }
89 _pdo_context.reset();
90 }
91
92 EcZmqPdoContext(EcZmqPdoContext &&) = delete;
93 EcZmqPdoContext(const EcZmqPdoContext&) = delete;
94 EcZmqPdoContext& operator=(const EcZmqPdoContext&) = delete;
95
96private:
97 EcZmqPdoContext()
98 {
99 }
100
101 ~EcZmqPdoContext()
102 {
103 }
104
105 static std::unique_ptr<zmq::context_t> _pdo_context;
106};
107*/