line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# -*- Mode: Perl -*- |
2
|
|
|
|
|
|
|
# Server.pm -- |
3
|
|
|
|
|
|
|
# ITIID : $ITI$ $Header $__Header$ |
4
|
|
|
|
|
|
|
# Author : Ulrich Pfeifer |
5
|
|
|
|
|
|
|
# Created On : Sat Sep 28 13:53:36 1996 |
6
|
|
|
|
|
|
|
# Last Modified By: Ulrich Pfeifer |
7
|
|
|
|
|
|
|
# Last Modified On: Tue Apr 1 13:23:28 1997 |
8
|
|
|
|
|
|
|
# Language : CPerl |
9
|
|
|
|
|
|
|
# Update Count : 154 |
10
|
|
|
|
|
|
|
# Status : Unknown, Use with caution! |
11
|
|
|
|
|
|
|
# |
12
|
|
|
|
|
|
|
# (C) Copyright 1996, Universität Dortmund, all rights reserved. |
13
|
|
|
|
|
|
|
# |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
package NNML::Server; |
16
|
1
|
|
|
1
|
|
676
|
use vars qw($VERSION @ISA @EXPORT); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
72
|
|
17
|
1
|
|
|
1
|
|
601
|
use NNML::Connection; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
46
|
|
18
|
1
|
|
|
1
|
|
7
|
use NNML::Config qw($Config); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
106
|
|
19
|
1
|
|
|
1
|
|
1223
|
use IO::Socket; |
|
1
|
|
|
|
|
36723
|
|
|
1
|
|
|
|
|
5
|
|
20
|
1
|
|
|
1
|
|
702
|
use IO::Select; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
51
|
|
21
|
1
|
|
|
1
|
|
535
|
use NNML::Handle; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
43
|
|
22
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
621
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
require Exporter; |
25
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
26
|
|
|
|
|
|
|
@EXPORT = qw(server unspool); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
$VERSION = do{my @r=(q$Revision: 1.13 $=~/(\d+)/g);sprintf "%d."."%02d"x$#r,@r}; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub server { |
31
|
0
|
|
|
0
|
0
|
|
my %opt = @_; |
32
|
0
|
|
0
|
|
|
|
my $port = $opt{port} || $Config->port; |
33
|
|
|
|
|
|
|
|
34
|
0
|
0
|
|
|
|
|
if (exists $opt{base}) { |
35
|
0
|
|
|
|
|
|
$Config->base($opt{base}); |
36
|
|
|
|
|
|
|
} |
37
|
0
|
|
|
|
|
|
NNML::Auth::_update; # just for the message |
38
|
0
|
|
|
|
|
|
my $lsn = new NNML::Handle(Reuse => 1, |
39
|
|
|
|
|
|
|
Listen => 5, |
40
|
|
|
|
|
|
|
LocalPort => $port, |
41
|
|
|
|
|
|
|
Proto => 'tcp'); |
42
|
0
|
0
|
|
|
|
|
die "Could not connect to port $port: $!\n" unless defined $lsn; |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
|
my $SEL = new IO::Select( $lsn ); |
45
|
0
|
|
|
|
|
|
my %CON; |
46
|
|
|
|
|
|
|
my $fh; |
47
|
0
|
|
|
|
|
|
my @ready; |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
print STDERR "listening on port $port\n"; |
50
|
|
|
|
|
|
|
|
51
|
0
|
|
|
|
|
|
while(1) { |
52
|
0
|
|
|
|
|
|
@ready = $SEL->can_read; |
53
|
|
|
|
|
|
|
REQUEST: |
54
|
0
|
|
|
|
|
|
foreach $fh (@ready) { |
55
|
0
|
0
|
|
|
|
|
if($fh == $lsn) { |
56
|
0
|
|
|
|
|
|
my $new = $lsn->accept; # Create a new socket |
57
|
0
|
|
|
|
|
|
$CON{$new} = new NNML::Connection $new, $VERSION; |
58
|
0
|
|
|
|
|
|
$SEL->add($new); |
59
|
|
|
|
|
|
|
} else { |
60
|
0
|
|
|
|
|
|
my ($cmd, $func, @args); |
61
|
0
|
|
|
|
|
|
my $fno = fileno($fh); |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
|
$cmd = $fh->getline(); |
64
|
0
|
|
|
|
|
|
($func, @args) = split ' ', $cmd; |
65
|
0
|
0
|
|
|
|
|
unless (fileno($fh)) { |
66
|
|
|
|
|
|
|
# client has closed connection without sending 'quitt' |
67
|
0
|
|
|
|
|
|
printf STDERR "Shuttig down $fh(%d)\n", $fno; |
68
|
0
|
|
|
|
|
|
delete $CON{$fh}; |
69
|
0
|
|
|
|
|
|
$SEL->remove($fno); |
70
|
0
|
|
|
|
|
|
next REQUEST; |
71
|
|
|
|
|
|
|
} |
72
|
0
|
|
|
|
|
|
$func = lc($func); |
73
|
0
|
0
|
|
|
|
|
if ($func eq 'shut') { # shut down the server |
74
|
0
|
0
|
|
|
|
|
if (NNML::Auth::perm($CON{$fh}, $func)) { |
75
|
0
|
|
|
|
|
|
my $fx; |
76
|
0
|
|
|
|
|
|
print STDERR "Going down\n"; |
77
|
0
|
|
|
|
|
|
for $fx (keys %CON) { |
78
|
0
|
|
|
|
|
|
$CON{$fx}->msg(400); |
79
|
0
|
|
|
|
|
|
$CON{$fx}->close; |
80
|
0
|
|
|
|
|
|
delete $CON{$fx}; |
81
|
|
|
|
|
|
|
} |
82
|
0
|
|
|
|
|
|
$SEL->remove($lsn); |
83
|
0
|
|
|
|
|
|
$lsn->close(); |
84
|
0
|
|
|
|
|
|
return; |
85
|
|
|
|
|
|
|
} else { |
86
|
0
|
|
|
|
|
|
$CON{$fh}->msg(480); |
87
|
0
|
|
|
|
|
|
next REQUEST; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
} else { |
90
|
0
|
|
|
|
|
|
$func = $CON{$fh}->dispatch($func, @args); |
91
|
0
|
0
|
|
|
|
|
if ($func eq 'quit') { |
92
|
0
|
|
|
|
|
|
print STDERR "closed\n"; |
93
|
0
|
|
|
|
|
|
$SEL->remove($fh); |
94
|
0
|
|
|
|
|
|
$CON{$fh}->close; |
95
|
0
|
|
|
|
|
|
delete $CON{$fh}; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
1; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
__END__ |