@@ -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
3435ClientAdapterManager::~ClientAdapterManager ()
3536{ };
3637
3738void 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
52101void ClientAdapterManager::send (const std::string& value, const size_t & caller_id) {
@@ -179,7 +228,6 @@ void ClientAdapterManager::shutdownEndpoint(const std::string& endpoint_id) {
179228
180229void 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
441489void 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
459508PushInputAdapter* 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
481532OutputAdapter* 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
493549OutputAdapter * ClientAdapterManager::getHeaderUpdateAdapter ()
0 commit comments