]> git.mxchange.org Git - flightgear.git/blob - scripts/perl/autopilot/telnet.pl
Updated license statement.
[flightgear.git] / scripts / perl / autopilot / telnet.pl
1 #!/usr/bin/perl
2 #
3 # Written by Curtis L. Olson, started December 2002
4 # Some code portions courtesy of Melchior FRANZ
5 #
6 # This file is in the Public Domain and comes with no warranty.
7 #
8 # $Id$
9 # ----------------------------------------------------------------------------
10
11
12 use IO::Socket;
13
14 use strict;
15
16
17 sub get_prop() {
18     my( $handle ) = shift;
19
20     &send( $handle, "get " . shift );
21     eof $handle and die "\nconnection closed by host";
22     $_ = <$handle>;
23     s/\015?\012$//;
24     /^-ERR (.*)/ and die "\nfgfs error: $1\n";
25
26     return $_;
27 }
28
29
30 sub set_prop() {        
31     my( $handle ) = shift;
32     my( $prop ) = shift;
33     my( $value ) = shift;
34
35     &send( $handle, "set $prop $value");
36
37     # eof $handle and die "\nconnection closed by host";
38 }
39
40
41 sub send() {
42     my( $handle ) = shift;
43
44     print $handle shift, "\015\012";
45 }
46
47
48 sub connect() {
49     my( $host ) = shift;
50     my( $port ) = shift;
51     my( $timeout ) = (shift || 120);
52     my( $socket );
53     STDOUT->autoflush(1);
54     while ($timeout--) {
55         if ($socket = IO::Socket::INET->new( Proto => 'tcp',
56                                              PeerAddr => $host,
57                                              PeerPort => $port) )
58         {
59             $socket->autoflush(1);
60             return $socket;
61         }       
62         print "Attempting to connect ... " . $timeout . "\n";
63         sleep(1);
64     }
65     return 0;
66 }
67
68
69 return 1;                    # make perl happy