ngc 1.5.0
 
Loading...
Searching...
No Matches
ngcbSIO.hpp
Go to the documentation of this file.
1
8#ifndef ngcbSIO_H
9#define ngcbSIO_H
10
11
12#ifndef __cplusplus
13#error This is a C++ include file and cannot be used from plain C
14#endif
15
16
17#include <ngc/core/ngcb.h>
18#include <ngc/core/ngcbSocket.h>
19#include <poll.h>
20
24#define ngcbSIO_MAX_CON 16
25
29#define ngcbSIO_MAX_PORT 4
30
35#define ngcbSIO_MAX_FD 64
36
37#define ngcbSIO_NFDS (ngcbSIO_MAX_FD + (ngcbSIO_MAX_CON * 2) + ngcbSIO_MAX_PORT + 4)
38
39/*
40 * Tokens for system control
41 */
42#define ngcbSIO_CMD_EXIT 0x00000010 //< EXIT command
43#define ngcbSIO_CMD_RESET 0x00000020 //< RESET command
44#define ngcbSIO_CMD_PING 0x00000040 //< PING command
45#define ngcbSIO_CMD_CONNECT 0x00000080 //< CONNECT command
46#define ngcbSIO_CMD_DISCONNECT 0x00000100 //< DISCONNECT command
47#define ngcbSIO_CMD_TIMEOUT 0x00000200 //< TIMEOUT command
48#define ngcbSIO_CMD_SIMULAT 0x00000400 //< SIMULATION command
49#define ngcbSIO_CMD_STOPSIM 0x00000800 //< STOP SIMULATION command
50#define ngcbSIO_CMD_STATUS 0x00001000 //< STATUS command
51
52
53/*
54 * Supported protocols
55 */
56#define ngcbSIO_PROTOCOL_GENERIC 0 //< Generic protocol
57#define ngcbSIO_PROTOCOL_BIN 1 //< Binary protocol
58#define ngcbSIO_PROTOCOL_ASCII 2 //< ASCII protocol
59#define ngcbSIO_PROTOCOL_ASCII0 3 //< raw ASCII (no startup messages)
60
66 int socket;
67
70
72 int port;
73
76 socket = -1; // not open
77 protocol = 0; // use standard protocol
78 port = 0;
79 }
80};
81
86{
87public:
89 ngcbSIO();
90
92 virtual ~ngcbSIO();
93
96
99
102
104 char *ErrMsg() {return (erms_);}
105
106
107 /*
108 * Common socket i/o
109 */
110
112 int PortNum(int fd);
113
115 int GetIpAddr(const char *name, char *ipAddr);
116
118 int WaitSocket(int fd, int timeout);
119
121 int TcpService(const char *service = NULL);
122
124 int TcpService(int port);
125
127 int TcpAccept(int srvSocket);
128
130 int TcpConnect(const char *service);
131
133 int TcpConnect(int port, const char *host = NULL);
134
136 int TcpClose(int fd);
137
139 int TcpRead(int fd, void *buffer, size_t size);
140
142 int TcpWrite(int fd, const void *buffer, size_t size);
143
145 int TcpPuts(int fd, const char *string);
146
148 int TcpGets(int fd, char *string, int maxLen);
149
151 int TcpReadRequest(int fd, char *request, int maxSize);
152
154 int TcpWriteReply(int fd, const char *reply, int success);
155
157 int ExecVersion(const char *host);
158
160 int Exec(const char *host, const char *cmd, char *reply, int port = 0);
161
163 int ExecXt(const char *host, const char *cmd, char *reply,
164 const char *geom = NULL);
165
167 int Kill(const char *host, int pid);
168
170 void ExecReply(int fd, char success, const char *reply);
171
172
173 /*
174 * Command/event client interface
175 */
176
178 int Discover(const char *host, const char *name, const char *instance,
179 int *port, int *protocol = NULL);
180
182 int Discover(const char *name, const char *instance, int *port,
183 int *protocol = NULL);
184
186 int Connect(const char *host, int port, int protocol, int *evtPort);
187
189 int Connect(const char *host, int port, int protocol);
190
192 int Connect(const char *service, int protocol = ngcbSIO_PROTOCOL_ASCII);
193
195 void Disconnect();
196
198 int Connected();
199
201 void ReplyBufferSize(size_t size);
202
204 int SendRequest(const char *request);
205
207 int GetReply(char *s = NULL);
208
210 int PrintReply(FILE *fd);
211
213 int ReplyLine(char *line);
214
216 char *Reply() {return (replyBuffer_);}
217
219 int RequestReply(const char *request, char *reply = NULL);
220
221 /*
222 * Server interface
223 */
224
226 int OpenServer(int cmdPort, int evtPort = -1);
227
229 void CloseServer();
230
232 void StopServer();
233
235 int SrvOpened();
236
238 virtual void SetSrvAborted(int flag);
239
241 virtual int SrvAborted();
242
244 int SrvCmdPort();
245
247 int SrvEvtPort();
248
250 int SrvCmdSocket();
251
253 int SrvEvtSocket();
254
256 int OpenPort(int port, int protocol = 0);
257
259 int ClosePort(int port);
260
262 int Port2Fd(int port);
263
265 int Fd2Port(int fd);
266
268 int ConnectCmd(int srvSocket, int protocol = 0);
269
271 int NumCmdClient();
272
274 int HaveCmdClient();
275
277 int ConnectEvt(int srvSocket);
278
280 int NumEvtClient();
281
283 int HaveEvtClient();
284
286 void Broadcast(const void *buffer, size_t size);
287
289 void Broadcast(const char *s);
290
292 int Protocol(int fd);
293
295 int ProtocolType();
296
298 void ProtocolType(int protocol);
299
301 int GetProtocol(int fd);
302
304 void EnableStdIn();
305
307 void DisableStdIn();
308
310 int StdIn();
311
313 int FdAttach(int fd);
314
316 void FdDetach(int fd);
317
319 void TimerSet(int ms);
320
322 void TimerOn();
323
325 void TimerOff();
326
328 int MainLoop();
329
331 void Lock();
332
334 void Unlock();
335
337 void CleanupCmd(char *cmdLine, int *cmdLen);
338
340 virtual void Verbose(const char *format, ...)
341 __attribute__ ((format(printf, 2, 3)));
342
344 virtual void Verbose(int level, const char *format, ...)
345 __attribute__ ((format(printf, 3, 4)));
346
348 virtual void Log(const char *format, ...)
349 __attribute__ ((format(printf, 2, 3)));
350
352 virtual void Log(int level, const char *format, ...)
353 __attribute__ ((format(printf, 3, 4)));
354
356 virtual void LogMsg(const char *msg);
357
358protected:
360 virtual int ProtocolBin(int fd);
361
363 virtual int ProtocolAscii(int fd);
364
366 virtual int Service(int fd, int protocol);
367
369 virtual int ServiceConnect(int fd, int protocol);
370
372 virtual void ProtocolErr(const char *erms);
373
375 virtual int RegisterService(int fd);
376
378 virtual int UnregisterService(int fd);
379
381 virtual int NewEventConnection(int fd);
382
384 virtual int FdCB(int fd);
385
387 virtual int TimerCB();
388
390 virtual void MainLoopInit();
391
392private:
394 char erms_[256];
395
397 char versionString_[256];
398
400 int endian_;
401
403 int clientStat_;
404
406 FILE *fdIn_;
407
409 FILE *fdOut_;
410
412 int fdLock_[2];
413
415 char *replyBuffer_;
416
418 size_t replyBufferSize_;
419
421 int srvCmdPort_;
422
424 int srvEvtPort_;
425
428
430 int protocol_;
431
433 int protocolStd_;
434
436 int srvOpen_;
437
439 int srvAbort_;
440
442 int execReplyDone_;
443
445 int srvCmdSocket_;
446
448 int srvEvtSocket_;
449
451 int stdin_;
452
454 struct pollfd fds_[ngcbSIO_NFDS];
455
457 int nfds_;
458
460 int fd_[ngcbSIO_MAX_FD];
461
463 int cmdSocket_[ngcbSIO_MAX_CON];
464
466 int evtSocket_[ngcbSIO_MAX_CON];
467
469 int cmdProtocol_[ngcbSIO_MAX_CON];
470
472 struct timeval timerVal_;
473
475 struct timeval ts_;
476
478 int timerEnabled_;
479
481 int CmdSocketIdx_(int fd);
482
484 int EvtSocketIdx_(int fd);
485
487 int FdIsSet_(int fd);
488
490 void FdSet_(int fd);
491
493 void FdClr_(int fd);
494
496 int FdValid_(int fd);
497
499 void Host_(const char *host, char *name, int n = 64);
500};
501
502#endif
void ReplyBufferSize(size_t size)
Set reply buffer size of the client interface.
Definition ngcbSIO.cpp:913
int WaitSocket(int fd, int timeout)
Wait with timeout until socket becomes readable.
Definition ngcbSIO.cpp:341
int MainLoop()
Enter socket server main-loop.
Definition ngcbSIO.cpp:2074
virtual void virtual void virtual void virtual void virtual void LogMsg(const char *msg)
Message logger.
Definition ngcbSIO.cpp:2467
int TcpWriteReply(int fd, const char *reply, int success)
Write reply to tcp/ip socket.
Definition ngcbSIO.cpp:479
void Broadcast(const void *buffer, size_t size)
Broadcat buffer to all event clients.
Definition ngcbSIO.cpp:1710
int SendRequest(const char *request)
Send request to server.
Definition ngcbSIO.cpp:919
int GetReply(char *s=NULL)
Copy reply from server to string.
Definition ngcbSIO.cpp:942
virtual void SetSrvAborted(int flag)
Set server-aborted flag.
Definition ngcbSIO.cpp:1305
void TimerOn()
Enable timer.
Definition ngcbSIO.cpp:2004
void Lock()
Lock service main-loop.
Definition ngcbSIO.cpp:2051
void DisableStdIn()
Disable input via stdin.
Definition ngcbSIO.cpp:1915
int ExecXt(const char *host, const char *cmd, char *reply, const char *geom=NULL)
Execute process on host (in new terminal)
Definition ngcbSIO.cpp:553
virtual int ProtocolBin(int fd)
Binary protocol implementation.
Definition ngcbSIO.cpp:1850
virtual int Service(int fd, int protocol)
Application specific service protocol.
Definition ngcbSIO.cpp:1864
int Kill(const char *host, int pid)
Terminate process on host.
Definition ngcbSIO.cpp:577
virtual void virtual void virtual void Log(const char *format,...) __attribute__((format(printf
Logging function.
Definition ngcbSIO.cpp:2437
ngcbSIO()
Constructor.
Definition ngcbSIO.cpp:31
int Fd2Port(int fd)
Return service port associated to server socket.
Definition ngcbSIO.cpp:1439
int SrvCmdPort()
Get actual server command port.
Definition ngcbSIO.cpp:1307
void TimerSet(int ms)
Set timer (in milliseconds)
Definition ngcbSIO.cpp:2034
int FdAttach(int fd)
Attach file-descriptor to the file descriptor event call-back.
Definition ngcbSIO.cpp:1928
virtual void MainLoopInit()
Indicate main-loop initialization.
Definition ngcbSIO.cpp:2072
int SrvEvtPort()
Get actual server event port.
Definition ngcbSIO.cpp:1308
int SrvEvtSocket()
Get actual event server socket.
Definition ngcbSIO.cpp:1310
int StdIn()
Get file-descriptor associated to the standard input.
Definition ngcbSIO.cpp:1895
virtual void Verbose(const char *format,...) __attribute__((format(printf
Verbose output function.
Definition ngcbSIO.cpp:2397
virtual ~ngcbSIO()
Destructor.
Definition ngcbSIO.cpp:105
int TcpConnect(const char *service)
Connect to a tcp/ip socket service.
Definition ngcbSIO.cpp:293
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:616
virtual int SrvAborted()
Returns 1 when server-aborted flag is set.
Definition ngcbSIO.cpp:1306
int ExecVersion(const char *host)
Return version of exec daemon or -1 if version cannot be retrieved.
Definition ngcbSIO.cpp:519
int Port2Fd(int port)
Return server socket associated to service port.
Definition ngcbSIO.cpp:1426
int HaveCmdClient()
Returns 1 in case any command client connection is established.
Definition ngcbSIO.cpp:1616
int Exec(const char *host, const char *cmd, char *reply, int port=0)
Execute process on host.
Definition ngcbSIO.cpp:530
virtual int FdCB(int fd)
File descriptor event call-back.
Definition ngcbSIO.cpp:1995
virtual void ProtocolErr(const char *erms)
Call-back to log protocol errors.
Definition ngcbSIO.cpp:1887
int PortNum(int fd)
Get port number.
Definition ngcbSIO.cpp:244
int HaveEvtClient()
Returns 1 in case any event client connection is established.
Definition ngcbSIO.cpp:1694
virtual int RegisterService(int fd)
Register command service.
Definition ngcbSIO.cpp:1414
int TcpWrite(int fd, const void *buffer, size_t size)
Write buffer to tcp/ip socket.
Definition ngcbSIO.cpp:371
virtual int TimerCB()
Call-back when time expires.
Definition ngcbSIO.cpp:2026
void Disconnect()
Disconnect from command/reply service.
Definition ngcbSIO.cpp:877
virtual int ProtocolAscii(int fd)
ASCII protocol implementation.
Definition ngcbSIO.cpp:1857
int ConnectCmd(int srvSocket, int protocol=0)
Connect command client.
Definition ngcbSIO.cpp:1452
void CloseServer()
Close socket server.
Definition ngcbSIO.cpp:1213
int TcpRead(int fd, void *buffer, size_t size)
Read buffer from tcp/ip socket.
Definition ngcbSIO.cpp:356
int GetIpAddr(const char *name, char *ipAddr)
Get IP-address.
Definition ngcbSIO.cpp:246
virtual int ServiceConnect(int fd, int protocol)
Application specific service connection protocol.
Definition ngcbSIO.cpp:1601
int TcpReadRequest(int fd, char *request, int maxSize)
Read request from tcp/ip socket.
Definition ngcbSIO.cpp:442
int TcpService(const char *service=NULL)
Open a tcp/ip server socket.
Definition ngcbSIO.cpp:261
void ExecReply(int fd, char success, const char *reply)
Send reply to calling process.
Definition ngcbSIO.cpp:600
int GetProtocol(int fd)
Get protocol type assigned to a given socket.
Definition ngcbSIO.cpp:1741
int NumCmdClient()
Return number of command client connections.
Definition ngcbSIO.cpp:1608
int NumEvtClient()
Return number of event client connections.
Definition ngcbSIO.cpp:1686
int SrvCmdSocket()
Get actual command server socket.
Definition ngcbSIO.cpp:1309
void FdDetach(int fd)
Detach file-descriptor from the file descriptor event call-back.
Definition ngcbSIO.cpp:1975
char * ErrMsg()
Error message.
Definition ngcbSIO.hpp:104
int RequestReply(const char *request, char *reply=NULL)
Run request/reply protocol.
Definition ngcbSIO.cpp:1016
int ProtocolType()
Get preferred protocol type.
Definition ngcbSIO.cpp:1733
int TcpClose(int fd)
Close tcp/ip socket.
Definition ngcbSIO.cpp:318
int ReplyLine(char *line)
Read next reply line.
Definition ngcbSIO.cpp:1090
int ignoreSignal
Ignore signal in main-loop select call.
Definition ngcbSIO.hpp:101
void Unlock()
Unlock service main-loop.
Definition ngcbSIO.cpp:2063
void StopServer()
Terminate server main-loop.
Definition ngcbSIO.cpp:1295
void TimerOff()
Stop timmer.
Definition ngcbSIO.cpp:2018
int Connected()
Return client connection status.
Definition ngcbSIO.cpp:911
virtual int NewEventConnection(int fd)
Call-back for new event client connection.
Definition ngcbSIO.cpp:1701
char * Reply()
Reply buffer.
Definition ngcbSIO.hpp:216
int ClosePort(int port)
Close service port.
Definition ngcbSIO.cpp:1383
int TcpPuts(int fd, const char *string)
Put string to tcp/ip socket.
Definition ngcbSIO.cpp:386
int Connect(const char *host, int port, int protocol, int *evtPort)
Connect client to command/reply service.
Definition ngcbSIO.cpp:698
int logLevel
Loggin level.
Definition ngcbSIO.hpp:98
int TcpGets(int fd, char *string, int maxLen)
Get string from tcp/ip socket.
Definition ngcbSIO.cpp:414
int ConnectEvt(int srvSocket)
Connect event client.
Definition ngcbSIO.cpp:1623
int TcpAccept(int srvSocket)
Accept a tcp/ip connection request from a client.
Definition ngcbSIO.cpp:285
int verboseLevel
Verbose level.
Definition ngcbSIO.hpp:95
int SrvOpened()
Returns 1 when server is open.
Definition ngcbSIO.cpp:1304
virtual int UnregisterService(int fd)
Unregister command service.
Definition ngcbSIO.cpp:1420
void CleanupCmd(char *cmdLine, int *cmdLen)
Clean command string (remove extra spaces etc.)
Definition ngcbSIO.cpp:2350
int OpenPort(int port, int protocol=0)
Open service port.
Definition ngcbSIO.cpp:1312
void EnableStdIn()
Enable input via stdin.
Definition ngcbSIO.cpp:1903
int PrintReply(FILE *fd)
Print server reply to stream.
Definition ngcbSIO.cpp:1034
int Protocol(int fd)
Run protocol when socket becomes readable.
Definition ngcbSIO.cpp:1792
int OpenServer(int cmdPort, int evtPort=-1)
Open socket server.
Definition ngcbSIO.cpp:1134
#define ngcbSIO_PROTOCOL_ASCII
Definition ngcbSIO.hpp:58
#define ngcbSIO_MAX_PORT
Definition ngcbSIO.hpp:29
#define ngcbSIO_MAX_CON
Definition ngcbSIO.hpp:24
#define ngcbSIO_NFDS
Definition ngcbSIO.hpp:37
#define ngcbSIO_MAX_FD
Definition ngcbSIO.hpp:35
Definition ngcbSIO.hpp:64
ngcb_port_t()
Constructor.
Definition ngcbSIO.hpp:75
int port
Port number.
Definition ngcbSIO.hpp:72
int socket
Socket descriptor.
Definition ngcbSIO.hpp:66
int protocol
Service protocol.
Definition ngcbSIO.hpp:69