Troy
Troy

Reputation: 68

Accessing other threads or data from POCO HTTPRequestHandler

I have an C++ application that reads a variety of sensors and then acts on them as required. Currently the sensors run in their own threads and have get/set methods for their values.

I'm trying to integrate a web socket server using POCO libraries to display the state of the sensors.

How do I go about getting the sensor information into the HTTPRequestHandler?

Should I be using the POCO::Application class and defining the sensors & server as subsystems? Is there another approach that I should be taking?

Upvotes: 1

Views: 1352

Answers (2)

Alex
Alex

Reputation: 5330

See how WebEventService in macchina.io is implemented - using Poco::Net::HTTPServer, WebSocket and Poco::NotificationQueue.

The design "in a nutshell" is a pub/sub pattern, client subscribes to notifications and receives them through WebSocket; in-process subscriptions/notifications (using Poco events) are also supported. There is a single short-living thread (HTTP handler) launched at subscription time and the rest of communication is through WebSocket reactor-like functionality, so performance and scalability is reasonably good (although there is room for improvement, depending on target platform).

You may consider using macchina.io itself (Apache license) - it is based on POCO/OSP and targets the type of application you have. WebEvent functionality will be part of Poco::NetEx in 1.7.0 release (sheduled for September this year).

Upvotes: 0

doqtor
doqtor

Reputation: 8484

You can derive from HTTPRequestHandler and override handleRequest() and give access to the sensor information by for example storing a reference to your sensor info object as a member of the class derived from HTTPRequestHandler.

class SensorStateRequestHandler : public Poco::Net::HTTPRequestHandler
{
public:
    SensorStateRequestHandler(SensorInfo &sensorInfo)
        : sensorInfo_(sensorInfo)
    {}

    virtual void handleRequest(Poco::Net::HTTPServerRequest &request, Poco::Net::HTTPServerResponse &response) override
    {
        // receive request websocket frame
        sensorInfo_.get_state(); // must be thread safe
        // send response websocket frame with sensor state
    }

private:
    sensorInfo &sensorInfo_;
};

Upvotes: 1

Related Questions