[316] | 1 | #!/usr/bin/perl |
---|
[121] | 2 | #------------------------------------------------------------------------------- |
---|
[296] | 3 | # This file is part of the FLARM®-Radar Project. |
---|
[121] | 4 | # |
---|
[314] | 5 | # Copyright by the Authors |
---|
[121] | 6 | # |
---|
| 7 | # Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 8 | # you may not use this file except in compliance with the License. |
---|
| 9 | # You may obtain a copy of the License at |
---|
| 10 | # |
---|
| 11 | # http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 12 | # |
---|
| 13 | # Unless required by applicable law or agreed to in writing, software |
---|
| 14 | # distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 16 | # See the License for the specific language governing permissions and |
---|
| 17 | # limitations under the License. |
---|
| 18 | # |
---|
| 19 | # Project Website: www.flarmradar.ch |
---|
| 20 | # Email: info@flarmradar.ch |
---|
[314] | 21 | # Authors: |
---|
| 22 | # 2012-2014 Simon Moser |
---|
| 23 | # 2013-2014 Dominic Spreitz |
---|
| 24 | # 2014-2014 Giorgio Tresoldi |
---|
[121] | 25 | #------------------------------------------------------------------------------- |
---|
[103] | 26 | |
---|
| 27 | use strict; |
---|
| 28 | use Getopt::Std; |
---|
| 29 | # load LWP library: |
---|
| 30 | use LWP::UserAgent; |
---|
| 31 | use HTML::Parse; |
---|
| 32 | use Time::HiRes; |
---|
| 33 | |
---|
| 34 | # parse options |
---|
| 35 | my %options; |
---|
| 36 | getopts('f:u:s:', \%options); |
---|
| 37 | unless(defined($options{'f'}) && defined($options{u})) { |
---|
| 38 | usage(); |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | my $url = $options{u}; |
---|
| 42 | my $file = $options{f}; |
---|
| 43 | my $speed = defined($options{s}) ? $options{s} : 1; |
---|
| 44 | if (! -f $file || ! -w $file) { |
---|
| 45 | print "error reading input file: ", $file, "\n"; |
---|
| 46 | exit 1; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | # create UserAgent object |
---|
| 53 | my $ua = new LWP::UserAgent; |
---|
| 54 | |
---|
| 55 | # set a user agent (browser-id) |
---|
| 56 | # $ua->agent('Mozilla/5.5 (compatible; MSIE 5.5; Windows NT 5.1)'); |
---|
| 57 | |
---|
| 58 | # timeout: |
---|
| 59 | $ua->timeout(15); |
---|
| 60 | |
---|
| 61 | my $i = 0; |
---|
| 62 | my $buf; |
---|
| 63 | |
---|
| 64 | open(FILE, "< $file") || die("error while reading file ", $file); |
---|
| 65 | while (my $record = <FILE>) { |
---|
| 66 | chomp($record); |
---|
| 67 | if ($record =~ /^\$GPGGA,/) { |
---|
| 68 | flush($buf, $url, $i); |
---|
| 69 | #sleep(1); |
---|
| 70 | Time::HiRes::sleep(1/$speed); |
---|
| 71 | $buf = undef; |
---|
| 72 | } |
---|
| 73 | $i++; |
---|
| 74 | $buf = (defined($buf)) ? "$buf;$record" : $record; |
---|
| 75 | } |
---|
| 76 | flush($buf, $url, $i); |
---|
| 77 | close(FILE); |
---|
| 78 | |
---|
| 79 | exit 0; |
---|
| 80 | |
---|
| 81 | sub flush() { |
---|
| 82 | my ($records, $url, $count) = @_; |
---|
| 83 | my $request = HTTP::Request->new('PUT'); |
---|
| 84 | $request->url($url); |
---|
| 85 | $request->content($records); |
---|
| 86 | print "flushing at $count\n"; |
---|
| 87 | # run the request |
---|
| 88 | my $response = $ua->request($request); |
---|
| 89 | my $code = $response->code; |
---|
| 90 | die("error processing records ", $records) if (200 != $response->code); |
---|
| 91 | } |
---|
| 92 | # proceed the request: |
---|
| 93 | my $request = HTTP::Request->new('PUT'); |
---|
| 94 | $request->url($url); |
---|
| 95 | $request->content("blubb"); |
---|
| 96 | |
---|
| 97 | my $response = $ua->request($request); |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | # |
---|
| 101 | # responses: |
---|
| 102 | # |
---|
| 103 | |
---|
| 104 | # response code (like 200, 404, etc) |
---|
| 105 | my $code = $response->code; |
---|
| 106 | print "Return code: ", $code, "\n"; |
---|
| 107 | |
---|
| 108 | # headers (Server: Apache, Content-Type: text/html, ...) |
---|
| 109 | my $headers = $response->headers_as_string; |
---|
| 110 | print "Headers: ", $headers, "\n"; |
---|
| 111 | |
---|
| 112 | # HTML body: |
---|
| 113 | my $body = $response->content; |
---|
| 114 | print "Body: ", $body, "\n"; |
---|
| 115 | |
---|
| 116 | sub usage() { |
---|
| 117 | print "usage: bulb\n"; |
---|
| 118 | exit 1; |
---|
| 119 | } |
---|