class mrpt::hwdrivers::testing::FakeTcpDevice

Overview

A TCP server on the loopback interface that plays the role of a networked sensor: it accepts one connection and, for every chunk of bytes received from the driver, it calls a handler that returns the bytes to send back (an empty string sends nothing).

Bytes can also be pushed with no request at all (the spontaneous constructor argument), for sensors that stream data on their own, and the connection can be closed right after the first reply, like an HTTP/1.0 server does.

#include </home/jlblanco/mrpt/modules/mrpt_hwdrivers/tests/fake_tcp_device.h>

class FakeTcpDevice
{
public:
    // typedefs

    typedef std::function<std::string(const std::string&request)> Handler;

    // construction

    FakeTcpDevice(
        Handler handler,
        std::string spontaneous = {},
        bool closeAfterReply = false
        );

    FakeTcpDevice(const FakeTcpDevice&);
    FakeTcpDevice(FakeTcpDevice&&);

    // methods

    FakeTcpDevice& operator = (const FakeTcpDevice&);
    FakeTcpDevice& operator = (FakeTcpDevice&&);
    bool isReady() const;
    unsigned short port() const;
    std::vector<std::string> requests() const;
    void disconnect();
};

Methods

std::vector<std::string> requests() const

Everything the driver has sent so far, one entry per received chunk.

void disconnect()

Hang up the connection (the next driver read sees a closed socket).