MercuryManager.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #include "MercuryManager.h"
  2. #include <iostream>
  3. #include "Logger.h"
  4. std::map<MercuryType, std::string> MercuryTypeMap({
  5. {MercuryType::GET, "GET"},
  6. {MercuryType::SEND, "SEND"},
  7. {MercuryType::SUB, "SUB"},
  8. {MercuryType::UNSUB, "UNSUB"},
  9. });
  10. MercuryManager::MercuryManager(std::unique_ptr<Session> session): bell::Task("mercuryManager", 6 * 1024, +1, 1)
  11. {
  12. this->timeProvider = std::make_shared<TimeProvider>();
  13. this->callbacks = std::map<uint64_t, mercuryCallback>();
  14. this->subscriptions = std::map<std::string, mercuryCallback>();
  15. this->session = std::move(session);
  16. this->sequenceId = 0x00000001;
  17. this->audioChunkManager = std::make_unique<AudioChunkManager>();
  18. this->audioChunkSequence = 0;
  19. this->audioKeySequence = 0;
  20. this->queue = std::vector<std::unique_ptr<Packet>>();
  21. queueSemaphore = std::make_unique<WrappedSemaphore>(200);
  22. this->session->shanConn->conn->timeoutHandler = [this]() {
  23. return this->timeoutHandler();
  24. };
  25. }
  26. bool MercuryManager::timeoutHandler()
  27. {
  28. auto currentTimestamp = timeProvider->getSyncedTimestamp();
  29. if (this->lastRequestTimestamp != -1 && currentTimestamp - this->lastRequestTimestamp > AUDIOCHUNK_TIMEOUT_MS)
  30. {
  31. CSPOT_LOG(debug, "Reconnection required, no mercury response");
  32. return true;
  33. }
  34. if (currentTimestamp - this->lastPingTimestamp > PING_TIMEOUT_MS)
  35. {
  36. CSPOT_LOG(debug, "Reconnection required, no ping received");
  37. return true;
  38. }
  39. return false;
  40. }
  41. void MercuryManager::unregisterMercuryCallback(uint64_t seqId)
  42. {
  43. auto element = this->callbacks.find(seqId);
  44. if (element != this->callbacks.end())
  45. {
  46. this->callbacks.erase(element);
  47. }
  48. }
  49. void MercuryManager::requestAudioKey(std::vector<uint8_t> trackId, std::vector<uint8_t> fileId, audioKeyCallback& audioCallback)
  50. {
  51. std::lock_guard<std::mutex> guard(reconnectionMutex);
  52. auto buffer = fileId;
  53. this->keyCallback = audioCallback;
  54. // Structure: [FILEID] [TRACKID] [4 BYTES SEQUENCE ID] [0x00, 0x00]
  55. buffer.insert(buffer.end(), trackId.begin(), trackId.end());
  56. auto audioKeySequence = pack<uint32_t>(htonl(this->audioKeySequence));
  57. buffer.insert(buffer.end(), audioKeySequence.begin(), audioKeySequence.end());
  58. auto suffix = std::vector<uint8_t>({ 0x00, 0x00 });
  59. buffer.insert(buffer.end(), suffix.begin(), suffix.end());
  60. // Bump audio key sequence
  61. this->audioKeySequence += 1;
  62. // Used for broken connection detection
  63. this->lastRequestTimestamp = timeProvider->getSyncedTimestamp();
  64. this->session->shanConn->sendPacket(static_cast<uint8_t>(MercuryType::AUDIO_KEY_REQUEST_COMMAND), buffer);
  65. }
  66. void MercuryManager::freeAudioKeyCallback()
  67. {
  68. this->keyCallback = nullptr;
  69. }
  70. std::shared_ptr<AudioChunk> MercuryManager::fetchAudioChunk(std::vector<uint8_t> fileId, std::vector<uint8_t>& audioKey, uint16_t index)
  71. {
  72. return this->fetchAudioChunk(fileId, audioKey, index * AUDIO_CHUNK_SIZE / 4, (index + 1) * AUDIO_CHUNK_SIZE / 4);
  73. }
  74. std::shared_ptr<AudioChunk> MercuryManager::fetchAudioChunk(std::vector<uint8_t> fileId, std::vector<uint8_t>& audioKey, uint32_t startPos, uint32_t endPos)
  75. {
  76. std::lock_guard<std::mutex> guard(reconnectionMutex);
  77. auto sampleStartBytes = pack<uint32_t>(htonl(startPos));
  78. auto sampleEndBytes = pack<uint32_t>(htonl(endPos));
  79. auto buffer = pack<uint16_t>(htons(this->audioChunkSequence));
  80. auto hardcodedData = std::vector<uint8_t>(
  81. { 0x00, 0x01, // Channel num, currently just hardcoded to 1
  82. 0x00, 0x00,
  83. 0x00, 0x00, 0x00, 0x00, // bytes magic
  84. 0x00, 0x00, 0x9C, 0x40,
  85. 0x00, 0x02, 0x00, 0x00 });
  86. buffer.insert(buffer.end(), hardcodedData.begin(), hardcodedData.end());
  87. buffer.insert(buffer.end(), fileId.begin(), fileId.end());
  88. buffer.insert(buffer.end(), sampleStartBytes.begin(), sampleStartBytes.end());
  89. buffer.insert(buffer.end(), sampleEndBytes.begin(), sampleEndBytes.end());
  90. // Bump chunk sequence
  91. this->audioChunkSequence += 1;
  92. this->session->shanConn->sendPacket(static_cast<uint8_t>(MercuryType::AUDIO_CHUNK_REQUEST_COMMAND), buffer);
  93. // Used for broken connection detection
  94. this->lastRequestTimestamp = this->timeProvider->getSyncedTimestamp();
  95. return this->audioChunkManager->registerNewChunk(this->audioChunkSequence - 1, audioKey, startPos, endPos);
  96. }
  97. void MercuryManager::reconnect()
  98. {
  99. std::lock_guard<std::mutex> guard(this->reconnectionMutex);
  100. this->lastPingTimestamp = -1;
  101. this->lastRequestTimestamp = -1;
  102. RECONNECT:
  103. if (!isRunning) return;
  104. CSPOT_LOG(debug, "Trying to reconnect...");
  105. try
  106. {
  107. if (this->session->shanConn->conn != nullptr)
  108. {
  109. this->session->shanConn->conn->timeoutHandler = nullptr;
  110. }
  111. this->audioChunkManager->failAllChunks();
  112. if (this->session->authBlob != nullptr)
  113. {
  114. this->lastAuthBlob = this->session->authBlob;
  115. }
  116. this->session = std::make_unique<Session>();
  117. this->session->connectWithRandomAp();
  118. this->session->authenticate(this->lastAuthBlob);
  119. this->session->shanConn->conn->timeoutHandler = [this]() {
  120. return this->timeoutHandler();
  121. };
  122. CSPOT_LOG(debug, "Reconnected successfuly :)");
  123. }
  124. catch (...)
  125. {
  126. CSPOT_LOG(debug, "Reconnection failed, willl retry in %d secs", RECONNECTION_RETRY_MS / 1000);
  127. usleep(RECONNECTION_RETRY_MS * 1000);
  128. goto RECONNECT;
  129. //reconnect();
  130. }
  131. }
  132. void MercuryManager::runTask()
  133. {
  134. std::scoped_lock lock(this->runningMutex);
  135. // Listen for mercury replies and handle them accordingly
  136. isRunning = true;
  137. while (isRunning)
  138. {
  139. std::unique_ptr<Packet> packet;
  140. try
  141. {
  142. packet = this->session->shanConn->recvPacket();
  143. }
  144. catch (const std::runtime_error& e)
  145. {
  146. if (!isRunning) break;
  147. // Reconnection required
  148. this->reconnect();
  149. this->reconnectedCallback();
  150. continue;
  151. }
  152. if (static_cast<MercuryType>(packet->command) == MercuryType::PING) // @TODO: Handle time synchronization through ping
  153. {
  154. CSPOT_LOG(debug, "Got ping, syncing timestamp");
  155. this->timeProvider->syncWithPingPacket(packet->data);
  156. this->lastPingTimestamp = this->timeProvider->getSyncedTimestamp();
  157. this->session->shanConn->sendPacket(0x49, packet->data);
  158. }
  159. else if (static_cast<MercuryType>(packet->command) == MercuryType::AUDIO_CHUNK_SUCCESS_RESPONSE)
  160. {
  161. this->lastRequestTimestamp = -1;
  162. this->audioChunkManager->handleChunkData(packet->data, false);
  163. }
  164. else
  165. {
  166. this->queue.push_back(std::move(packet));
  167. this->queueSemaphore->give();
  168. }
  169. }
  170. }
  171. void MercuryManager::stop() {
  172. CSPOT_LOG(debug, "Stopping mercury manager");
  173. isRunning = false;
  174. audioChunkManager->close();
  175. std::scoped_lock lock(audioChunkManager->runningMutex, this->runningMutex);
  176. CSPOT_LOG(debug, "mercury stopped");
  177. }
  178. void MercuryManager::updateQueue() {
  179. if (queueSemaphore->twait() == 0) {
  180. if (this->queue.size() > 0)
  181. {
  182. auto packet = std::move(this->queue[0]);
  183. this->queue.erase(this->queue.begin());
  184. CSPOT_LOG(debug, "Received packet with code %d of length %d", packet->command, packet->data.size());
  185. switch (static_cast<MercuryType>(packet->command))
  186. {
  187. case MercuryType::COUNTRY_CODE_RESPONSE:
  188. {
  189. countryCode = std::string(packet->data.begin(), packet->data.end());
  190. CSPOT_LOG(debug, "Received country code: %s", countryCode.c_str());
  191. break;
  192. }
  193. case MercuryType::AUDIO_KEY_FAILURE_RESPONSE:
  194. case MercuryType::AUDIO_KEY_SUCCESS_RESPONSE:
  195. {
  196. this->lastRequestTimestamp = -1;
  197. // First four bytes mark the sequence id
  198. auto seqId = ntohl(extract<uint32_t>(packet->data, 0));
  199. if (seqId == (this->audioKeySequence - 1) && this->keyCallback != nullptr)
  200. {
  201. auto success = static_cast<MercuryType>(packet->command) == MercuryType::AUDIO_KEY_SUCCESS_RESPONSE;
  202. this->keyCallback(success, packet->data);
  203. }
  204. break;
  205. }
  206. case MercuryType::AUDIO_CHUNK_FAILURE_RESPONSE:
  207. {
  208. CSPOT_LOG(error, "Audio Chunk failure!");
  209. this->audioChunkManager->handleChunkData(packet->data, true);
  210. this->lastRequestTimestamp = -1;
  211. break;
  212. }
  213. case MercuryType::SEND:
  214. case MercuryType::SUB:
  215. case MercuryType::UNSUB:
  216. {
  217. auto response = std::make_unique<MercuryResponse>(packet->data);
  218. if (response->parts.size() > 0)
  219. {
  220. CSPOT_LOG(debug, " MercuryType::UNSUB response->parts[0].size() = %d", response->parts[0].size());
  221. }
  222. if (this->callbacks.count(response->sequenceId) > 0)
  223. {
  224. auto seqId = response->sequenceId;
  225. this->callbacks[response->sequenceId](std::move(response));
  226. this->callbacks.erase(this->callbacks.find(seqId));
  227. }
  228. break;
  229. }
  230. case MercuryType::SUBRES:
  231. {
  232. auto response = std::make_unique<MercuryResponse>(packet->data);
  233. if (this->subscriptions.count(response->mercuryHeader.uri.value()) > 0)
  234. {
  235. this->subscriptions[response->mercuryHeader.uri.value()](std::move(response));
  236. //this->subscriptions.erase(std::string(response->mercuryHeader.uri));
  237. }
  238. break;
  239. }
  240. default:
  241. break;
  242. }
  243. }
  244. }
  245. }
  246. void MercuryManager::handleQueue()
  247. {
  248. while (isRunning)
  249. {
  250. this->updateQueue();
  251. }
  252. }
  253. uint64_t MercuryManager::execute(MercuryType method, std::string uri, mercuryCallback& callback, mercuryCallback& subscription, mercuryParts& payload)
  254. {
  255. if (!isRunning) return -1;
  256. std::lock_guard<std::mutex> guard(reconnectionMutex);
  257. // Construct mercury header
  258. CSPOT_LOG(debug, "executing MercuryType %s", MercuryTypeMap[method].c_str());
  259. Header mercuryHeader;
  260. mercuryHeader.uri = uri;
  261. mercuryHeader.method = MercuryTypeMap[method];
  262. // GET and SEND are actually the same. Therefore the override
  263. // The difference between them is only in header's method
  264. if (method == MercuryType::GET)
  265. {
  266. method = MercuryType::SEND;
  267. }
  268. auto headerBytes = encodePb(mercuryHeader);
  269. // Register a subscription when given method is called
  270. if (method == MercuryType::SUB)
  271. {
  272. this->subscriptions.insert({ uri, subscription });
  273. }
  274. this->callbacks.insert({ sequenceId, callback });
  275. // Structure: [Sequence size] [SequenceId] [0x1] [Payloads number]
  276. // [Header size] [Header] [Payloads (size + data)]
  277. // Pack sequenceId
  278. auto sequenceIdBytes = pack<uint64_t>(hton64(this->sequenceId));
  279. auto sequenceSizeBytes = pack<uint16_t>(htons(sequenceIdBytes.size()));
  280. sequenceIdBytes.insert(sequenceIdBytes.begin(), sequenceSizeBytes.begin(), sequenceSizeBytes.end());
  281. sequenceIdBytes.push_back(0x01);
  282. auto payloadNum = pack<uint16_t>(htons(payload.size() + 1));
  283. sequenceIdBytes.insert(sequenceIdBytes.end(), payloadNum.begin(), payloadNum.end());
  284. auto headerSizePayload = pack<uint16_t>(htons(headerBytes.size()));
  285. sequenceIdBytes.insert(sequenceIdBytes.end(), headerSizePayload.begin(), headerSizePayload.end());
  286. sequenceIdBytes.insert(sequenceIdBytes.end(), headerBytes.begin(), headerBytes.end());
  287. // Encode all the payload parts
  288. for (int x = 0; x < payload.size(); x++)
  289. {
  290. headerSizePayload = pack<uint16_t>(htons(payload[x].size()));
  291. sequenceIdBytes.insert(sequenceIdBytes.end(), headerSizePayload.begin(), headerSizePayload.end());
  292. sequenceIdBytes.insert(sequenceIdBytes.end(), payload[x].begin(), payload[x].end());
  293. }
  294. // Bump sequence id
  295. this->sequenceId += 1;
  296. this->session->shanConn->sendPacket(static_cast<std::underlying_type<MercuryType>::type>(method), sequenceIdBytes);
  297. return this->sequenceId - 1;
  298. }
  299. uint64_t MercuryManager::execute(MercuryType method, std::string uri, mercuryCallback& callback, mercuryParts& payload)
  300. {
  301. mercuryCallback subscription = nullptr;
  302. return this->execute(method, uri, callback, subscription, payload);
  303. }
  304. uint64_t MercuryManager::execute(MercuryType method, std::string uri, mercuryCallback& callback, mercuryCallback& subscription)
  305. {
  306. auto payload = mercuryParts(0);
  307. return this->execute(method, uri, callback, subscription, payload);
  308. }
  309. uint64_t MercuryManager::execute(MercuryType method, std::string uri, mercuryCallback& callback)
  310. {
  311. auto payload = mercuryParts(0);
  312. return this->execute(method, uri, callback, payload);
  313. }