|
| 1 | +#include <csp/adapters/websocket/ClientAdapterManager.h> |
| 2 | + |
| 3 | +#include <csp/core/Platform.h> |
| 4 | +#include <chrono> |
| 5 | +#include <iomanip> |
| 6 | +#include <iostream> |
| 7 | + |
| 8 | +namespace csp { |
| 9 | + |
| 10 | +INIT_CSP_ENUM( adapters::websocket::ClientStatusType, |
| 11 | + "ACTIVE", |
| 12 | + "GENERIC_ERROR", |
| 13 | + "CONNECTION_FAILED", |
| 14 | + "CLOSED", |
| 15 | + "MESSAGE_SEND_FAIL", |
| 16 | +); |
| 17 | + |
| 18 | +} |
| 19 | + |
| 20 | +// With TLS |
| 21 | +namespace csp::adapters::websocket { |
| 22 | + |
| 23 | +ClientAdapterManager::ClientAdapterManager( Engine* engine, const Dictionary & properties |
| 24 | +) : AdapterManager( engine ), |
| 25 | + m_active(false), |
| 26 | + m_shouldRun(false), |
| 27 | + m_endpoint(nullptr), |
| 28 | + m_inputAdapter(nullptr), |
| 29 | + m_outputAdapter(nullptr), |
| 30 | + m_updateAdapter(nullptr), |
| 31 | + m_thread(nullptr), |
| 32 | + m_properties(properties) |
| 33 | +{ |
| 34 | + if (m_properties.get<bool>("use_tls")) { |
| 35 | + m_endpoint = new WebsocketEndpointTLS(properties); |
| 36 | + } else { |
| 37 | + m_endpoint = new WebsocketEndpointNoTLS(properties); |
| 38 | + } |
| 39 | + |
| 40 | + if (m_inputAdapter != nullptr) |
| 41 | + { |
| 42 | + m_endpoint->setOnMessageCb([this](std::string msg) { |
| 43 | + PushBatch batch( m_engine -> rootEngine() ); |
| 44 | + m_inputAdapter->processMessage(msg, &batch); |
| 45 | + }); |
| 46 | + } |
| 47 | + m_endpoint->setOnOpenCb([this](){ |
| 48 | + m_active = true; |
| 49 | + pushStatus(StatusLevel::INFO, ClientStatusType::ACTIVE, "Connected successfully"); |
| 50 | + }); |
| 51 | + m_endpoint->setOnFailCb([this](){ |
| 52 | + m_active = false; |
| 53 | + pushStatus(StatusLevel::ERROR, ClientStatusType::CONNECTION_FAILED, "Connection failed, will try to reconnect"); |
| 54 | + }); |
| 55 | + m_endpoint->setOnCloseCb([this](){ |
| 56 | + m_active = false; |
| 57 | + pushStatus(StatusLevel::INFO, ClientStatusType::CLOSED, "Connection closed"); |
| 58 | + }); |
| 59 | + m_endpoint->setOnSendFailCb([this](const std::string& s){ |
| 60 | + std::stringstream ss; |
| 61 | + ss << "Failed to send: " << s; |
| 62 | + pushStatus(StatusLevel::ERROR, ClientStatusType::MESSAGE_SEND_FAIL, ss.str()); |
| 63 | + }); |
| 64 | + |
| 65 | +}; |
| 66 | + |
| 67 | +ClientAdapterManager::~ClientAdapterManager() |
| 68 | +{ }; |
| 69 | + |
| 70 | +void ClientAdapterManager::start( DateTime starttime, DateTime endtime ) |
| 71 | +{ |
| 72 | + AdapterManager::start( starttime, endtime ); |
| 73 | + // start the bg thread |
| 74 | + m_shouldRun = true; |
| 75 | + m_thread = std::make_unique<std::thread>( [ this ](){ |
| 76 | + while (m_shouldRun) |
| 77 | + { |
| 78 | + m_endpoint->run(); |
| 79 | + m_active=false; |
| 80 | + if(m_shouldRun) sleep( m_properties.get<TimeDelta>("reconnect_interval") ); |
| 81 | + } |
| 82 | + }); |
| 83 | +}; |
| 84 | + |
| 85 | +void ClientAdapterManager::stop() { |
| 86 | + AdapterManager::stop(); |
| 87 | + |
| 88 | + m_shouldRun=false; |
| 89 | + if(m_active) { |
| 90 | + m_endpoint->close(); |
| 91 | + } |
| 92 | + |
| 93 | + if(m_thread) { |
| 94 | + m_thread->join(); |
| 95 | + } |
| 96 | +}; |
| 97 | + |
| 98 | +PushInputAdapter* ClientAdapterManager::getInputAdapter(CspTypePtr & type, PushMode pushMode, const Dictionary & properties) |
| 99 | +{ |
| 100 | + if (m_inputAdapter == nullptr) |
| 101 | + { |
| 102 | + m_inputAdapter = m_engine->createOwnedObject<ClientInputAdapter>( |
| 103 | + // m_engine, |
| 104 | + type, |
| 105 | + pushMode, |
| 106 | + properties |
| 107 | + ); |
| 108 | + } |
| 109 | + return m_inputAdapter; |
| 110 | +}; |
| 111 | + |
| 112 | +OutputAdapter* ClientAdapterManager::getOutputAdapter() |
| 113 | +{ |
| 114 | + if (m_outputAdapter == nullptr) |
| 115 | + { |
| 116 | + m_outputAdapter = m_engine->createOwnedObject<ClientOutputAdapter>(m_endpoint); |
| 117 | + } |
| 118 | + |
| 119 | + return m_outputAdapter; |
| 120 | +} |
| 121 | + |
| 122 | +OutputAdapter * ClientAdapterManager::getHeaderUpdateAdapter() |
| 123 | +{ |
| 124 | + if (m_updateAdapter == nullptr) |
| 125 | + { |
| 126 | + m_updateAdapter = m_engine->createOwnedObject<ClientHeaderUpdateOutputAdapter>( |
| 127 | + m_endpoint->getProperties() |
| 128 | + ); |
| 129 | + } |
| 130 | + |
| 131 | + return m_updateAdapter; |
| 132 | +} |
| 133 | + |
| 134 | +DateTime ClientAdapterManager::processNextSimTimeSlice( DateTime time ) |
| 135 | +{ |
| 136 | + // no sim data |
| 137 | + return DateTime::NONE(); |
| 138 | +} |
| 139 | + |
| 140 | +} |
0 commit comments