|
rllib
1
|
#include <rlcommandlineinterface.h>

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] |
| rlSocket * | sock |
| rlSpawn * | spawn |
| rlSerial * | tty |
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.
| rlCommandlineInterface::rlCommandlineInterface | ( | ) |
Definition at line 23 of file rlcommandlineinterface.cpp.
| rlCommandlineInterface::~rlCommandlineInterface | ( | ) | [virtual] |
| int rlCommandlineInterface::printf | ( | const char * | format, |
| ... | |||
| ) |
Definition at line 180 of file rlcommandlineinterface.cpp.
{
va_list ap;
va_start(ap,format);
int ret = rlvsnprintf(line, sizeof(line) - 1, format, ap);
va_end(ap);
if(ret < 0) return ret;
if(spawn != NULL)
{
return spawn->printf("%s",line);
}
else if(sock != NULL)
{
if(sock->isConnected() == 0) return -1;
int ret = sock->printf("%s",line);
return ret;
}
else if(tty != NULL)
{
return tty->writeBlock((unsigned char *) line, strlen(line));
}
else
{
int ret = ::printf("%s",line);
fflush(stdout);
return ret;
}
}
| int rlCommandlineInterface::readBlock | ( | void * | buf, |
| int | len, | ||
| int | timeout = 0 |
||
| ) |
Definition at line 107 of file rlcommandlineinterface.cpp.
{
if(spawn != NULL)
{
unsigned char *cbuf = (unsigned char *) buf;
int i = 0;
while(i<len)
{
if(timeout > 0)
{
if(spawn->select(timeout) == 0) return -1;
}
cbuf[i++] = spawn->getchar();
}
return len;
}
else if(sock != NULL)
{
if(sock->isConnected() == 0) return -1;
int ret = sock->read(buf,len,timeout);
if(ret <= 0)
{
sock->disconnect();
return -1;
}
return len;
}
else if(tty != NULL)
{
int timout = 0;
if(timeout > 0) timout = timeout;
return tty->readBlock ((unsigned char *) buf, len, timout);
}
else
{
int ret = read(0,buf,len);
return ret;
}
}
| const char * rlCommandlineInterface::readLine | ( | int | timeout = 0 | ) |
Definition at line 147 of file rlcommandlineinterface.cpp.
{
if(spawn != NULL)
{
if(timeout > 0)
{
if(spawn->select(timeout) == 0) return NULL;
}
return spawn->readLine();
}
else if(sock != NULL)
{
if(sock->isConnected() == 0) return NULL;
int ret = sock->readStr(line,sizeof(line)-1,timeout);
if(ret <= 0)
{
sock->disconnect();
return NULL;
}
return line;
}
else if(tty != NULL)
{
int ret = tty->readLine((unsigned char *) line,sizeof(line)-1, timeout);
if(ret <= 0) return NULL;
return line;
}
else
{
return fgets(line,sizeof(line)-1,stdin);
}
}
| int rlCommandlineInterface::start | ( | const char * | how, |
| const char * | command = NULL |
||
| ) |
Definition at line 36 of file rlcommandlineinterface.cpp.
{
if(sock != NULL) delete sock;
sock = NULL;
if(spawn != NULL) delete spawn;
spawn = NULL;
tty = NULL;
if(strcmp(how,"pipe") == 0)
{
if(command == NULL) return -1;
spawn = new rlSpawn();
int ret = spawn->spawn(command);
if(ret < 0)
{
delete spawn;
spawn = NULL;
return -1;
}
return ret;
}
else if(strcmp(how,"stdio") == 0)
{
return 1;
}
else
{
rlString rlhow(how);
char *cptr, *host;
host = rlhow.text();
cptr = strchr(host,':');
if(cptr == NULL) return -1;
*cptr = '\0'; cptr++;
int port = atoi(cptr);
if(strcmp(host,"server.localhost") == 0)
{
sock = new rlSocket(host,port,0);
}
else
{
if(strcmp(host,"localhost") == 0 && command != NULL)
{
rlString cmd(command);
#ifdef RLUNIX
cmd += " &";
#endif
rlsystem(cmd.text());
}
sock = new rlSocket(host,port,1);
}
for(int itry=0; itry<10; itry++)
{
sock->connect();
if(sock->isConnected()) return sock->s;
rlsleep(10);
}
return -1;
}
}
| int rlCommandlineInterface::start | ( | rlSerial * | tty | ) |
| int rlCommandlineInterface::writeBlock | ( | void * | buf, |
| int | len | ||
| ) |
Definition at line 210 of file rlcommandlineinterface.cpp.
char rlCommandlineInterface::line[rl_PRINTF_LENGTH] [private] |
Definition at line 54 of file rlcommandlineinterface.h.
rlSocket* rlCommandlineInterface::sock [private] |
Definition at line 55 of file rlcommandlineinterface.h.
rlSpawn* rlCommandlineInterface::spawn [private] |
Definition at line 56 of file rlcommandlineinterface.h.
rlSerial* rlCommandlineInterface::tty [private] |
Definition at line 57 of file rlcommandlineinterface.h.
1.7.5.1