Hermes.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "admin/angel/Hermes.h"
  16. #include "benc/Dict.h"
  17. #include "benc/String.h"
  18. #include "benc/serialization/standard/BencMessageWriter.h"
  19. #include "benc/serialization/standard/BencMessageReader.h"
  20. #include "memory/Allocator.h"
  21. #include "util/events/Event.h"
  22. #include "util/events/EventBase.h"
  23. #include "util/log/Log.h"
  24. #include "util/events/Timeout.h"
  25. #include "util/Identity.h"
  26. #include "util/Hex.h"
  27. #define REQ_TIMEOUT 10000
  28. struct Request
  29. {
  30. struct Allocator* alloc;
  31. Hermes_onResponse onResponse;
  32. void* onResponseContext;
  33. struct Hermes* hermes;
  34. uint32_t handle;
  35. Identity
  36. };
  37. #define Map_NAME RequestSet
  38. #define Map_VALUE_TYPE struct Request*
  39. #define Map_ENABLE_HANDLES
  40. #include "util/Map.h"
  41. struct Hermes
  42. {
  43. struct Iface iface;
  44. struct Allocator* alloc;
  45. struct EventBase* eventBase;
  46. struct Map_RequestSet requestSet;
  47. struct Log* logger;
  48. Identity
  49. };
  50. /** Called when the request allocator is freed. */
  51. static int removeReqFromSet(struct Allocator_OnFreeJob* job)
  52. {
  53. struct Request* req = Identity_check((struct Request*) job->userData);
  54. struct Hermes* h = Identity_check(req->hermes);
  55. int index = Map_RequestSet_indexForHandle(req->handle, &h->requestSet);
  56. if (index > -1) {
  57. Map_RequestSet_remove(index, &h->requestSet);
  58. } else {
  59. Log_error(h->logger, "request with handle [%u] missing from set", req->handle);
  60. }
  61. return 0;
  62. }
  63. static void timeout(void* vrequest)
  64. {
  65. struct Request* req = Identity_check((struct Request*) vrequest);
  66. Dict resp = Dict_CONST(String_CONST("error"), String_OBJ(String_CONST("timeout")), NULL);
  67. req->onResponse(&resp, req->onResponseContext);
  68. Allocator_free(req->alloc);
  69. }
  70. static Iface_DEFUN receiveMessage(struct Message* msg, struct Iface* iface)
  71. {
  72. struct Hermes* hermes = Identity_check((struct Hermes*) iface);
  73. struct Allocator* tempAlloc = msg->alloc;
  74. #ifdef Log_KEYS
  75. char lastChr = msg->bytes[msg->length - 1];
  76. msg->bytes[msg->length - 1] = '\0';
  77. Log_keys(hermes->logger, "Got message from angel [%s%c]", msg->bytes, lastChr);
  78. msg->bytes[msg->length - 1] = lastChr;
  79. #else
  80. Log_debug(hermes->logger, "Got message from angel");
  81. #endif
  82. Dict* d = NULL;
  83. char* err = BencMessageReader_readNoExcept(msg, tempAlloc, &d);
  84. if (err) {
  85. Log_warn(hermes->logger, "Failed to parse message from angel [%s]", err);
  86. return 0;
  87. }
  88. String* txid = Dict_getString(d, String_CONST("txid"));
  89. uint32_t handle;
  90. if (!txid || txid->len != 8 || 4 != Hex_decode((uint8_t*)&handle, 4, (uint8_t*)txid->bytes, 8))
  91. {
  92. Log_warn(hermes->logger, "Message from angel; txid missing or unrecognized");
  93. return 0;
  94. }
  95. int index = Map_RequestSet_indexForHandle(handle, &hermes->requestSet);
  96. if (index < 0) {
  97. Log_warn(hermes->logger, "Message from angel references nonexistant request");
  98. return 0;
  99. }
  100. struct Request* req = Identity_check((struct Request*) hermes->requestSet.values[index]);
  101. req->onResponse(d, req->onResponseContext);
  102. Allocator_free(req->alloc);
  103. return 0;
  104. }
  105. void Hermes_callAngel(Dict* message,
  106. Hermes_onResponse onResponse,
  107. void* onResponseContext,
  108. struct Allocator* alloc,
  109. struct Except* eh,
  110. struct Hermes* hermes)
  111. {
  112. Identity_check(hermes);
  113. struct Allocator* reqAlloc = Allocator_child(alloc);
  114. struct Request* req = Allocator_clone(reqAlloc, (&(struct Request) {
  115. .alloc = reqAlloc,
  116. .onResponse = onResponse,
  117. .onResponseContext = onResponseContext,
  118. .hermes = hermes
  119. }));
  120. Identity_set(req);
  121. int index = Map_RequestSet_put(&req, &hermes->requestSet);
  122. Allocator_onFree(reqAlloc, removeReqFromSet, req);
  123. uint32_t handle = hermes->requestSet.handles[index];
  124. req->handle = handle;
  125. uint8_t handleHex[9];
  126. Hex_encode(handleHex, 9, (uint8_t*)&handle, 4);
  127. Dict_putString(message, String_CONST("txid"), String_CONST((char*)handleHex), reqAlloc);
  128. struct Message* m = Message_new(0, 1024, reqAlloc);
  129. BencMessageWriter_write(message, m, eh);
  130. // Remove the txid string so there is not a dangling pointer in the message.
  131. Dict_remove(message, String_CONST("txid"));
  132. Log_debug(hermes->logger, "Sending [%d] bytes to angel", m->length);
  133. Iface_send(&hermes->iface, m);
  134. // Use interval as defensive programming
  135. // the Allocator_free() in the timeout callback deactivates it.
  136. Timeout_setInterval(timeout, req, REQ_TIMEOUT, hermes->eventBase, reqAlloc);
  137. }
  138. struct Hermes* Hermes_new(struct Iface* angelIface,
  139. struct EventBase* eventBase,
  140. struct Log* logger,
  141. struct Allocator* alloc)
  142. {
  143. struct Hermes* out = Allocator_clone(alloc, (&(struct Hermes) {
  144. .iface = { .send = receiveMessage },
  145. .alloc = alloc,
  146. .eventBase = eventBase,
  147. .requestSet = {
  148. .allocator = alloc
  149. },
  150. .logger = logger
  151. }));
  152. Identity_set(out);
  153. Iface_plumb(angelIface, &out->iface);
  154. return out;
  155. }