MADARA  3.1.8
ZMQContext.cpp
Go to the documentation of this file.
1 #include "ZMQContext.h"
2 #include <zmq.h>
4 
5 madara::transport::ZMQContext madara::transport::zmq_context;
6 
7 madara::transport::ZMQContext::ZMQContext ()
8  : context_ (0), references_ (0)
9 {
10 }
11 
12 void
13 madara::transport::ZMQContext::set_context (void * context)
14 {
16  "ZMQContext::set_context:" \
17  " setting the context\n");
18 
19  if (context != context_)
20  {
22  "ZMQContext::set_context:" \
23  " context is different from new context. Destroying old context.\n");
24 
25  destroy_context ();
26 
28  "ZMQContext::set_context:" \
29  " updating context and reference count.\n");
30 
31  context_ = context;
32  references_ = 1;
33 
35  "ZMQContext::set_context:" \
36  " result: context=%p, references=%d\n",
37  context_, int(references_));
38  }
39 }
40 
41 void
42 madara::transport::ZMQContext::add_ref (void)
43 {
44  if (context_ == 0)
45  {
47  "ZMQContext::add_ref:" \
48  " context didn't exist. Creating new context.\n",
49  context_, int(references_));
50 
51  references_ = 1;
52  context_ = zmq_ctx_new ();
53  }
54  else
55  {
56  ++references_;
57  }
58 
60  "ZMQContext::add_ref:" \
61  " result: context=%p, references=%d\n",
62  context_, int(references_));
63 }
64 
65 void
66 madara::transport::ZMQContext::rem_ref (void)
67 {
68  if (references_ > 0)
69  {
71  "ZMQContext::rem_ref:" \
72  " removing reference from context.\n",
73  context_, int(references_));
74 
75  --references_;
76 
77  if (references_ == 0)
78  {
79  destroy_context ();
80  }
81  }
82 
84  "ZMQContext::rem_ref:" \
85  " result: context=%p, references=%d\n",
86  context_, int(references_));
87 }
88 
89 void
90 madara::transport::ZMQContext::destroy_context (void)
91 {
92  if (context_ != 0)
93  {
95  "ZMQContext::destroy_context:" \
96  " destroying context.\n",
97  context_, int(references_));
98 
99  zmq_ctx_destroy (context_);
100  context_ = 0;
101  references_ = 0;
102  }
103 }
104 
108 madara::transport::ZMQContext::~ZMQContext ()
109 {
110  destroy_context ();
111 }
knowledge::ThreadSafeContext * context_
knowledge context
MADARA_Export utility::Refcounter< logger::Logger > global_logger
#define madara_logger_ptr_log(logger, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:32