22 #ifndef REGILO_CONTROLLER_HPP 23 #define REGILO_CONTROLLER_HPP 27 #include <boost/asio/io_service.hpp> 28 #include <boost/asio/read_until.hpp> 29 #include <boost/asio/streambuf.hpp> 30 #include <boost/asio/write.hpp> 37 namespace ba = boost::asio;
54 virtual void connect(
const std::string& endpoint) = 0;
72 virtual std::shared_ptr<ILog>
getLog() = 0;
78 virtual std::shared_ptr<const ILog>
getLog()
const = 0;
84 virtual void setLog(std::shared_ptr<ILog> log) = 0;
91 virtual std::string
sendCommand(
const std::string& command) = 0;
97 template<
typename StreamT>
101 ba::streambuf istreamBuffer;
102 std::istream istream;
104 ba::streambuf ostreamBuffer;
105 std::ostream ostream;
119 template<typename Response = void, typename std::enable_if<std::is_void<Response>::value>::type* =
nullptr>
126 template<typename Response, typename std::enable_if<!std::is_void<Response>::value>::type* =
nullptr>
132 std::string REQUEST_END =
"\n";
133 std::string RESPONSE_END =
"\n";
135 bool readResponse =
true;
136 bool readCommand =
true;
160 virtual inline bool isConnected()
const override {
return stream.is_open(); }
162 virtual inline std::shared_ptr<ILog>
getLog()
override {
return log; }
163 virtual inline std::shared_ptr<const ILog>
getLog()
const override {
return log; }
165 virtual void setLog(std::shared_ptr<ILog> log)
override;
167 virtual std::string
sendCommand(
const std::string& command)
final override;
174 template<
typename Response =
void,
typename Command>
183 template<
typename Response = void,
typename Command,
typename... Args>
184 Response
sendCommand(
const Command& command,
const Args& ... params);
192 template<
typename Response = void,
typename... Args>
193 Response sendFormattedCommand(
const std::string& commandFormat, Args... params);
201 template<
typename... Args>
202 std::string createFormattedCommand(
const std::string& commandFormat, Args... params)
const;
205 template<
typename StreamT>
207 istream(&istreamBuffer),
208 ostream(&ostreamBuffer),
213 template<
typename StreamT>
218 log.reset(
new Log(logPath));
222 template<
typename StreamT>
225 this->
log.reset(
new Log(logStream));
228 template<
typename StreamT>
234 template<
typename StreamT>
237 std::shared_ptr<Log> logPointer = std::dynamic_pointer_cast<
Log>(
log);
238 this->log.swap(logPointer);
241 template<
typename StreamT>
244 sendCommand<>(command);
246 std::string response;
252 template<
typename StreamT>
253 template<
typename Response,
typename Command>
257 return sendCommand<Response>();
260 template<
typename StreamT>
261 template<
typename Response,
typename Command,
typename... Args>
265 return sendCommand<Response>(params...);
268 template<
typename StreamT>
269 template<
typename Response,
typename... Args>
275 template<
typename StreamT>
276 template<typename Response, typename std::enable_if<std::is_void<Response>::value>::type*>
284 ba::write(
stream, ostreamBuffer);
297 std::string cmdInput;
298 getLine(istream, cmdInput, REQUEST_END);
299 cmdInput = cmdInput.substr(0, cmdInput.length() - REQUEST_END.length());
307 if(
log !=
nullptr)
log->write(input, output);
310 template<
typename StreamT>
311 template<typename Response, typename std::enable_if<!std::is_void<Response>::value>::type*>
322 template<
typename StreamT>
323 template<
typename... Args>
326 std::size_t size = std::snprintf(
nullptr, 0, command.c_str(), params...) + 1;
327 char *buffer =
new char[size];
328 std::snprintf(buffer, size, command.c_str(), params...);
330 std::string result(buffer, buffer + size - 1);
338 #endif // REGILO_CONTROLLER_HPP void sendCommand()
Send a command from the device input to the device.
Definition: controller.hpp:277
virtual void setLog(std::shared_ptr< ILog > log) override
Set a Log (it can be shared between more controllers).
Definition: controller.hpp:235
StreamT stream
A stream (TCP, socket, etc.) that is used for read/write operations.
Definition: controller.hpp:112
Definition: controller.hpp:35
std::shared_ptr< Log > log
A log that is connected to the controller.
Definition: controller.hpp:114
bool readResponse
If true the sendCommand method reads a response.
Definition: controller.hpp:135
virtual ~StreamController()
Default destructor.
Definition: controller.hpp:229
std::istringstream deviceOutput
A buffer for the device output.
Definition: controller.hpp:108
virtual std::string getEndpoint() const =0
Get the endpoint of device.
The Log class is a basic log with a simple read/write functionality.
Definition: log.hpp:108
StreamController()
Default constructor.
Definition: controller.hpp:206
The IController interface is used for all controller classes.
Definition: controller.hpp:42
std::string REQUEST_END
A string that the request ends with.
Definition: controller.hpp:132
virtual std::shared_ptr< ILog > getLog()=0
Get the current Log.
virtual std::string sendCommand(const std::string &command)=0
Send a command to the device.
std::string RESPONSE_END
A string that the response ends with.
Definition: controller.hpp:133
virtual bool isConnected() const override
Test if the controller is connected.
Definition: controller.hpp:160
bool readCommand
If true the input command is read from the response at first.
Definition: controller.hpp:136
StreamT Stream
The stream type for this Controller.
Definition: controller.hpp:130
virtual std::shared_ptr< ILog > getLog() override
Get the current Log.
Definition: controller.hpp:162
Response sendFormattedCommand(const std::string &commandFormat, Args...params)
Create a command with the specified parameters (printf formatting is used) and send it to the device...
Definition: controller.hpp:270
std::string createFormattedCommand(const std::string &commandFormat, Args...params) const
Create a command with the specified parameters (printf formatting is used).
Definition: controller.hpp:324
virtual void connect(const std::string &endpoint)=0
Connect the controller to a device.
ba::io_service ioService
The Boost IO service.
Definition: controller.hpp:111
virtual std::shared_ptr< const ILog > getLog() const override
Get the current Log (a const variant).
Definition: controller.hpp:163
std::ostringstream deviceInput
A buffer for the device input.
Definition: controller.hpp:109
virtual ~IController()=default
Default destructor.
The StreamController class is used to communicate with a device.
Definition: controller.hpp:98
virtual void setLog(std::shared_ptr< ILog > log)=0
Set a Log (it can be shared between more controllers).
virtual bool isConnected() const =0
Test if the controller is connected.