bbclient.cpp

00001
00002 /***************************************************************************
00003  *  bbclient.cpp - implementation of bbclient for Fountain, only for sync of
00004  *                 aliveness
00005  *
00006  *  Generated: Wed Feb 08 12:47:29 2006
00007  *  Copyright  2005-2006  Tim Niemueller [www.niemueller.de]
00008  *
00009  ****************************************************************************/
00010
00011 /*  This program is free software; you can redistribute it and/or modify
00012  *  it under the terms of the GNU General Public License as published by
00013  *  the Free Software Foundation; either version 2 of the License, or
00014  *  (at your option) any later version.
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL file in the doc directory.
00022  */
00023 
00024 /// @cond RCSoftX
00025 
00026 #include <apps/fountain/bbclient.h>
00027 #include <utils/system/console_colors.h>
00028
00029 using namespace std;
00030
00031 // ==============================================================================
00032 // Constructor
00033 FirevisionFountainBBClient::FirevisionFountainBBClient(int argc, char* argv[]) :
00034   bb::ClientAppl(argc, argv)
00035 {
00036   BBOperate();
00037   msg_prefix = cblue + "FirevisionFountainBBClient: " + cnormal;
00038   cout << msg_prefix << "My name is " << argv[0] << endl;
00039
00040   alive_clients.clear();
00041 }
00042
00043
00044 // ==============================================================================
00045 // Destructor
00046 FirevisionFountainBBClient::~FirevisionFountainBBClient()
00047 {
00048   Exit();
00049 }
00050
00051
00052
00053 // =============================================================================
00054 // "Init"
00055 void FirevisionFountainBBClient::Init ()
00056 {
00057   hostname = GetEnvVar("HOSTNAME", "");
00058   BBSetNr( hostname );
00059   BBOperate();
00060
00061   alive_server = new bbClients::Alive_Server("firevision_fountain", hostname);
00062   BBRegisterObj( alive_server );
00063   BBOperate();
00064
00065   cout << msg_prefix << "Connected to BB on " << hostname << endl;
00066
00067   alive_server->PingAlive();
00068   alive_server->UpdateBB();
00069   BBOperate();
00070 }
00071
00072
00073 // "Loop"
00074 void
00075 FirevisionFountainBBClient::Loop(int Count)
00076 {
00077   alive.clear();
00078   map< string, bbClients::Alive_Client * >::iterator acit;
00079   for (acit = alive_clients.begin(); acit != alive_clients.end(); ++acit) {
00080     (*acit).second->Update();
00081   }
00082   alive_server->PingAlive();
00083   alive_server->UpdateBB();
00084   BBOperate();
00085   for (acit = alive_clients.begin(); acit != alive_clients.end(); ++acit) {
00086     alive[(*acit).first] = (*acit).second->GetIsAlive(3);
00087   }
00088 }
00089
00090
00091 // ===================================================================================
00092 // "Exit"
00093 void
00094 FirevisionFountainBBClient::Exit()
00095 {
00096   map< string, bbClients::Alive_Client * >::iterator acit;
00097   for (acit = alive_clients.begin(); acit != alive_clients.end(); ++acit) {
00098     delete (*acit).second;
00099   }
00100   alive_clients.clear();
00101 }
00102
00103
00104
00105 void
00106 FirevisionFountainBBClient::addAliveClient(string program)
00107 {
00108   if (alive_clients.find( program ) == alive_clients.end()) {
00109
00110     bbClients::Alive_Client *alive_client;
00111
00112     alive_client = new bbClients::Alive_Client(program, hostname);
00113     BBRegisterObj( alive_client );
00114     BBOperate();
00115
00116     alive_clients[program] = alive_client;
00117   }
00118 }
00119
00120
00121 bool
00122 FirevisionFountainBBClient::isAlive(string program)
00123 {
00124   if (alive_clients.find( program ) == alive_clients.end()) {
00125     addAliveClient(program);
00126     alive_clients[program]->Update();
00127     BBOperate();
00128     alive[program] = alive_clients[program]->GetIsAlive(-1);
00129   }
00130
00131   // has to be done twice to yield correct results
00132   return alive[program];
00133 }
00134 
00135 /// @endcond