Solution: Proxy (Remote Thermometer)

Client (Proxy) Thermometer

#pragma once

#include <sensor.h>
#include <server-thread.h>


class RemoteSensor : public Sensor
{
public:
    RemoteSensor(jf::utilities::ServerThread* server) : _server(server) {}
    virtual double get_temperature() override;
    
private:
    jf::utilities::ServerThread* _server;
};
#include "sensor-remote.h"

#include <cstdio>
#include <stdexcept>


double RemoteSensor::get_temperature()
{
    std::string response = _server->write("get_temperature");
    double value;
    int num_items = std::sscanf(response.c_str(), "%lf", &value);
    if (num_items != 1)
        throw std::runtime_error("invalid response");
    return value;
}

Remote Stub

#pragma once

#include "sensor.h"
#include "server-thread.h"


class RemoteSensorAdapter : public jf::utilities::ServerThread::RemoteAdapter
{
public:
    RemoteSensorAdapter(Sensor* sensor) : _sensor(sensor) {}
    virtual std::string execute(const std::string& request) override;

private:
    Sensor* _sensor;
};
#include "sensor-remote-adapter.h"

#include <sstream>
#include <stdexcept>


std::string RemoteSensorAdapter::execute(const std::string& request)
{
    if (request == "get_temperature") {
        double value = _sensor->get_temperature();
        std::ostringstream buffer;
        buffer << value;
        return buffer.str();
    }
    // ... more requests here maybe ...
    else
        throw std::runtime_error("invalid request");
}