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