Added the keys for the mailer and the frequency
[breadcrumbs.git] / src / lib / Bcd / Bots / Manager.pm
blobd67c8f00c3efc6451e591f6fc038cee9e5c02b02
1 package Bcd::Bots::Manager;
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
14 # GNU 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 is the Manager of the bots... It has a main thread of execution,
24 #when started using the model
26 use strict;
27 use warnings;
29 use Bcd::Data::StatementsStash;
30 use Bcd::Bots::NewAntNestsBot;
31 use Bcd::Bots::TrustBot;
33 use constant {
34 MANAGER_THREAD_CYCLE => 2,
38 sub run{
39 my $server = shift;
41 #ok, now I have the connection, then I will have the stash
42 #HACK, HACK, I init the cache here...
43 my $stash = Bcd::Data::StatementsStash->new($server, 1);
44 my $test = $server->{test};
46 my @bots = ();
48 my $bot = Bcd::Bots::NewAntNestsBot->new($server);
50 my $bot_info = {
51 bot => $bot,
52 seconds_to_go => $bot->get_frequency(),
53 is_running => 0,
56 push(@bots, $bot_info);
58 $bot = Bcd::Bots::TrustBot->new();
60 my $bot_info2 = {
61 bot => $bot,
62 seconds_to_go => $bot->get_frequency(),
63 is_running => 0,
66 push(@bots, $bot_info2);
69 while (1){
72 my $num_bots = scalar(@bots);
73 #now, I should have the possibility to run the bot
74 for (@bots){
76 sleep(MANAGER_THREAD_CYCLE);
78 #this bot can be running or not.
79 if ( $_->{is_running} == 1){
80 my ($rules, $cycle_finished ) = $_->{bot}->go($stash);
81 if ($cycle_finished == 1){
82 #ok, this bot has finished, wait for another epoch
83 $_->{is_running} = 0;
84 $_->{seconds_to_go} = $_->{bot}->get_frequency();
86 next;
89 #not running...
90 #this is not very precise, but I don't need here precision...
91 $_->{seconds_to_go} -= MANAGER_THREAD_CYCLE * $num_bots;
93 if ($_->{seconds_to_go} <= 0){
94 #ok, let's awake this bot
95 if ($_->{bot}->awake($stash) == 1){
96 #this bot starts a new cycle
97 $_->{is_running} = 1;
98 } else {
99 #wait for another epoch
100 $_->{seconds_to_go} = $_->{bot}->get_frequency();