Browse Source

comment svrhandling

master
philipp schoenberger 10 years ago
parent
commit
3f76e933bd
  1. 82
      lwrserv/src/SvrHandling.cpp

82
lwrserv/src/SvrHandling.cpp

@ -12,6 +12,12 @@
#include "Trajectroy.h"
#include "LinearTrajectory.h"
/**
* Prints for each command within the command array the help texts
*
* @param client The reference to the client socket Object to exchange messages
* @param arg Unused parameter to match Signature of normal commands.
*/
void printUsage(SocketObject& client, std::string& arg)
{
(void) arg;
@ -25,6 +31,10 @@ void printUsage(SocketObject& client, std::string& arg)
}
/**
* Normal Constructor
* Initalizes the command array with long/short string version and function pointers for each command.
*/
SvrHandling::SvrHandling()
{
// add each command which is possible
@ -242,48 +252,80 @@ end:
static int trajcetorycount = 0;
trajcetorycount +=1;
currentTrajectory->saveToFile(std::string("trajectory/a.csv"));
currentTrajectory->saveJointToFile(std::string("trajectory/a.csv"));
// invalid trajectory skip it
delete currentTrajectory;
currentTrajectory = NULL;
}
}
}
/**
* default destructor
*
* nothing to be done here
*/
SvrHandling::~SvrHandling()
{
}
/**
* Starts the server thread with default port.
*
* @info will never return and should run in a thread
*/
void SvrHandling::run()
{
this->run(SVR_DEFAULT_PORT);
}
/**
* Starts the server thread with a specific port.
*
* @info will never return and should run in a thread
* @param port the specific port for the tellnet client interface.
*/
void SvrHandling::run(int port)
{
std::cout << timestamp() + "Starting " << SVR_NAME << " on port "<< port <<"\n";
while (c_state !=done)
// only abourt loop if state done is reached
while (c_state != done)
{
// create new socket and bind it to the speciffic port
SocketObject *socket = new SocketObject;
if (socket->Bind(port) <0)
{
exit (1);
}
// change state for the port to listen
socket->Listen();
// wait forever for a client to connect
while(1)
{
SocketObject *client = new SocketObject;
// wait for new client
if (socket->Accept(*client))
{
// do the init verification of the client
std::cout << timestamp() + "Client accepted!\n";
if (handshakeAccepted(*client))
{
// verification was correct
// process all comming commands from client within this loop
clientCommandLoop(*client);
}
// client command loop was abourted/closed
// disconnect the client
client->Disconnect();
std::cout << timestamp() + "Client disconnected.\n";
}
else
{
// failed to wait for a client
std::cout << timestamp() + "Client bin error.\n";
}
delete client;
@ -292,27 +334,46 @@ void SvrHandling::run(int port)
}
}
/**
* Verification of client by initial sequence commands
*
* @param client The reference to the client socket Object to exchange messages
* @retval Returns true if the client was successfully verified. False otherwise.
*/
bool SvrHandling::handshakeAccepted(SocketObject& client)
{
this->c_state = handshake;
std::string message = SVR_HELLO_MSG;
client.Send(message);
client.Recv(message);
/// check for initial sequence comming from client
if (message.find(SVR_HANDSHAKE,0) != std::string::npos)
{
/// inital sequence was correct
/// accept client
c_state = accepted;
message = SVR_ACCEPTED;
}
else
{
/// initial sequence was incorrect
/// refuse client connection
c_state = waiting;
message = SVR_FAILED;
std::cout << timestamp() + "Handshake failed. " << "Invalid recv msg \'" << message <<"\'";
}
/// send response to the client
client.Send(message);
return(c_state == accepted);
return (c_state == accepted);
}
/**
* Processes each command comming from client.
*
* @param client The reference to the client socket Object to exchange messages
* @retval Returns true if the client was successfully verified. False otherwise.
*/
void SvrHandling::clientCommandLoop(SocketObject& client)
{
std::string message, cmd, arg;
@ -327,12 +388,14 @@ void SvrHandling::clientCommandLoop(SocketObject& client)
if (cmd == "")
continue;
// check command table for equivalence
/// check command table for equivalence command
bool match = false;
for (unsigned int i = 0 ; i < commandCount ; ++ i)
{
if (cmd == commands[i].longVersion || cmd == commands[i].aberration)
{
/// found equivalent command
/// process command function if implemented
if (commands[i].processCommand != NULL)
{
commands[i].processCommand(client,arg);
@ -340,7 +403,8 @@ void SvrHandling::clientCommandLoop(SocketObject& client)
{
client.Send(SVR_UNIMPLEMENTED_COMMAND);
}
match = true;
// break up search
match = true;
break;
}
@ -348,6 +412,7 @@ void SvrHandling::clientCommandLoop(SocketObject& client)
// command not found
if (match == false)
{
// send returnvalue and print help
client.Send(SVR_UNKNOWN_COMMAND);
printUsage(client, arg);
}
@ -360,6 +425,13 @@ void SvrHandling::clientCommandLoop(SocketObject& client)
}
}
/**
* Helper function to generate a string with courent timestamp.
*
* Output is in Y-m-d-H-M-S format.
*
* @retval String containg the current timespamp.
*/
std::string SvrHandling::timestamp()
{
time_t rawtime;

Loading…
Cancel
Save