rllib  1
Public Member Functions | Private Attributes | List of all members
rlCommandlineInterface Class Reference

#include <rlcommandlineinterface.h>

Collaboration diagram for rlCommandlineInterface:
Collaboration graph
[legend]

Public Member Functions

 rlCommandlineInterface ()
 
virtual ~rlCommandlineInterface ()
 
int start (const char *how, const char *command=NULL)
 
int start (rlSerial *tty)
 
const char * readLine (int timeout=0)
 
int readBlock (void *buf, int len, int timeout=0)
 
int printf (const char *format,...)
 
int writeBlock (void *buf, int len)
 

Private Attributes

char line [rl_PRINTF_LENGTH]
 
rlSocketsock
 
rlSpawnspawn
 
rlSerialtty
 

Detailed Description

Commandline interface that allows applications to communicate over pipe || socket || serial line || stdio .
The parameters of start() are as follows:
start("stdio");                    // use stdin and stdout
start("pipe","command");           // run "command" and connect it's stdio with us (runs on unix only)
start("host:5050");                // connect to "host" on port 5050 and communicate with the server (tip: define a server with xinted on "host" port 5050)
start("localhost:5050","command"); // run "command" and then try to connect to "localhost" port 5050 for communication with "command"
start("server.localhost:5050");    // act as a server on "localhost" port 5050 (can serve only 1 client. if you want to serve more clients use rlSocket)
start(tty);                        // use serial interface for communication
Return values:
start()      returns -1 on error
readLine()   returns the read string or NULL
readBlock()  returns the number of read bytes or -1 on error
printf()     returns the number of written characters or -1 on error
writeBlock() returns the number of written bytes or -1 on error

Definition at line 42 of file rlcommandlineinterface.h.

Constructor & Destructor Documentation

◆ rlCommandlineInterface()

rlCommandlineInterface::rlCommandlineInterface ( )

Definition at line 23 of file rlcommandlineinterface.cpp.

24 {
25  sock = NULL;
26  spawn = NULL;
27  tty = NULL;
28 }

◆ ~rlCommandlineInterface()

rlCommandlineInterface::~rlCommandlineInterface ( )
virtual

Definition at line 30 of file rlcommandlineinterface.cpp.

31 {
32  if(sock != NULL) delete sock;
33  if(spawn != NULL) delete spawn;
34 }

Member Function Documentation

◆ printf()

int rlCommandlineInterface::printf ( const char *  format,
  ... 
)

Definition at line 180 of file rlcommandlineinterface.cpp.

181 {
182  va_list ap;
183  va_start(ap,format);
184  int ret = rlvsnprintf(line, sizeof(line) - 1, format, ap);
185  va_end(ap);
186  if(ret < 0) return ret;
187 
188  if(spawn != NULL)
189  {
190  return spawn->printf("%s",line);
191  }
192  else if(sock != NULL)
193  {
194  if(sock->isConnected() == 0) return -1;
195  int ret = sock->printf("%s",line);
196  return ret;
197  }
198  else if(tty != NULL)
199  {
200  return tty->writeBlock((unsigned char *) line, strlen(line));
201  }
202  else
203  {
204  int ret = ::printf("%s",line);
205  fflush(stdout);
206  return ret;
207  }
208 }
int printf(const char *format,...)
int printf(const char *format,...)
Definition: rlspawn.cpp:402
int rlvsnprintf(char *text, int len, const char *format, va_list ap)
Definition: rlcutil.cpp:197
char line[rl_PRINTF_LENGTH]
int isConnected()
Definition: rlsocket.cpp:559
int writeBlock(const unsigned char *buf, int len)
Definition: rlserial.cpp:584
int printf(const char *format,...)
Definition: rlsocket.cpp:586

◆ readBlock()

int rlCommandlineInterface::readBlock ( void *  buf,
int  len,
int  timeout = 0 
)

Definition at line 107 of file rlcommandlineinterface.cpp.

108 {
109  if(spawn != NULL)
110  {
111  unsigned char *cbuf = (unsigned char *) buf;
112  int i = 0;
113  while(i<len)
114  {
115  if(timeout > 0)
116  {
117  if(spawn->select(timeout) == 0) return -1;
118  }
119  cbuf[i++] = spawn->getchar();
120  }
121  return len;
122  }
123  else if(sock != NULL)
124  {
125  if(sock->isConnected() == 0) return -1;
126  int ret = sock->read(buf,len,timeout);
127  if(ret <= 0)
128  {
129  sock->disconnect();
130  return -1;
131  }
132  return len;
133  }
134  else if(tty != NULL)
135  {
136  int timout = 0;
137  if(timeout > 0) timout = timeout;
138  return tty->readBlock ((unsigned char *) buf, len, timout);
139  }
140  else
141  {
142  int ret = read(0,buf,len);
143  return ret;
144  }
145 }
int select(int timeout=50)
Definition: rlspawn.cpp:456
int readBlock(unsigned char *buf, int len, int timeout=-1)
Definition: rlserial.cpp:498
int getchar()
Definition: rlspawn.cpp:355
int read(void *buf, int len, int timeout=0)
Definition: rlsocket.cpp:191
int disconnect()
Definition: rlsocket.cpp:545
int isConnected()
Definition: rlsocket.cpp:559

◆ readLine()

const char * rlCommandlineInterface::readLine ( int  timeout = 0)

Definition at line 147 of file rlcommandlineinterface.cpp.

148 {
149  if(spawn != NULL)
150  {
151  if(timeout > 0)
152  {
153  if(spawn->select(timeout) == 0) return NULL;
154  }
155  return spawn->readLine();
156  }
157  else if(sock != NULL)
158  {
159  if(sock->isConnected() == 0) return NULL;
160  int ret = sock->readStr(line,sizeof(line)-1,timeout);
161  if(ret <= 0)
162  {
163  sock->disconnect();
164  return NULL;
165  }
166  return line;
167  }
168  else if(tty != NULL)
169  {
170  int ret = tty->readLine((unsigned char *) line,sizeof(line)-1, timeout);
171  if(ret <= 0) return NULL;
172  return line;
173  }
174  else
175  {
176  return fgets(line,sizeof(line)-1,stdin);
177  }
178 }
int select(int timeout=50)
Definition: rlspawn.cpp:456
int readLine(unsigned char *buf, int maxlen, int timeout=1000)
Definition: rlserial.cpp:687
int readStr(char *buf, int len, int timeout=0)
Definition: rlsocket.cpp:224
const char * readLine()
Definition: rlspawn.cpp:307
char line[rl_PRINTF_LENGTH]
int disconnect()
Definition: rlsocket.cpp:545
int isConnected()
Definition: rlsocket.cpp:559

◆ start() [1/2]

int rlCommandlineInterface::start ( const char *  how,
const char *  command = NULL 
)

Definition at line 36 of file rlcommandlineinterface.cpp.

37 {
38  if(sock != NULL) delete sock;
39  sock = NULL;
40  if(spawn != NULL) delete spawn;
41  spawn = NULL;
42  tty = NULL;
43 
44  if(strcmp(how,"pipe") == 0)
45  {
46  if(command == NULL) return -1;
47  spawn = new rlSpawn();
48  int ret = spawn->spawn(command);
49  if(ret < 0)
50  {
51  delete spawn;
52  spawn = NULL;
53  return -1;
54  }
55  return ret;
56  }
57  else if(strcmp(how,"stdio") == 0)
58  {
59  return 1;
60  }
61  else
62  {
63  rlString rlhow(how);
64  char *cptr, *host;
65  host = rlhow.text();
66  cptr = strchr(host,':');
67  if(cptr == NULL) return -1;
68  *cptr = '\0'; cptr++;
69  int port = atoi(cptr);
70  if(strcmp(host,"server.localhost") == 0)
71  {
72  sock = new rlSocket(host,port,0);
73  }
74  else
75  {
76  if(strcmp(host,"localhost") == 0 && command != NULL)
77  {
78  rlString cmd(command);
79 #ifdef RLUNIX
80  cmd += " &";
81 #endif
82  rlsystem(cmd.text());
83  }
84  sock = new rlSocket(host,port,1);
85  }
86  for(int itry=0; itry<10; itry++)
87  {
88  sock->connect();
89  if(sock->isConnected()) return sock->s;
90  rlsleep(10);
91  }
92  return -1;
93  }
94 }
void rlsleep(long msec)
Definition: rlwthread.cpp:396
int connect()
Definition: rlsocket.cpp:321
int s
Definition: rlsocket.h:197
int isConnected()
Definition: rlsocket.cpp:559
int rlsystem(const char *command)
Definition: rlcutil.cpp:558
int spawn(const char *command)
Definition: rlspawn.cpp:168

◆ start() [2/2]

int rlCommandlineInterface::start ( rlSerial tty)

Definition at line 96 of file rlcommandlineinterface.cpp.

97 {
98  if(sock != NULL) delete sock;
99  sock = NULL;
100  if(spawn != NULL) delete spawn;
101  spawn = NULL;
102  tty = ttyinterface;
103  if(tty == NULL) return -1;
104  return 1;
105 }

◆ writeBlock()

int rlCommandlineInterface::writeBlock ( void *  buf,
int  len 
)

Definition at line 210 of file rlcommandlineinterface.cpp.

211 {
212  if(spawn != NULL)
213  {
214  return spawn->write((const char *) buf, len);
215  }
216  else if(sock != NULL)
217  {
218  if(sock->isConnected() == 0) return -1;
219  return sock->write(buf,len);
220  }
221  else if(tty != NULL)
222  {
223  return tty->writeBlock((unsigned char *) buf, len);
224  }
225  else
226  {
227  return write(1,buf,len);
228  }
229 }
int write(const void *buf, int len)
Definition: rlsocket.cpp:292
int write(const char *buf, int len)
Definition: rlspawn.cpp:385
int isConnected()
Definition: rlsocket.cpp:559
int writeBlock(const unsigned char *buf, int len)
Definition: rlserial.cpp:584

Member Data Documentation

◆ line

char rlCommandlineInterface::line[rl_PRINTF_LENGTH]
private

Definition at line 54 of file rlcommandlineinterface.h.

◆ sock

rlSocket* rlCommandlineInterface::sock
private

Definition at line 55 of file rlcommandlineinterface.h.

◆ spawn

rlSpawn* rlCommandlineInterface::spawn
private

Definition at line 56 of file rlcommandlineinterface.h.

◆ tty

rlSerial* rlCommandlineInterface::tty
private

Definition at line 57 of file rlcommandlineinterface.h.


The documentation for this class was generated from the following files: