Skip to content

Commit 9b82506

Browse files
Nijat KNijat K
authored andcommitted
Make backward-compatible with dynamic=False for websockets
Signed-off-by: Nijat K <neej@Nijats-MBP.fios-router.home>
1 parent d91616a commit 9b82506

11 files changed

Lines changed: 144 additions & 188 deletions

cpp/csp/adapters/websocket/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ set(WS_CLIENT_HEADER_FILES
77
ClientOutputAdapter.h
88
ClientHeaderUpdateAdapter.h
99
WebsocketEndpoint.h
10-
# WebsocketConnectionManager.h
1110
${WEBSOCKET_HEADER}
1211
)
1312

@@ -18,7 +17,6 @@ set(WS_CLIENT_SOURCE_FILES
1817
ClientOutputAdapter.cpp
1918
ClientHeaderUpdateAdapter.cpp
2019
WebsocketEndpoint.cpp
21-
# WebsocketConnectionManager.cpp
2220
${WS_CLIENT_HEADER_FILES}
2321
${WEBSOCKET_SOURCE}
2422
)

cpp/csp/adapters/websocket/ClientAdapterManager.cpp

Lines changed: 105 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,74 @@ ClientAdapterManager::ClientAdapterManager( Engine* engine, const Dictionary & p
2828
m_updateAdapter( nullptr ),
2929
m_thread( nullptr ),
3030
m_properties( properties ),
31-
m_work_guard(boost::asio::make_work_guard(m_ioc))
31+
m_work_guard(boost::asio::make_work_guard(m_ioc)),
32+
m_dynamic( properties.get<bool>("dynamic") )
3233
{ };
3334

3435
ClientAdapterManager::~ClientAdapterManager()
3536
{ };
3637

3738
void ClientAdapterManager::start(DateTime starttime, DateTime endtime) {
38-
AdapterManager::start(starttime, endtime);
39-
// m_shouldRun = true;
40-
std::cout << "WE START" << "\n";
41-
// Just run the io_context in the thread
39+
AdapterManager::start(starttime, endtime);
40+
if( m_dynamic ){
4241
m_thread = std::make_unique<std::thread>([this]() {
4342
m_ioc.run();
4443
});
45-
// this is where we do the updates and manage the endpoints
46-
// We should consider all the different possibilities
47-
// Also, if dynamic, we need the input adapter to return
48-
// A wrapped object, or something to signify which
49-
// endpoint a response is from.
44+
}
45+
else {
46+
m_shouldRun = true;
47+
m_endpoint -> setOnOpen(
48+
[ this ]() {
49+
m_active = true;
50+
pushStatus( StatusLevel::INFO, ClientStatusType::ACTIVE, "Connected successfully" );
51+
}
52+
);
53+
m_endpoint -> setOnFail(
54+
[ this ]( const std::string& reason ) {
55+
std::stringstream ss;
56+
ss << "Connection Failure: " << reason;
57+
m_active = false;
58+
pushStatus( StatusLevel::ERROR, ClientStatusType::CONNECTION_FAILED, ss.str() );
59+
}
60+
);
61+
if( m_inputAdapter ) {
62+
m_endpoint -> setOnMessage(
63+
[ this ]( void* c, size_t t ) {
64+
std::cout << "YUR";
65+
PushBatch batch( m_engine -> rootEngine() );
66+
m_inputAdapter -> processMessage( c, t, &batch );
67+
}
68+
);
69+
} else {
70+
// if a user doesn't call WebsocketAdapterManager.subscribe, no inputadapter will be created
71+
// but we still need something to avoid on_message_cb not being set in the endpoint.
72+
m_endpoint -> setOnMessage( []( void* c, size_t t ){} );
73+
}
74+
m_endpoint -> setOnClose(
75+
[ this ]() {
76+
m_active = false;
77+
pushStatus( StatusLevel::INFO, ClientStatusType::CLOSED, "Connection closed" );
78+
}
79+
);
80+
m_endpoint -> setOnSendFail(
81+
[ this ]( const std::string& s ) {
82+
std::stringstream ss;
83+
ss << "Failed to send: " << s;
84+
pushStatus( StatusLevel::ERROR, ClientStatusType::MESSAGE_SEND_FAIL, ss.str() );
85+
}
86+
);
87+
88+
m_thread = std::make_unique<std::thread>( [ this ]() {
89+
while( m_shouldRun )
90+
{
91+
std::cout << "WE ARE RUNNING\n";
92+
m_endpoint -> run();
93+
std::cout << "WE ARE NOT RUNNING\n";
94+
m_active = false;
95+
if( m_shouldRun ) sleep( m_properties.get<TimeDelta>( "reconnect_interval" ) );
96+
}
97+
});
98+
}
5099
};
51100

52101
void ClientAdapterManager::send(const std::string& value, const size_t& caller_id) {
@@ -179,7 +228,6 @@ void ClientAdapterManager::shutdownEndpoint(const std::string& endpoint_id) {
179228

180229
void ClientAdapterManager::setupEndpoint(const std::string& endpoint_id,
181230
std::unique_ptr<WebsocketEndpoint>& endpoint) {
182-
std::cout << "WE ARE SETTING UPA NEW EDNPOINT HERE " << "\n";
183231
boost::asio::post(m_ioc, [this, endpoint_id, ep = std::move(endpoint)]() mutable {
184232
ep->setOnOpen([this, endpoint_id]() {
185233
auto [iter, inserted] = m_endpoint_configs.try_emplace(endpoint_id, m_ioc);
@@ -440,54 +488,62 @@ void ClientAdapterManager::removeProducer(const std::string& endpoint_id, size_t
440488

441489
void ClientAdapterManager::stop() {
442490
AdapterManager::stop();
443-
444-
// m_shouldRun=false;
445-
446-
// Stop the work guard to allow the io_context to complete
447-
m_work_guard.reset();
448-
449-
// Stop all endpoints
450-
for (auto& [endpoint_id, _] : m_endpoints) {
451-
shutdownEndpoint(endpoint_id);
452-
// endpoint->stop();
491+
if( m_dynamic ){
492+
// Stop the work guard to allow the io_context to complete
493+
m_work_guard.reset();
494+
495+
// Stop all endpoints
496+
for (auto& [endpoint_id, _] : m_endpoints) {
497+
shutdownEndpoint(endpoint_id);
498+
// endpoint->stop();
499+
}
500+
}
501+
else{
502+
m_shouldRun=false;
503+
if( m_active ) m_endpoint->stop();
453504
}
454-
455-
// if( m_active ) m_endpoint->stop();
456505
if( m_thread ) m_thread->join();
457506
};
458507

459508
PushInputAdapter* ClientAdapterManager::getInputAdapter(CspTypePtr & type, PushMode pushMode, const Dictionary & properties)
460509
{
461-
auto input_adapter = m_engine -> createOwnedObject<ClientInputAdapter>(
462-
// m_engine,
463-
type,
464-
pushMode,
465-
properties
466-
);
467-
assert(properties.get<bool>("is_subscribe"));
468-
m_inputAdapters.push_back(input_adapter);
469-
// if (m_inputAdapter == nullptr)
470-
// {
471-
// m_inputAdapter = m_engine -> createOwnedObject<ClientInputAdapter>(
472-
// // m_engine,
473-
// type,
474-
// pushMode,
475-
// properties
476-
// );
477-
// }
478-
return input_adapter;
510+
if ( m_dynamic ){
511+
auto input_adapter = m_engine -> createOwnedObject<ClientInputAdapter>(
512+
// m_engine,
513+
type,
514+
pushMode,
515+
properties
516+
);
517+
m_inputAdapters.push_back(input_adapter);
518+
return input_adapter;
519+
}
520+
if (m_inputAdapter == nullptr)
521+
{
522+
m_inputAdapter = m_engine -> createOwnedObject<ClientInputAdapter>(
523+
// m_engine,
524+
type,
525+
pushMode,
526+
properties
527+
);
528+
}
529+
return m_inputAdapter;
479530
};
480531

481532
OutputAdapter* ClientAdapterManager::getOutputAdapter( const Dictionary & properties )
482533
{
483-
auto caller_id = properties.get<int64_t>("caller_id");
484-
size_t validated_id = validateCallerId(caller_id);
485-
assert(!properties.get<bool>("is_subscribe"));
486-
assert(m_outputAdapters.size() == validated_id);
487-
488-
auto output_adapter = m_engine -> createOwnedObject<ClientOutputAdapter>(*m_endpoint, this, validated_id, m_ioc);
489-
m_outputAdapters.push_back(output_adapter);
490-
return output_adapter;
534+
if ( m_dynamic ){
535+
auto caller_id = properties.get<int64_t>("caller_id");
536+
size_t validated_id = validateCallerId(caller_id);
537+
assert(!properties.get<bool>("is_subscribe"));
538+
assert(m_outputAdapters.size() == validated_id);
539+
540+
auto output_adapter = m_engine -> createOwnedObject<ClientOutputAdapter>( *m_endpoint, this, validated_id, m_ioc, m_dynamic );
541+
m_outputAdapters.push_back(output_adapter);
542+
return output_adapter;
543+
}
544+
// validated_id does not matter here
545+
if (m_outputAdapter == nullptr) m_outputAdapter = m_engine -> createOwnedObject<ClientOutputAdapter>( *m_endpoint, this, 0, m_ioc, m_dynamic );
546+
return m_outputAdapter;
491547
}
492548

493549
OutputAdapter * ClientAdapterManager::getHeaderUpdateAdapter()

cpp/csp/adapters/websocket/ClientAdapterManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ class CSP_PUBLIC ClientAdapterManager final : public AdapterManager
159159
std::unordered_map<std::string, EndpointConfig> m_endpoint_configs;
160160
std::vector<ClientInputAdapter*> m_inputAdapters;
161161
std::vector<ClientOutputAdapter*> m_outputAdapters;
162+
bool m_dynamic;
162163
};
163164

164165
}

cpp/csp/adapters/websocket/ClientInputAdapter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ClientInputAdapter final: public PushInputAdapter {
2121

2222
// void processMessage( void* c, size_t t, PushBatch* batch );
2323
void processMessage( void* c, size_t t, PushBatch* batch );
24-
void processMessage(std::tuple<std::string, void*> data, size_t t, PushBatch* batch);
24+
void processMessage( std::tuple<std::string, void*> data, size_t t, PushBatch* batch );
2525

2626
private:
2727
adapters::utils::MessageStructConverterPtr m_converter;

cpp/csp/adapters/websocket/ClientOutputAdapter.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,30 @@ ClientOutputAdapter::ClientOutputAdapter(
77
WebsocketEndpoint& endpoint,
88
ClientAdapterManager * clientAdapterManager,
99
size_t caller_id,
10-
net::io_context& ioc
10+
net::io_context& ioc,
11+
bool dynamic
1112
) : OutputAdapter( engine ),
1213
m_endpoint( endpoint ),
1314
m_clientAdapterManager( clientAdapterManager ),
1415
m_callerId( caller_id ),
15-
m_ioc( ioc )
16+
m_ioc( ioc ),
17+
m_dynamic( dynamic )
1618
{ };
1719

1820
void ClientOutputAdapter::executeImpl()
1921
{
2022
// TODO Add here picking the right endpoints to send to
2123
// Based on the caller id
2224
const std::string & value = input() -> lastValueTyped<std::string>();
23-
boost::asio::post(m_ioc, [this, value=value]() {
24-
// something something lifetime? Not sure
25-
m_clientAdapterManager->send(value, m_callerId);
26-
});
27-
// m_endpoint.send( value );
25+
if( m_dynamic ){
26+
boost::asio::post(m_ioc, [this, value=value]() {
27+
// something something lifetime? Not sure
28+
m_clientAdapterManager->send(value, m_callerId);
29+
});
30+
}
31+
else{
32+
m_endpoint.send( value );
33+
}
2834
};
2935

3036
}

cpp/csp/adapters/websocket/ClientOutputAdapter.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class ClientOutputAdapter final: public OutputAdapter
2121
WebsocketEndpoint& endpoint,
2222
ClientAdapterManager * clientAdapterManager,
2323
size_t caller_id,
24-
net::io_context& ioc
24+
net::io_context& ioc,
25+
bool dynamic
2526
);
2627

2728
void executeImpl() override;
@@ -33,6 +34,7 @@ class ClientOutputAdapter final: public OutputAdapter
3334
ClientAdapterManager* m_clientAdapterManager;
3435
size_t m_callerId;
3536
net::io_context& m_ioc;
37+
bool m_dynamic;
3638
// std::unordered_map<std::string, std::vector<bool>>& m_endpoint_consumers;
3739
};
3840

cpp/csp/adapters/websocket/WebsocketConnectionManager.cpp

Lines changed: 0 additions & 85 deletions
This file was deleted.

0 commit comments

Comments
 (0)