An open API service indexing awesome lists of open source software.

https://github.com/oatpp/oatpp-ssdp

Oat++ extension module to work with SSDP protocol.
https://github.com/oatpp/oatpp-ssdp

cpp oatpp ssdp

Last synced: 12 months ago
JSON representation

Oat++ extension module to work with SSDP protocol.

Awesome Lists containing this project

README

          

# oatpp-ssdp [![Build Status](https://dev.azure.com/lganzzzo/lganzzzo/_apis/build/status/oatpp.oatpp-ssdp?branchName=master)](https://dev.azure.com/lganzzzo/lganzzzo/_build/latest?definitionId=28&branchName=master)

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

πŸ‘‰Find the complete example project using **oatpp-ssdp** module - [Example IoT Hue](https://github.com/oatpp/example-iot-hue-ssdp)πŸ‘ˆ

More about Oat++:

- [Oat++ Website](https://oatpp.io/)
- [Oat++ Github Repository](https://github.com/oatpp/oatpp)

## Build And Install

*Note: you need to install the main [oatpp](https://github.com/oatpp/oatpp) module first.*

- Clone this repository.
- In the root of the repository run:
```bash
mkdir build && cd build
cmake ..
make install
```

## API

### Declare Necessary Components

In the `AppComponent.hpp` file:

```cpp
#include "oatpp-ssdp/SimpleSsdpUdpStreamProvider.hpp"
#include "oatpp-ssdp/SsdpStreamHandler.hpp"

...

/**
* Create provider of SSDP-UDP packets stream.
*/
OATPP_CREATE_COMPONENT(std::shared_ptr, ssdpStreamProvider)("ssdp", [] {
return oatpp::ssdp::SimpleSsdpUdpStreamProvider::createShared();
}());

/**
* We can reuse the HttpRouter for SSDP since SSDP message is complient to HTTP1.1.
*/
OATPP_CREATE_COMPONENT(std::shared_ptr, ssdpRouter)("ssdp", [] {
return oatpp::web::server::HttpRouter::createShared();
}());

/**
* Create SsdpStreamHandler component which uses Router component to route requests.
* It looks like a normal ConnectionHandler but is specialized on SsdpStreams and returns something conceptually very different
*/
OATPP_CREATE_COMPONENT(std::shared_ptr, ssdpStreamHandler)("ssdp", [] {
OATPP_COMPONENT(std::shared_ptr, router, "ssdp"); // get Router component
return oatpp::ssdp::SsdpStreamHandler::createShared(router);
}());
```

### Run SSDP Server

In the `App.cpp` file:

```cpp
/* Get stream provider component */
OATPP_COMPONENT(std::shared_ptr, ssdpStreamProvider, "ssdp");

/* Get stream handler component */
OATPP_COMPONENT(std::shared_ptr, ssdpStreamHandler, "ssdp");

/* Create server which takes provided streams and passes them to stream handler */
oatpp::network::server::Server server(ssdpStreamProvider, ssdpStreamHandler);

/* Priny info about server port */
OATPP_LOGD("Server", "Running SSDP on port %s...", ssdpStreamProvider->getProperty("port").getData());

/* Run server */
server.run();
```

### Handle SSDP Messages

In the `Controller.hpp` file:

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