|
1 | 1 | #include <csp/adapters/websocket/ClientAdapterManager.h> |
2 | 2 |
|
3 | | -namespace csp { |
4 | | - |
5 | | -INIT_CSP_ENUM( adapters::websocket::ClientStatusType, |
6 | | - "ACTIVE", |
7 | | - "GENERIC_ERROR", |
8 | | - "CONNECTION_FAILED", |
9 | | - "CLOSED", |
10 | | - "MESSAGE_SEND_FAIL", |
11 | | -); |
12 | | - |
13 | | -} |
14 | | - |
15 | | -// With TLS |
16 | 3 | namespace csp::adapters::websocket { |
17 | 4 |
|
18 | 5 | ClientAdapterManager::ClientAdapterManager( Engine* engine, const Dictionary & properties ) |
19 | 6 | : AdapterManager( engine ), |
20 | | - m_active( false ), |
21 | | - m_shouldRun( false ), |
22 | | - m_endpoint( std::make_unique<WebsocketEndpoint>( properties ) ), |
23 | | - m_inputAdapter( nullptr ), |
24 | | - m_outputAdapter( nullptr ), |
25 | | - m_updateAdapter( nullptr ), |
26 | | - m_thread( nullptr ), |
27 | | - m_properties( properties ) |
28 | | -{ }; |
| 7 | + m_properties( properties ) |
| 8 | +{ } |
29 | 9 |
|
30 | 10 | ClientAdapterManager::~ClientAdapterManager() |
31 | | -{ }; |
| 11 | +{ } |
32 | 12 |
|
33 | | -void ClientAdapterManager::start( DateTime starttime, DateTime endtime ) |
34 | | -{ |
35 | | - AdapterManager::start( starttime, endtime ); |
36 | | - |
37 | | - m_shouldRun = true; |
38 | | - m_endpoint -> setOnOpen( |
39 | | - [ this ]() { |
40 | | - m_active = true; |
41 | | - pushStatus( StatusLevel::INFO, ClientStatusType::ACTIVE, "Connected successfully" ); |
42 | | - } |
43 | | - ); |
44 | | - m_endpoint -> setOnFail( |
45 | | - [ this ]( const std::string& reason ) { |
46 | | - std::stringstream ss; |
47 | | - ss << "Connection Failure: " << reason; |
48 | | - m_active = false; |
49 | | - pushStatus( StatusLevel::ERROR, ClientStatusType::CONNECTION_FAILED, ss.str() ); |
50 | | - } |
51 | | - ); |
52 | | - if( m_inputAdapter ) { |
53 | | - m_endpoint -> setOnMessage( |
54 | | - [ this ]( void* c, size_t t ) { |
55 | | - PushBatch batch( m_engine -> rootEngine() ); |
56 | | - m_inputAdapter -> processMessage( c, t, &batch ); |
57 | | - } |
58 | | - ); |
59 | | - } else { |
60 | | - // if a user doesn't call WebsocketAdapterManager.subscribe, no inputadapter will be created |
61 | | - // but we still need something to avoid on_message_cb not being set in the endpoint. |
62 | | - m_endpoint -> setOnMessage( []( void* c, size_t t ){} ); |
63 | | - } |
64 | | - m_endpoint -> setOnClose( |
65 | | - [ this ]() { |
66 | | - m_active = false; |
67 | | - pushStatus( StatusLevel::INFO, ClientStatusType::CLOSED, "Connection closed" ); |
68 | | - } |
69 | | - ); |
70 | | - m_endpoint -> setOnSendFail( |
71 | | - [ this ]( const std::string& s ) { |
72 | | - std::stringstream ss; |
73 | | - ss << "Failed to send: " << s; |
74 | | - pushStatus( StatusLevel::ERROR, ClientStatusType::MESSAGE_SEND_FAIL, ss.str() ); |
75 | | - } |
76 | | - ); |
| 13 | +WebsocketEndpointManager* ClientAdapterManager::getWebsocketManager(){ |
| 14 | + if( m_endpointManager == nullptr ) |
| 15 | + return nullptr; |
| 16 | + return m_endpointManager.get(); |
| 17 | +} |
77 | 18 |
|
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 | | -}; |
| 19 | +void ClientAdapterManager::start(DateTime starttime, DateTime endtime) { |
| 20 | + AdapterManager::start(starttime, endtime); |
| 21 | + if (m_endpointManager != nullptr) |
| 22 | + m_endpointManager -> start(starttime, endtime); |
| 23 | +} |
87 | 24 |
|
88 | 25 | void ClientAdapterManager::stop() { |
89 | 26 | AdapterManager::stop(); |
90 | | - |
91 | | - m_shouldRun=false; |
92 | | - if( m_active ) m_endpoint->stop(); |
93 | | - if( m_thread ) m_thread->join(); |
94 | | -}; |
| 27 | + if (m_endpointManager != nullptr) |
| 28 | + m_endpointManager -> stop(); |
| 29 | +} |
95 | 30 |
|
96 | 31 | 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 | | -}; |
| 32 | +{ |
| 33 | + if (m_endpointManager == nullptr) |
| 34 | + m_endpointManager = std::make_unique<WebsocketEndpointManager>(this, m_properties, m_engine); |
| 35 | + return m_endpointManager -> getInputAdapter( type, pushMode, properties ); |
| 36 | +} |
109 | 37 |
|
110 | | -OutputAdapter* ClientAdapterManager::getOutputAdapter() |
| 38 | +OutputAdapter* ClientAdapterManager::getOutputAdapter( const Dictionary & properties ) |
111 | 39 | { |
112 | | - if (m_outputAdapter == nullptr) m_outputAdapter = m_engine -> createOwnedObject<ClientOutputAdapter>(*m_endpoint); |
113 | | - |
114 | | - return m_outputAdapter; |
| 40 | + if (m_endpointManager == nullptr) |
| 41 | + m_endpointManager = std::make_unique<WebsocketEndpointManager>(this, m_properties, m_engine); |
| 42 | + return m_endpointManager -> getOutputAdapter( properties ); |
115 | 43 | } |
116 | 44 |
|
117 | 45 | OutputAdapter * ClientAdapterManager::getHeaderUpdateAdapter() |
118 | 46 | { |
119 | | - if (m_updateAdapter == nullptr) m_updateAdapter = m_engine -> createOwnedObject<ClientHeaderUpdateOutputAdapter>( m_endpoint -> getProperties() ); |
| 47 | + if (m_endpointManager == nullptr) |
| 48 | + m_endpointManager = std::make_unique<WebsocketEndpointManager>(this, m_properties, m_engine); |
| 49 | + return m_endpointManager -> getHeaderUpdateAdapter(); |
| 50 | +} |
120 | 51 |
|
121 | | - return m_updateAdapter; |
| 52 | +OutputAdapter * ClientAdapterManager::getConnectionRequestAdapter( const Dictionary & properties ) |
| 53 | +{ |
| 54 | + if (m_endpointManager == nullptr) |
| 55 | + m_endpointManager = std::make_unique<WebsocketEndpointManager>(this, m_properties, m_engine); |
| 56 | + return m_endpointManager -> getConnectionRequestAdapter( properties ); |
122 | 57 | } |
123 | 58 |
|
124 | 59 | DateTime ClientAdapterManager::processNextSimTimeSlice( DateTime time ) |
|
0 commit comments