ngc 1.4.0
 
Loading...
Searching...
No Matches
ngcbSIO.hpp
Go to the documentation of this file.
1
7#ifndef ngcbSIO_H
8#define ngcbSIO_H
9
10
11#ifndef __cplusplus
12#error This is a C++ include file and cannot be used from plain C
13#endif
14
15
16#include <ngc/core/ngcb.h>
17#include <ngc/core/ngcbSocket.h>
18#include <poll.h>
19
23#define ngcbSIO_MAX_CON 16
24
28#define ngcbSIO_MAX_PORT 4
29
34#define ngcbSIO_MAX_FD 64
35
36#define ngcbSIO_NFDS (ngcbSIO_MAX_FD + (ngcbSIO_MAX_CON * 2) + ngcbSIO_MAX_PORT + 4)
37
38/*
39 * Tokens for system control
40 */
41#define ngcbSIO_CMD_EXIT 0x00000010 //< EXIT command
42#define ngcbSIO_CMD_RESET 0x00000020 //< RESET command
43#define ngcbSIO_CMD_PING 0x00000040 //< PING command
44#define ngcbSIO_CMD_CONNECT 0x00000080 //< CONNECT command
45#define ngcbSIO_CMD_DISCONNECT 0x00000100 //< DISCONNECT command
46#define ngcbSIO_CMD_TIMEOUT 0x00000200 //< TIMEOUT command
47#define ngcbSIO_CMD_SIMULAT 0x00000400 //< SIMULATION command
48#define ngcbSIO_CMD_STOPSIM 0x00000800 //< STOP SIMULATION command
49#define ngcbSIO_CMD_STATUS 0x00001000 //< STATUS command
50
51
52/*
53 * Supported protocols
54 */
55#define ngcbSIO_PROTOCOL_GENERIC 0 //< Generic protocol
56#define ngcbSIO_PROTOCOL_BIN 1 //< Binary protocol
57#define ngcbSIO_PROTOCOL_ASCII 2 //< ASCII protocol
58#define ngcbSIO_PROTOCOL_ASCII0 3 //< raw ASCII (no startup messages)
59
65 int socket;
66
69
71 int port;
72
75 socket = -1; // not open
76 protocol = 0; // use standard protocol
77 port = 0;
78 }
79};
80
85{
86public:
88 ngcbSIO();
89
91 virtual ~ngcbSIO();
92
95
98
101
103 char *ErrMsg() {return (erms_);}
104
105
106 /*
107 * Common socket i/o
108 */
109
111 int PortNum(int fd);
112
114 int GetIpAddr(const char *name, char *ipAddr);
115
117 int WaitSocket(int fd, int timeout);
118
120 int TcpService(const char *service = NULL);
121
123 int TcpService(int port);
124
126 int TcpAccept(int srvSocket);
127
129 int TcpConnect(const char *service);
130
132 int TcpConnect(int port, const char *host = NULL);
133
135 int TcpClose(int fd);
136
138 int TcpRead(int fd, void *buffer, size_t size);
139
141 int TcpWrite(int fd, const void *buffer, size_t size);
142
144 int TcpPuts(int fd, const char *string);
145
147 int TcpGets(int fd, char *string, int maxLen);
148
150 int TcpReadRequest(int fd, char *request, int maxSize);
151
153 int TcpWriteReply(int fd, const char *reply, int success);
154
156 int ExecVersion(const char *host);
157
159 int Exec(const char *host, const char *cmd, char *reply, int port = 0);
160
162 int ExecXt(const char *host, const char *cmd, char *reply,
163 const char *geom = NULL);
164
166 int Kill(const char *host, int pid);
167
169 void ExecReply(int fd, char success, const char *reply);
170
171
172 /*
173 * Command/event client interface
174 */
175
177 int Discover(const char *host, const char *name, const char *instance,
178 int *port, int *protocol = NULL);
179
181 int Discover(const char *name, const char *instance, int *port,
182 int *protocol = NULL);
183
185 int Connect(const char *host, int port, int protocol, int *evtPort);
186
188 int Connect(const char *host, int port, int protocol);
189
191 int Connect(const char *service, int protocol = ngcbSIO_PROTOCOL_ASCII);
192
194 void Disconnect();
195
197 int Connected();
198
200 void ReplyBufferSize(size_t size);
201
203 int SendRequest(const char *request);
204
206 int GetReply(char *s = NULL);
207
209 int PrintReply(FILE *fd);
210
212 int ReplyLine(char *line);
213
215 char *Reply() {return (replyBuffer_);}
216
218 int RequestReply(const char *request, char *reply = NULL);
219
220 /*
221 * Server interface
222 */
223
225 int OpenServer(int cmdPort, int evtPort = -1);
226
228 void CloseServer();
229
231 void StopServer();
232
234 int SrvOpened();
235
237 virtual void SetSrvAborted(int flag);
238
240 virtual int SrvAborted();
241
243 int SrvCmdPort();
244
246 int SrvEvtPort();
247
249 int SrvCmdSocket();
250
252 int SrvEvtSocket();
253
255 int OpenPort(int port, int protocol = 0);
256
258 int ClosePort(int port);
259
261 int Port2Fd(int port);
262
264 int Fd2Port(int fd);
265
267 int ConnectCmd(int srvSocket, int protocol = 0);
268
270 int NumCmdClient();
271
273 int HaveCmdClient();
274
276 int ConnectEvt(int srvSocket);
277
279 int NumEvtClient();
280
282 int HaveEvtClient();
283
285 void Broadcast(const void *buffer, size_t size);
286
288 void Broadcast(const char *s);
289
291 int Protocol(int fd);
292
294 int ProtocolType();
295
297 void ProtocolType(int protocol);
298
300 int GetProtocol(int fd);
301
303 void EnableStdIn();
304
306 void DisableStdIn();
307
309 int StdIn();
310
312 int FdAttach(int fd);
313
315 void FdDetach(int fd);
316
318 void TimerSet(int ms);
319
321 void TimerOn();
322
324 void TimerOff();
325
327 int MainLoop();
328
330 void Lock();
331
333 void Unlock();
334
336 void CleanupCmd(char *cmdLine, int *cmdLen);
337
339 virtual void Verbose(const char *format, ...)
340 __attribute__ ((format(printf, 2, 3)));
341
343 virtual void Verbose(int level, const char *format, ...)
344 __attribute__ ((format(printf, 3, 4)));
345
347 virtual void Log(const char *format, ...)
348 __attribute__ ((format(printf, 2, 3)));
349
351 virtual void Log(int level, const char *format, ...)
352 __attribute__ ((format(printf, 3, 4)));
353
355 virtual void LogMsg(const char *msg);
356
357protected:
359 virtual int ProtocolBin(int fd);
360
362 virtual int ProtocolAscii(int fd);
363
365 virtual int Service(int fd, int protocol);
366
368 virtual int ServiceConnect(int fd, int protocol);
369
371 virtual void ProtocolErr(const char *erms);
372
374 virtual int RegisterService(int fd);
375
377 virtual int UnregisterService(int fd);
378
380 virtual int NewEventConnection(int fd);
381
383 virtual int FdCB(int fd);
384
386 virtual int TimerCB();
387
389 virtual void MainLoopInit();
390
391private:
393 char erms_[256];
394
396 char versionString_[256];
397
399 int endian_;
400
402 int clientStat_;
403
405 FILE *fdIn_;
406
408 FILE *fdOut_;
409
411 int fdLock_[2];
412
414 char *replyBuffer_;
415
417 size_t replyBufferSize_;
418
420 int srvCmdPort_;
421
423 int srvEvtPort_;
424
427
429 int protocol_;
430
432 int protocolStd_;
433
435 int srvOpen_;
436
438 int srvAbort_;
439
441 int execReplyDone_;
442
444 int srvCmdSocket_;
445
447 int srvEvtSocket_;
448
450 int stdin_;
451
453 struct pollfd fds_[ngcbSIO_NFDS];
454
456 int nfds_;
457
459 int fd_[ngcbSIO_MAX_FD];
460
462 int cmdSocket_[ngcbSIO_MAX_CON];
463
465 int evtSocket_[ngcbSIO_MAX_CON];
466
468 int cmdProtocol_[ngcbSIO_MAX_CON];
469
471 struct timeval timerVal_;
472
474 struct timeval ts_;
475
477 int timerEnabled_;
478
480 int CmdSocketIdx_(int fd);
481
483 int EvtSocketIdx_(int fd);
484
486 int FdIsSet_(int fd);
487
489 void FdSet_(int fd);
490
492 void FdClr_(int fd);
493
495 int FdValid_(int fd);
496
498 void Host_(const char *host, char *name, int n = 64);
499};
500
501#endif
void ReplyBufferSize(size_t size)
Set reply buffer size of the client interface.
Definition ngcbSIO.cpp:912
int WaitSocket(int fd, int timeout)
Wait with timeout until socket becomes readable.
Definition ngcbSIO.cpp:340
int MainLoop()
Enter socket server main-loop.
Definition ngcbSIO.cpp:2073
virtual void virtual void virtual void virtual void virtual void LogMsg(const char *msg)
Message logger.
Definition ngcbSIO.cpp:2466
int TcpWriteReply(int fd, const char *reply, int success)
Write reply to tcp/ip socket.
Definition ngcbSIO.cpp:478
void Broadcast(const void *buffer, size_t size)
Broadcat buffer to all event clients.
Definition ngcbSIO.cpp:1709
int SendRequest(const char *request)
Send request to server.
Definition ngcbSIO.cpp:918
int GetReply(char *s=NULL)
Copy reply from server to string.
Definition ngcbSIO.cpp:941
virtual void SetSrvAborted(int flag)
Set server-aborted flag.
Definition ngcbSIO.cpp:1304
void TimerOn()
Enable timer.
Definition ngcbSIO.cpp:2003
void Lock()
Lock service main-loop.
Definition ngcbSIO.cpp:2050
void DisableStdIn()
Disable input via stdin.
Definition ngcbSIO.cpp:1914
int ExecXt(const char *host, const char *cmd, char *reply, const char *geom=NULL)
Execute process on host (in new terminal)
Definition ngcbSIO.cpp:552
virtual int ProtocolBin(int fd)
Binary protocol implementation.
Definition ngcbSIO.cpp:1849
virtual int Service(int fd, int protocol)
Application specific service protocol.
Definition ngcbSIO.cpp:1863
int Kill(const char *host, int pid)
Terminate process on host.
Definition ngcbSIO.cpp:576
virtual void virtual void virtual void Log(const char *format,...) __attribute__((format(printf
Logging function.
Definition ngcbSIO.cpp:2436
ngcbSIO()
Constructor.
Definition ngcbSIO.cpp:30
int Fd2Port(int fd)
Return service port associated to server socket.
Definition ngcbSIO.cpp:1438
int SrvCmdPort()
Get actual server command port.
Definition ngcbSIO.cpp:1306
void TimerSet(int ms)
Set timer (in milliseconds)
Definition ngcbSIO.cpp:2033
int FdAttach(int fd)
Attach file-descriptor to the file descriptor event call-back.
Definition ngcbSIO.cpp:1927
virtual void MainLoopInit()
Indicate main-loop initialization.
Definition ngcbSIO.cpp:2071
int SrvEvtPort()
Get actual server event port.
Definition ngcbSIO.cpp:1307
int SrvEvtSocket()
Get actual event server socket.
Definition ngcbSIO.cpp:1309
int StdIn()
Get file-descriptor associated to the standard input.
Definition ngcbSIO.cpp:1894
virtual void Verbose(const char *format,...) __attribute__((format(printf
Verbose output function.
Definition ngcbSIO.cpp:2396
virtual ~ngcbSIO()
Destructor.
Definition ngcbSIO.cpp:104
int TcpConnect(const char *service)
Connect to a tcp/ip socket service.
Definition ngcbSIO.cpp:292
int Discover(const char *host, const char *name, const char *instance, int *port, int *protocol=NULL)
Discover command/reply service on host.
Definition ngcbSIO.cpp:615
virtual int SrvAborted()
Returns 1 when server-aborted flag is set.
Definition ngcbSIO.cpp:1305
int ExecVersion(const char *host)
Return version of exec daemon or -1 if version cannot be retrieved.
Definition ngcbSIO.cpp:518
int Port2Fd(int port)
Return server socket associated to service port.
Definition ngcbSIO.cpp:1425
int HaveCmdClient()
Returns 1 in case any command client connection is established.
Definition ngcbSIO.cpp:1615
int Exec(const char *host, const char *cmd, char *reply, int port=0)
Execute process on host.
Definition ngcbSIO.cpp:529
virtual int FdCB(int fd)
File descriptor event call-back.
Definition ngcbSIO.cpp:1994
virtual void ProtocolErr(const char *erms)
Call-back to log protocol errors.
Definition ngcbSIO.cpp:1886
int PortNum(int fd)
Get port number.
Definition ngcbSIO.cpp:243
int HaveEvtClient()
Returns 1 in case any event client connection is established.
Definition ngcbSIO.cpp:1693
virtual int RegisterService(int fd)
Register command service.
Definition ngcbSIO.cpp:1413
int TcpWrite(int fd, const void *buffer, size_t size)
Write buffer to tcp/ip socket.
Definition ngcbSIO.cpp:370
virtual int TimerCB()
Call-back when time expires.
Definition ngcbSIO.cpp:2025
void Disconnect()
Disconnect from command/reply service.
Definition ngcbSIO.cpp:876
virtual int ProtocolAscii(int fd)
ASCII protocol implementation.
Definition ngcbSIO.cpp:1856
int ConnectCmd(int srvSocket, int protocol=0)
Connect command client.
Definition ngcbSIO.cpp:1451
void CloseServer()
Close socket server.
Definition ngcbSIO.cpp:1212
int TcpRead(int fd, void *buffer, size_t size)
Read buffer from tcp/ip socket.
Definition ngcbSIO.cpp:355
int GetIpAddr(const char *name, char *ipAddr)
Get IP-address.
Definition ngcbSIO.cpp:245
virtual int ServiceConnect(int fd, int protocol)
Application specific service connection protocol.
Definition ngcbSIO.cpp:1600
int TcpReadRequest(int fd, char *request, int maxSize)
Read request from tcp/ip socket.
Definition ngcbSIO.cpp:441
int TcpService(const char *service=NULL)
Open a tcp/ip server socket.
Definition ngcbSIO.cpp:260
void ExecReply(int fd, char success, const char *reply)
Send reply to calling process.
Definition ngcbSIO.cpp:599
int GetProtocol(int fd)
Get protocol type assigned to a given socket.
Definition ngcbSIO.cpp:1740
int NumCmdClient()
Return number of command client connections.
Definition ngcbSIO.cpp:1607
int NumEvtClient()
Return number of event client connections.
Definition ngcbSIO.cpp:1685
int SrvCmdSocket()
Get actual command server socket.
Definition ngcbSIO.cpp:1308
void FdDetach(int fd)
Detach file-descriptor from the file descriptor event call-back.
Definition ngcbSIO.cpp:1974
char * ErrMsg()
Error message.
Definition ngcbSIO.hpp:103
int RequestReply(const char *request, char *reply=NULL)
Run request/reply protocol.
Definition ngcbSIO.cpp:1015
int ProtocolType()
Get preferred protocol type.
Definition ngcbSIO.cpp:1732
int TcpClose(int fd)
Close tcp/ip socket.
Definition ngcbSIO.cpp:317
int ReplyLine(char *line)
Read next reply line.
Definition ngcbSIO.cpp:1089
int ignoreSignal
Ignore signal in main-loop select call.
Definition ngcbSIO.hpp:100
void Unlock()
Unlock service main-loop.
Definition ngcbSIO.cpp:2062
void StopServer()
Terminate server main-loop.
Definition ngcbSIO.cpp:1294
void TimerOff()
Stop timmer.
Definition ngcbSIO.cpp:2017
int Connected()
Return client connection status.
Definition ngcbSIO.cpp:910
virtual int NewEventConnection(int fd)
Call-back for new event client connection.
Definition ngcbSIO.cpp:1700
char * Reply()
Reply buffer.
Definition ngcbSIO.hpp:215
int ClosePort(int port)
Close service port.
Definition ngcbSIO.cpp:1382
int TcpPuts(int fd, const char *string)
Put string to tcp/ip socket.
Definition ngcbSIO.cpp:385
int Connect(const char *host, int port, int protocol, int *evtPort)
Connect client to command/reply service.
Definition ngcbSIO.cpp:697
int logLevel
Loggin level.
Definition ngcbSIO.hpp:97
int TcpGets(int fd, char *string, int maxLen)
Get string from tcp/ip socket.
Definition ngcbSIO.cpp:413
int ConnectEvt(int srvSocket)
Connect event client.
Definition ngcbSIO.cpp:1622
int TcpAccept(int srvSocket)
Accept a tcp/ip connection request from a client.
Definition ngcbSIO.cpp:284
int verboseLevel
Verbose level.
Definition ngcbSIO.hpp:94
int SrvOpened()
Returns 1 when server is open.
Definition ngcbSIO.cpp:1303
virtual int UnregisterService(int fd)
Unregister command service.
Definition ngcbSIO.cpp:1419
void CleanupCmd(char *cmdLine, int *cmdLen)
Clean command string (remove extra spaces etc.)
Definition ngcbSIO.cpp:2349
int OpenPort(int port, int protocol=0)
Open service port.
Definition ngcbSIO.cpp:1311
void EnableStdIn()
Enable input via stdin.
Definition ngcbSIO.cpp:1902
int PrintReply(FILE *fd)
Print server reply to stream.
Definition ngcbSIO.cpp:1033
int Protocol(int fd)
Run protocol when socket becomes readable.
Definition ngcbSIO.cpp:1791
int OpenServer(int cmdPort, int evtPort=-1)
Open socket server.
Definition ngcbSIO.cpp:1133
#define ngcbSIO_PROTOCOL_ASCII
Definition ngcbSIO.hpp:57
#define ngcbSIO_MAX_PORT
Definition ngcbSIO.hpp:28
#define ngcbSIO_MAX_CON
Definition ngcbSIO.hpp:23
#define ngcbSIO_NFDS
Definition ngcbSIO.hpp:36
#define ngcbSIO_MAX_FD
Definition ngcbSIO.hpp:34
Definition ngcbSIO.hpp:63
ngcb_port_t()
Constructor.
Definition ngcbSIO.hpp:74
int port
Port number.
Definition ngcbSIO.hpp:71
int socket
Socket descriptor.
Definition ngcbSIO.hpp:65
int protocol
Service protocol.
Definition ngcbSIO.hpp:68