This class allows the user to type in commands that will be parsed and used to supply information to the remote controller.
#include "textIf.h"
#include <vlutil/vlPrint.h>
#include <vl/hostStructs.h>
#include <vlpi/disEnums.h>
#include <vl/startResumeInteraction.h>
#include <vl/stopFreezeInteraction.h>
#include <time.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#if DT_HAVE_UNISTD_H
#include <unistd.h>
#endif
#if !WIN32
#include <sys/fcntl.h>
#else
#include <fcntl.h>
#endif
#if WIN32
#include <io.h>
#include <conio.h>
#endif
#if !WIN32
#define UNBLOCK() setBlock(0, 0)
#define RESTORE() setBlock(0, 1)
#else
#define UNBLOCK()
#define RESTORE()
#endif
const char* terrainDir = "..\\userData\\terrains\\";
const char* scenariosDir = "..\\userData\\scenarios\\";
static bool monitorResourcesCallbackAdded = false;
struct Cmd
{
char *name;
CmdFunc func;
};
Cmd allCmds[] = {
};
int nCmds = DtNUMBER(allCmds);
{
if (!strncmp(s, "create", 6))
{
DtInfo("\n"
"\tcreate waypoint <\"name\"> <p1>\n"
"\tcreate route <\"name\"> <p1 p2 ... pn>\n"
"\tcreate tank <p1>\n"
"\tcreate aggregate <p1>\n"
"\t\twhere px has form x,y,z\n");
}
else if (!strncmp(s, "set", 3))
{
DtInfo("\n"
"\tset plan <\"name\">\n"
"\tset label <objectId> <\"label\">\n"
"\tset location <\"name\"> <pt>\n"
"\tset fuel <\"name\"> <double>\n"
"\tset target <\"name\"> <\"name\">\n"
"\tset restore <\"name\"> \n");
}
else if (!strncmp(s, "task", 4))
{
DtInfo("\n"
"\ttask patrolRoute <\"name\"> <\"route\">\n"
"\ttask moveToPoint <\"name\"> <\"waypoint\">\n"
"\ttask follow <\"name\"> <\"leader\">\n"
"\ttask wait <\"name\"> <time>\n");
}
else if (!strncmp(s, "monitor", 4))
{
DtInfo("\nmonitor <\"resource\" | \"ALL\"> <\"entity name\"> [update time in seconds]\n");
}
else if (!strncmp(s, "unmonitor", 4))
{
DtInfo("\nunmonitor <\"resource\" | \"ALL\"> <\"entity name\">\n");
}
else if (!strncmp(s, "simrun", 4))
{
DtInfo("simrun [site:host]\n");
}
else if (!strncmp(s, "turnonsnapshots", 4))
{
DtInfo("turnonsnapshots <simulation time> <number to keep in memory>\n");
}
else if (!strncmp(s, "simpause", 4))
{
DtInfo("simpause [site:host]\n");
}
else if (!strncmp(s, "timemultiplier", 4))
{
DtInfo("timemultiplier number\n");
}
else if (!strncmp(s, "aggregate", 4))
{
DtInfo("\naggregate <name1> <name2> <name3>\n");
}
else
{
DtInfo("\n"
"\tload <\"filename\">\n"
"\tsave <\"filename\">\n"
"\tnew <\"terrain db filename\">\n"
"\tclose\n"
"\trewind\n"
"\trun\n"
"\tpause\n"
"\tsimrun\n"
"\tsimpause\n"
"\tlistsnapshots\n"
"\ttakesnapshot\n"
"\tturnonsnapshots <simulation time> <number to keep>\n"
"\tturnoffsnapshots\n"
"\trollback <simulation time>\n"
"\ttimemultiplier <number>\n"
"\tlist\n"
"\tcreate [ waypoint | route | tank | aggregate]\n"
"\tdelete [ <object_name> | <object_id> ]\n"
"\tset [ plan | label | location | fuel | target | restore ]\n"
"\ttask [ patrolRoute | moveToPoint | follow | wait ]\n"
"\taggregate <name1> <name2> <name3>\n"
"\tmonitor <\"resource\" | \"ALL\"> <\"entity name\"> [update time in seconds]\n"
"\tunmonitor <\"resource\" | \"ALL\"> <\"entity name\">\n"
"\thelp <command_name>\n"
"\tquit\n");
}
}
{
}
{
}
{
DtInfo << "Snapshot taken" << std::endl;
}
{
#if WIN32
str[strlen(str)]='\0';
#else
str[strlen(str)-1]='\0';
#endif
if (!strcmp(str, ""))
{
}
else
{
char *t1, *t2;
t1 = strtok(str, " ");
if (t1)
{
t2 = strtok(NULL, " ");
if (t2)
{
DtInfo << "Turning on snapshots every " << std::max(1, atoi(t1)) << " seconds. Storing " << std::max(1, atoi(t2)) << " snapshots." << std::endl;
std::max(1, atoi(t2)));
}
else
{
}
}
else
{
}
}
}
{
}
{
#if WIN32
str[strlen(str)]='\0';
#else
str[strlen(str)-1]='\0';
#endif
if (!strcmp(str, "")) return;
}
{
int i = 0;
DtInfo("Listing known backends\n");
for (DtListItem* item = backends.first(); item; item = item->next())
{
DtSimulationAddress* addr = (DtSimulationAddress*) item->data();
DtInfo("\t%d) %s\n", ++i, addr->string());
if (i == 5) DtInfo("\n");
}
}
{
#if WIN32
str[strlen(str)]='\0';
#else
str[strlen(str)-1]='\0';
#endif
if (!strcmp(str, "")) return;
char *name;
name = strtok(str, "\"");
a);
}
void* usr)
{
DtWarn("Cannot load scenario due to missing backends:\n");
DtListItem* item;
for (item = map->first(); item; item = item->next())
{
if (entry)
{
}
}
return false;
}
{
#if WIN32
str[strlen(str)]='\0';
#else
str[strlen(str)-1]='\0';
#endif
int iPoints = 0;
if (strlen(str) == 0)
{
iPoints = 800;
}
else
{
iPoints = atoi(str);
}
}
{
srand(time(NULL));
DtList verts;
for (int i = 0; i < iPoints; i++)
{
verts.add(
new DtVector((
double)((
double)rand() / (
double)RAND_MAX) * 2000.,
(double)((double)rand() / (double)RAND_MAX) * 2000., 100.));
}
DtListItem* item = verts.first();
while (item)
{
item = item->next();
}
}
{
#if WIN32
str[strlen(str)]='\0';
#else
str[strlen(str)-1]='\0';
#endif
if (!strcmp(str, "")) return;
if (!strstr(str, ".scn"))
strcat(str, ".scn");
char *name;
name = strtok(str, "\"");
}
{
}
{
}
{
#if WIN32
str[strlen(str)]='\0';
#else
str[strlen(str)-1]='\0';
#endif
if (!strcmp(str, ""))
{
DtInfo("Give me a terrain database, please!\n> ");
}
else
{
char *name;
name = strtok(str, "\"");
}
}
{
#if WIN32
str[strlen(str)]='\0';
#else
str[strlen(str)-1]='\0';
#endif
if (!strcmp(str, ""))
{
DtInfo("Create what?!\n");
}
else if (!strncmp(str, "waypoint", 8))
{
}
else if (!strncmp(str, "route", 5))
{
}
else if (!strncmp(str, "tank", 4))
{
}
else if (!strncmp(str,"aggregate",9))
{
}
}
{
char *point;
point = strtok(&str[9], " ");
if (!point)
{
DtInfo("\tusage: aggregate <geocentric_location>\n");
return;
}
sscanf(point, "%lf,%lf,%lf", &vec[0], &vec[1], &vec[2]);
DtEntityType(11, 1, 225, 3, 0, 0, 0), vec,
DtForceFriendly, 0,pltName);
DtEntityType(1, 1, 225, 1, 1, 3, 0), vec,
vec[DtX]+=10;
DtEntityType(1, 1, 225, 1, 1, 3, 0), vec,
vec[DtY]+=10;
DtEntityType(1, 1, 225, 1, 1, 3, 0), vec,
}
{
{
DtInfo("%s\n", str.string());
}
}
{
if (!strcmp(str, ""))
{
DtInfo("Monitor what?!\n");
}
else
{
char *res;
char *entity;
char *time;
char* temp;
int iTime = 10;
res = strtok(str, "\"");
temp = strtok(NULL, "\"");
entity = strtok(NULL, "\"");
time = strtok(NULL, "\"");
if (time)
{
iTime = atoi(time);
}
if (strcmp(res, "ALL"))
{
}
if (!monitorResourcesCallbackAdded)
{
monitorResourcesCallbackAdded = true;
}
}
}
{
if (!strcmp(str, ""))
{
DtInfo("unmonitor what?!\n");
}
else
{
char *res;
char *entity;
char* temp;
res = strtok(str, "\"");
temp = strtok(NULL, "\"");
entity = strtok(NULL, "\"");
temp = strtok(NULL, "\"");
if (strcmp(res, "ALL"))
{
}
}
}
{
#if WIN32
str[strlen(str)]='\0';
#else
str[strlen(str)-1]='\0';
#endif
if (!strcmp(str, ""))
{
DtInfo("Aggregate what?!\n");
}
else
{
char *e1, *e2, *e3, *temp;
e1 = strtok(str, "\"");
temp = strtok(NULL, "\"");
e2 = strtok(NULL, "\"");
temp = strtok(NULL, "\"");
e3 = strtok(NULL, "\"");
temp = strtok(NULL, "\"");
DtList entities;
DtEntityType(11, 1, 0, 3, 2, 0, 0),
DtForceFriendly, 0.0, &entities);
DtListItem* next = NULL;
for (DtListItem* item = entities.first(); item; item = next)
{
next = item->next();
delete (
DtString*) entities.remove(item);
}
}
}
{
#if WIN32
str[strlen(str)]='\0';
#else
str[strlen(str)-1]='\0';
#endif
if (!strcmp(str, ""))
{
DtInfo("Set what?!\n");
}
else if (!strncmp(str, "plan", 4))
{
}
else if (!strncmp(str, "label", 5))
{
}
else if (!strncmp(str, "location", 8))
{
}
else if (!strncmp(str, "fuel", 4))
{
}
else if (!strncmp(str, "target", 6))
{
}
else if (!strncmp(str, "restore", 7))
{
}
}
{
#if WIN32
str[strlen(str)]='\0';
#else
str[strlen(str)-1]='\0';
#endif
if (!strcmp(str, ""))
{
DtInfo("Task what?!\n");
}
else if (!strncmp(str, "patrolRoute", 11))
{
}
else if (!strncmp(str, "moveToPoint", 11))
{
}
else if (!strncmp(str, "follow", 6))
{
}
else if (!strncmp(str, "wait", 4))
{
}
}
{
#if WIN32
s[strlen(s)]='\0';
#else
s[strlen(s)-1]='\0';
#endif
if (strchr(s, ':'))
{
}
else
{
char* strname = strtok(s, "\"");
}
}
{
}
{
DtStartResumeInteraction ds;
#if WIN32
s[strlen(s)]='\0';
#else
s[strlen(s)-1]='\0';
#endif
if (strcmp(s, "") == 0)
{
ds.setReceiverId(DtEntityIdentifier(DtSimSendToAll,0));
}
else
{
if (strchr(s, ':'))
{
ds.setReceiverId(DtEntityIdentifier(DtSimulationAddress(s),0));
}
else
{
DtWarn << "Start who? (use site:host)";
return;
}
}
}
{
}
{
DtStopFreezeInteraction ds;
#if WIN32
s[strlen(s)]='\0';
#else
s[strlen(s)-1]='\0';
#endif
if (strcmp(s, "") == 0)
{
ds.setReceiverId(DtEntityIdentifier(DtSimSendToAll,0));
}
else
{
if (strchr(s, ':'))
{
ds.setReceiverId(DtEntityIdentifier(DtSimulationAddress(s),0));
}
else
{
DtWarn << "Stop who? (use site:host)";
return;
}
}
}
{
char* t;
t = strtok(str, " ");
double multiplier = atof(t);
if (multiplier == 0.0)
{
DtInfo("\tusage: timemultiplier <number>\n");
return;
}
}
#ifdef _PowerUX
int strcasecmp(const char *s1, const char *s2)
{
char *s1Temp = strdup(s1);
char *s2Temp = strdup(s2);
for (int strIndex= 0; strIndex< strlen(s1Temp); strIndex++)
if ((64 < s1Temp[strIndex]) && (s1Temp[strIndex] < 91))
s1Temp[strIndex] += 32;
for (int strIndex= 0; strIndex< strlen(s2Temp); strIndex++)
if ((64 < s2Temp[strIndex]) && (s2Temp[strIndex] < 91))
s2Temp[strIndex] += 32;
int retVal = (strcmp(s1Temp,s2Temp));
free (s1Temp);
free (s2Temp);
return retVal;
}
#endif
#if WIN32
static char nbdf_buff[128];
static int nbdf_ptr=0;
char *non_blocking_dos_fgets(char *buff, int size, FILE *f)
{
while (_kbhit())
{
nbdf_buff[nbdf_ptr] = _getche();
if ( nbdf_buff[nbdf_ptr] == '\r'
|| nbdf_buff[nbdf_ptr] == '\n'
|| nbdf_ptr >= size )
{
nbdf_buff[nbdf_ptr] = 0;
strcpy(buff,nbdf_buff);
nbdf_ptr=0;
DtInfo("\n");
return(buff);
}
else if ( nbdf_buff[nbdf_ptr] == '\b')
{
if (nbdf_ptr > 0)
{
_putch(' ');
_putch('\b');
--nbdf_ptr;
}
}
else
{
++nbdf_ptr;
}
}
return NULL;
}
#endif
#define LASTCHAR(s) (s[strlen(s)-1])
#define STERMINATE(s) do{ char *t=(s); if (*t) (s)++, *t='\0';} while(0)
char *gettok(char **nextp, char *sep)
{
char *start, *end;
start = *nextp;
while (*start && strchr(sep, *start))
start++;
if (!*start)
return NULL;
end = start;
while (*end && !strchr(sep, *end) )
end++;
*nextp = end;
STERMINATE(*nextp);
return start;
}
#if !WIN32
void setBlock(int fd, int on)
{
static int blockf, nonblockf;
static int first = 1;
if (first) {
first = 0;
int flags = fcntl(fd, F_GETFL, 0);
if (flags == -1)
DtWarnPerror("fcntl(F_GETFL) failed");
blockf = flags & ~O_NONBLOCK;
nonblockf = flags | O_NONBLOCK;
}
if (fcntl(0, F_SETFL, on ? blockf : nonblockf) < 0)
DtWarnPerror("fcntl(F_SETFL) failed");
}
#endif
Cmd *lookupCmd(char *cmdname)
{
if (!cmdname)
return NULL;
for (int j=0; j<nCmds; j++)
#if !WIN32
if (!strcasecmp(cmdname, allCmds[j].name))
#else
if (!_stricmp(cmdname, allCmds[j].name))
#endif
return allCmds+j;
return NULL;
}
kbInterest(0), myTimeToQuit(0),
myController(controller),
myRemoveListCallback(false)
{
DtInfo("Scenario directory: %s\n", scenariosDir);
DtInfo("Terrain directory: %s\n", terrainDir);
myController->addBackendDiscoveryCallback(backendAddedCb, NULL);
myController->addBackendRemovalCallback(backendRemovedCb, NULL);
myController->addBackendLoadedCallback(backendLoadedCb, NULL);
myController->addScenarioLoadedCallback(ScenarioLoadedCb, NULL);
myController->addBackendSavedCallback(backendSavedCb, NULL);
myController->addScenarioSavedCallback(ScenarioSavedCb, NULL);
kbInterest++;
prompt();
}
{
}
{
}
{
}
{
}
{
char buff[256];
int discardNext = 0;
UNBLOCK();
#if !WIN32
while(fgets(buff, sizeof(buff), stdin))
#else
while (non_blocking_dos_fgets(buff, sizeof(buff), stdin))
#endif
{
{
}
int discardThis = discardNext;
discardNext = (LASTCHAR(buff) != 10);
if(!discardThis)
{
RESTORE();
{
return;
}
UNBLOCK();
}
}
RESTORE();
if(feof(stdin))
{
DtInfo("EOF\n");
exit(0);
}
}
{
DtInfo("> ");
fflush(stdout);
}
{
char *cmdname = gettok(&buff, "\t \n");
Cmd *cmd = lookupCmd(cmdname);
if (cmd)
(*cmd->func)(buff, this);
else if (cmdname)
DtWarn("VRF Remote controller: no such command: \"%s\"\n", cmdname);
}
{
DtInfo("\nBackend %s Added\n> ", addr.string());
}
{
DtInfo("\nBackend %s Removed\n> ", addr.string());
}
{
DtInfo("\nBackend %s Loaded\n> ", addr.string());
}
{
DtInfo("\nScenario Loaded\n> ");
}
{
DtInfo("\nBackend %s Saved (%s)\n> ",
addr.string(), result ==
DtSuccess?
"success" :
"failure");
}
{
DtInfo("\nScenario Saved\n> ");
}
const DtString& name,
const DtEntityIdentifier&
id,
const DtUUID& uuid,
void* usr)
{
if( search.findString("Tank_Plt") >=0 )
{
DtInfo("\n aggregate has been created with name and id: \"%s\", %s\n> ",
name.string(), id.string());
aggId = uuid;
}
else
{
DtInfo("\n entity has been created with name and id: \"%s\", %s\n> ",
name.string(), id.string());
}
if ( sub.findString("Plt_Sub") >= 0 )
{
if ( pVrfRemoteCtrl )
{
DtInfo(
"\n %s has been added to aggregate with id: %s\n> ",name.string(),aggId.
string());
}
}
}
{
char *point, *name;
name = strtok(str, " ");
name = strtok(NULL, "\"");
point = strtok(NULL, " ");
if (!point || !name)
{
DtInfo("\tusage: create waypoint <\"name\"> <pt>\n");
return;
}
sscanf(point, "%lf,%lf,%lf", &vec[0], &vec[1], &vec[2]);
name);
}
{
char *name, *point;
name = strtok(str, " ");
name = strtok(NULL, "\"");
if (!name)
{
DtInfo("\tusage: create route <\"name\"> <p1 p2 ... pn>\n");
return;
}
DtList vertices;
while ((point = strtok(NULL, " ")) != NULL)
{
double x, y, z;
sscanf(point, "%lf,%lf,%lf", &x, &y, &z);
vertices.add(vec);
}
name);
DtListItem* next = NULL;
for (DtListItem* item = vertices.first(); item; item = next)
{
next = item->next();
delete (
DtVector*) vertices.remove(item);
}
}
{
char *point;
point = strtok(&str[4], " ");
if (!point)
{
DtInfo("\tusage: tank <geocentric_location>\n");
return;
}
sscanf(point, "%lf,%lf,%lf", &vec[0], &vec[1], &vec[2]);
DtEntityType(1, 1, 225, 1, 1, 3, 0), vec,
DtForceFriendly, 90.0);
}
{
char *name, *route;
name = strtok(str, " ");
name = strtok(NULL, "\"");
route = strtok(NULL, "\"");
route = strtok(NULL, "\"");
if (!name || !route)
{
DtInfo("\tusage: task patrolRoute <\"name\"> <\"route\">\n");
return;
}
}
{
char *name, *point;
name = strtok(str, " ");
name = strtok(NULL, "\"");
point = strtok(NULL, "\"");
point = strtok(NULL, "\"");
if (!name || !point)
{
DtInfo("\tusage: task moveToPoint <\"name\"> <\"waypoint\">\n");
return;
}
}
{
char *name, *leader;
name = strtok(str, " ");
name = strtok(NULL, "\"");
leader = strtok(NULL, "\"");
leader = strtok(NULL, "\"");
if (!name || !leader)
{
DtInfo("\tusage: task follow <name> <leader>\n");
return;
}
}
{
char *name, *t;
name = strtok(str, " ");
name = strtok(NULL, "\"");
t = strtok(NULL, " ");
if (!name || !t)
{
DtInfo("\tusage: task wait <\"name\"> <time>\n");
return;
}
}
{
char *name = strtok(str, " ");
name = strtok(NULL, "\"");
if (!name)
{
DtInfo("\tusage: set plan <\"entity name\">\n");
return;
}
conditionalExpression.
setOper(
">");
}
void printIndent(int i)
{
i = i * 4;
while (i--)
{
DtInfo(" ");
}
}
{
while (stmt)
{
}
}
{
printIndent(indent);
if (ifStmt)
{
{
}
{
DtInfo("\n");
printIndent(indent);
DtInfo("else\n");
}
printIndent(indent);
return;
}
else
{
if (trigger)
{
DtInfo("\n");
return;
}
else
{
if (whileStmt)
{
DtInfo("\n");
return;
}
}
}
}
const std::vector<DtSimStatement*>& stmts, bool append, void* usr)
{
if (append)
{
DtInfo(
"\nAdditional plan statements for %s.\n", name.
string());
}
else
{
DtInfo(
"\nNew plan statements for %s.\n", name.
string());
}
int i = 0;
std::vector<DtSimStatement*>::const_iterator iter = stmts.begin();
while (iter != stmts.end())
{
DtInfo("%d)\n", ++i);
++iter;
}
DtInfo("\n> ");
}
void* usr)
{
{
DtInfo("\n%s executing statement %d\n> ", id.string(),
}
}
{
DtInfo("\n%s has completed its plan!\n> ", id.string());
}
{
char *objId, *label;
objId = strtok(str, " ");
objId = strtok(NULL, " ");
label = strtok(NULL, "\"");
if (!objId || !label)
{
DtInfo("\tusage: set label <object id> <\"label\">\n");
return;
}
}
{
char *name, *point;
name = strtok(str, " ");
name = strtok(NULL, "\"");
point = strtok(NULL, " ");
if (!name || !point)
{
DtInfo("\tusage: set location <\"name\"> <pt>\n");
return;
}
sscanf(point, "%lf,%lf,%lf", &vec[0], &vec[1], &vec[2]);
}
{
char *name, *amount;
name = strtok(str, " ");
name = strtok(NULL, "\"");
amount = strtok(NULL, " ");
if (!name || !amount)
{
DtInfo("\tusage: set fuel <\"name\"> <double>\n");
return;
}
double fa = atof(amount);
}
{
char *name, *target;
name = strtok(str, " ");
name = strtok(NULL, "\"");
target = strtok(NULL, "\"");
target = strtok(NULL, "\"");
if (!name || !target)
{
DtInfo("\tusage: set target <\"name\"> <\"name\">\n");
return ;
}
}
{
char *name;
name = strtok(str, " ");
name = strtok(NULL, "\"");
if (!name)
{
DtInfo("\tusage: set restore <\"name\"> \n");
return;
}
}
{
DtIfSnapshotsResponse::SnapshotStatus::const_iterator iter = rsp->
snapshots().begin();
DtInfo << "\nSnapshots Available: ";
{
DtInfo << "None" << std::endl;
return;
}
DtInfo << std::endl;
{
DtInfo << " Simulation Time: " << floor(iter->second.simTime) << std::endl;
++iter;
}
}
{
if (
controller()->backendListener()->backends().count())
{
(
controller()->backendListener()->backendList()->first()->data())->address(), request);
}
}