View | Details | Raw Unified | Return to bug 15006
Collapse All | Expand All

(-)a/C4/SIP/SIPServer.pm (-5 / +50 lines)
Lines 135-142 sub raw_transport { Link Here
135
    # Timeout the while loop if we get stuck in it
135
    # Timeout the while loop if we get stuck in it
136
    # In practice it should only iterate once but be prepared
136
    # In practice it should only iterate once but be prepared
137
    local $SIG{ALRM} = sub { die 'raw transport Timed Out!' };
137
    local $SIG{ALRM} = sub { die 'raw transport Timed Out!' };
138
    syslog('LOG_DEBUG', "raw_transport: timeout is $service->{timeout}");
138
    my $timeout = $self->get_timeout({ transport => 1 });
139
    alarm $service->{timeout};
139
    syslog('LOG_DEBUG', "raw_transport: timeout is $timeout");
140
    alarm $timeout;
140
    while (!$self->{account}) {
141
    while (!$self->{account}) {
141
        $input = read_request();
142
        $input = read_request();
142
        if (!$input) {
143
        if (!$input) {
Lines 193-200 sub telnet_transport { Link Here
193
    my $account = undef;
194
    my $account = undef;
194
    my $input;
195
    my $input;
195
    my $config  = $self->{config};
196
    my $config  = $self->{config};
196
	my $timeout = $self->{service}->{timeout} || $config->{timeout} || 30;
197
    my $timeout = $self->get_timeout({ transport => 1 });
197
	syslog("LOG_DEBUG", "telnet_transport: timeout is %s", $timeout);
198
    syslog("LOG_DEBUG", "telnet_transport: timeout is $timeout");
198
199
199
    eval {
200
    eval {
200
	local $SIG{ALRM} = sub { die "telnet_transport: Timed Out ($timeout seconds)!\n"; };
201
	local $SIG{ALRM} = sub { die "telnet_transport: Timed Out ($timeout seconds)!\n"; };
Lines 256-262 sub sip_protocol_loop { Link Here
256
    my $self = shift;
257
    my $self = shift;
257
    my $service = $self->{service};
258
    my $service = $self->{service};
258
    my $config  = $self->{config};
259
    my $config  = $self->{config};
259
    my $timeout = $self->{service}->{timeout} || $config->{timeout} || 30;
260
    my $timeout = $self->get_timeout({ client => 1 });
260
261
261
    # The spec says the first message will be:
262
    # The spec says the first message will be:
262
    #     SIP v1: SC_STATUS
263
    #     SIP v1: SC_STATUS
Lines 343-347 sub read_request { Link Here
343
      syslog( 'LOG_INFO', "INPUT MSG: '$buffer'" );
344
      syslog( 'LOG_INFO', "INPUT MSG: '$buffer'" );
344
      return $buffer;
345
      return $buffer;
345
}
346
}
347
348
# $server->get_timeout({ $type => 1, fallback => $fallback });
349
#     where $type is transport | client | policy
350
#
351
# Centralizes all timeout logic.
352
# Transport refers to login process, client to active connections.
353
# Policy timeout is transaction timeout (used in ACS status message).
354
#
355
# Fallback is optional. If you do not pass transport, client or policy,
356
# you will get fallback or hardcoded default.
357
358
sub get_timeout {
359
    my ( $server, $params ) = @_;
360
    my $fallback = $params->{fallback} || 30;
361
    my $service = $server->{service} // {};
362
    my $config = $server->{config} // {};
363
364
    if( $params->{transport} ||
365
        ( $params->{client} && !exists $service->{client_timeout} )) {
366
        # We do not allow zero values here.
367
        # Note: config/timeout seems to be deprecated.
368
        return $service->{timeout} || $config->{timeout} || $fallback;
369
370
    } elsif( $params->{client} ) {
371
        # We know that client_timeout exists now.
372
        # We do allow zero values here to indicate no timeout.
373
        return 0 if $service->{client_timeout} =~ /^0+$|\D/;
374
        return $service->{client_timeout};
375
376
    } elsif( $params->{policy} ) {
377
        my $policy = $server->{policy} // {};
378
        my $rv = sprintf( "%03d", $policy->{timeout} // 0 );
379
        if( length($rv) != 3 ) {
380
            syslog( "LOG_ERR", "Policy timeout has wrong size: '%s'", $rv );
381
            return '000';
382
        }
383
        return $rv;
384
385
    } else {
386
        return $fallback;
387
    }
388
}
389
346
1;
390
1;
391
347
__END__
392
__END__
(-)a/C4/SIP/Sip/MsgType.pm (-6 / +1 lines)
Lines 1505-1518 sub send_acs_status { Link Here
1505
    $ACS_renewal_policy = sipbool( $policy->{renewal} );
1505
    $ACS_renewal_policy = sipbool( $policy->{renewal} );
1506
    $status_update_ok   = sipbool( $ils->status_update_ok );
1506
    $status_update_ok   = sipbool( $ils->status_update_ok );
1507
    $offline_ok         = sipbool( $ils->offline_ok );
1507
    $offline_ok         = sipbool( $ils->offline_ok );
1508
    $timeout            = sprintf( "%03d", $policy->{timeout} );
1508
    $timeout            = $server->get_timeout({ policy => 1 });
1509
    $retries            = sprintf( "%03d", $policy->{retries} );
1509
    $retries            = sprintf( "%03d", $policy->{retries} );
1510
1510
1511
    if ( length($timeout) != 3 ) {
1512
        syslog( "LOG_ERR", "handle_acs_status: timeout field wrong size: '%s'", $timeout );
1513
        $timeout = '000';
1514
    }
1515
1516
    if ( length($retries) != 3 ) {
1511
    if ( length($retries) != 3 ) {
1517
        syslog( "LOG_ERR", "handle_acs_status: retries field wrong size: '%s'", $retries );
1512
        syslog( "LOG_ERR", "handle_acs_status: retries field wrong size: '%s'", $retries );
1518
        $retries = '000';
1513
        $retries = '000';
(-)a/t/db_dependent/SIP/SIPServer.t (-1 / +78 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This test is db dependent: SIPServer needs MsgType which needs Auth.
4
# And Auth needs config vars and prefences in its BEGIN block.
5
6
# This file is part of Koha.
7
#
8
# Koha is free software; you can redistribute it and/or modify it
9
# under the terms of the GNU General Public License as published by
10
# the Free Software Foundation; either version 3 of the License, or
11
# (at your option) any later version.
12
#
13
# Koha is distributed in the hope that it will be useful, but
14
# WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
17
#
18
# You should have received a copy of the GNU General Public License
19
# along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21
use Modern::Perl;
22
23
use Test::More tests => 1;
24
use Test::Warn;
25
26
my ( $mockConfig, $mockPrefork );
27
28
BEGIN {
29
    # In order to test SIPServer::get_timeout, we need to mock
30
    # Configuration->new and PreFork->run.
31
    use Test::MockModule;
32
    use C4::SIP::Sip::Configuration;
33
    $mockConfig = Test::MockModule->new( 'C4::SIP::Sip::Configuration' );
34
    $mockConfig->mock( 'new', sub { return {}; } );
35
    use Net::Server::PreFork;
36
    $mockPrefork = Test::MockModule->new( 'Net::Server::PreFork' );
37
    $mockPrefork->mock( 'run', sub {} );
38
}
39
40
use C4::SIP::SIPServer;
41
42
# Start testing !
43
# TODO We should include more tests here.
44
45
subtest 'Get_timeout' => sub {
46
    plan tests => 11;
47
48
    my $server = { policy => { timeout => 1 },
49
                   config => { timeout => 2 },
50
                   service => {
51
                       timeout => 3,
52
                       client_timeout => 4,
53
                   },
54
    };
55
56
    is( C4::SIP::SIPServer::get_timeout(), 30, "Default fallback" );
57
    is( C4::SIP::SIPServer::get_timeout( undef, { fallback => 25 } ), 25, "Fallback parameter" );
58
    is( C4::SIP::SIPServer::get_timeout( $server, { transport => 1 } ), 3, "Transport value" );
59
    is( C4::SIP::SIPServer::get_timeout( $server, { client => 1 } ), 4, "Client value" );
60
    is( C4::SIP::SIPServer::get_timeout( $server, { policy => 1 } ), '001', "Policy value" );
61
62
    delete $server->{policy}->{timeout};
63
    is( C4::SIP::SIPServer::get_timeout( $server, { policy => 1 } ), '000', "No policy" );
64
65
    $server->{service}->{client_timeout} = '0';
66
    is( C4::SIP::SIPServer::get_timeout( $server, { client => 1 } ), 0, "Client zero" );
67
    $server->{service}->{client_timeout} = 'no';
68
    is( C4::SIP::SIPServer::get_timeout( $server, { client => 1 } ), 0, "Client no" );
69
    delete $server->{service}->{client_timeout};
70
    is( C4::SIP::SIPServer::get_timeout( $server, { client => 1 } ), 3, "Fallback to service" );
71
72
    delete $server->{service}->{timeout};
73
    is( C4::SIP::SIPServer::get_timeout( $server, { transport => 1 } ), 2, "Back to old config" );
74
    delete $server->{config}->{timeout};
75
    is( C4::SIP::SIPServer::get_timeout( $server, { transport => 1 } ), 30, "Fallback again" );
76
};
77
78
1;

Return to bug 15006