source: A2P/a2p/A2P/Com.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.0 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: Com.pm 3 2007-10-18 16:20:19Z guillaume $
21#
22
23package A2P::Com ;
24
25use strict ;
26use Socket ;
27use Time::HiRes qw( gettimeofday usleep );
28use Fcntl qw(:flock F_SETLK F_WRLCK SEEK_SET);
29use IO::Socket ;
30use A2P::Globals ;
31
32BEGIN {
33    use Exporter ();
34
35    our ( $VERSION , @ISA , @EXPORT_OK , @TT );
36
37    $VERSION = sprintf "%s", q$Rev: 490 $ =~ /(\d[0-9.]+)\s+/ ;
38
39    @ISA = qw(Exporter);
40
41    @EXPORT_OK = qw( &IsCom &GetCom
42        &comCOM &comASK &comREQ &comLOG &comJOB &comSAY &comDONE &comINF &comTEST
43        &comZIP &comUPD &comFILE &GetTmpFile &TakeComLocking &ReleaseComLocking
44        &comXML
45        );
46
47    # Set timer ref to starting time
48    @TT = ( $^T , ( $^T * 1_000_000 ) % 1_000_000 ) unless @TT ;
49}
50our $VERSION ;
51our @TT ;
52
53#############################################################################
54##          Com tags lists                                                 ##
55#############################################################################
56# Constant tag index
57sub comCOM  ()   {   0  };
58sub comASK  ()   {   1  };
59sub comREQ  ()   {   2  };
60sub comLOG  ()   {   3  };
61sub comJOB  ()   {   4  };
62sub comSAY  ()   {   5  };
63sub comINF  ()   {   6  };
64sub comZIP  ()   {   7  };
65sub comUPD  ()   {   8  };
66sub comFILE ()   {  50  };
67sub comXML  ()   { 1111 };
68sub comTEST ()   { 8888 };
69sub comDONE ()   { 9999 };
70
71my $id = 1000 ;
72
73my %Coms = (
74    comCOM  ,  [  "<com id=", 0, " tid='"   , ""  , "'>" , "</com>" , 'COM' ],
75    comASK  ,  [  "<ask id=", 0, " for='"   , ""  , "'>" , "</ask>" , 'ASK' ],
76    comREQ  ,  [  "<req id=", 0, " to='"    , ""  , "'>" , "</req>" , 'REQ' ],
77    comLOG  ,  [  "<log id=", 0, " level='" , ""  , "'>" , "</log>" , 'LOG' ],
78    comJOB  ,  [  "<job id=", 0, " jid='"   , ""  , "'>" , "</job>" , 'JOB' ],
79    comSAY  ,  [  "<say id=", 0, " for='"   , ""  , "'>" , "</say>" , 'SAY' ],
80    comINF  ,  [  "<info id=",0, " kind='"  , ""  , "'>" , "</info>", 'INFO'],
81    comZIP  ,  [  "<zip id=", 0, " file='"  , ""  , "'>" , "</zip>" , 'ZIP' ],
82    comUPD  ,  [  "<maj id=", 0, " what='"  , ""  , "'>" , "</maj>" , 'MAJ' ],
83    comFILE ,  [  "<file id=",0, " name='"  , ""  , "'>" , "</file>", 'FILE'],
84    comDONE ,  [  "<done id=",0, ""         , ""  , ">"  , "</done>", 'DONE']
85);
86
87my %RegExp = (
88    comCOM  ,  qr|^<com +id=\d{4} +tid=(["']?)(\d+)\1 *>(.*)</com>$|i   ,
89    comASK  ,  qr|^<ask +id=\d{4} +for=(["']?)(\w+)\1 *>(.*)</ask>$|i   ,
90    comREQ  ,  qr|^<req +id=\d{4} +to=(["']?)(\w+)\1 *>(.*)</req>$|i    ,
91    comLOG  ,  qr|^<log +id=\d{4} +level=(["']?)(\w+)\1 *>(.*)</log>$|i ,
92    comJOB  ,  qr|^<job +id=\d{4} +jid=(["']?)([-0-9a-zA-Z_.]+)\1 *>(.*)</job>$|i   ,
93    comSAY  ,  qr|^<say +id=\d{4} +for=(["']?)(\w+)\1 *>(.*)</say>$|i,
94    comINF  ,  qr|^<info +id=\d{4} +kind=(["']?)([\s\w]+)\1 *>(.*)</info>$|i,
95    comZIP  ,  qr|^<zip +id=\d{4} +file=(["']?)([^\1>]+)\1 *>(.*)</zip>$|i,
96    comUPD  ,  qr|^<maj +id=\d{4} +what=(["']?)(\w+)\1 *>(.*)</maj>$|i,
97    comFILE ,  qr|^<file +id=\d{4} +name=(["']?)([^\1>]+)\1 *>(.*)</file>$|i,
98    comDONE ,  qr|^<(done) +id=\d{4}.*>(.*)</done>$|i,
99    comXML  ,  qr|^\s*<\w+.*>\s*$|s
100);
101
102sub GetCom {
103    local $" = '' ;
104    my $com = shift ;
105    $Coms{$com}->[1] = $id < 10000 ? $id ++ : ( $id = 1000 ) ;
106    $Coms{$com}->[3] = shift || "" ;
107    return "@{$Coms{$com}}[0..4]@_" . $Coms{$com}->[5] ;
108}
109
110# IsCom returns a list of matched pattern (pattern is s=choosen from first arg)
111sub IsCom {
112    if ( $_[0] > 999 ) {
113        return $_[1] =~ $RegExp{$_[0]} ;
114
115    } else {
116        @_ = $_[1] =~ $RegExp{$_[0]} ;
117        # Remove matching apos before returning
118        shift unless ( @_ and $_[0] =~ /^[^"']+$/ );
119        return @_ ;
120    }
121}
122
123sub timestamp {
124    # Keep timer in the current minute
125    my @tt = &gettimeofday() ;
126    return sprintf("%.8d > ",
127        (( $tt[0] - $TT[0] ) * 1_000_000 + ( $tt[1] - $TT[1] )) % 60_000_000 );
128}
129
130sub GetTmpFile {
131    return $SERVICE_TMP . '/' . $_[0] ;
132}
133
134sub TakeComLocking {
135    my $fh = shift ;
136    my $locked = 0 ;
137
138    if (defined($fh)) {
139        # Try non blocking exclusive lock
140        $locked = flock( $fh, LOCK_EX | LOCK_NB );
141        # Else try blocking one
142        $locked = flock( $fh, LOCK_EX ) unless ( $locked );
143
144        &UPSTAT($locked?'GOOD-COMLOCKING':'BAD-COMLOCKING');
145    }
146
147    return $locked ;
148}
149
150sub ReleaseComLocking {
151    my $fh = shift or return 0 ;
152    flock( $fh, LOCK_UN );
153}
154
1551;
Note: See TracBrowser for help on using the repository browser.