项目作者: oatpp

项目描述 :
Oat++ extension module to work with SSDP protocol.
高级语言: C++
项目地址: git://github.com/oatpp/oatpp-ssdp.git
创建时间: 2020-06-10T23:02:11Z
项目社区:https://github.com/oatpp/oatpp-ssdp

开源协议:Apache License 2.0

下载


oatpp-ssdp Build Status

Oat++ extension module to work with SSDP (Simple Service Discovery Protocol) protocol.

👉Find the complete example project using oatpp-ssdp module - Example IoT Hue👈

More about Oat++:

Build And Install

Note: you need to install the main oatpp module first.

  • Clone this repository.
  • In the root of the repository run:
    1. mkdir build && cd build
    2. cmake ..
    3. make install

API

Declare Necessary Components

In the AppComponent.hpp file:

  1. #include "oatpp-ssdp/SimpleSsdpUdpStreamProvider.hpp"
  2. #include "oatpp-ssdp/SsdpStreamHandler.hpp"
  3. ...
  4. /**
  5. * Create provider of SSDP-UDP packets stream.
  6. */
  7. OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::ssdp::UdpStreamProvider>, ssdpStreamProvider)("ssdp", [] {
  8. return oatpp::ssdp::SimpleSsdpUdpStreamProvider::createShared();
  9. }());
  10. /**
  11. * We can reuse the HttpRouter for SSDP since SSDP message is complient to HTTP1.1.
  12. */
  13. OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, ssdpRouter)("ssdp", [] {
  14. return oatpp::web::server::HttpRouter::createShared();
  15. }());
  16. /**
  17. * Create SsdpStreamHandler component which uses Router component to route requests.
  18. * It looks like a normal ConnectionHandler but is specialized on SsdpStreams and returns something conceptually very different
  19. */
  20. OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::ssdp::SsdpStreamHandler>, ssdpStreamHandler)("ssdp", [] {
  21. OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router, "ssdp"); // get Router component
  22. return oatpp::ssdp::SsdpStreamHandler::createShared(router);
  23. }());

Run SSDP Server

In the App.cpp file:

  1. /* Get stream provider component */
  2. OATPP_COMPONENT(std::shared_ptr<oatpp::ssdp::UdpStreamProvider>, ssdpStreamProvider, "ssdp");
  3. /* Get stream handler component */
  4. OATPP_COMPONENT(std::shared_ptr<oatpp::ssdp::SsdpStreamHandler>, ssdpStreamHandler, "ssdp");
  5. /* Create server which takes provided streams and passes them to stream handler */
  6. oatpp::network::server::Server server(ssdpStreamProvider, ssdpStreamHandler);
  7. /* Priny info about server port */
  8. OATPP_LOGD("Server", "Running SSDP on port %s...", ssdpStreamProvider->getProperty("port").getData());
  9. /* Run server */
  10. server.run();

Handle SSDP Messages

In the Controller.hpp file:

  1. /**
  2. * Other devices that want to discover you send 'M-SEARCH *' SSDP packages.
  3. * You have to answer with a corresponding packet on this discovery.
  4. */
  5. ENDPOINT("M-SEARCH", "*", star) {
  6. auto response = createResponse(Status::CODE_200, "" /* empty body */);
  7. // TODO - add correct response headers.
  8. return response;
  9. }