listen on IP address and port
[virtual-ldap] / bin / ldap-koha.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use IO::Select;
7 use IO::Socket;
8 use lib 'lib';
9 use LDAP::Koha;
10
11 my $listen = '10.60.0.13:2389';
12
13 my $sock = IO::Socket::INET->new(
14         Listen => 5,
15         Proto => 'tcp',
16         Reuse => 1,
17         LocalAddr => $listen,
18 ) || die;
19
20 warn "# listening on $listen";
21
22 my $sel = IO::Select->new($sock);
23 my %Handlers;
24 while (my @ready = $sel->can_read) {
25         foreach my $fh (@ready) {
26                 if ($fh == $sock) {
27                         # let's create a new socket
28                         my $psock = $sock->accept;
29                         $sel->add($psock);
30                         $Handlers{*$psock} = LDAP::Koha->new($psock);
31                 } else {
32                         my $result = $Handlers{*$fh}->handle;
33                         if ($result) {
34                                 # we have finished with the socket
35                                 $sel->remove($fh);
36                                 $fh->close;
37                                 delete $Handlers{*$fh};
38                         }
39                 }
40         }
41 }
42
43 1;