You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

zmqnotificationinterface.cpp 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #include "zmqnotificationinterface.h"
  5. #include "zmqpublishnotifier.h"
  6. #include "version.h"
  7. #include "main.h"
  8. #include "streams.h"
  9. #include "util.h"
  10. void zmqError(const char *str)
  11. {
  12. LogPrint("zmq", "zmq: Error: %s, errno=%s\n", str, zmq_strerror(errno));
  13. }
  14. CZMQNotificationInterface::CZMQNotificationInterface() : pcontext(NULL)
  15. {
  16. }
  17. CZMQNotificationInterface::~CZMQNotificationInterface()
  18. {
  19. Shutdown();
  20. for (std::list<CZMQAbstractNotifier*>::iterator i=notifiers.begin(); i!=notifiers.end(); ++i)
  21. {
  22. delete *i;
  23. }
  24. }
  25. CZMQNotificationInterface* CZMQNotificationInterface::CreateWithArguments(const std::map<std::string, std::string> &args)
  26. {
  27. CZMQNotificationInterface* notificationInterface = NULL;
  28. std::map<std::string, CZMQNotifierFactory> factories;
  29. std::list<CZMQAbstractNotifier*> notifiers;
  30. factories["pubhashblock"] = CZMQAbstractNotifier::Create<CZMQPublishHashBlockNotifier>;
  31. factories["pubhashtx"] = CZMQAbstractNotifier::Create<CZMQPublishHashTransactionNotifier>;
  32. factories["pubrawblock"] = CZMQAbstractNotifier::Create<CZMQPublishRawBlockNotifier>;
  33. factories["pubrawtx"] = CZMQAbstractNotifier::Create<CZMQPublishRawTransactionNotifier>;
  34. for (std::map<std::string, CZMQNotifierFactory>::const_iterator i=factories.begin(); i!=factories.end(); ++i)
  35. {
  36. std::map<std::string, std::string>::const_iterator j = args.find("-zmq" + i->first);
  37. if (j!=args.end())
  38. {
  39. CZMQNotifierFactory factory = i->second;
  40. std::string address = j->second;
  41. CZMQAbstractNotifier *notifier = factory();
  42. notifier->SetType(i->first);
  43. notifier->SetAddress(address);
  44. notifiers.push_back(notifier);
  45. }
  46. }
  47. if (!notifiers.empty())
  48. {
  49. notificationInterface = new CZMQNotificationInterface();
  50. notificationInterface->notifiers = notifiers;
  51. if (!notificationInterface->Initialize())
  52. {
  53. delete notificationInterface;
  54. notificationInterface = NULL;
  55. }
  56. }
  57. return notificationInterface;
  58. }
  59. // Called at startup to conditionally set up ZMQ socket(s)
  60. bool CZMQNotificationInterface::Initialize()
  61. {
  62. LogPrint("zmq", "zmq: Initialize notification interface\n");
  63. assert(!pcontext);
  64. pcontext = zmq_init(1);
  65. if (!pcontext)
  66. {
  67. zmqError("Unable to initialize context");
  68. return false;
  69. }
  70. std::list<CZMQAbstractNotifier*>::iterator i=notifiers.begin();
  71. for (; i!=notifiers.end(); ++i)
  72. {
  73. CZMQAbstractNotifier *notifier = *i;
  74. if (notifier->Initialize(pcontext))
  75. {
  76. LogPrint("zmq", " Notifier %s ready (address = %s)\n", notifier->GetType(), notifier->GetAddress());
  77. }
  78. else
  79. {
  80. LogPrint("zmq", " Notifier %s failed (address = %s)\n", notifier->GetType(), notifier->GetAddress());
  81. break;
  82. }
  83. }
  84. if (i!=notifiers.end())
  85. {
  86. return false;
  87. }
  88. return true;
  89. }
  90. // Called during shutdown sequence
  91. void CZMQNotificationInterface::Shutdown()
  92. {
  93. LogPrint("zmq", "zmq: Shutdown notification interface\n");
  94. if (pcontext)
  95. {
  96. for (std::list<CZMQAbstractNotifier*>::iterator i=notifiers.begin(); i!=notifiers.end(); ++i)
  97. {
  98. CZMQAbstractNotifier *notifier = *i;
  99. LogPrint("zmq", " Shutdown notifier %s at %s\n", notifier->GetType(), notifier->GetAddress());
  100. notifier->Shutdown();
  101. }
  102. zmq_ctx_destroy(pcontext);
  103. pcontext = 0;
  104. }
  105. }
  106. void CZMQNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindex)
  107. {
  108. for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin(); i!=notifiers.end(); )
  109. {
  110. CZMQAbstractNotifier *notifier = *i;
  111. if (notifier->NotifyBlock(pindex))
  112. {
  113. i++;
  114. }
  115. else
  116. {
  117. notifier->Shutdown();
  118. i = notifiers.erase(i);
  119. }
  120. }
  121. }
  122. void CZMQNotificationInterface::SyncTransaction(const CTransaction& tx, const CBlockIndex* pindex, const CBlock* pblock)
  123. {
  124. for (std::list<CZMQAbstractNotifier*>::iterator i = notifiers.begin(); i!=notifiers.end(); )
  125. {
  126. CZMQAbstractNotifier *notifier = *i;
  127. if (notifier->NotifyTransaction(tx))
  128. {
  129. i++;
  130. }
  131. else
  132. {
  133. notifier->Shutdown();
  134. i = notifiers.erase(i);
  135. }
  136. }
  137. }