Browse Source

comment svrhandling

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

78
lwrserv/src/SvrHandling.cpp

@ -12,6 +12,12 @@
#include "Trajectroy.h" #include "Trajectroy.h"
#include "LinearTrajectory.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 printUsage(SocketObject& client, std::string& arg)
{ {
(void) 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() SvrHandling::SvrHandling()
{ {
// add each command which is possible // add each command which is possible
@ -242,48 +252,80 @@ end:
static int trajcetorycount = 0; static int trajcetorycount = 0;
trajcetorycount +=1; trajcetorycount +=1;
currentTrajectory->saveToFile(std::string("trajectory/a.csv"));
currentTrajectory->saveJointToFile(std::string("trajectory/a.csv"));
// invalid trajectory skip it // invalid trajectory skip it
delete currentTrajectory; delete currentTrajectory;
currentTrajectory = NULL; currentTrajectory = NULL;
} }
} }
} }
/**
* default destructor
*
* nothing to be done here
*/
SvrHandling::~SvrHandling() SvrHandling::~SvrHandling()
{ {
} }
/**
* Starts the server thread with default port.
*
* @info will never return and should run in a thread
*/
void SvrHandling::run() void SvrHandling::run()
{ {
this->run(SVR_DEFAULT_PORT); 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) void SvrHandling::run(int port)
{ {
std::cout << timestamp() + "Starting " << SVR_NAME << " on port "<< port <<"\n"; std::cout << timestamp() + "Starting " << SVR_NAME << " on port "<< port <<"\n";
// only abourt loop if state done is reached
while (c_state != done) while (c_state != done)
{ {
// create new socket and bind it to the speciffic port
SocketObject *socket = new SocketObject; SocketObject *socket = new SocketObject;
if (socket->Bind(port) <0) if (socket->Bind(port) <0)
{ {
exit (1); exit (1);
} }
// change state for the port to listen
socket->Listen(); socket->Listen();
// wait forever for a client to connect
while(1) while(1)
{ {
SocketObject *client = new SocketObject; SocketObject *client = new SocketObject;
// wait for new client
if (socket->Accept(*client)) if (socket->Accept(*client))
{ {
// do the init verification of the client
std::cout << timestamp() + "Client accepted!\n"; std::cout << timestamp() + "Client accepted!\n";
if (handshakeAccepted(*client)) if (handshakeAccepted(*client))
{ {
// verification was correct
// process all comming commands from client within this loop
clientCommandLoop(*client); clientCommandLoop(*client);
} }
// client command loop was abourted/closed
// disconnect the client
client->Disconnect(); client->Disconnect();
std::cout << timestamp() + "Client disconnected.\n"; std::cout << timestamp() + "Client disconnected.\n";
} }
else else
{ {
// failed to wait for a client
std::cout << timestamp() + "Client bin error.\n"; std::cout << timestamp() + "Client bin error.\n";
} }
delete client; 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) bool SvrHandling::handshakeAccepted(SocketObject& client)
{ {
this->c_state = handshake; this->c_state = handshake;
std::string message = SVR_HELLO_MSG; std::string message = SVR_HELLO_MSG;
client.Send(message); client.Send(message);
client.Recv(message); client.Recv(message);
/// check for initial sequence comming from client
if (message.find(SVR_HANDSHAKE,0) != std::string::npos) if (message.find(SVR_HANDSHAKE,0) != std::string::npos)
{ {
/// inital sequence was correct
/// accept client
c_state = accepted; c_state = accepted;
message = SVR_ACCEPTED; message = SVR_ACCEPTED;
} }
else else
{ {
/// initial sequence was incorrect
/// refuse client connection
c_state = waiting; c_state = waiting;
message = SVR_FAILED; message = SVR_FAILED;
std::cout << timestamp() + "Handshake failed. " << "Invalid recv msg \'" << message <<"\'"; std::cout << timestamp() + "Handshake failed. " << "Invalid recv msg \'" << message <<"\'";
} }
/// send response to the client
client.Send(message); 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) void SvrHandling::clientCommandLoop(SocketObject& client)
{ {
std::string message, cmd, arg; std::string message, cmd, arg;
@ -327,12 +388,14 @@ void SvrHandling::clientCommandLoop(SocketObject& client)
if (cmd == "") if (cmd == "")
continue; continue;
// check command table for equivalence
/// check command table for equivalence command
bool match = false; bool match = false;
for (unsigned int i = 0 ; i < commandCount ; ++ i) for (unsigned int i = 0 ; i < commandCount ; ++ i)
{ {
if (cmd == commands[i].longVersion || cmd == commands[i].aberration) if (cmd == commands[i].longVersion || cmd == commands[i].aberration)
{ {
/// found equivalent command
/// process command function if implemented
if (commands[i].processCommand != NULL) if (commands[i].processCommand != NULL)
{ {
commands[i].processCommand(client,arg); commands[i].processCommand(client,arg);
@ -340,7 +403,8 @@ void SvrHandling::clientCommandLoop(SocketObject& client)
{ {
client.Send(SVR_UNIMPLEMENTED_COMMAND); client.Send(SVR_UNIMPLEMENTED_COMMAND);
} }
match = true;
// break up search
match = true; match = true;
break; break;
} }
@ -348,6 +412,7 @@ void SvrHandling::clientCommandLoop(SocketObject& client)
// command not found // command not found
if (match == false) if (match == false)
{ {
// send returnvalue and print help
client.Send(SVR_UNKNOWN_COMMAND); client.Send(SVR_UNKNOWN_COMMAND);
printUsage(client, arg); 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() std::string SvrHandling::timestamp()
{ {
time_t rawtime; time_t rawtime;

Loading…
Cancel
Save