source: A2P/a2p/A2P/Client.pm @ 3

Last change on this file since 3 was 3, checked in by guillaume, 17 years ago
  • AUTHORS: Ajout des différents contributeurs
  • COPYING: Ajout de la licence GPL v3
  • a2p: Préparation des sources pour leur publication sous GPL
  • Property svn:keywords set to Id
File size: 5.2 KB
Line 
1#
2# Copyright (c) 2004-2007 - Consultas, PKG.fr
3#
4# This file is part of A2P.
5#
6# A2P is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# A2P is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with A2P; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19#
20# $Id: Client.pm 3 2007-10-18 16:20:19Z guillaume $
21#
22# WARN SIGALRM is used to check time out,
23# so remember it can override a client definition
24
25package A2P::Client;
26
27use strict ;
28use Socket ;
29use IO::Socket ;
30use Time::HiRes  qw(ualarm gettimeofday tv_interval);
31use A2P::Globals ;
32use A2P::Syslog ;
33use A2P::Com   qw( IsCom GetCom comJOB comREQ comCOM comDONE comASK comUPD GetTmpFile comTEST );
34use A2P::Tools qw( ShortID );
35
36BEGIN {
37    use Exporter () ;
38    our ( $VERSION , @ISA , @EXPORT );
39    $VERSION = sprintf "%s", q$Rev: 415 $ =~ /(\d[0-9.]+)\s+/ ;
40    @ISA = qw(Exporter);
41    @EXPORT = qw(&TellListener &DoJob);
42}
43our $VERSION ;
44
45# Set OUTPUT_AUTOFLUSH
46$| = 1 ;
47
48# Last argument to TellListener can be a sub callback
49sub TellListener {
50    my $which = shift ;
51    $LISTENER = &GetTmpFile( $which . '-listener' );
52    my $callback = pop if ( ref($_[$#_]) =~ /^CODE$/ );
53    my @Default  = ( "NO RESPONSE" );
54    my @Response = ();
55    my $timer    = [ &gettimeofday() ] ;
56    my $shutdown = 0 ;
57    my $response = 0 ;
58
59    return ( "SERVICE NOT AVAILABLE" ) if ( ! -S $LISTENER );
60    return ( "NOTHING TO TELL" ) unless @_ ;
61
62    $SIG{ALRM} = sub {
63        # Check my time out
64        return unless ( &tv_interval($timer) > $CLIENT_TIMEOUT );
65        push @Default , "TIME OUT" ;
66        $shutdown ++ ;
67        shutdown SOCK , 2 ;
68    };
69
70    # Use ualarm to be advertize to check time-out
71    &ualarm( 1000000 , 1000000 );
72
73    socket(SOCK, PF_UNIX, SOCK_STREAM, 0)
74        or die "Can't open a socket: $!" ;
75
76    SOCK->autoflush(1);
77
78    connect(SOCK, pack_sockaddr_un($LISTENER))
79        or die "Can't connect to $LISTENER socket: $!" ;
80
81    my $Question = &GetCom( comREQ , TODO , &GetCom( comASK , @_ ));
82    &Debug("Asking '" . $Question . "' to $which service listener");
83
84    local $\ = "\n" ;
85
86    # Talk now with service
87    if ( print SOCK $Question ) {
88        while ( my $line = <SOCK> ) {
89            chomp $line ;
90            next unless $line ;
91
92            my @content = &IsCom( comCOM , $line );
93            my $info = @content ? $content[1] : $line ;
94
95            # Update my time-out timer
96            $timer = [ &gettimeofday() ] ;
97
98            # Will leave loop if server answer COM is DONE, but keeping info
99            if ( @content = &IsCom( comDONE , $info ) ) {
100                $info = $content[1] ;
101
102            } elsif ( $info =~ /^\d+$/ and $info == comTEST ) {
103                print SOCK &GetCom( comCOM , $$ , comDONE );
104                next ;
105            }
106
107            # Call back to client if requested
108            defined($callback) ? &$callback($info) : push @Response, $info ;
109            $response ++ ;
110
111            last if ( @content and $content[0] =~ /^done$/ );
112        }
113    }
114
115    &ualarm(0);
116
117    @Response = @Default unless $response ;
118
119    map {
120        &Debug("Listener response: '" . $_ . "'")
121    } @Response ;
122
123    shutdown SOCK , 2
124        unless $shutdown ;
125
126    return @Response ;
127}
128
129sub JustOutPut {
130    local $\ = "\n" ;
131    print @_ ;
132}
133
134sub DoJob {
135    my $which = shift ;
136    my $file  = shift ;
137
138    print "\nStarting job with [$which] service and '$file' file...\n";
139
140    # Find an suitable Id for the job
141    my ( $jid ) = $file =~ m|.*/(.+)(\.\w+)?$| ;
142    ( $jid ) = $file =~ m|/?(\w+)(\.\w+)+$| unless (defined($jid));
143    $jid = "JOB" . &ShortID(5) unless (defined($jid));
144
145    if ( $which and $file and $jid ) {
146        print "A2P JobID = $jid\n" ;
147        &TellListener( $which,
148            JobManager => &GetCom( comJOB , $jid => $file ), \&JustOutPut );
149
150    } else {
151        print "But it's impossible when an argument is missing\n" ;
152        print "Tell me which service to ask as first argument\n" unless $which ;
153        print "Tell me which file to compute as second argument\n" unless $file ;
154        print "I can't compute Job ID with '$file'\n" unless $jid ;
155    }
156}
157
158sub DoUpdate {
159    my $service  = shift ;
160    my $module   = shift ;
161    my $varname  = shift ;
162    my $value    = shift ;
163
164    #print "\nUpdating value $varname on [$which-$module] service...\n";
165
166    if ( $service and $module and $varname and defined($value) ) {
167        &TellListener( $service,
168            $module => &GetCom( comUPD , $varname => $value ), \&JustOutPut );
169
170    } else {
171        print "But it's impossible when an argument is missing\n" ;
172        #print "Tell me which service to ask as first argument\n" unless $which ;
173        #print "Tell me which file to compute as second argument\n" unless $file ;
174        #print "I can't compute Job ID with '$file'\n" unless $jid ;
175    }
176}
177
178&Debug("Module " . __PACKAGE__ . " v$VERSION loaded") unless ( $0 =~ /cgi/ );
179
1801;
Note: See TracBrowser for help on using the repository browser.