|
| 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 | + { |
| 36 | + m_endpoint = new WebsocketEndpointTLS( properties ); |
| 37 | + } |
| 38 | + else |
| 39 | + { |
| 40 | + m_endpoint = new WebsocketEndpointNoTLS( properties ); |
| 41 | + } |
| 42 | + |
| 43 | + if( m_inputAdapter != nullptr ) |
| 44 | + { |
| 45 | + m_endpoint -> setOnMessageCb( [ this ]( std::string msg ) { |
| 46 | + PushBatch batch( m_engine -> rootEngine() ); |
| 47 | + m_inputAdapter -> processMessage( msg, &batch ); |
| 48 | + }); |
| 49 | + } |
| 50 | + m_endpoint -> setOnOpenCb( [ this ]() { |
| 51 | + m_active = true; |
| 52 | + pushStatus( StatusLevel::INFO, ClientStatusType::ACTIVE, "Connected successfully" ); |
| 53 | + }); |
| 54 | + m_endpoint -> setOnFailCb( [ this ]() { |
| 55 | + m_active = false; |
| 56 | + pushStatus( StatusLevel::ERROR, ClientStatusType::CONNECTION_FAILED, "Connection failed, will try to reconnect" ); |
| 57 | + }); |
| 58 | + m_endpoint -> setOnCloseCb( [ this ]() { |
| 59 | + m_active = false; |
| 60 | + pushStatus( StatusLevel::INFO, ClientStatusType::CLOSED, "Connection closed" ); |
| 61 | + }); |
| 62 | + m_endpoint -> setOnSendFailCb( [ this ]( const std::string& s ) { |
| 63 | + std::stringstream ss; |
| 64 | + ss << "Failed to send: " << s; |
| 65 | + pushStatus( StatusLevel::ERROR, ClientStatusType::MESSAGE_SEND_FAIL, ss.str() ); |
| 66 | + }); |
| 67 | + |
| 68 | +}; |
| 69 | + |
| 70 | +ClientAdapterManager::~ClientAdapterManager() |
| 71 | +{ }; |
| 72 | + |
| 73 | +void ClientAdapterManager::start( DateTime starttime, DateTime endtime ) |
| 74 | +{ |
| 75 | + AdapterManager::start( starttime, endtime ); |
| 76 | + // start the bg thread |
| 77 | + m_shouldRun = true; |
| 78 | + m_thread = std::make_unique<std::thread>( [ this ]() { |
| 79 | + while( m_shouldRun ) |
| 80 | + { |
| 81 | + m_endpoint -> run(); |
| 82 | + m_active = false; |
| 83 | + if( m_shouldRun ) sleep( m_properties.get<TimeDelta>( "reconnect_interval" ) ); |
| 84 | + } |
| 85 | + }); |
| 86 | +}; |
| 87 | + |
| 88 | +void ClientAdapterManager::stop() { |
| 89 | + AdapterManager::stop(); |
| 90 | + |
| 91 | + m_shouldRun=false; |
| 92 | + if( m_active ) m_endpoint->close(); |
| 93 | + if( m_thread ) m_thread->join(); |
| 94 | +}; |
| 95 | + |
| 96 | +PushInputAdapter* ClientAdapterManager::getInputAdapter(CspTypePtr & type, PushMode pushMode, const Dictionary & properties) |
| 97 | +{ |
| 98 | + if (m_inputAdapter == nullptr) |
| 99 | + { |
| 100 | + m_inputAdapter = m_engine -> createOwnedObject<ClientInputAdapter>( |
| 101 | + // m_engine, |
| 102 | + type, |
| 103 | + pushMode, |
| 104 | + properties |
| 105 | + ); |
| 106 | + } |
| 107 | + return m_inputAdapter; |
| 108 | +}; |
| 109 | + |
| 110 | +OutputAdapter* ClientAdapterManager::getOutputAdapter() |
| 111 | +{ |
| 112 | + if (m_outputAdapter == nullptr) m_outputAdapter = m_engine -> createOwnedObject<ClientOutputAdapter>(m_endpoint); |
| 113 | + |
| 114 | + return m_outputAdapter; |
| 115 | +} |
| 116 | + |
| 117 | +OutputAdapter * ClientAdapterManager::getHeaderUpdateAdapter() |
| 118 | +{ |
| 119 | + if (m_updateAdapter == nullptr) m_updateAdapter = m_engine -> createOwnedObject<ClientHeaderUpdateOutputAdapter>( m_endpoint -> getProperties() ); |
| 120 | + |
| 121 | + return m_updateAdapter; |
| 122 | +} |
| 123 | + |
| 124 | +DateTime ClientAdapterManager::processNextSimTimeSlice( DateTime time ) |
| 125 | +{ |
| 126 | + // no sim data |
| 127 | + return DateTime::NONE(); |
| 128 | +} |
| 129 | + |
| 130 | +} |
0 commit comments