ChimeraTK-DeviceAccess  03.18.00
RebotDummyServer.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2 // SPDX-License-Identifier: LGPL-3.0-or-later
3 #pragma once
4 
5 #include "DummyBackend.h"
7 
8 #include <boost/asio.hpp>
9 
10 #include <atomic>
11 #include <memory>
12 #include <string>
13 
14 namespace ip = boost::asio::ip;
15 
16 namespace ChimeraTK {
17 
18  extern std::atomic<bool> stop_rebot_server;
19 
20  /*
21  * starts a blocking Rebot server on localhost:port. where port is the
22  * portNumber specified during object creation.
23  */
24  class RebotDummySession : public std::enable_shared_from_this<RebotDummySession> {
25  // everything is public so all protocol implementors can reach it. They are
26  // only called from within the server
27  public:
29  unsigned int protocolVersion, ip::tcp::socket socket, std::shared_ptr<DummyBackend> regsiterSpace);
30  void start();
31  virtual ~RebotDummySession();
32 
33  // The following stuff is only intended for the protocol implementors and the
34  // server itself
35 
36  static const int BUFFER_SIZE_IN_WORDS = 256;
37  static const int32_t READ_SUCCESS_INDICATION = 1000;
38  static const int32_t WRITE_SUCCESS_INDICATION = 1001;
39  static const uint32_t PONG = 1005;
40  static const int32_t TOO_MUCH_DATA_REQUESTED = -1010;
41  static const int32_t UNKNOWN_INSTRUCTION = -1040;
42 
43  static const uint32_t SINGLE_WORD_WRITE = 1;
44  static const uint32_t MULTI_WORD_WRITE = 2;
45  static const uint32_t MULTI_WORD_READ = 3;
46  static const uint32_t HELLO = 4;
47  static const uint32_t PING = 5;
48  static const uint32_t REBOT_MAGIC_WORD = 0x72626f74; // ascii code 'rbot'
49 
50  // internal states. Currently there are only two when the connection is open
51  static const uint32_t ACCEPT_NEW_COMMAND = 1;
52  // a multi word write can be so large that it needs more than one package
53  static const uint32_t INSIDE_MULTI_WORD_WRITE = 2;
54 
55  // The actual state: ready for new command or not
56  uint32_t _state;
57 
58  std::atomic<uint32_t> _heartbeatCount;
59  std::atomic<uint32_t> _helloCount; // in protocol version 1 we have to send
60  // hello instead of heartbeat
61  std::atomic<bool> _dont_answer; // flag to cause an error condition
62  std::shared_ptr<DummyBackend> _registerSpace;
63  std::vector<uint32_t> _dataBuffer;
64 
65  unsigned int _serverPort;
66  unsigned int _protocolVersion;
67  ip::tcp::socket _currentClientConnection;
68  std::unique_ptr<DummyProtocolImplementor> _protocolImplementor;
69 
70  void processReceivedPackage(std::vector<uint32_t>& buffer);
71  void writeWordToRequestedAddress(std::vector<uint32_t>& buffer);
72  void readRegisterAndSendData(std::vector<uint32_t>& buffer);
73 
74  // most commands have a single word as response. Avoid code duplication.
75  void sendSingleWord(int32_t response);
76  void acceptHandler(const boost::system::error_code& error);
77  void doRead();
78  void doWrite();
79  void write(std::vector<uint32_t> data);
80  };
81 
83  public:
84  RebotDummyServer(unsigned int portNumber, std::string mapFile, unsigned int protocolVersion);
85 
86  void start();
87  void stop();
88  bool is_running();
89  unsigned int port() const { return _connectionAcceptor.local_endpoint().port(); }
90 
91  boost::asio::io_service& service() { return _io; }
92  std::shared_ptr<RebotDummySession> session() { return _currentSession.lock(); }
93 
94  private:
95  void do_accept();
96  unsigned int _protocolVersion;
97  boost::asio::io_service _io;
98  ip::tcp::acceptor _connectionAcceptor;
99  std::weak_ptr<RebotDummySession> _currentSession;
100  ip::tcp::socket _socket;
101  std::shared_ptr<DummyBackend> _registerSpace;
102  };
103 
104 } /* namespace ChimeraTK */
ChimeraTK::RebotDummySession::~RebotDummySession
virtual ~RebotDummySession()
Definition: RebotDummyServer.cc:164
ChimeraTK::RebotDummySession::_protocolImplementor
std::unique_ptr< DummyProtocolImplementor > _protocolImplementor
Definition: RebotDummyServer.h:68
ChimeraTK::RebotDummySession::acceptHandler
void acceptHandler(const boost::system::error_code &error)
ChimeraTK::RebotDummyServer::is_running
bool is_running()
Definition: RebotDummyServer.cc:211
ChimeraTK::RebotDummySession::BUFFER_SIZE_IN_WORDS
static const int BUFFER_SIZE_IN_WORDS
Definition: RebotDummyServer.h:36
ChimeraTK::RebotDummySession
Definition: RebotDummyServer.h:24
ChimeraTK::RebotDummySession::_registerSpace
std::shared_ptr< DummyBackend > _registerSpace
Definition: RebotDummyServer.h:62
ChimeraTK::RebotDummySession::_heartbeatCount
std::atomic< uint32_t > _heartbeatCount
Definition: RebotDummyServer.h:58
ChimeraTK::RebotDummySession::MULTI_WORD_READ
static const uint32_t MULTI_WORD_READ
Definition: RebotDummyServer.h:45
DummyBackend.h
ChimeraTK::stop_rebot_server
std::atomic< bool > stop_rebot_server
ChimeraTK::RebotDummySession::TOO_MUCH_DATA_REQUESTED
static const int32_t TOO_MUCH_DATA_REQUESTED
Definition: RebotDummyServer.h:40
ChimeraTK::RebotDummySession::sendSingleWord
void sendSingleWord(int32_t response)
Definition: RebotDummyServer.cc:158
ChimeraTK::RebotDummyServer::port
unsigned int port() const
Definition: RebotDummyServer.h:89
ChimeraTK::RebotDummySession::PING
static const uint32_t PING
Definition: RebotDummyServer.h:47
ChimeraTK::RebotDummySession::HELLO
static const uint32_t HELLO
Definition: RebotDummyServer.h:46
ChimeraTK::RebotDummySession::INSIDE_MULTI_WORD_WRITE
static const uint32_t INSIDE_MULTI_WORD_WRITE
Definition: RebotDummyServer.h:53
ChimeraTK::RebotDummySession::READ_SUCCESS_INDICATION
static const int32_t READ_SUCCESS_INDICATION
Definition: RebotDummyServer.h:37
DummyProtocolImplementor.h
ChimeraTK::RebotDummySession::PONG
static const uint32_t PONG
Definition: RebotDummyServer.h:39
ChimeraTK::RebotDummyServer
Definition: RebotDummyServer.h:82
ChimeraTK::RebotDummySession::_dont_answer
std::atomic< bool > _dont_answer
Definition: RebotDummyServer.h:61
ChimeraTK::RebotDummySession::SINGLE_WORD_WRITE
static const uint32_t SINGLE_WORD_WRITE
Definition: RebotDummyServer.h:43
ChimeraTK::RebotDummySession::MULTI_WORD_WRITE
static const uint32_t MULTI_WORD_WRITE
Definition: RebotDummyServer.h:44
ChimeraTK::RebotDummySession::_protocolVersion
unsigned int _protocolVersion
Definition: RebotDummyServer.h:66
ChimeraTK::RebotDummyServer::service
boost::asio::io_service & service()
Definition: RebotDummyServer.h:91
ChimeraTK::RebotDummySession::UNKNOWN_INSTRUCTION
static const int32_t UNKNOWN_INSTRUCTION
Definition: RebotDummyServer.h:41
ChimeraTK::RebotDummyServer::stop
void stop()
Definition: RebotDummyServer.cc:207
ChimeraTK::RebotDummySession::_currentClientConnection
ip::tcp::socket _currentClientConnection
Definition: RebotDummyServer.h:67
ChimeraTK::RebotDummySession::start
void start()
Definition: RebotDummyServer.cc:35
ChimeraTK::RebotDummyServer::RebotDummyServer
RebotDummyServer(unsigned int portNumber, std::string mapFile, unsigned int protocolVersion)
Definition: RebotDummyServer.cc:172
ChimeraTK::RebotDummySession::WRITE_SUCCESS_INDICATION
static const int32_t WRITE_SUCCESS_INDICATION
Definition: RebotDummyServer.h:38
ChimeraTK::RebotDummySession::processReceivedPackage
void processReceivedPackage(std::vector< uint32_t > &buffer)
Definition: RebotDummyServer.cc:77
ChimeraTK::RebotDummySession::ACCEPT_NEW_COMMAND
static const uint32_t ACCEPT_NEW_COMMAND
Definition: RebotDummyServer.h:51
ChimeraTK::RebotDummySession::writeWordToRequestedAddress
void writeWordToRequestedAddress(std::vector< uint32_t > &buffer)
Definition: RebotDummyServer.cc:119
ChimeraTK::RebotDummySession::_serverPort
unsigned int _serverPort
Definition: RebotDummyServer.h:65
ChimeraTK::RebotDummySession::doWrite
void doWrite()
Definition: RebotDummyServer.cc:55
ChimeraTK::RebotDummySession::write
void write(std::vector< uint32_t > data)
Definition: RebotDummyServer.cc:152
ChimeraTK::RebotDummySession::RebotDummySession
RebotDummySession(unsigned int protocolVersion, ip::tcp::socket socket, std::shared_ptr< DummyBackend > regsiterSpace)
Definition: RebotDummyServer.cc:18
ChimeraTK::RebotDummySession::_state
uint32_t _state
Definition: RebotDummyServer.h:56
ChimeraTK::RebotDummySession::doRead
void doRead()
Definition: RebotDummyServer.cc:39
ChimeraTK::RebotDummyServer::session
std::shared_ptr< RebotDummySession > session()
Definition: RebotDummyServer.h:92
ChimeraTK::RebotDummySession::_dataBuffer
std::vector< uint32_t > _dataBuffer
Definition: RebotDummyServer.h:63
ChimeraTK
Definition: DummyBackend.h:16
ChimeraTK::RebotDummySession::readRegisterAndSendData
void readRegisterAndSendData(std::vector< uint32_t > &buffer)
Definition: RebotDummyServer.cc:132
ChimeraTK::RebotDummyServer::start
void start()
Definition: RebotDummyServer.cc:202
ChimeraTK::RebotDummySession::_helloCount
std::atomic< uint32_t > _helloCount
Definition: RebotDummyServer.h:59
ChimeraTK::RebotDummySession::REBOT_MAGIC_WORD
static const uint32_t REBOT_MAGIC_WORD
Definition: RebotDummyServer.h:48