PRCYCoin  2.0.0.7rc1
P2P Digital Currency
zmqnotificationinterface.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
6 #include "zmqpublishnotifier.h"
7 
8 #include "version.h"
9 #include "main.h"
10 #include "streams.h"
11 #include "util.h"
12 
13 void zmqError(const char *str)
14 {
15  LogPrint(BCLog::ZMQ, "Error: %s, errno=%s\n", str, zmq_strerror(errno));
16 }
17 
19 {
20 }
21 
23 {
24  Shutdown();
25 
26  for (std::list<CZMQAbstractNotifier*>::iterator i=notifiers.begin(); i!=notifiers.end(); ++i)
27  {
28  delete *i;
29  }
30 }
31 
32 CZMQNotificationInterface* CZMQNotificationInterface::CreateWithArguments(const std::map<std::string, std::string> &args)
33 {
34  CZMQNotificationInterface* notificationInterface = NULL;
35  std::map<std::string, CZMQNotifierFactory> factories;
36  std::list<CZMQAbstractNotifier*> notifiers;
37 
38  factories["pubhashblock"] = CZMQAbstractNotifier::Create<CZMQPublishHashBlockNotifier>;
39  factories["pubhashtx"] = CZMQAbstractNotifier::Create<CZMQPublishHashTransactionNotifier>;
40  factories["pubhashtxlock"] = CZMQAbstractNotifier::Create<CZMQPublishHashTransactionLockNotifier>;
41  factories["pubrawblock"] = CZMQAbstractNotifier::Create<CZMQPublishRawBlockNotifier>;
42  factories["pubrawtx"] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionNotifier>;
43  factories["pubrawtxlock"] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionLockNotifier>;
44 
45  for (std::map<std::string, CZMQNotifierFactory>::const_iterator i=factories.begin(); i!=factories.end(); ++i)
46  {
47  std::map<std::string, std::string>::const_iterator j = args.find("-zmq" + i->first);
48  if (j!=args.end())
49  {
50  CZMQNotifierFactory factory = i->second;
51  std::string address = j->second;
52  CZMQAbstractNotifier *notifier = factory();
53  notifier->SetType(i->first);
54  notifier->SetAddress(address);
55  notifiers.push_back(notifier);
56  }
57  }
58 
59  if (!notifiers.empty())
60  {
61  notificationInterface = new CZMQNotificationInterface();
62  notificationInterface->notifiers = notifiers;
63 
64  if (!notificationInterface->Initialize())
65  {
66  delete notificationInterface;
67  notificationInterface = NULL;
68  }
69  }
70 
71  return notificationInterface;
72 }
73 
74 // Called at startup to conditionally set up ZMQ socket(s)
76 {
77  LogPrint(BCLog::ZMQ, "Initialize notification interface\n");
78  assert(!pcontext);
79 
80  pcontext = zmq_init(1);
81 
82  if (!pcontext)
83  {
84  zmqError("Unable to initialize context");
85  return false;
86  }
87 
88  std::list<CZMQAbstractNotifier*>::iterator i=notifiers.begin();
89  for (; i!=notifiers.end(); ++i)
90  {
91  CZMQAbstractNotifier *notifier = *i;
92  if (notifier->Initialize(pcontext))
93  {
94  LogPrint(BCLog::ZMQ, "Notifier %s ready (address = %s)\n", notifier->GetType(), notifier->GetAddress());
95  }
96  else
97  {
98  LogPrint(BCLog::ZMQ, "Notifier %s failed (address = %s)\n", notifier->GetType(), notifier->GetAddress());
99  break;
100  }
101  }
102 
103  if (i!=notifiers.end())
104  {
105  return false;
106  }
107 
108  return true;
109 }
110 
111 // Called during shutdown sequence
113 {
114  LogPrint(BCLog::ZMQ, "Shutdown notification interface\n");
115  if (pcontext)
116  {
117  for (std::list<CZMQAbstractNotifier*>::iterator i=notifiers.begin(); i!=notifiers.end(); ++i)
118  {
119  CZMQAbstractNotifier *notifier = *i;
120  LogPrint(BCLog::ZMQ, "Shutdown notifier %s at %s\n", notifier->GetType(), notifier->GetAddress());
121  notifier->Shutdown();
122  }
123  zmq_ctx_destroy(pcontext);
124 
125  pcontext = 0;
126  }
127 }
128 
130 {
131  for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin(); i!=notifiers.end(); )
132  {
133  CZMQAbstractNotifier *notifier = *i;
134  if (notifier->NotifyBlock(pindex))
135  {
136  i++;
137  }
138  else
139  {
140  notifier->Shutdown();
141  i = notifiers.erase(i);
142  }
143  }
144 }
145 
147 {
148  for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin(); i!=notifiers.end(); )
149  {
150  CZMQAbstractNotifier *notifier = *i;
151  if (notifier->NotifyTransaction(tx))
152  {
153  i++;
154  }
155  else
156  {
157  notifier->Shutdown();
158  i = notifiers.erase(i);
159  }
160  }
161 }
162 
164 {
165  for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin(); i!=notifiers.end(); )
166  {
167  CZMQAbstractNotifier *notifier = *i;
168  if (notifier->NotifyTransactionLock(tx))
169  {
170  i++;
171  }
172  else
173  {
174  notifier->Shutdown();
175  i = notifiers.erase(i);
176  }
177  }
178 }
CZMQNotificationInterface::CZMQNotificationInterface
CZMQNotificationInterface()
Definition: zmqnotificationinterface.cpp:18
BCLog::ZMQ
@ ZMQ
Definition: logging.h:45
CZMQNotificationInterface::NotifyTransactionLock
void NotifyTransactionLock(const CTransaction &tx)
Definition: zmqnotificationinterface.cpp:163
CZMQNotificationInterface::pcontext
void * pcontext
Definition: zmqnotificationinterface.h:34
streams.h
CZMQAbstractNotifier::NotifyTransactionLock
virtual bool NotifyTransactionLock(const CTransaction &transaction)
Definition: zmqabstractnotifier.cpp:24
version.h
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:269
CZMQAbstractNotifier
Definition: zmqabstractnotifier.h:15
CZMQNotificationInterface::CreateWithArguments
static CZMQNotificationInterface * CreateWithArguments(const std::map< std::string, std::string > &args)
Definition: zmqnotificationinterface.cpp:32
CZMQAbstractNotifier::Shutdown
virtual void Shutdown()=0
CZMQNotificationInterface::Shutdown
void Shutdown()
Definition: zmqnotificationinterface.cpp:112
CZMQNotificationInterface::UpdatedBlockTip
void UpdatedBlockTip(const CBlockIndex *pindex)
Definition: zmqnotificationinterface.cpp:129
CZMQAbstractNotifier::SetAddress
void SetAddress(const std::string &a)
Definition: zmqabstractnotifier.h:30
CZMQAbstractNotifier::SetType
void SetType(const std::string &t)
Definition: zmqabstractnotifier.h:28
CZMQAbstractNotifier::GetType
std::string GetType() const
Definition: zmqabstractnotifier.h:27
LogPrint
#define LogPrint(category,...)
Definition: logging.h:162
zmqnotificationinterface.h
CZMQAbstractNotifier::Initialize
virtual bool Initialize(void *pcontext)=0
zmqpublishnotifier.h
CBlock
Definition: block.h:142
CZMQAbstractNotifier::NotifyTransaction
virtual bool NotifyTransaction(const CTransaction &transaction)
Definition: zmqabstractnotifier.cpp:19
main.h
CZMQAbstractNotifier::GetAddress
std::string GetAddress() const
Definition: zmqabstractnotifier.h:29
zmqError
void zmqError(const char *str)
Definition: zmqnotificationinterface.cpp:13
CZMQAbstractNotifier::NotifyBlock
virtual bool NotifyBlock(const CBlockIndex *pindex)
Definition: zmqabstractnotifier.cpp:14
CZMQNotificationInterface::SyncTransaction
void SyncTransaction(const CTransaction &tx, const CBlock *pblock)
Definition: zmqnotificationinterface.cpp:146
CZMQNotifierFactory
CZMQAbstractNotifier *(* CZMQNotifierFactory)()
Definition: zmqabstractnotifier.h:13
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:162
CZMQNotificationInterface::notifiers
std::list< CZMQAbstractNotifier * > notifiers
Definition: zmqnotificationinterface.h:35
CZMQNotificationInterface
Definition: zmqnotificationinterface.h:15
CZMQNotificationInterface::Initialize
bool Initialize()
Definition: zmqnotificationinterface.cpp:75
CZMQNotificationInterface::~CZMQNotificationInterface
virtual ~CZMQNotificationInterface()
Definition: zmqnotificationinterface.cpp:22