Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
ClientConnection.hxx
Go to the documentation of this file.
1
35#ifndef _UTILS_CLIENTCONNECTION_HXX_
36#define _UTILS_CLIENTCONNECTION_HXX_
37
38#include <stdio.h>
39#include <unistd.h>
40#include <fcntl.h>
41
43#include "utils/DirectHub.hxx"
45#include "utils/FdUtils.hxx"
46
49{
50public:
53 virtual bool ping() = 0;
54
57 virtual int fd() { return -1; }
58
59 virtual ~ConnectionClient()
60 {
61 }
62};
63
68{
69public:
76 DeviceClosedNotify(int *fd, string name)
77 : fd_(fd)
78 , name_(name)
79 {
80 }
82 void notify() override
83 {
84 LOG_ERROR("Connection to %s closed.", name_.c_str());
85 *fd_ = -1;
86 }
87
88private:
89 int *fd_;
90 string name_;
91};
92
95{
96public:
101 GCFdConnectionClient(const string &name, CanHubFlow *hub)
102 : closedNotify_(&fd_, name)
103 , hub_(hub)
104 {
105 }
106
111 GCFdConnectionClient(const string &name, ByteDirectHubInterface *direct_hub)
112 : closedNotify_(&fd_, name)
113 , directHub_(direct_hub)
114 {
115 }
116
117 virtual ~GCFdConnectionClient()
118 {
119 }
120
124 {
125 if (fd_ < 0)
126 {
127 try_connect();
128 }
129 return fd_ >= 0;
130 }
131
132 int fd() override
133 {
134 return fd_;
135 }
136
137protected:
140 virtual void try_connect() = 0;
141
144 void connection_complete(int fd);
145
146private:
151 int fd_{-1};
153 CanHubFlow *hub_{nullptr};
156};
157
162{
163public:
169 template<typename HubType>
171 const string &name, HubType *hub, const string &dev)
172 : GCFdConnectionClient(name, hub)
173 , dev_(dev)
174 {
175 }
176
178 {
179 }
180
181private:
184 {
185 int fd = ::open(dev_.c_str(), O_RDWR);
186 if (fd >= 0)
187 {
189 LOG(INFO, "Opened device %s.\n", dev_.c_str());
191 }
192 else
193 {
194 LOG_ERROR("Failed to open device %s: %s\n", dev_.c_str(),
195 strerror(errno));
196 }
197 }
198
199 string dev_;
200};
201
205{
206public:
213 template<typename HubType>
215 const string &name, HubType *hub, const string &host, int port)
216 : GCFdConnectionClient(name, hub)
217 , host_(host)
218 , port_(port)
219 {
220 }
221
223 {
224 }
225
226private:
229 {
230 int fd = ConnectSocket(host_.c_str(), port_);
231 if (fd >= 0)
232 {
233 LOG_ERROR("Connected to %s:%d\n", host_.c_str(), port_);
235 // create_gc_port_for_can_hub(&can_hub0, fd, &closedNotify_);
236 }
237 else
238 {
239 LOG_ERROR("Failed to connect to %s:%d: (%d) %s\n", host_.c_str(),
240 port_, errno, strerror(errno));
241 }
242 }
243
245 string host_;
247 int port_;
248};
249
250#endif // _UTILS_CLIENTCONNECTION_HXX_
Abstract base class for the Hub's connections.
virtual bool ping()=0
Test the connection whether it is alive; establish the connection if it is dead.
Notification implementation that sets an external variable to -1 when notified.
int * fd_
pointer to file descriptor variable.
void notify() override
Callback from the application when an error is encountered.
DeviceClosedNotify(int *fd, string name)
Constructor.
string name_
info to print to stderr upon error.
Connection client that opens a character device (such as an usb-serial) and sets the termios attribut...
DeviceConnectionClient(const string &name, HubType *hub, const string &dev)
Constructor.
string dev_
filename of device to open
void try_connect() OVERRIDE
Attempts to open the device.
Base class for FD-based GridConnect connection clients.
ByteDirectHubInterface * directHub_
DirectHub to read/write data to.
GCFdConnectionClient(const string &name, ByteDirectHubInterface *direct_hub)
Constructor.
DeviceClosedNotify closedNotify_
Will be called when the descriptor experiences an error (typivcally upon device closed or connection ...
int fd_
File descriptor of the currently open device/socket.
void connection_complete(int fd)
Callback from try_connect to donate the file descriptor.
bool ping() OVERRIDE
Tests if the device is alive or encountered an error.
virtual void try_connect()=0
Abstrct base function to attempt to connect (or open device) to the destination.
CanHubFlow * hub_
CAN hub to read-write data to.
GCFdConnectionClient(const string &name, CanHubFlow *hub)
Constructor.
An object that can schedule itself on an executor to run.
Connection client that connects to an upstream GridConnect-TCP hub via TCP client socket.
void try_connect() OVERRIDE
Implementation of connection method.
string host_
where to connect to (host name)
UpstreamConnectionClient(const string &name, HubType *hub, const string &host, int port)
Constructor.
int port_
where to connect to (TCP port number)
#define LOG(level, message...)
Conditionally write a message to the logging output.
Definition logging.h:99
static const int INFO
Loglevel that is printed by default, reporting some status information.
Definition logging.h:57
#define LOG_ERROR(message...)
Shorthand for LOG(LEVEL_ERROR, message...). See LOG.
Definition logging.h:124
#define OVERRIDE
Function attribute for virtual functions declaring that this funciton is overriding a funciton that s...
Definition macros.h:180
int ConnectSocket(const char *host, int port)
Connects a tcp socket to the specified remote host:port.
static void optimize_tty_fd(int fd)
Sets the kernel settings like queuing and terminal settings for an fd that is an outgoing tty.
Definition FdUtils.cxx:102