Now the listeners are able to make a round robin between the clients
[breadcrumbs.git] / src / lib / Bcd / Clients / ClientsListeners.pm
blobc075b6eaad597761947b5df3e280fccc9b23d042
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
19 # 02110-1301, USA.
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.
28 use strict;
29 use warnings;
30 use threads;
31 use threads::shared;
32 use Class::Singleton;
34 use Bcd::Clients::Listener;
35 use Data::Dumper;
37 use vars qw(@ISA);
38 @ISA = qw(Class::Singleton);
39 use constant EOL => "\015\012";
41 use constant{
42 # Some constants to initialize the number of threads in this program...
43 INITAL_LISTENERS => 1,
44 MAX_LISTENERS => 5,
49 sub _new_instance{
50 my ($class, $script, $files_to_delete, $queue_of_files_to_delete) = @_;
52 my $self = {};
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);
63 return $self;
66 sub _create_the_listeners{
68 my ($self, $script) = @_;
70 #if you want the script you have only one listener...
71 my @listeners;
73 my $listeners_num;
75 if ($script == 1) {
76 $listeners_num = 1;
77 } else {
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});
119 return;
121 } else {
122 #I cannot create another listener...
123 #pick one at random
124 my $index = int(rand(scalar(@{$self->{listeners}})));
125 $self->{listeners}->[$index]->serve_this_client($client, $self->{files_to_delete});