Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Server.hxx
Go to the documentation of this file.
1
28#ifndef _WITHROTTLE_SERVER_HXX_
29#define _WITHROTTLE_SERVER_HXX_
30
31#include <string>
32#include <memory>
33
35#include "executor/Service.hxx"
38#include "withrottle/Defs.hxx"
41
42namespace withrottle
43{
44/* forward declaration */
45class ThrottleFlow;
46
49class Server : public Service
50{
51public:
57 Server(const char *name, int port, openlcb::Node *node)
59 , executor(name, 0, 2048)
60 , node(node)
61 , listener((port >= 0 || port <= UINT16_MAX) ? port : Defs::DEFAULT_PORT,
62 std::bind(&Server::on_new_connection, this,
63 std::placeholders::_1))
64 {
65 }
66
70 {
72 }
73
76 void start()
77 {
78 }
79
80private:
84 void on_new_connection(int fd);
85
88
91
94
96 friend class ThrottleFlow;
97
99};
100
104{
105public:
112
116 {
117 close(fd);
118 command->unref();
119 }
120
123 void start()
124 {
126 }
127
128private:
132 bool parse();
133
135 void parse_command();
136
138 void parse_multi_type();
139
141 void parse_train();
142
146 bool parse_subcommand();
147
150
155
160
165
167 openlcb::TractionThrottle olcbThrottle;
168
171
172 string name;
173 string id;
178 int fd;
179
181 char readRaw[128];
182
184 string data;
185
187 size_t dataIndex;
188
191
194
197
200
203
206
208 friend class ServerCommandBase;
209
211 friend class ServerCommandLoco;
212};
213
214/*
215 * Server::on_new_connection()
216 */
217inline void Server::on_new_connection(int fd)
218{
219 ThrottleFlow *flow = new ThrottleFlow(this, fd, node);
220 flow->start();
221}
222
223} /* namespace withrottle */
224
225#endif /* _WITHROTTLE_SERVER_HXX_ */
int bind(int socket, const struct sockaddr *address, socklen_t address_len)
Bind a name to a socket.
Definition Socket.cxx:159
#define STATE(_fn)
Turns a function name into an argument to be supplied to functions expecting a state.
Definition StateFlow.hxx:61
Base class for all QMember types that hold data in an expandable format.
Definition Buffer.hxx:195
Type-specific implementations of the DispatchFlow methods.
Implementation the ExecutorBase with a specific number of priority bands.
Definition Executor.hxx:266
Collection of related state machines that pend on incoming messages.
void shutdown()
Shuts down the socket listener.
Return type for a state flow callback.
Base class for state machines.
void start_flow(Callback c)
Resets the flow to the specified state and starts it.
Base class for NMRAnet nodes conforming to the asynchronous interface.
Definition Node.hxx:52
Interface for a single throttle for running a train node.
WiThrottle server command handler base object.
WiThrottle server command handler base object for multi, primary, and secondary locomotive.
WiThrottle server object.
Definition Server.hxx:50
~Server()
Destructor.
Definition Server.hxx:69
void start()
Start the server.
Definition Server.hxx:76
friend class ThrottleFlow
allow access from ThrottleFlow
Definition Server.hxx:96
Server(const char *name, int port, openlcb::Node *node)
Constructor.
Definition Server.hxx:57
SocketListener listener
listen socket for new connections
Definition Server.hxx:93
openlcb::Node * node
node reference
Definition Server.hxx:90
void on_new_connection(int fd)
A new throttle connection is made.
Definition Server.hxx:217
Executor< 1 > executor
The executor that will run the WiThrottle flows.
Definition Server.hxx:87
State flow for handling a throttle instance.
Definition Server.hxx:104
StateFlowBase::Action data_sent()
Data sent successfully.
Definition Server.cxx:67
string data
agrigate pre-processed stream data
Definition Server.hxx:184
CommandDispatchFlow dispatcher
flow responsible for routing incoming messages to handlers.
Definition Server.hxx:199
Server * server
reference to parent server
Definition Server.hxx:170
void parse_multi_type()
Parse the incoming data.
Definition Server.cxx:193
void parse_multi()
Parse the incoming data.
ServerCommandLoco serverCommandLoco
handler for locomotive commands
Definition Server.hxx:205
void parse_command()
Parse the incoming data.
Definition Server.cxx:158
StateFlowBase::Action data_received()
Process read data.
Definition Server.cxx:76
StateFlowSelectHelper selectHelper
Helper for waiting on data from a file descriptor.
Definition Server.hxx:193
LocoAddress address
primary locomitve addres
Definition Server.hxx:174
bool parse()
Parse the incoming data.
Definition Server.cxx:110
void parse_train()
Parse the incoming data.
Definition Server.cxx:214
string id
id of throttle
Definition Server.hxx:173
ServerState state
Current state of parsing the data.
Definition Server.hxx:190
LocoAddress secondaryAddress
secondary locomotive address
Definition Server.hxx:175
DispatchFlow< Buffer< ThrottleCommand >, 1 > CommandDispatchFlow
dispatch flow that will handle messages incoming from the cab
Definition Server.hxx:196
~ThrottleFlow()
Destructor.
Definition Server.hxx:115
size_t dataIndex
data index for parsing
Definition Server.hxx:187
int fd
socket descriptor of throttle connection
Definition Server.hxx:178
bool parse_subcommand()
Parse the incoming data.
Definition Server.cxx:240
Buffer< ThrottleCommand > * command
throttle command
Definition Server.hxx:202
char readRaw[128]
read data buffer
Definition Server.hxx:181
string name
name of throttle
Definition Server.hxx:172
StateFlowBase::Action entry()
Beginning of state flow.
Definition Server.cxx:56
void start()
Start the service.
Definition Server.hxx:123
#define DISALLOW_COPY_AND_ASSIGN(TypeName)
Removes default copy-constructor and assignment added by C++.
Definition macros.h:171
Use this class to read from an fd using select() in a state flow.
The interface definitions for WiThrottle.
Locomotive address.
ServerState
Server state machine commands.