1 package Bcd
::Clients
::ClientsListeners
;
3 # This file is part of the breadcrumbs daemon (bcd).
4 # Copyright (C) 2007 Pasqualino Ferrentino
6 # This program 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.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 # Contact: lino.ferrentino@yahoo.it (in Italian, English or German).
23 #this file contains the set of the listeners in our program.
24 #each listener is in its own thread.
26 #this file is a singleton.
34 use Bcd
::Clients
::Listener
;
38 @ISA = qw(Class::Singleton);
39 use constant EOL
=> "\015\012";
42 # Some constants to initialize the number of threads in this program...
43 INITAL_LISTENERS
=> 1,
50 my ($class, $script, $files_to_delete, $queue_of_files_to_delete) = @_;
54 bless ($self, $class);
56 $self->{script
} = $script;
57 $self->{files_to_delete
} = $files_to_delete;
58 $self->{queue_of_files_to_delete
} = $queue_of_files_to_delete;
60 $self->_create_the_listeners($script);
66 sub _create_the_listeners
{
68 my ($self, $script) = @_;
70 #if you want the script you have only one listener...
78 $listeners_num = INITAL_LISTENERS
;
81 for ( my $i = 0 ; $i < $listeners_num ; ++$i){
82 #ok, let's create them
84 my $listener = Bcd
::Clients
::Listener
->new
85 ($script, $self->{queue_of_files_to_delete
});
86 push (@listeners, $listener);
89 $self->{"listeners"} = \
@listeners;
92 #this function is called whenever a new client wants to talk to the
93 #daemon. this function can blocks the incoming client because we have
94 #not reasource available to serve it
95 sub a_new_client_has_arrived
{
97 my ($self, $client) = @_;
99 #ok, let's try to get a free listener for this client
101 foreach (@
{$self->{listeners
}}){
102 if ($_->are_you_free()){
103 #ok... this listener is free
104 $_->serve_this_client($client, $self->{files_to_delete
});
105 return; #all ok, you can return
110 #can I create another listener?
111 if ( scalar(@
{$self->{listeners
}}) < MAX_LISTENERS
){
112 #I can add a listener
113 my $listener = Bcd
::Clients
::Listener
->new
114 ($self->{script
}, $self->{queue_of_files_to_delete
});
115 push (@
{$self->{listeners
}}, $listener);
117 #This listener is obviously free
118 $listener->serve_this_client($client, $self->{files_to_delete
});
122 #I cannot create another listener...
124 my $index = int(rand(scalar(@
{$self->{listeners
}})));
125 $self->{listeners
}->[$index]->serve_this_client($client, $self->{files_to_delete
});