Alexa SDK庖丁解牛-第一回:主程序结构

第一回:init()剖析
main()主程序分为以下两个部分:
 1)auto sampleApplication =
alexaClientSDK::sampleApp::SampleApplication:: create(pathToConfig, pathToInputFolder, logLevel);
 2)sampleApplication-> run();
本章只介绍相关的初始化工作,主要在以下部分:
std::unique_ptr<SampleApplication> SampleApplication::create(
 auto clientApplication = std::unique_ptr<SampleApplication>(new SampleApplication);
 if (!clientApplication-> initialize(pathToConfig, pathToInputFolder, logLevel)) {
  ConsolePrinter::simplePrint("Failed to initialize SampleApplication");
   return nullptr;
 }
}
接着来看bool SampleApplication:: initialize()究竟干了什么东东呢!!
1)Set up the SDK logging system to write to the SampleApp's ConsolePrinter,log设置
2)MediaPlayer, audio streams, DefaultClient的准备工作
 /* curl version,https version,ConfigurationNode init,curl_global_init,*/
 bool AlexaClientSDKInit::initialize(const std::vector<std::istream*>& jsonStreams);

 /* SpeakMediaPlayer,AudioMediaPlayer,AlertsMediaPlayer 关于gstreamer的一堆初始化*/
 m_speakMediaPlayer = alexaClientSDK::mediaPlayer::MediaPlayer::create(); //AVS_SYNCED
 m_audioMediaPlayer= alexaClientSDK::mediaPlayer::MediaPlayer::create(); //AVS_SYNCED
 m_alertsMediaPlayer= alexaClientSDK::mediaPlayer::MediaPlayer::create(); //LOCAL

 auto audioFactory = std::make_shared<alexaClientSDK::applicationUtilities::resources::audio::AudioFactory>();

 // Creating the alert storage object to be used for rendering and storing alerts.
 auto alertStorage =
   std::make_shared<alexaClientSDK::capabilityAgents::alerts::storage::SQLiteAlertStorage>(audioFactory->alerts());

 /* Creating settings storage object to be used for storing <key, value> pairs of AVS Settings. */
 auto settingsStorage = std::make_shared<alexaClientSDK::capabilityAgents::settings::SQLiteSettingStorage>();

 /* Creating the UI component that observes various components and prints to the console accordingly */
 auto userInterfaceManager = std::make_shared<alexaClientSDK::sampleApp::UIManager>();

 /* Setting up a connection observer to wait for connection and authorization prior to accepting user input at startup */
 auto connectionObserver= std::make_shared<alexaClientSDK::sampleApp::ConnectionObserver>();

 /* Creating the AuthDelegate - this component takes care of LWA and authorization of the client. At the moment, this must be done and authorization must be achieved prior to making the call to connect().
这里对AlexaClientSDKConfig.json中的参数进行解析:clientId,clientSecret,refreshTotoken,AuthDelegate 类创建的初始化参数为avsCommon::utils::libcurlUtils::HttpPost::create()
1)首先判断key值是否为空,为空则退出;
2)key均不为空则创建thread,bool AuthDelegate::init(),线程函数refreshAndNotifyThreadFunction(),refreshAuthToken()
*/
 std::shared_ptr<alexaClientSDK::authDelegate::AuthDelegate> authDelegate =
   alexaClientSDK::authDelegate::AuthDelegate::create();

 authDelegate->addAuthObserver( connectionObserver);

 // 重点来了Creating the DefaultClient,这里面做很多事情,暂时只关注创建了那些对象
 std::shared_ptr<alexaClientSDK::defaultClient::DefaultClient> client =
   alexaClientSDK::defaultClient::DefaultClient::create()
 1)m_dialogUXStateAggregator观察者设置,alexa使用了观察者模式设计
   m_dialogUXStateAggregator = std::make_shared<avsCommon::avs::DialogUXStateAggregator>();

   for (auto observer : alexaDialogStateObservers) {
     m_dialogUXStateAggregator->addObserver(observer);
   }
 2) /*Creating the message router - This component actually maintains the connection to AVS over HTTP2,使用delegate作为  输入参数来创建,提供了连接到AVS的认证,协助ACL接收AVSattachments,此对象是连接亚马逊云服务的实例
  m_messageRouter = std::make_shared<acl::HTTP2MessageRouter>(authDelegate, attachmentManager);
 3) Creating the connection manager连接各个网络模块,在refreshtoken更新完成后等待连接成功状态,此处只负责创建没有去连接  云端服务,因为flag为false
  m_connectionManager=
     acl::AVSConnectionManager::create( m_messageRouter, false, connectionObservers, {m_dialogUXStateAggregator});
 4) Creating our certified sender:管理发给AVS的json格式event消息
   auto messageStorage = std::make_shared<certifiedSender::SQLiteMessageStorage>();
   m_certifiedSender =
     certifiedSender::CertifiedSender::create(m_connectionManager, m_connectionManager, messageStorage);
 5) Creating the Exception Sender:负责处理execption消息,即无法处理的AVS指令事件,the Directive Sequencer and each      Capability Agent需要这个模块
   std::shared_ptr<avsCommon::avs::ExceptionEncounteredSender> exceptionSender =
   avsCommon::avs::ExceptionEncounteredSender::create(m_connectionManager);
 6) Creating the Directive Sequencer:负责把AVS指令序列化管理,根据namespace推到对应的组件模块
   m_directiveSequencer = adsl::DirectiveSequencer::create(exceptionSender);
 7) Creating the Message Interpreter:负责ACL消息转换为指令格式,负责连接ACL(Alexa Communications Library)与   ADSL(Alexa Directive Sequencer Library)
   auto messageInterpreter =
     std::make_shared<adsl::MessageInterpreter>(exceptionSender, m_directiveSequencer, attachmentManager);
   m_connectionManager->addMessageObserver(messageInterpreter);
 8) Creating the Context Manager:状态管理
   auto contextManager = contextManager::ContextManager::create();
   acl::PostConnectObject::init(contextManager);
 9) Creating the User Inactivity Monitor:闲置状态事件监视器
   auto userInactivityMonitor =
     capabilityAgents::system::UserInactivityMonitor::create(m_connectionManager, exceptionSender);
 10)Creating the Audio Input Processor: SpeechRecognizer interface
   m_audioInputProcessor = capabilityAgents::aip::AudioInputProcessor::create();
   m_audioInputProcessor->addObserver(m_dialogUXStateAggregator);

 11)Creating the Speech Synthesizer: SpeechSynthesizerinterface
   m_speechSynthesizer = capabilityAgents::speechSynthesizer::SpeechSynthesizer::create();
   m_speechSynthesizer->addObserver(m_dialogUXStateAggregator);

 12)Creating the Audio Player: AudioPlayerinterface
   m_audioPlayer = capabilityAgents::audioPlayer::AudioPlayer::create()
 13)Creating the Alerts Capability Agent
   m_alertsCapabilityAgent = capabilityAgents::alerts::AlertsCapabilityAgent::create()
   addConnectionObserver(m_alertsCapabilityAgent);
   addConnectionObserver(m_dialogUXStateAggregator);

 14)Creating the PlaybackController Capability Agent
   m_playbackController =
     capabilityAgents::playbackController::PlaybackController::create(contextManager, m_connectionManager);

 15)Creating the Setting object:This component implements the Setting interface of AVS.
   m_settings = capabilityAgents::settings::Settings::create(settingsStorage, {settingsUpdatedEventSender});

 16)Creating the SpeakerManager Capability Agent
   m_speakerManager = capabilityAgents::speakerManager::SpeakerManager::create()

 17)Creating the TemplateRuntime Capability Agent
   m_templateRuntime = capabilityAgents::templateRuntime::TemplateRuntime::create(m_audioPlayer, exceptionSender);

 18)Creating the Endpoint Handler
   auto endpointHandler = capabilityAgents::system::EndpointHandler::create(m_connectionManager, exceptionSender);

 19) register capability agents to the directive sequencer
 (!m_directiveSequencer->addDirectiveHandler(m_speechSynthesizer))
 (!m_directiveSequencer->addDirectiveHandler(m_audioPlayer))
 (!m_directiveSequencer->addDirectiveHandler(m_audioInputProcessor))
 (!m_directiveSequencer->addDirectiveHandler(m_alertsCapabilityAgent))
 (!m_directiveSequencer->addDirectiveHandler(endpointHandler))
 (!m_directiveSequencer->addDirectiveHandler(userInactivityMonitor))
 (!m_directiveSequencer->addDirectiveHandler(m_speakerManager))
 (!m_directiveSequencer->addDirectiveHandler(m_templateRuntime))
 //DefaultClient介绍结束,创建结束后等待鉴权认证是否成功,此处是等待 AuthDelegate 创建的线程状态, 使用了c++11 的 std::condition_variable::wait_for() 阻塞等待
 if (!connectionObserver->waitFor(
   alexaClientSDK::avsCommon::sdkInterfaces:: AuthObserverInterface::State::REFRESHED)) {
   alexaClientSDK::sampleApp::ConsolePrinter::simplePrint("Failed to authorize SDK client!");
   return false;
 }
========================================================
连接判断:此处调用的实例为
1) 创建http2实例
void MessageRouter::enable()=》
void MessageRouter::createActiveTransportLocked()=》
std::shared_ptr<TransportInterface> HTTP2MessageRouter::createTransport()=>
HTTP2Transport::create()
2)连接目标IP,http get请求:curl接口的初始化,url设置,header设置,token设置具体参考:
setupDownchannelStream(&reason);
transport->connect(); //原型bool HTTP2Transport::connect()
解析如下:
bool HTTP2Transport::connect() {
/* 原型:bool PostConnectSynchronizer::doPostConnect(std::shared_ptr<HTTP2Transport> transport);
此种会创建线程判断连接状态,外部的循环等待设置的原因是什么??*/
(!m_postConnectObject-> doPostConnect(shared_from_this()));
m_networkThread = std::thread(&HTTP2Transport:: networkLoop, this);
}
此处有两个问题:
1)doPostConnect:会创建线程判断post http connected是否成功;
2)创建networkLoop()多次尝试连接机制,每次尝试连接会阻塞等待一会,check状态是否发生更新,
networkLoop()=》 //主要流程分为两个while循环:1,循环执行连接目标服务器;2,连接成功后,进入线程的循环机制,猜测后面的http get都会交给这里来完成
establishConnection()=》 //内部也有等待机制 m_multi->perform()

========================================================
client->connect(endpoint);//内部创建线程,http连接任务交给了线程来完成,创建结束后会在此处等待网络的状态更新为CONNECTED,具体的连接内容和判断原则需要进一步消化、吸收

if (!connectionObserver-> waitFor(avsCommon::sdkInterfaces:: ConnectionStatusObserverInterface::Status::CONNECTED)) {
alexaClientSDK::sampleApp::ConsolePrinter::simplePrint("Failed to connect to AVS!");
return false;
}

//Creating the buffer
size_t bufferSize = alexaClientSDK::avsCommon::avs::AudioInputStream::calculateBufferSize(
BUFFER_SIZE_IN_SAMPLES, WORD_SIZE, MAX_READERS);
auto buffer = std::make_shared<alexaClientSDK::avsCommon::avs::AudioInputStream::Buffer>(bufferSize);
std::shared_ptr<alexaClientSDK::avsCommon::avs::AudioInputStream> sharedDataStream =
alexaClientSDK::avsCommon::avs::AudioInputStream::create(buffer, WORD_SIZE, MAX_READERS);

// Creating tap to talk audio provider
// Creating hold to talk audio provider
// Creating wake word audio provider, if necessary,重点看看唤醒词模式
alexaClientSDK::capabilityAgents::aip::AudioProvider wakeWordAudioProvider();
auto keywordObserver = std::make_shared<alexaClientSDK::sampleApp::KeywordObserver>(client, wakeWordAudioProvider);
m_keywordDetector = alexaClientSDK::kwd::KittAiKeyWordDetector::create();
auto interactionManager = std::make_shared<alexaClientSDK::sampleApp::InteractionManager>();
client->addAlexaDialogStateObserver(interactionManager);
m_userInputManager= alexaClientSDK::sampleApp:: UserInputManager::create(interactionManager);



  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值