Debian Codegraph @

scripts/example/fgfsclient.cxx

raw symbols SCIP
1 // $Id$
2 // g++ -O2 -g -pedantic -Wall fgfsclient.cxx -o fgfsclient -lstdc++
3 // USAGE: ./fgfsclient [hostname [port]]
4 // Public Domain
5
6 #include <errno.h>
7 #include <iostream>
8 #include <netdb.h>
9 #include <netinet/in.h>
10 #include <stdarg.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <sys/socket.h>
15 #include <sys/time.h>
16 #include <sys/types.h>
17 #include <unistd.h>
18
19
20 const char* HOST = "localhost";
21 const unsigned PORT = 5501;
22 const int BUFLEN = 256;
23
24
25 class FGFSSocket
26 {
27 public:
28 FGFSSocket(const char* name, unsigned port);
29 ~FGFSSocket();
30
31 int write(const char* msg, ...);
32 const char* read(void);
33 inline void flush(void);
34 void settimeout(unsigned t) { _timeout = t; }
35
36 private:
37 int close(void);
38
39 int _sock;
40 bool _connected;
41 unsigned _timeout;
42 char _buffer[BUFLEN];
43 bool _isShutdown = false;
44 };
45
46
47 FGFSSocket::FGFSSocket(const char* hostname = HOST, unsigned port = PORT) : _sock(-1),
48 _connected(false),
49 _timeout(1)
50 {
51 _sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
52 if (_sock < 0)
53 throw("FGFSSocket/socket");
54
55 struct hostent* hostinfo;
56 hostinfo = gethostbyname(hostname);
57 if (!hostinfo) {
58 close();
59 throw("FGFSSocket/gethostbyname: unknown host");
60 }
61
62 struct sockaddr_in serv_addr;
63 serv_addr.sin_family = AF_INET;
64 serv_addr.sin_port = htons(port);
65 serv_addr.sin_addr = *(struct in_addr*)hostinfo->h_addr;
66
67 if (connect(_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) {
68 close();
69 throw("FGFSSocket/connect");
70 }
71 _connected = true;
72 try {
73 write("data");
74 } catch (...) {
75 close();
76 throw;
77 }
78 }
79
80
81 FGFSSocket::~FGFSSocket()
82 {
83 _isShutdown = true;
84 close();
85 }
86
87
88 int FGFSSocket::close(void)
89 {
90 if (_connected)
91 write("quit");
92 if (_sock < 0)
93 return 0;
94 int ret = ::close(_sock);
95 _sock = -1;
96 return ret;
97 }
98
99
100 int FGFSSocket::write(const char* msg, ...)
101 {
102 va_list va;
103 ssize_t len;
104 char buf[BUFLEN];
105 fd_set fd;
106 struct timeval tv;
107
108 FD_ZERO(&fd);
109 FD_SET(_sock, &fd);
110 tv.tv_sec = _timeout;
111 tv.tv_usec = 0;
112 if (!select(FD_SETSIZE, 0, &fd, 0, &tv))
113 if (_isShutdown) // don't throw during dtor
114 std::cout << "FGFSSocket::write/select: timeout exceeded" << std::endl;
115 else
116 throw("FGFSSocket::write/select: timeout exceeded");
117
118 va_start(va, msg);
119 vsnprintf(buf, BUFLEN - 2, msg, va);
120 va_end(va);
121 std::cout << "SEND: " << buf << std::endl;
122 strcat(buf, "\015\012");
123
124 len = ::write(_sock, buf, strlen(buf));
125 if (len < 0)
126 if (_isShutdown) // don't throw during dtor
127 std::cout << "FGFSSocket::write error" << std::endl;
128 else
129 throw("FGFSSocket::write");
130
131 return len;
132 }
133
134
135 const char* FGFSSocket::read(void)
136 {
137 char* p;
138 fd_set fd;
139 struct timeval tv;
140 ssize_t len;
141
142 FD_ZERO(&fd);
143 FD_SET(_sock, &fd);
144 tv.tv_sec = _timeout;
145 tv.tv_usec = 0;
146 if (!select(FD_SETSIZE, &fd, 0, 0, &tv)) {
147 if (_timeout == 0)
148 return 0;
149 else
150 throw("FGFSSocket::read/select: timeout exceeded");
151 }
152
153 len = ::read(_sock, _buffer, BUFLEN - 1);
154 if (len < 0)
155 throw("FGFSSocket::read/read");
156 if (len == 0)
157 return 0;
158
159 for (p = &_buffer[len - 1]; p >= _buffer; p--)
160 if (*p != '\015' && *p != '\012')
161 break;
162 *++p = '\0';
163 return strlen(_buffer) ? _buffer : 0;
164 }
165
166
167 inline void FGFSSocket::flush(void)
168 {
169 int i = _timeout;
170 _timeout = 0;
171 while (read())
172 ;
173 _timeout = i;
174 }
175
176
177 int main(const int argc, const char* argv[])
178 try {
179 const char* hostname = argc > 1 ? argv[1] : "localhost";
180 int port = argc > 2 ? atoi(argv[2]) : 5501;
181
182 FGFSSocket f(hostname, port);
183 f.flush();
184 f.write("set /controls/engines/engine[%d]/throttle %lg", 0, 1.0);
185 f.write("set /controls/engines/engine[%d]/throttle %lg", 1, 1.0);
186 f.write("get /sim/aircraft");
187 const char* p = f.read();
188 if (p)
189 std::cout << "RECV: " << p << std::endl;
190 return EXIT_SUCCESS;
191
192 } catch (const char s[]) {
193 std::cerr << "Error: " << s << ": " << strerror(errno) << std::endl;
194 return EXIT_FAILURE;
195
196 } catch (...) {
197 std::cerr << "Error: unknown exception" << std::endl;
198 return EXIT_FAILURE;
199 }
200
201 // vim:cindent