source: core/trunk/client/restclient.pl @ 387

Last change on this file since 387 was 345, checked in by smoser, 10 years ago

apply copyright

File size: 3.8 KB
Line 
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
51use strict;
52use Getopt::Std;
53# load LWP library:
54use LWP::UserAgent;
55use HTML::Parse;
56use Time::HiRes;
57
58# parse options
59my %options;
60getopts('f:u:s:', \%options);
61unless(defined($options{'f'}) && defined($options{u})) {
62  usage();
63}
64
65my $url = $options{u};
66my $file = $options{f};
67my $speed = defined($options{s}) ? $options{s} : 1; 
68if (! -f $file || ! -w $file) {
69  print "error reading input file: ", $file, "\n";
70  exit 1;
71}
72
73
74 
75
76# create UserAgent object
77my $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
85my $i = 0;
86my $buf;
87
88open(FILE, "< $file") || die("error while reading file ", $file);
89while (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}
100flush($buf, $url, $i);
101close(FILE);
102
103exit 0;
104
105sub 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:
117my $request = HTTP::Request->new('PUT');
118$request->url($url);
119$request->content("blubb");
120 
121my $response = $ua->request($request);
122 
123 
124#
125# responses:
126#
127 
128# response code (like 200, 404, etc)
129my $code = $response->code;
130print "Return code: ", $code, "\n";
131 
132# headers (Server: Apache, Content-Type: text/html, ...)
133my $headers = $response->headers_as_string;
134print "Headers: ", $headers, "\n";
135 
136# HTML body:
137my $body =  $response->content;
138print "Body: ", $body, "\n";
139
140sub usage() {
141  print "usage: bulb\n";
142  exit 1;
143}
Note: See TracBrowser for help on using the repository browser.