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

Last change on this file since 121 was 121, checked in by smoser, 12 years ago

#71

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