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

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

Update Copyright, fix encoding issue

File size: 2.8 KB
RevLine 
[121]1#-------------------------------------------------------------------------------
[296]2# This file is part of the FLARM®-Radar Project.
[121]3#   
[296]4#   Copyright 2012-2014 Simon Moser
5#   Copyright 2013-2014 Dominic Spreitz
[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
21#-------------------------------------------------------------------------------
[103]22#!/usr/bin/perl
23
24use strict;
25use Getopt::Std;
26# load LWP library:
27use LWP::UserAgent;
28use HTML::Parse;
29use Time::HiRes;
30
31# parse options
32my %options;
33getopts('f:u:s:', \%options);
34unless(defined($options{'f'}) && defined($options{u})) {
35  usage();
36}
37
38my $url = $options{u};
39my $file = $options{f};
40my $speed = defined($options{s}) ? $options{s} : 1; 
41if (! -f $file || ! -w $file) {
42  print "error reading input file: ", $file, "\n";
43  exit 1;
44}
45
46
47 
48
49# create UserAgent object
50my $ua = new LWP::UserAgent;
51 
52# set a user agent (browser-id)
53# $ua->agent('Mozilla/5.5 (compatible; MSIE 5.5; Windows NT 5.1)');
54 
55# timeout:
56$ua->timeout(15);
57
58my $i = 0;
59my $buf;
60
61open(FILE, "< $file") || die("error while reading file ", $file);
62while (my $record = <FILE>) {
63  chomp($record);
64  if ($record =~ /^\$GPGGA,/) {
65    flush($buf, $url, $i);
66    #sleep(1);
67    Time::HiRes::sleep(1/$speed);
68    $buf = undef;
69  }
70  $i++;
71  $buf = (defined($buf)) ? "$buf;$record" : $record; 
72}
73flush($buf, $url, $i);
74close(FILE);
75
76exit 0;
77
78sub flush() {
79  my ($records, $url, $count) = @_;
80  my $request = HTTP::Request->new('PUT');
81  $request->url($url);
82  $request->content($records);
83  print "flushing at $count\n";
84  # run the request
85  my $response = $ua->request($request);
86  my $code = $response->code;
87  die("error processing records ", $records) if (200 != $response->code); 
88}
89# proceed the request:
90my $request = HTTP::Request->new('PUT');
91$request->url($url);
92$request->content("blubb");
93 
94my $response = $ua->request($request);
95 
96 
97#
98# responses:
99#
100 
101# response code (like 200, 404, etc)
102my $code = $response->code;
103print "Return code: ", $code, "\n";
104 
105# headers (Server: Apache, Content-Type: text/html, ...)
106my $headers = $response->headers_as_string;
107print "Headers: ", $headers, "\n";
108 
109# HTML body:
110my $body =  $response->content;
111print "Body: ", $body, "\n";
112
113sub usage() {
114  print "usage: bulb\n";
115  exit 1;
116}
Note: See TracBrowser for help on using the repository browser.