Renderer, ...: use PixelRect::GetCenter()
[xcsoar.git] / test / src / FeedTCP.cpp
blob7f559258e83b31d9c54d49a219a29617534d6516
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * This program tries to connect to a TCP server at the localhost at
26 * port 4353 and feeds NMEA data read from stdin to it.
29 #include "OS/SocketDescriptor.hpp"
30 #include "OS/SocketAddress.hpp"
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <stdlib.h>
39 int main(int argc, char **argv)
41 // Determine on which TCP port to connect to the server
42 const char *tcp_port;
43 if (argc < 2) {
44 fprintf(stderr, "This program opens a TCP connection to a server which is assumed ");
45 fprintf(stderr, "to be at 127.0.0.1, and sends NMEA data which is read from stdin.\n\n");
46 fprintf(stderr, "Usage: %s PORT\n", argv[0]);
47 fprintf(stderr, "Defaulting to port 4353\n");
48 tcp_port = "4353";
49 } else {
50 tcp_port = argv[1];
53 // Convert IP address to binary form
54 SocketAddress server_address;
55 if (!server_address.Lookup("127.0.0.1", tcp_port, AF_INET)) {
56 fprintf(stderr, "Failed to look up address\n");
57 exit(1);
60 // Create socket for the outgoing connection
61 SocketDescriptor sock;
62 if (!sock.CreateTCP()) {
63 perror("Socket");
64 exit(1);
67 // Connect to the specified server
68 if (!sock.Connect(server_address)) {
69 perror("Connect");
70 exit(1);
73 char stamp[6] = "";
75 char line[1024];
76 long baudrate = 9600;
77 useconds_t sleep_acc = 0;
79 // Number of characters sent
80 long c_count = 0;
81 // Number of sentence packages sent
82 long l_count = 0;
84 while (fgets(line, sizeof(line), stdin) != NULL) {
85 int l = strlen(line);
86 c_count += l;
87 long tsleep = l*1e6/(baudrate/10);
88 usleep(tsleep);
89 sleep_acc += tsleep;
91 if (memcmp(line, "$GP", 3) == 0 &&
92 (memcmp(line + 3, "GGA", 3) == 0 ||
93 memcmp(line + 3, "RMC", 3) == 0) &&
94 line[6] == ',' &&
95 strncmp(stamp, line + 7, sizeof(stamp)) != 0) {
96 /* the time stamp has changed - sleep for one second */
97 usleep(1e6-sleep_acc);
98 strncpy(stamp, line + 7, sizeof(stamp));
99 sleep_acc = 0;
100 l_count++;
101 printf(".");
102 fflush(stdout);
105 sock.Write(line, l);
108 printf(">>>> Av %ld\n", c_count/l_count);
109 return 0;