version 1.4, 2001/05/10 08:20:06 |
version 1.5, 2001/05/18 16:51:33 |
| |
| |
/* 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 <fcntl.h> | #include <fcntl.h> |
#include <sys/stat.h> | #include <sys/stat.h> |
| |
| #include "nel/misc/types_nl.h" |
| |
| #ifdef NL_OS_WINDOWS |
| #include <direct.h> |
| #else |
| #include <unistd.h> |
| #endif |
| |
#include <string> | #include <string> |
#include <list> | #include <list> |
| |
| |
return sit; | return sit; |
} | } |
| |
SIT findService (uint32 sid) | SIT findService (uint32 sid, bool asrt = true) |
{ | { |
SIT sit; | SIT sit; |
for (sit = Services.begin(); sit != Services.end(); sit++) | for (sit = Services.begin(); sit != Services.end(); sit++) |
{ | { |
if ((*sit).Id == sid) break; | if ((*sit).Id == sid) break; |
} | } |
| if (asrt) |
| nlassert (sit != Services.end()); |
return sit; | return sit; |
} | } |
| |
| |
class CExecuteCommandThread : public IRunnable | class CExecuteCommandThread : public IRunnable |
{ | { |
public: | public: |
string Command; | string Command, Path; |
| |
CExecuteCommandThread (string command) : Command(command) { } | CExecuteCommandThread (string command, string path = "") : Command(command), Path(path) { } |
| |
void run () | void run () |
{ | { |
nlinfo ("start executing: %s", Command.c_str()); | nlinfo ("start executing '%s' in '%s' directory", Command.c_str(), Path.c_str()); |
| |
| char oldpath[256]; |
| if (!Path.empty()) |
| { |
| _getcwd(oldpath,256); |
| _chdir(Path.c_str()); |
| } |
| |
system (Command.c_str()); | system (Command.c_str()); |
| |
| if (!Path.empty()) |
| _chdir(oldpath); |
| |
nlinfo ("end executing: %s", Command.c_str()); | nlinfo ("end executing: %s", Command.c_str()); |
} | } |
}; | }; |
| |
CNetManager::send ("AESAS", msgout); | CNetManager::send ("AESAS", msgout); |
} | } |
| |
| static void cbLog (CMessage& msgin, TSockId from, CCallbackNetBase &netbase) |
| { |
| // received an answer for a command, give it to the AS |
| |
| // broadcast the message to the admin service |
| CMessage msgout (CNetManager::getSIDA ("AESAS"), "LOG"); |
| string log; |
| msgin.serial (log); |
| msgout.serial (log); |
| CNetManager::send ("AESAS", msgout); |
| } |
| |
void serviceConnection (const string &serviceName, TSockId from, void *arg) | void serviceConnection (const string &serviceName, TSockId from, void *arg) |
{ | { |
Services.push_back (CService (from)); | Services.push_back (CService (from)); |
| |
{ | { |
{ "SID", cbServiceIdentification }, | { "SID", cbServiceIdentification }, |
{ "SR", cbServiceReady }, | { "SR", cbServiceReady }, |
| { "LOG", cbLog }, |
}; | }; |
| |
//////////////////////////////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////////////////////////////// |
| |
//////////////////////////////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////////////////////////////// |
//////////////////////////////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////////////////////////////// |
| |
| void errorMessage(string message, TSockId from) |
| { |
| CMessage msgout (CNetManager::getSIDA ("AESAS"), "ERR"); |
| msgout.serial (message); |
| CNetManager::send ("AESAS", msgout, from); |
| } |
| |
static void cbExecuteSystemCommand (CMessage& msgin, TSockId from, CCallbackNetBase &netbase) | static void cbExecuteSystemCommand (CMessage& msgin, TSockId from, CCallbackNetBase &netbase) |
{ | { |
string systemCommand; | string command; |
uint8 background; | |
msgin.serial (systemCommand); | msgin.serial (command); |
msgin.serial (background); | |
| IThread *thread = IThread::create (new CExecuteCommandThread (command)); |
| thread->start (); |
| } |
| |
| static void cbStartService (CMessage& msgin, TSockId from, CCallbackNetBase &netbase) |
| { |
| string serviceAlias, command, path; |
| msgin.serial (serviceAlias); |
| |
nlinfo ("I have to execute '%s'", systemCommand.c_str()); | nlinfo ("Starting the service alias '%s'", serviceAlias.c_str()); |
| |
executeCommand (systemCommand, background==1); | try |
| { |
| path = IService::ConfigFile.getVar(serviceAlias).asString(0); |
| command = IService::ConfigFile.getVar(serviceAlias).asString(1); |
| } |
| catch(EConfigFile &e) |
| { |
| nlwarning ("error in serviceAlias '%s' in config file (%s)", serviceAlias.c_str(), e.what()); |
| return; |
| } |
| |
| command += " >NUL:"; |
| |
| IThread *thread = IThread::create (new CExecuteCommandThread (command, path)); |
| thread->start (); |
} | } |
| |
static void cbStopService (CMessage& msgin, TSockId from, CCallbackNetBase &netbase) | static void cbStopService (CMessage& msgin, TSockId from, CCallbackNetBase &netbase) |
{ | { |
string service; | uint32 sid; |
| |
| msgin.serial (sid); |
| |
| nlinfo ("I have to stop service '%s'"); |
| |
nlinfo ("I have to stop service"); | SIT sit = findService (sid, false); |
| if (sit == Services.end()) |
| { |
| // don't find the aes, send an error message |
| errorMessage ("couldn't stop service, aes didn't find the service", from); |
| return; |
| } |
| |
| CMessage msgout (CNetManager::getSIDA("AES"), "STOPS"); |
| CNetManager::send ("AES", msgout, (*sit).SockId); |
} | } |
| |
| static void cbExecCommand (CMessage& msgin, TSockId from, CCallbackNetBase &netbase) |
| { |
| uint32 sid; |
| string command; |
| |
| msgin.serial (sid); |
| msgin.serial (command); |
| |
| SIT sit = findService (sid, false); |
| if (sit == Services.end()) |
| { |
| // don't find the aes, send an error message |
| errorMessage ("couldn't stop service, aes didn't find the service", from); |
| return; |
| } |
| |
| CMessage msgout (CNetManager::getSIDA("AES"), "EXEC_COMMAND"); |
| msgout.serial (command); |
| CNetManager::send ("AES", msgout, (*sit).SockId); |
| } |
| |
| void loadAndSendServicesAliasList (CConfigFile::CVar &var); |
| |
void cbASServiceConnection (const string &serviceName, TSockId from, void *arg) | void cbASServiceConnection (const string &serviceName, TSockId from, void *arg) |
{ | { |
// new admin service, send him all out info about services | // new admin service, send him all out info about services |
| |
msgout.serial ((*sit).Ready); | msgout.serial ((*sit).Ready); |
} | } |
CNetManager::send ("AESAS", msgout, from); | CNetManager::send ("AESAS", msgout, from); |
| |
| loadAndSendServicesAliasList (IService::ConfigFile.getVar ("Services")); |
} | } |
| |
TCallbackItem AESASCallbackArray[] = | TCallbackItem AESASCallbackArray[] = |
{ | { |
{ "ESC", cbExecuteSystemCommand }, | { "SYS", cbExecuteSystemCommand }, |
{ "SS", cbStopService }, | { "STARTS", cbStartService }, |
| { "STOPS", cbStopService }, |
| { "EXEC_COMMAND", cbExecCommand }, |
}; | }; |
| |
//////////////////////////////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////////////////////////////// |
| |
//////////////////////////////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////////////////////////////// |
//////////////////////////////////////////////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////////////////////////////////////////////// |
| |
| void loadAndSendServicesAliasList (CConfigFile::CVar &services) |
| { |
| vector<string> servicesaliaslist; |
| for (sint i = 0 ; i < services.size (); i++) |
| { |
| servicesaliaslist.push_back (services.asString(i)); |
| } |
| |
| CMessage msgout2 (CNetManager::getSIDA ("AESAS"), "SAL"); |
| msgout2.serialCont (servicesaliaslist); |
| CNetManager::send ("AESAS", msgout2, 0); |
| } |
| |
| |
class CAdminExecutorService : public IService | class CAdminExecutorService : public IService |
{ | { |
public: | public: |
| |
CNetManager::setConnectionCallback ("AESAS", cbASServiceConnection, NULL); | CNetManager::setConnectionCallback ("AESAS", cbASServiceConnection, NULL); |
CNetManager::addServer ("AESAS", 49996); | CNetManager::addServer ("AESAS", 49996); |
CNetManager::addCallbackArray ("AESAS", AESASCallbackArray, sizeof(AESASCallbackArray)/sizeof(AESASCallbackArray[0])); | CNetManager::addCallbackArray ("AESAS", AESASCallbackArray, sizeof(AESASCallbackArray)/sizeof(AESASCallbackArray[0])); |
| |
| ConfigFile.setCallback ("Services", loadAndSendServicesAliasList); |
| loadAndSendServicesAliasList (IService::ConfigFile.getVar ("Services")); |
} | } |
| |
bool update () | bool update () |