Better CLI
This commit is contained in:
parent
29e43c2f18
commit
4440075d7f
|
@ -127,7 +127,7 @@ struct {
|
|||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
//
|
||||
// Server
|
||||
// Supervisor
|
||||
//
|
||||
struct {
|
||||
pthread_t *id;
|
||||
|
|
|
@ -23,15 +23,19 @@
|
|||
#include "../include/base.h"
|
||||
#endif
|
||||
|
||||
void *launchScheduler(void*);
|
||||
char *CmdHelp(char**, Server_t*);
|
||||
char *CmdModel(char**, Server_t*);
|
||||
char *CmdShutdown(char**, Server_t*);
|
||||
|
||||
struct {
|
||||
const char *name;
|
||||
void* (*execute)(void*);
|
||||
char* (*execute)(char**, Server_t*);
|
||||
const char *help;
|
||||
} typedef Command_t;
|
||||
|
||||
Command_t cmdList[] =
|
||||
{
|
||||
{"launch scheduler", launchScheduler, "Launches a scheduler instance"},
|
||||
{"help", CmdHelp, "Help command"},
|
||||
{"model", CmdModel, "Model command"},
|
||||
{"shutdown", CmdShutdown, "Shutdown command"},
|
||||
};
|
||||
|
|
|
@ -32,3 +32,9 @@ int ModelLoad(int id);
|
|||
void ModelCreate(Model_t **newModel);
|
||||
|
||||
int ModelLoad(int id);
|
||||
|
||||
void ModelRun(int id);
|
||||
|
||||
void ModelStop(int id);
|
||||
|
||||
void ModelShutdown(void);
|
||||
|
|
|
@ -33,7 +33,7 @@ void ServerInit(Server_t *server);
|
|||
// -------------------------------------------------------------------------- //
|
||||
static inline void ServerDestroy(Server_t *server)
|
||||
{
|
||||
;
|
||||
free(server->id);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------- //
|
||||
|
|
|
@ -102,8 +102,7 @@ static inline int receiveSocket(int effectiveSocket, char *serverAnswer,
|
|||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
int socketDescriptor, nextSocketDescriptor, readSize;
|
||||
struct sockaddr_in server;
|
||||
int socketDescriptor;
|
||||
|
||||
char clientMessage[255] = {0};
|
||||
char serverReply[255] = {0};
|
||||
|
@ -142,7 +141,7 @@ int main(int argc, char **argv)
|
|||
sendSocket(socketDescriptor, clientMessage, strlen(clientMessage));
|
||||
|
||||
//Received the data from the server
|
||||
readSize = receiveSocket(socketDescriptor, serverReply, 200);
|
||||
receiveSocket(socketDescriptor, serverReply, 200);
|
||||
printf("%s\n", serverReply);
|
||||
|
||||
memset(serverReply, 0, 255);
|
||||
|
|
53
src/cmds.c
53
src/cmds.c
|
@ -23,9 +23,58 @@
|
|||
#include "../include/scheduler.h"
|
||||
#include "../include/model.h"
|
||||
|
||||
#define LINE_LENGTH 80
|
||||
#define LINE_NUMBER 15
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
void *CmdModel(void *args)
|
||||
char *CmdHelp(char **argv, Server_t *args)
|
||||
{
|
||||
|
||||
return "OK\n";
|
||||
}
|
||||
|
||||
char *CmdModel(char **argv, Server_t *args)
|
||||
{
|
||||
char *buf;
|
||||
buf = (char *) calloc(LINE_NUMBER, LINE_LENGTH * sizeof(char));
|
||||
|
||||
if (!argv[1]) {
|
||||
strcat(buf, "{create | delete | load | run}\n");
|
||||
goto CmdModelEnd;
|
||||
}
|
||||
|
||||
if (strcmp(argv[1], "create") == 0) {
|
||||
strcat(buf, "You asked for us to create a model\n");
|
||||
}
|
||||
|
||||
if (strcmp(argv[1], "delete") == 0) {
|
||||
strcat(buf, "You asked for us to delete a model\n");
|
||||
}
|
||||
|
||||
if (strcmp(argv[1], "load") == 0) {
|
||||
strcat(buf, "You asked for us to load a model\n");
|
||||
}
|
||||
|
||||
if (strcmp(argv[1], "run") == 0) {
|
||||
strcat(buf, "You asked for us to run a model\n");
|
||||
}
|
||||
|
||||
CmdModelEnd:
|
||||
return buf;
|
||||
}
|
||||
|
||||
char *CmdShutdown(char **argv, Server_t *args)
|
||||
{
|
||||
char *buf;
|
||||
buf = (char *) calloc(LINE_NUMBER, LINE_LENGTH * sizeof(char));
|
||||
|
||||
ModelShutdown();
|
||||
strcat(buf, "All model shutted down\n");
|
||||
|
||||
args->pleaseStop = true;
|
||||
strcat(buf, "Server stopping\n");
|
||||
|
||||
|
||||
CmdShutdownEnd:
|
||||
return buf;
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
|
||||
#include "../include/base.h"
|
||||
#include "../include/server.h"
|
||||
#include "../include/model.h"
|
||||
|
||||
|
||||
int main(int argc, char **argv)
|
||||
|
@ -31,19 +32,25 @@ int main(int argc, char **argv)
|
|||
// Go!
|
||||
printLog("Starting gem-graph-server...\n");
|
||||
|
||||
// random generator
|
||||
// Initializing random generator
|
||||
t = time(&t);
|
||||
srand((unsigned) t);
|
||||
|
||||
server0 = (Server_t*) calloc(1, sizeof(Server_t));
|
||||
|
||||
// Initializing model system
|
||||
ModelSystemInit();
|
||||
|
||||
// Launching server
|
||||
ServerInit(server0);
|
||||
|
||||
// Waiting for termination
|
||||
ServerWait(server0);
|
||||
|
||||
// Exiting
|
||||
ServerDestroy(server0);
|
||||
ModelSystemDestroy();
|
||||
free(server0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -110,6 +110,7 @@ void ModelStop(int id)
|
|||
{
|
||||
// Creating structure for the Scheduler
|
||||
knownModel[id]->scheduler->pleaseStop = true;
|
||||
printLog("Model %d stopped!\n", id);
|
||||
}
|
||||
|
||||
void ModelDelete(Model_t *newModel)
|
||||
|
@ -117,6 +118,13 @@ void ModelDelete(Model_t *newModel)
|
|||
return;
|
||||
}
|
||||
|
||||
void ModelShutdown(void)
|
||||
{
|
||||
for (int i = 0; i < loadedModelIndex; i++) {
|
||||
ModelStop(i);
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
static void ModelPrepareSpace(Space_t *globalDrawingSpace, Model_t *model)
|
||||
|
|
|
@ -90,7 +90,7 @@ static void *schedulerMain(void *scheduler)
|
|||
args = (Scheduler_t*) scheduler;
|
||||
printLog("Scheduler #%lu online\n", *args->id);
|
||||
|
||||
if (!args->nmaxThread) { // nmaxthread = 0 => no minimum
|
||||
if (!args->nMaxThread) { // nmaxthread = 0 => no minimum
|
||||
ncpu = get_nprocs(); // allocating all the cpus available
|
||||
} else { // n thread = min(cpu, nmaxthread)
|
||||
ncpu = MIN(get_nprocs(), args->nMaxThread);
|
||||
|
|
24
src/server.c
24
src/server.c
|
@ -79,6 +79,9 @@ static void *serverMain(void *server)
|
|||
struct sockaddr_in client;
|
||||
char clientRequest[255]= {0};
|
||||
char serverAnswer[255] = {0};
|
||||
char **argv = NULL;
|
||||
char *commandReturn;
|
||||
int tokenIndex;
|
||||
|
||||
// Get args
|
||||
args = (Server_t*) server;
|
||||
|
@ -131,10 +134,22 @@ static void *serverMain(void *server)
|
|||
|
||||
printLog("Client request : %s\n",clientRequest);
|
||||
|
||||
for (int i = 0; i < LEN(cmdList); i++) { // TODO extract first command name
|
||||
if (strcmp(cmdList[i].name, clientRequest) == 0) {
|
||||
cmdList[i].execute(NULL);
|
||||
strcpy(serverAnswer,"OK\0");
|
||||
// get args in an array
|
||||
tokenIndex = 0;
|
||||
argv = (char**) realloc(argv, 1 * sizeof(char*));
|
||||
argv[0] = strtok(clientRequest, " ");
|
||||
while (argv[tokenIndex]) {
|
||||
tokenIndex++;
|
||||
argv = (char**) realloc(argv, (tokenIndex+1) * sizeof(char*));
|
||||
argv[tokenIndex] = strtok(NULL, " ");
|
||||
}
|
||||
|
||||
// test first arg to find command in cmdList
|
||||
for (int i = 0; i < LEN(cmdList); i++) {
|
||||
if (strcmp(cmdList[i].name, argv[0]) == 0) {
|
||||
commandReturn = cmdList[i].execute(argv, args);
|
||||
strcpy(serverAnswer, commandReturn);
|
||||
free(commandReturn);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -147,6 +162,7 @@ static void *serverMain(void *server)
|
|||
close(effectiveSocket);
|
||||
}
|
||||
|
||||
free(argv);
|
||||
printLog("Server #%lu offline\n", *args->id);
|
||||
|
||||
return NULL;
|
||||
|
|
Loading…
Reference in New Issue