version 1.1, 2001/05/02 12:36:39 |
version 1.5, 2001/05/31 16:44:54 |
| |
| |
/* Copyright, 2000 Nevrax Ltd. | /* Copyright, 2000 Nevrax Ltd. |
* | * |
* This file is part of NEVRAX D.T.C. SYSTEM. | * This file is part of NEVRAX NeL Network Services. |
* NEVRAX D.T.C. SYSTEM is free software; you can redistribute it and/or modify | * NEVRAX NeL Network Services is free software; you can redistribute it and/or modify |
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by |
* the Free Software Foundation; either version 2, or (at your option) | * the Free Software Foundation; either version 2, or (at your option) |
* any later version. | * any later version. |
* | * |
* NEVRAX D.T.C. SYSTEM is distributed in the hope that it will be useful, but | * NEVRAX NeL Network Services is distributed in the hope that it will be useful, but |
* WITHOUT ANY WARRANTY; without even the implied warranty of | * WITHOUT ANY WARRANTY; without even the implied warranty of |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
* General Public License for more details. | * General Public License for more details. |
* | * |
* You should have received a copy of the GNU General Public License | * You should have received a copy of the GNU General Public License |
* along with NEVRAX D.T.C. SYSTEM; see the file COPYING. If not, write to the | * along with NEVRAX NeL Network Services; see the file COPYING. If not, write to the |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
* MA 02111-1307, USA. | * MA 02111-1307, USA. |
*/ | */ |
| |
#include <conio.h> | |
| |
#include <string> | #include <string> |
#include <list> | #include <list> |
| |
| #include "nel/net/service.h" |
#include "nel/misc/debug.h" | #include "nel/misc/debug.h" |
#include "nel/misc/config_file.h" | #include "nel/misc/config_file.h" |
| |
#include "nel/net/service.h" | |
#include "nel/net/net_manager.h" | #include "nel/net/net_manager.h" |
| |
| |
using namespace std; | using namespace std; |
using namespace NLMISC; | using namespace NLMISC; |
using namespace NLNET; | using namespace NLNET; |
| |
#include <process.h> | //////////////////////// |
| |
| |
struct CService | struct CService |
{ | { |
CService(TSockId s) : AESSockId(s), AESId(NextAESId++) { } | CService () : Id(0xFFFFFFFF), Ready(false), Connected(false), InConfig(false) { } |
| |
TSockId AESSockId; // TSockId of the AES that have this service | uint32 Id; /// uint32 to identify the service |
uint32 AESId; // TSockId of the AES that have this service | string AliasName; /// alias of the service used in the AES and AS to find him (unique per AES) |
uint32 SId; // uint32 to identify the service | string ShortName; /// name of the service in short format ("NS" for example) |
string ShortName; | string LongName; /// name of the service in long format ("naming_service") |
string LongName; | bool Ready; /// true if the service is ready |
| bool Connected; /// true if the service is connected to the AES |
| bool InConfig; /// true if the service is in the configuration |
| }; |
| |
private: | typedef list<CService> TServices; |
| typedef list<CService>::iterator SIT; |
| |
| struct CAdminExecutorService |
| { |
| CAdminExecutorService () : Id(NextId++), SockId(NULL), Connected(false) { } |
| |
| TSockId SockId; /// connection to the AES |
| uint32 Id; /// uint32 to identify the AES where the service is running |
| |
| string ServerAlias; /// name of the layer4 connection, used to send message to this AES |
| string ServerAddr; /// address in a string format (only the ip) |
| bool Connected; /// true if the AES is connected |
| |
| TServices Services; |
| |
| vector<string> ServiceAliasList; |
| |
| SIT findService (uint32 sid, bool asrt = true) |
| { |
| SIT sit; |
| for (sit = Services.begin(); sit != Services.end(); sit++) |
| if ((*sit).Id == sid) |
| break; |
| |
| if (asrt) |
| nlassert (sit != Services.end()); |
| return sit; |
| } |
| |
| SIT findService (const string &alias, bool asrt = true) |
| { |
| SIT sit; |
| for (sit = Services.begin(); sit != Services.end(); sit++) |
| if ((*sit).AliasName == alias) |
| break; |
| |
| if (asrt) |
| nlassert (sit != Services.end()); |
| return sit; |
| } |
| |
static uint32 NextAESId; | private: |
| static uint32 NextId; |
}; | }; |
| |
uint32 CService::NextAESId = 0; | uint32 CAdminExecutorService::NextId = 1; |
| |
| typedef list<CAdminExecutorService> TAdminExecutorServices; |
| typedef list<CAdminExecutorService>::iterator AESIT; |
| |
| TAdminExecutorServices AdminExecutorServices; |
| |
list<CService> Services; | ///////////////// |
typedef list<CService>::iterator sit; | |
| |
sit find (TSockId aesid, uint32 sid) | AESIT findAdminExecutorService (uint32 aesid, bool asrt = true) |
{ | { |
sit it; | AESIT aesit; |
for (it = Services.begin(); it != Services.end(); it++) | for (aesit = AdminExecutorServices.begin(); aesit != AdminExecutorServices.end(); aesit++) |
| if ((*aesit).Id == aesid) |
| break; |
| |
| if (asrt) |
| nlassert (aesit != AdminExecutorServices.end()); |
| return aesit; |
| } |
| |
| AESIT findAdminExecutorService (string ServerAlias, bool asrt = true) |
{ | { |
if ((*it).AESSockId== aesid && (*it).SId == sid) break; | AESIT aesit; |
| for (aesit = AdminExecutorServices.begin(); aesit != AdminExecutorServices.end(); aesit++) |
| if ((*aesit).ServerAlias == ServerAlias) |
| break; |
| |
| if (asrt) |
| nlassert (aesit != AdminExecutorServices.end()); |
| return aesit; |
| } |
| |
| void displayServices () |
| { |
| for (AESIT aesit = AdminExecutorServices.begin(); aesit != AdminExecutorServices.end(); aesit++) |
| { |
| for (SIT sit = (*aesit).Services.begin(); sit != (*aesit).Services.end(); sit++) |
| { |
| nlinfo ("> %s %s %s %d %d", (*aesit).SockId->asString().c_str(), (*sit).ShortName.c_str(), (*sit).LongName.c_str(), (*aesit).Id, (*sit).Id); |
} | } |
return it; | |
} | } |
| } |
| |
| |
sit find (uint32 aesid, uint32 sid) | //////////////////////////////////////////////////////////////////////////////////////////////////////// |
| //////////////////////////////////////////////////////////////////////////////////////////////////////// |
| /////////////////// SCRIPT MANAGER ///////////////////////////////////////////////////////////////////// |
| //////////////////////////////////////////////////////////////////////////////////////////////////////// |
| //////////////////////////////////////////////////////////////////////////////////////////////////////// |
| |
| bool StartAllServices = false; |
| uint32 StartAllServicesPos; |
| |
| void doNextStartAllServicesStep () |
{ | { |
sit it; | nlassert (StartAllServices); |
for (it = Services.begin(); it != Services.end(); it++) | |
| // get the script |
| try |
{ | { |
if ((*it).AESId == aesid && (*it).SId == sid) break; | CConfigFile::CVar &script = IService::ConfigFile.getVar("Services"); |
| |
| // check the position |
| |
| if (StartAllServicesPos*2 >= (uint32)script.size()) |
| { |
| StartAllServices = false; |
| nlinfo("end of the script"); |
| // todo send that the script is finish to the admin |
| return; |
} | } |
return it; | |
| // get the script line |
| |
| string serverAlias; |
| string serviceAlias; |
| |
| try |
| { |
| serverAlias = script.asString (StartAllServicesPos*2); |
| serviceAlias = script.asString (StartAllServicesPos*2+1); |
| } |
| catch(EBadSize &) |
| { |
| nlwarning ("'Services' variable does not contains a good number of entries (must be a multiple of 2)"); |
| StartAllServices = false; |
| return; |
} | } |
| |
/* | AESIT aesit = findAdminExecutorService (serverAlias, false); |
NLMISC_COMMAND (start, "start a service", "<service_name>") | if (aesit == AdminExecutorServices.end()) |
{ | { |
if(args.size() != 1 && args.size() != 2) return false; | StartAllServices = false; |
| nlwarning("don't find the server"); |
| return; |
| } |
| |
CMessage msgout (CNetManager::getSIDA("AES"), "ESC"); | // check if the service is not currently running |
msgout.serial (args[1]); | |
| |
uint8 background = 0; | StartAllServicesPos++; |
| |
if (args.size() == 2) | SIT sit = (*aesit).findService (serviceAlias); |
| if ((*sit).Connected) |
{ | { |
if (args[1] == "&") | // the service is already running, go to the next process |
uint8 background = 1; | doNextStartAllServicesStep (); |
else | |
return false; | |
} | } |
| else |
| { |
| // send the resquest to the AES |
| |
msgout.serial (background); | CMessage msgout (CNetManager::getSIDA((*aesit).ServerAlias), "STARTS"); |
CNetManager::send ("AES", msgout); | msgout.serial (serviceAlias); |
return true; | CNetManager::send ((*aesit).ServerAlias, msgout); |
| } |
| } |
| catch(EUnknownVar&) |
| { |
| nlwarning ("'Services' variable is not found"); |
| StartAllServices = false; |
| return; |
| } |
} | } |
| |
| void initStartAllServices () |
| { |
| if (StartAllServices) |
| { |
| nlwarning("already running a script, reset it"); |
| StartAllServices = false; |
| } |
| |
CLog logstdout; | try |
CStdDisplayer dispstdout; | { |
| CConfigFile::CVar &script = IService::ConfigFile.getVar("Services"); |
| |
NLMISC_COMMAND (stop, "stop a service", "<aesid> <sid>") | for (sint i = 0 ; i < script.size (); i+=2) |
| { |
| string serverAlias = script.asString(i); |
| AESIT aesit = findAdminExecutorService (serverAlias, false); |
| if (aesit == AdminExecutorServices.end()) |
{ | { |
if(args.size() != 3) return false; | nlwarning("aes '%s' not running, can't run the script", serverAlias.c_str()); |
| return; |
| } |
| } |
| } |
| catch(EConfigFile &) |
| { |
| nlwarning ("bad config file"); |
| return; |
| } |
| |
| StartAllServicesPos = 0; |
| StartAllServices = true; |
| |
CMessage msgout (CNetManager::getSIDA("AES"), "SS"); | doNextStartAllServicesStep(); |
msgout.serial (args[2]); | |
CNetManager::send ("AES", msgout); | |
} | } |
*/ | |
| |
//////////////////////////////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////////////////////////////// |
//////////////////////////////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////////////////////////////// |
| |
nlinfo("end of command result"); | nlinfo("end of command result"); |
} | } |
| |
| static void cbServiceList (CMessage& msgin, TSockId from, CCallbackNetBase &netbase) |
| { |
| /* // get the service list from the admin exec and send the list to all admin client |
| CAdminExecutorService *aes = (CAdminExecutorService*) from->appId(); |
| aes->Services.clear(); |
| |
| CMessage msgout (CNetManager::getSIDA ("AS"), "SL"); |
| |
| uint32 nbaes = 1; |
| msgout.serial (nbaes); |
| msgout.serial (aes->Id); |
| |
| uint32 nbs; |
| msgin.serial (nbs); |
| msgout.serial (nbs); |
| |
| for (uint32 i = 0; i < nbs; i++) |
| { |
| uint32 sid; |
| msgin.serial(sid); |
| |
| aes->Services.push_back (CService(sid)); |
| CService *s = &(aes->Services.back()); |
| |
| msgin.serial(s->ShortName, s->LongName, s->Ready); |
| |
| msgout.serial (s->Id, s->ShortName, s->LongName, s->Ready); |
| } |
| CNetManager::send ("AS", msgout, 0); |
| |
| displayServices (); |
| */} |
| |
| static void cbServiceAliasList (CMessage& msgin, TSockId from, CCallbackNetBase &netbase) |
| { |
| // get the service list from the admin exec and send the list to all admin client |
| CAdminExecutorService *aes = (CAdminExecutorService*) from->appId(); |
| |
| aes->ServiceAliasList.clear (); |
| msgin.serialCont (aes->ServiceAliasList); |
| |
| CMessage msgout (CNetManager::getSIDA ("AS"), "SAL"); |
| msgout.serial (aes->Id); |
| msgout.serialCont (aes->ServiceAliasList); |
| CNetManager::send ("AS", msgout, 0); |
| |
| nlinfo("new service alias list"); |
| } |
| |
| |
static void cbServiceIdentification (CMessage& msgin, TSockId from, CCallbackNetBase &netbase) | static void cbServiceIdentification (CMessage& msgin, TSockId from, CCallbackNetBase &netbase) |
{ | { |
string ShortName, LongName; | CAdminExecutorService *aes = (CAdminExecutorService*) from->appId(); |
| |
msgin.serial (ShortName); | uint32 sid; |
msgin.serial (LongName); | string alias; |
| |
nlinfo ("%s %s %s is identified", from->asString().c_str(), ShortName.c_str(), LongName.c_str()); | msgin.serial (sid, alias); |
| |
| SIT sit; |
| if (!alias.empty()) |
| { |
| sit = aes->findService (alias, false); |
| |
| if (sit == aes->Services.end ()) |
| { |
| // the alias is not found |
| nlwarning ("new service with alias (%s) but not in my list", alias.c_str()); |
| |
| aes->Services.push_back (CService ()); |
| sit = aes->Services.end(); |
| sit--; |
| } |
| else |
| { |
| // normal case |
| } |
| } |
| else |
| { |
| sit = aes->findService (sid, false); |
| |
| if (sit == aes->Services.end ()) |
| { |
| // normal case for unknown services |
| nlwarning ("new service with alias (%s) but not in my list", alias.c_str()); |
| } |
| else |
| { |
| nlwarning ("new service without alias is already in my list with id %d", sid); |
| } |
| aes->Services.push_back (CService ()); |
| sit = aes->Services.end(); |
| sit--; |
| } |
| |
| (*sit).Id = sid; |
| (*sit).AliasName = alias; |
| (*sit).Connected = true; |
| msgin.serial ((*sit).ShortName, (*sit).LongName); |
| |
| nlinfo ("*:%d:%d is identified to be '%s' '%s' '%s'", aes->Id, sid, (*sit).AliasName.c_str(), (*sit).ShortName.c_str(), (*sit).LongName.c_str()); |
| |
| // broadcast the message to all admin client |
| CMessage msgout (CNetManager::getSIDA ("AS"), "SID"); |
| msgout.serial (aes->Id, sid, (*sit).AliasName, (*sit).ShortName, (*sit).LongName); |
| CNetManager::send ("AS", msgout, 0); |
} | } |
| |
static void cbServiceReady (CMessage& msgin, TSockId from, CCallbackNetBase &netbase) | static void cbServiceReady (CMessage& msgin, TSockId from, CCallbackNetBase &netbase) |
{ | { |
string ShortName, LongName; | CAdminExecutorService *aes = (CAdminExecutorService*) from->appId(); |
| |
| uint32 sid; |
| msgin.serial (sid); |
| |
| SIT sit = aes->findService(sid); |
| (*sit).Ready = true; |
| |
| nlinfo ("*:%d:%d is ready", aes->Id, sid); |
| |
msgin.serial (ShortName); | // broadcast the message to all admin client |
msgin.serial (LongName); | CMessage msgout (CNetManager::getSIDA ("AS"), "SR"); |
| msgout.serial (aes->Id, sid); |
| CNetManager::send ("AS", msgout, 0); |
| |
nlinfo ("%s %s %s is ready", from->asString().c_str(), ShortName.c_str(), LongName.c_str()); | // if we are in a script execution, continue |
| if (StartAllServices) |
| doNextStartAllServicesStep(); |
} | } |
| |
static void cbServiceConnection (CMessage& msgin, TSockId from, CCallbackNetBase &netbase) | static void cbServiceConnection (CMessage& msgin, TSockId from, CCallbackNetBase &netbase) |
{ | { |
nlinfo ("%s a service is connected", from->asString().c_str()); | CAdminExecutorService *aes = (CAdminExecutorService*) from->appId(); |
| |
| uint32 sid; |
| msgin.serial (sid); |
| |
| nlinfo ("*:%d:%d connected", aes->Id, sid); |
| |
| // don't do anything. we have to wait identification to add it in out lists |
| |
| /* |
| aes->Services.push_back (CService(sid)); |
| */ |
| // broadcast the message to all admin client |
| CMessage msgout (CNetManager::getSIDA ("AS"), "SC"); |
| msgout.serial (aes->Id, sid); |
| CNetManager::send ("AS", msgout, 0); |
} | } |
| |
static void cbServiceDisconnection (CMessage& msgin, TSockId from, CCallbackNetBase &netbase) | static void cbServiceDisconnection (CMessage& msgin, TSockId from, CCallbackNetBase &netbase) |
{ | { |
string ShortName, LongName; | CAdminExecutorService *aes = (CAdminExecutorService*) from->appId(); |
| |
| uint32 sid; |
| msgin.serial (sid); |
| |
| nlinfo ("*:%d:%d disconnected", aes->Id, sid); |
| |
| SIT sit = aes->findService(sid); |
| |
| // broadcast the message to all admin client |
| CMessage msgout (CNetManager::getSIDA ("AS"), "SD"); |
| msgout.serial (aes->Id, (*sit).Id); |
| CNetManager::send ("AS", msgout, 0); |
| |
| if ((*sit).InConfig) |
| { |
| (*sit).Ready = (*sit).Connected = false; |
| (*sit).Id = 0xFFFFFFFF; |
| (*sit).ShortName = (*sit).LongName = ""; |
| } |
| else |
| { |
| // erase only if it's not a service in the config |
| aes->Services.erase (sit); |
| } |
| } |
| |
| // i'm connected to a new admin executor service |
| void cbAESConnection (const string &serviceName, TSockId from, void *arg) |
| { |
| AESIT aesit = findAdminExecutorService (serviceName); |
| CAdminExecutorService *aes = &(*aesit); |
| |
| // set the appid to find the aes in O(1) |
| from->setAppId ((uint64)aes); |
| |
| aes->Connected = true; |
| nlinfo ("*:%d:* connected", aes->Id); |
| /* |
| // broadcast the message that an admin exec is connected to all admin client |
| CMessage msgout (CNetManager::getSIDA ("AS"), "AESC"); |
| msgout.serial (aes->Id); |
| CNetManager::send ("AS", msgout, 0); |
| */ |
| |
| // broadcast the new state of this AES |
| CMessage msgout (CNetManager::getSIDA ("AS"), "AES_LIST"); |
| uint32 nbaes = 1; |
| msgout.serial (nbaes); |
| msgout.serial (aes->Id, aes->ServerAlias, aes->ServerAddr, aes->Connected); |
| CNetManager::send ("AS", msgout, 0); |
| } |
| |
| // i'm disconnected to an admin executor service |
| void cbAESDisconnection (const string &serviceName, TSockId from, void *arg) |
| { |
| // get the aes with the appid |
| CAdminExecutorService *aes = (CAdminExecutorService*) from->appId(); |
| |
| aes->Connected = false; |
| |
| SIT sit; |
| for (sit = aes->Services.begin(); sit != aes->Services.end(); sit++) |
| { |
| (*sit).Id = 0xFFFFFFFF; |
| (*sit).ShortName = (*sit).LongName = ""; |
| (*sit).Ready = (*sit).Connected = false; |
| } |
| |
msgin.serial (ShortName); | nlinfo ("*:%d:* disconnected", aes->Id); |
msgin.serial (LongName); | /* |
| // broadcast the message to all admin client that an admin exec is disconnected |
| CMessage msgout (CNetManager::getSIDA ("AS"), "AESD"); |
| msgout.serial (aes->Id); |
| CNetManager::send ("AS", msgout, 0); |
| */ |
| |
nlinfo ("%s %s %s is disconnected", from->asString().c_str(), ShortName.c_str(), LongName.c_str()); | // broadcast the new state of this AES |
| CMessage msgout (CNetManager::getSIDA ("AS"), "AES_LIST"); |
| uint32 nbaes = 1; |
| msgout.serial (nbaes); |
| msgout.serial (aes->Id, aes->ServerAlias, aes->ServerAddr, aes->Connected); |
| CNetManager::send ("AS", msgout, 0); |
| } |
| |
| static void cbLog (CMessage& msgin, TSockId from, CCallbackNetBase &netbase) |
| { |
| // received an answer for a command, give it to all admin client |
| |
| // broadcast the message to the admin service |
| CMessage msgout (CNetManager::getSIDA ("AS"), "LOG"); |
| string log; |
| msgin.serial (log); |
| msgout.serial (log); |
| CNetManager::send ("AS", msgout, 0); |
} | } |
| |
| |
| |
{ | { |
{ "ESCR", cbExecuteSystemCommandResult }, | { "ESCR", cbExecuteSystemCommandResult }, |
| |
| { "SL", cbServiceList }, |
{ "SID", cbServiceIdentification }, | { "SID", cbServiceIdentification }, |
{ "SR", cbServiceReady }, | { "SR", cbServiceReady }, |
{ "SC", cbServiceConnection }, | { "SC", cbServiceConnection }, |
{ "SD", cbServiceDisconnection }, | { "SD", cbServiceDisconnection }, |
| |
| { "SAL", cbServiceAliasList }, |
| |
| { "LOG", cbLog }, |
}; | }; |
| |
| |
| |
//////////////////////////////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////////////////////////////// |
//////////////////////////////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////////////////////////////// |
| |
| void errorMessage(string message, TSockId from, CCallbackNetBase &netbase) |
| { |
| CMessage msgout (netbase.getSIDA (), "ERR"); |
| msgout.serial (message); |
| netbase.send (msgout, from); |
| } |
| |
| // |
| // A new admin client is connected. |
| // |
void clientConnection (const string &serviceName, TSockId from, void *arg) | void clientConnection (const string &serviceName, TSockId from, void *arg) |
{ | { |
// new client, send him all out info about services | // new client, send him all out info about services |
| |
nlinfo ("client %s is connected", from->asString().c_str()); | nlinfo ("client %s is connected", from->asString().c_str()); |
| |
CMessage msgout (CNetManager::getSIDA ("AESAS"), "SL"); | // |
uint32 size = (uint32)Services.size(); | // send the list of all the aes |
msgout.serial (size); | // |
for (sit it = Services.begin(); it != Services.end(); it++) | |
{ | CMessage msgout2 (CNetManager::getSIDA ("AS"), "AES_LIST"); |
msgout.serial ((*it).AESId); | AESIT aesit; |
msgout.serial ((*it).SId); | uint32 nbaes = (uint32)AdminExecutorServices.size(); |
msgout.serial ((*it).ShortName); | msgout2.serial (nbaes); |
msgout.serial ((*it).LongName); | for (aesit = AdminExecutorServices.begin(); aesit != AdminExecutorServices.end(); aesit++) |
| { |
| // send info about the AES |
| |
| msgout2.serial ((*aesit).Id, (*aesit).ServerAlias, (*aesit).ServerAddr, (*aesit).Connected); |
| } |
| CNetManager::send ("AS", msgout2, from); |
| |
| // |
| // send the list of all services |
| // |
| |
| CMessage msgout (CNetManager::getSIDA ("AS"), "SERVICE_LIST"); |
| nbaes = (uint32)AdminExecutorServices.size(); |
| msgout.serial (nbaes); |
| for (aesit = AdminExecutorServices.begin(); aesit != AdminExecutorServices.end(); aesit++) |
| { |
| msgout.serial ((*aesit).Id); |
| |
| uint32 nbs = (uint32)(*aesit).Services.size(); |
| msgout.serial (nbs); |
| |
| for (SIT sit = (*aesit).Services.begin(); sit != (*aesit).Services.end(); sit++) |
| { |
| // send info about services of the AES |
| |
| msgout.serial ((*sit).Id, (*sit).AliasName, (*sit).ShortName, (*sit).LongName); |
| msgout.serial ((*sit).Ready, (*sit).Connected, (*sit).InConfig); |
| } |
} | } |
CNetManager::send ("AS", msgout, from); | CNetManager::send ("AS", msgout, from); |
| |
| // |
| // send service alias list |
| // |
| |
| for (aesit = AdminExecutorServices.begin(); aesit != AdminExecutorServices.end(); aesit++) |
| { |
| CMessage msgout2 (CNetManager::getSIDA ("AS"), "SAL"); |
| msgout2.serial ((*aesit).Id); |
| msgout2.serialCont ((*aesit).ServiceAliasList); |
| CNetManager::send ("AS", msgout2, from); |
| } |
| } |
| |
| |
| static void cbExecuteSystemCommand (CMessage& msgin, TSockId from, CCallbackNetBase &netbase) |
| { |
| string command; |
| uint32 aesid; |
| msgin.serial (aesid); |
| msgin.serial (command); |
| |
| AESIT aesit = findAdminExecutorService (aesid, false); |
| if (aesit == AdminExecutorServices.end()) |
| { |
| // don't find the aes, send an error message |
| errorMessage ("couldn't execute command, as didn't find the aes", from, netbase); |
| return; |
| } |
| |
| // send the resquest to the AES |
| |
| CMessage msgout (CNetManager::getSIDA((*aesit).ServerAlias), "SYS"); |
| msgout.serial (command); |
| CNetManager::send ((*aesit).ServerAlias, msgout); |
| } |
| |
| |
| static void cbStartService (CMessage& msgin, TSockId from, CCallbackNetBase &netbase) |
| { |
| string serviceAlias; |
| uint32 aesid; |
| msgin.serial (aesid); |
| msgin.serial (serviceAlias); |
| |
| AESIT aesit = findAdminExecutorService (aesid, false); |
| if (aesit == AdminExecutorServices.end()) |
| { |
| // don't find the aes, send an error message |
| errorMessage ("couldn't start service, as didn't find the aes", from, netbase); |
| return; |
| } |
| |
| // send the resquest to the AES |
| |
| CMessage msgout (CNetManager::getSIDA((*aesit).ServerAlias), "STARTS"); |
| msgout.serial (serviceAlias); |
| CNetManager::send ((*aesit).ServerAlias, msgout); |
| } |
| |
| static void cbStopService (CMessage& msgin, TSockId from, CCallbackNetBase &netbase) |
| { |
| uint32 aesid, sid; |
| msgin.serial (aesid); |
| msgin.serial (sid); |
| |
| AESIT aesit = findAdminExecutorService (aesid, false); |
| if (aesit == AdminExecutorServices.end()) |
| { |
| errorMessage ("couldn't stop service, as didn't find the aes", from, netbase); |
| // don't find the aes, send an error message |
| return; |
| } |
| |
| SIT sit = (*aesit).findService (sid, false); |
| if (sit == (*aesit).Services.end()) |
| { |
| // don't find the s, send an error message |
| errorMessage ("couldn't stop service, as didn't find the service", from, netbase); |
| return; |
| } |
| |
| // send the resquest to the AES |
| |
| CMessage msgout (CNetManager::getSIDA((*aesit).ServerAlias), "STOPS"); |
| msgout.serial (sid); |
| CNetManager::send ((*aesit).ServerAlias, msgout); |
| } |
| |
| static void cbExecCommand (CMessage& msgin, TSockId from, CCallbackNetBase &netbase) |
| { |
| uint32 aesid, sid; |
| string command; |
| msgin.serial (aesid); |
| msgin.serial (sid); |
| msgin.serial (command); |
| |
| AESIT aesit = findAdminExecutorService (aesid, false); |
| if (aesit == AdminExecutorServices.end()) |
| { |
| errorMessage ("couldn't stop service, as didn't find the aes", from, netbase); |
| // don't find the aes, send an error message |
| return; |
| } |
| |
| SIT sit = (*aesit).findService (sid, false); |
| if (sit == (*aesit).Services.end()) |
| { |
| // don't find the s, send an error message |
| errorMessage ("couldn't stop service, as didn't find the service", from, netbase); |
| return; |
} | } |
| |
TCallbackItem ASCallbackArray[] = | // send the resquest to the AES |
| |
| CMessage msgout (CNetManager::getSIDA((*aesit).ServerAlias), "EXEC_COMMAND"); |
| msgout.serial (sid); |
| msgout.serial (command); |
| CNetManager::send ((*aesit).ServerAlias, msgout); |
| } |
| |
| static void cbStartAllServices (CMessage& msgin, TSockId from, CCallbackNetBase &netbase) |
{ | { |
{ "", NULL }, | initStartAllServices (); |
| } |
| |
| static void cbStopAllServices (CMessage& msgin, TSockId from, CCallbackNetBase &netbase) |
| { |
| for (AESIT aesit = AdminExecutorServices.begin(); aesit != AdminExecutorServices.end(); aesit++) |
| { |
| for (SIT sit = (*aesit).Services.begin(); sit != (*aesit).Services.end(); sit++) |
| { |
| if ((*sit).Connected) |
| { |
| CMessage msgout (CNetManager::getSIDA((*aesit).ServerAlias), "STOPS"); |
| msgout.serial ((*sit).Id); |
| CNetManager::send ((*aesit).ServerAlias, msgout); |
| } |
| } |
| } |
| } |
| |
| TCallbackItem ClientCallbackArray[] = |
| { |
| { "SYS", cbExecuteSystemCommand }, |
| { "STARTS", cbStartService }, |
| { "STOPS", cbStopService }, |
| { "EXEC_COMMAND", cbExecCommand }, |
| { "START_ALL_SERVICES", cbStartAllServices }, |
| { "STOP_ALL_SERVICES", cbStopAllServices }, |
}; | }; |
| |
| //////////////////////////////////////////////////////////////////////////////////////////////////////// |
| //////////////////////////////////////////////////////////////////////////////////////////////////////// |
| ////////////////// SERVICE IMPLEMENTATION ////////////////////////////////////////////////////////////// |
| //////////////////////////////////////////////////////////////////////////////////////////////////////// |
| //////////////////////////////////////////////////////////////////////////////////////////////////////// |
| |
class CAdminService : public IService | class CAdminService : public IService |
{ | { |
public: | public: |
| |
/// Init the service, load the universal time. | /// Init the service, load the universal time. |
void init () | void init () |
{ | { |
CNetManager::setConnectionCallback ("AS", clientConnection, NULL); | // DebugLog->addNegativeFilter ("L0:"); |
| // DebugLog->addNegativeFilter ("L1:"); |
| // DebugLog->addNegativeFilter ("L2:"); |
| |
// connec to the AES | |
CNetManager::addClient ("AES", "localhost:49996"); | |
CNetManager::addCallbackArray ("AES", AESCallbackArray, sizeof (AESCallbackArray)/sizeof(AESCallbackArray[0])); | |
| |
| CNetManager::setConnectionCallback ("AS", clientConnection, NULL); |
| |
| // |
| // Get the list of AESHosts, add in the structures and create connection to all AES |
| // |
| |
| CConfigFile::CVar &host = ConfigFile.getVar ("AESHosts"); |
| for (sint i = 0 ; i < host.size (); i+=2) |
| { |
| string serverAlias = host.asString(i); |
| string serverAddr = host.asString(i+1); |
| |
| // add to the list |
| CAdminExecutorService aes; |
| aes.ServerAlias = serverAlias; |
| aes.ServerAddr = serverAddr; |
| AdminExecutorServices.push_back (aes); |
| |
| // connect to the AES |
| CNetManager::setConnectionCallback (serverAlias, cbAESConnection, NULL); |
| CNetManager::setDisconnectionCallback (serverAlias, cbAESDisconnection, NULL); |
| CNetManager::addClient (serverAlias, serverAddr+":49996"); |
| CNetManager::addCallbackArray (serverAlias, AESCallbackArray, sizeof (AESCallbackArray)/sizeof(AESCallbackArray[0])); |
| } |
| |
| // |
| // Get the list of services in the shard |
| // |
| |
| CConfigFile::CVar &serv = ConfigFile.getVar ("Services"); |
| for (i = 0 ; i < serv.size (); i+=2) |
| { |
| string serverAlias = serv.asString(i); |
| string serviceAlias = serv.asString(i+1); |
| |
| AESIT aesit = findAdminExecutorService (serverAlias); |
| |
| // add new AES in the list |
| CService s; |
| s.AliasName = serviceAlias; |
| s.InConfig = true; |
| (*aesit).Services.push_back (s); |
| } |
} | } |
| |
bool update () | bool update () |
| |
}; | }; |
| |
| |
| // AS is a server connection to the admin client |
| // AESAS is a client connection to the admin executor |
| |
/// Naming Service | /// Naming Service |
NLNET_SERVICE_MAIN (CAdminService, "AS", "admin_service", 49995, ASCallbackArray); | NLNET_SERVICE_MAIN (CAdminService, "AS", "admin_service", 49995, ClientCallbackArray); |