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

Last change on this file since 314 was 314, checked in by smoser, 11 years ago

Release 1.2.0

File size: 2.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-2014 Simon Moser
22#     2013-2014 Dominic Spreitz
23#     2014-2014 Giorgio Tresoldi
24#-------------------------------------------------------------------------------
25#!/usr/bin/perl
26
27use strict;
28use Getopt::Std;
29# load LWP library:
30use LWP::UserAgent;
31use HTML::Parse;
32use Time::HiRes;
33
34# parse options
35my %options;
36getopts('f:u:s:', \%options);
37unless(defined($options{'f'}) && defined($options{u})) {
38  usage();
39}
40
41my $url = $options{u};
42my $file = $options{f};
43my $speed = defined($options{s}) ? $options{s} : 1; 
44if (! -f $file || ! -w $file) {
45  print "error reading input file: ", $file, "\n";
46  exit 1;
47}
48
49
50 
51
52# create UserAgent object
53my $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
61my $i = 0;
62my $buf;
63
64open(FILE, "< $file") || die("error while reading file ", $file);
65while (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}
76flush($buf, $url, $i);
77close(FILE);
78
79exit 0;
80
81sub 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:
93my $request = HTTP::Request->new('PUT');
94$request->url($url);
95$request->content("blubb");
96 
97my $response = $ua->request($request);
98 
99 
100#
101# responses:
102#
103 
104# response code (like 200, 404, etc)
105my $code = $response->code;
106print "Return code: ", $code, "\n";
107 
108# headers (Server: Apache, Content-Type: text/html, ...)
109my $headers = $response->headers_as_string;
110print "Headers: ", $headers, "\n";
111 
112# HTML body:
113my $body =  $response->content;
114print "Body: ", $body, "\n";
115
116sub usage() {
117  print "usage: bulb\n";
118  exit 1;
119}
Note: See TracBrowser for help on using the repository browser.