2004-02-10 15:35:48 +00:00
|
|
|
// $Id$
|
|
|
|
// g++ -O2 -g -pedantic -Wall fgfsclient.cxx -o fgfsclient -lstdc++
|
2006-11-23 09:26:43 +00:00
|
|
|
// USAGE: ./fgfsclient [hostname [port]]
|
2006-02-04 23:07:41 +00:00
|
|
|
// Public Domain
|
2004-02-10 15:35:48 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2021-02-27 03:11:34 +00:00
|
|
|
#include <string.h>
|
2004-02-10 15:35:48 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2006-11-22 13:47:15 +00:00
|
|
|
|
2021-02-27 03:11:34 +00:00
|
|
|
const char* HOST = "localhost";
|
2006-11-23 09:26:43 +00:00
|
|
|
const unsigned PORT = 5501;
|
|
|
|
const int BUFLEN = 256;
|
2004-02-10 15:35:48 +00:00
|
|
|
|
|
|
|
|
2021-02-27 03:11:34 +00:00
|
|
|
class FGFSSocket
|
|
|
|
{
|
2006-11-22 13:47:15 +00:00
|
|
|
public:
|
2021-02-27 03:11:34 +00:00
|
|
|
FGFSSocket(const char* name, unsigned port);
|
|
|
|
~FGFSSocket();
|
2004-02-10 15:35:48 +00:00
|
|
|
|
2021-02-27 03:11:34 +00:00
|
|
|
int write(const char* msg, ...);
|
|
|
|
const char* read(void);
|
|
|
|
inline void flush(void);
|
|
|
|
void settimeout(unsigned t) { _timeout = t; }
|
2006-11-22 13:47:15 +00:00
|
|
|
|
|
|
|
private:
|
2021-02-27 03:11:34 +00:00
|
|
|
int close(void);
|
2006-11-22 13:47:15 +00:00
|
|
|
|
2021-02-27 03:11:34 +00:00
|
|
|
int _sock;
|
|
|
|
bool _connected;
|
|
|
|
unsigned _timeout;
|
|
|
|
char _buffer[BUFLEN];
|
|
|
|
bool _isShutdown = false;
|
2004-02-10 15:35:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-02-27 03:11:34 +00:00
|
|
|
FGFSSocket::FGFSSocket(const char* hostname = HOST, unsigned port = PORT) : _sock(-1),
|
|
|
|
_connected(false),
|
|
|
|
_timeout(1)
|
2004-02-10 15:35:48 +00:00
|
|
|
{
|
2021-02-27 03:11:34 +00:00
|
|
|
_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
if (_sock < 0)
|
|
|
|
throw("FGFSSocket/socket");
|
|
|
|
|
|
|
|
struct hostent* hostinfo;
|
|
|
|
hostinfo = gethostbyname(hostname);
|
|
|
|
if (!hostinfo) {
|
|
|
|
close();
|
|
|
|
throw("FGFSSocket/gethostbyname: unknown host");
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sockaddr_in serv_addr;
|
|
|
|
serv_addr.sin_family = AF_INET;
|
|
|
|
serv_addr.sin_port = htons(port);
|
|
|
|
serv_addr.sin_addr = *(struct in_addr*)hostinfo->h_addr;
|
|
|
|
|
|
|
|
if (connect(_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
|
|
|
|
close();
|
|
|
|
throw("FGFSSocket/connect");
|
|
|
|
}
|
|
|
|
_connected = true;
|
|
|
|
try {
|
|
|
|
write("data");
|
|
|
|
} catch (...) {
|
|
|
|
close();
|
|
|
|
throw;
|
|
|
|
}
|
2004-02-10 15:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-22 13:47:15 +00:00
|
|
|
FGFSSocket::~FGFSSocket()
|
|
|
|
{
|
2021-02-27 03:11:34 +00:00
|
|
|
_isShutdown = true;
|
|
|
|
close();
|
2006-11-22 13:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-10 15:35:48 +00:00
|
|
|
int FGFSSocket::close(void)
|
|
|
|
{
|
2021-02-27 03:11:34 +00:00
|
|
|
if (_connected)
|
|
|
|
write("quit");
|
|
|
|
if (_sock < 0)
|
|
|
|
return 0;
|
|
|
|
int ret = ::close(_sock);
|
|
|
|
_sock = -1;
|
|
|
|
return ret;
|
2004-02-10 15:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-27 03:11:34 +00:00
|
|
|
int FGFSSocket::write(const char* msg, ...)
|
2004-02-10 15:35:48 +00:00
|
|
|
{
|
2021-02-27 03:11:34 +00:00
|
|
|
va_list va;
|
|
|
|
ssize_t len;
|
|
|
|
char buf[BUFLEN];
|
|
|
|
fd_set fd;
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
FD_ZERO(&fd);
|
|
|
|
FD_SET(_sock, &fd);
|
|
|
|
tv.tv_sec = _timeout;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
if (!select(FD_SETSIZE, 0, &fd, 0, &tv))
|
|
|
|
if (_isShutdown) // don't throw during dtor
|
|
|
|
std::cout << "FGFSSocket::write/select: timeout exceeded" << std::endl;
|
|
|
|
else
|
|
|
|
throw("FGFSSocket::write/select: timeout exceeded");
|
|
|
|
|
|
|
|
va_start(va, msg);
|
|
|
|
vsnprintf(buf, BUFLEN - 2, msg, va);
|
|
|
|
va_end(va);
|
|
|
|
std::cout << "SEND: " << buf << std::endl;
|
|
|
|
strcat(buf, "\015\012");
|
|
|
|
|
|
|
|
len = ::write(_sock, buf, strlen(buf));
|
|
|
|
if (len < 0)
|
|
|
|
if (_isShutdown) // don't throw during dtor
|
|
|
|
std::cout << "FGFSSocket::write error" << std::endl;
|
|
|
|
else
|
|
|
|
throw("FGFSSocket::write");
|
|
|
|
|
|
|
|
return len;
|
2004-02-10 15:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-27 03:11:34 +00:00
|
|
|
const char* FGFSSocket::read(void)
|
2004-02-10 15:35:48 +00:00
|
|
|
{
|
2021-02-27 03:11:34 +00:00
|
|
|
char* p;
|
|
|
|
fd_set fd;
|
|
|
|
struct timeval tv;
|
|
|
|
ssize_t len;
|
|
|
|
|
|
|
|
FD_ZERO(&fd);
|
|
|
|
FD_SET(_sock, &fd);
|
|
|
|
tv.tv_sec = _timeout;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
if (!select(FD_SETSIZE, &fd, 0, 0, &tv)) {
|
|
|
|
if (_timeout == 0)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
throw("FGFSSocket::read/select: timeout exceeded");
|
|
|
|
}
|
|
|
|
|
|
|
|
len = ::read(_sock, _buffer, BUFLEN - 1);
|
|
|
|
if (len < 0)
|
|
|
|
throw("FGFSSocket::read/read");
|
|
|
|
if (len == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (p = &_buffer[len - 1]; p >= _buffer; p--)
|
|
|
|
if (*p != '\015' && *p != '\012')
|
|
|
|
break;
|
|
|
|
*++p = '\0';
|
|
|
|
return strlen(_buffer) ? _buffer : 0;
|
2004-02-10 15:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void FGFSSocket::flush(void)
|
|
|
|
{
|
2021-02-27 03:11:34 +00:00
|
|
|
int i = _timeout;
|
|
|
|
_timeout = 0;
|
|
|
|
while (read())
|
|
|
|
;
|
|
|
|
_timeout = i;
|
2004-02-10 15:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-27 03:11:34 +00:00
|
|
|
int main(const int argc, const char* argv[])
|
2004-02-10 15:35:48 +00:00
|
|
|
try {
|
2021-02-27 03:11:34 +00:00
|
|
|
const char* hostname = argc > 1 ? argv[1] : "localhost";
|
|
|
|
int port = argc > 2 ? atoi(argv[2]) : 5501;
|
|
|
|
|
|
|
|
FGFSSocket f(hostname, port);
|
|
|
|
f.flush();
|
|
|
|
f.write("set /controls/engines/engine[%d]/throttle %lg", 0, 1.0);
|
|
|
|
f.write("set /controls/engines/engine[%d]/throttle %lg", 1, 1.0);
|
|
|
|
f.write("get /sim/aircraft");
|
|
|
|
const char* p = f.read();
|
|
|
|
if (p)
|
|
|
|
std::cout << "RECV: " << p << std::endl;
|
|
|
|
return EXIT_SUCCESS;
|
2004-02-10 15:35:48 +00:00
|
|
|
|
|
|
|
} catch (const char s[]) {
|
2021-02-27 03:11:34 +00:00
|
|
|
std::cerr << "Error: " << s << ": " << strerror(errno) << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
2004-02-10 15:35:48 +00:00
|
|
|
|
|
|
|
} catch (...) {
|
2021-02-27 03:11:34 +00:00
|
|
|
std::cerr << "Error: unknown exception" << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
2004-02-10 15:35:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// vim:cindent
|