mirror of
https://bitbucket.org/theswgsource/src-1.2.git
synced 2026-08-01 02:15:54 -04:00
Added TaskManager app
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
// TaskManager.cpp
|
||||
// Copyright 2000-01, Sony Online Entertainment Inc., all rights reserved.
|
||||
// Author: Justin Randall
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
#include "FirstTaskManager.h"
|
||||
#include "Console.h"
|
||||
#include <stdio.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <curses.h>
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
struct SetBufferMode
|
||||
{
|
||||
SetBufferMode();
|
||||
~SetBufferMode() {};
|
||||
};
|
||||
|
||||
SetBufferMode::SetBufferMode()
|
||||
{
|
||||
setvbuf(stdin, NULL, _IONBF, 0);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
const char Console::getNextChar()
|
||||
{
|
||||
static SetBufferMode setBufferMode;
|
||||
char result = 0;
|
||||
fd_set rfds;
|
||||
struct timeval tv;
|
||||
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(STDIN_FILENO, &rfds);
|
||||
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 0;
|
||||
|
||||
int ready = select(1, &rfds, 0, 0, &tv);
|
||||
if(ready)
|
||||
{
|
||||
result = static_cast<char>(getchar());
|
||||
if(result < 1)
|
||||
result = 0;
|
||||
}
|
||||
/*
|
||||
if(_kbhit())
|
||||
result = static_cast<char>(_getche());
|
||||
*/
|
||||
return result;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
|
||||
#include "FirstTaskManager.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
|
||||
namespace EnvironmentVariable
|
||||
{
|
||||
bool addToEnvironmentVariable(const char* key, const char* value)
|
||||
{
|
||||
bool retval = false;
|
||||
const char* oldValue = getenv(key);
|
||||
if (oldValue)
|
||||
{
|
||||
std::string s(oldValue);
|
||||
s += ":";
|
||||
s += value;
|
||||
|
||||
//Bad things happen if the first character happens to be : (ie from an empty environment string)
|
||||
const char* newValue = s.c_str();
|
||||
if (newValue[0] == ':')
|
||||
++newValue;
|
||||
|
||||
retval = (setenv(key, newValue, 1) == 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
retval = (setenv(key, value, 0) == 0);
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
bool setEnvironmentVariable(const char* key, const char* value)
|
||||
{
|
||||
return (setenv(key, value, 1) == 0);
|
||||
}
|
||||
|
||||
};
|
||||
@@ -0,0 +1,167 @@
|
||||
#include "sharedFoundation/FirstSharedFoundation.h"
|
||||
#include "ProcessSpawner.h"
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
#include <wait.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
uint32 ProcessSpawner::prefix;
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
bool tokenize (const std::string & str, std::vector<std::string> & result)
|
||||
{
|
||||
size_t end_pos = 0;
|
||||
size_t start_pos = 0;
|
||||
|
||||
result.clear ();
|
||||
|
||||
for (;;)
|
||||
{
|
||||
if (end_pos >= str.size ())
|
||||
break;
|
||||
|
||||
start_pos = str.find_first_not_of (' ', end_pos);
|
||||
|
||||
if (start_pos == str.npos) //lint !e1705
|
||||
break;
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
if (str [start_pos] == '\"')
|
||||
{
|
||||
if (++start_pos >= str.size ())
|
||||
break;
|
||||
end_pos = str.find_first_of ('\"', start_pos);
|
||||
}
|
||||
else
|
||||
end_pos = str.find_first_of (' ', start_pos);
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
|
||||
if (start_pos == end_pos)
|
||||
break;
|
||||
|
||||
if (end_pos == str.npos) //lint !e1705
|
||||
{
|
||||
result.push_back (str.substr (start_pos));
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.push_back (str.substr (start_pos, end_pos - start_pos));
|
||||
}
|
||||
++start_pos;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
uint32 execute(const char * commandLine, char * const * parameters)
|
||||
{
|
||||
DEBUG_REPORT_LOG(true, ("Now Attempting To Spawn %s\n", commandLine));
|
||||
pid_t p;
|
||||
// fork
|
||||
p = fork();
|
||||
if (p == 0)
|
||||
{
|
||||
// i am child
|
||||
DEBUG_REPORT_LOG(true, ("Now Spawning %s\n", commandLine));
|
||||
|
||||
int result = execv(commandLine, parameters); //lint !e10 Expecting a function (huh?)
|
||||
if (result == -1)
|
||||
{
|
||||
IGNORE_RETURN(perror("execv")); //lint !e10
|
||||
_exit(0);
|
||||
}
|
||||
}
|
||||
return p; //lint !e732 Loss of sign
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void makeParameters(const std::vector<std::string> & parameters, std::vector<char *> & p)
|
||||
{
|
||||
std::vector<std::string>::const_iterator i;
|
||||
for(i = parameters.begin(); i != parameters.end(); ++i)
|
||||
{
|
||||
char * arg = new char[(*i).length() + 1];
|
||||
strcpy(arg, (*i).c_str()); //lint !e64 !e534
|
||||
p.push_back(arg);
|
||||
}
|
||||
p.push_back(NULL);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void freeParameters(std::vector<char *> & p)
|
||||
{
|
||||
std::vector<char *>::iterator piter;
|
||||
for(piter = p.begin(); piter != p.end(); ++piter)
|
||||
{
|
||||
char * arg = (*piter);
|
||||
delete[] arg;
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
uint32 ProcessSpawner::execute(const std::string & commandLine, const std::vector<std::string> & parameters)
|
||||
{
|
||||
|
||||
std::vector<char *> p;
|
||||
makeParameters(parameters, p);
|
||||
|
||||
std::string c = "./";
|
||||
c += commandLine;
|
||||
uint32 pid = ::execute(c.c_str(), &p[0]);
|
||||
|
||||
freeParameters(p);
|
||||
|
||||
return pid;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
uint32 ProcessSpawner::execute(const std::string & commandLine)
|
||||
{
|
||||
std::vector<std::string> parameters;
|
||||
IGNORE_RETURN(tokenize(commandLine, parameters));
|
||||
|
||||
std::vector<char *> p;
|
||||
makeParameters(parameters, p);
|
||||
|
||||
uint32 result = ::execute(p[0], &p[0]);
|
||||
|
||||
freeParameters(p);
|
||||
|
||||
return result;
|
||||
}
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
bool ProcessSpawner::isProcessActive(uint32 pid)
|
||||
{
|
||||
pid_t result = waitpid(static_cast<pid_t>(pid), 0, WNOHANG|WUNTRACED);
|
||||
return (result < 1);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void ProcessSpawner::kill(uint32 pid)
|
||||
{
|
||||
IGNORE_RETURN(::kill(static_cast<int>(pid), SIGKILL));
|
||||
int status;
|
||||
IGNORE_RETURN(waitpid(static_cast<int>(pid), &status, 0));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void ProcessSpawner::forceCore(const unsigned long pid)
|
||||
{
|
||||
IGNORE_RETURN(::kill(static_cast<int>(pid), SIGABRT));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
@@ -0,0 +1,85 @@
|
||||
// TaskManagerSysInfo.cpp
|
||||
// Copyright 2000-02, Sony Online Entertainment Inc., all rights reserved.
|
||||
// Author: Justin Randall
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
#include "FirstTaskManager.h"
|
||||
#include "TaskManagerSysInfo.h"
|
||||
#include <sys/sysinfo.h>
|
||||
|
||||
#include "TaskManager.h"
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
TaskManagerSysInfo::TaskManagerSysInfo() :
|
||||
averageScore()
|
||||
{
|
||||
update();
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
TaskManagerSysInfo::TaskManagerSysInfo(const TaskManagerSysInfo &)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
TaskManagerSysInfo::~TaskManagerSysInfo()
|
||||
{
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
TaskManagerSysInfo & TaskManagerSysInfo::operator = (const TaskManagerSysInfo & rhs)
|
||||
{
|
||||
if(this != &rhs)
|
||||
{
|
||||
// make assignments if right hand side is not this instance
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
const float TaskManagerSysInfo::getScore() const
|
||||
{
|
||||
#if 0
|
||||
//Temporariy remove this since it's not giving us good results
|
||||
FILE * avg = popen("uptime", "r");
|
||||
float a = 0.0f;
|
||||
if(avg)
|
||||
{
|
||||
std::string output;
|
||||
while(!feof(avg))
|
||||
{
|
||||
char buf[1024] = {"\0"};
|
||||
|
||||
fread(buf, sizeof(buf), 1, avg);
|
||||
output += buf;
|
||||
}
|
||||
char formatted[1024] = {"\0"};
|
||||
std::string load = output.substr(output.find("load average:"));
|
||||
sscanf(load.c_str(), "load average: %f", &a);
|
||||
pclose(avg);
|
||||
}
|
||||
return a;
|
||||
#else
|
||||
|
||||
float ret = static_cast<float>(TaskManager::getNumGameConnections());
|
||||
return ret;
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
void TaskManagerSysInfo::update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
#include "sharedFoundation/FirstSharedFoundation.h"
|
||||
|
||||
#include "ConfigTaskManager.h"
|
||||
#include "TaskManager.h"
|
||||
|
||||
#include "sharedCompression/SetupSharedCompression.h"
|
||||
#include "sharedDebug/SetupSharedDebug.h"
|
||||
#include "sharedFile/SetupSharedFile.h"
|
||||
#include "sharedFoundation/ConfigFile.h"
|
||||
#include "sharedFoundation/Os.h"
|
||||
#include "sharedFoundation/SetupSharedFoundation.h"
|
||||
#include "sharedNetworkMessages/SetupSharedNetworkMessages.h"
|
||||
#include "sharedRandom/SetupSharedRandom.h"
|
||||
#include "sharedThread/SetupSharedThread.h"
|
||||
|
||||
// ======================================================================
|
||||
|
||||
int main(int argc, char ** argv)
|
||||
{
|
||||
SetupSharedThread::install();
|
||||
SetupSharedDebug::install(1024);
|
||||
|
||||
//-- setup foundation
|
||||
SetupSharedFoundation::Data setupFoundationData(SetupSharedFoundation::Data::D_game);
|
||||
setupFoundationData.lpCmdLine = ConvertCommandLine(argc,argv);
|
||||
setupFoundationData.configFile = "taskmanager.cfg";
|
||||
SetupSharedFoundation::install (setupFoundationData);
|
||||
|
||||
SetupSharedCompression::install();
|
||||
SetupSharedFile::install(false);
|
||||
SetupSharedNetworkMessages::install();
|
||||
SetupSharedRandom::install(static_cast<uint32>(time(NULL))); //lint !e1924 !e64 // NULL is a C-Style cast?
|
||||
|
||||
Os::setProgramName("TaskManager");
|
||||
//setup the server
|
||||
ConfigTaskManager::install();
|
||||
|
||||
//-- run game
|
||||
TaskManager::install();
|
||||
TaskManager::run();
|
||||
TaskManager::remove();
|
||||
SetupSharedFoundation::remove();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user