Aria  2.8.0
ArNetServer Class Reference

Class for running a simple net server to send/recv commands via text. More...

#include <ArNetServer.h>

Public Member Functions

bool addCommand (const char *command, ArFunctor3< char **, int, ArSocket *> *functor, const char *help)
 Adds a new command. More...
 
 ArNetServer (bool addAriaExitCB=true, bool doNotAddShutdownServer=false)
 Constructor.
 
void close (void)
 Closes the server.
 
const char * getExtraString (void)
 Gets an extra string that the server holds for passing around.
 
bool getLoggingDataReceived (void)
 Gets whether we are logging all data received or not. More...
 
bool getLoggingDataSent (void)
 Gets whether we are logging all data sent or not. More...
 
bool getUseWrongEndChars (void)
 Gets whether we're using the wrong (legacy) end chars or not. More...
 
void internalAddSocketToDeleteList (ArSocket *socket)
 The internal function that adds a client to our delete list.
 
void internalAddSocketToList (ArSocket *socket)
 The internal function that adds a client to our list.
 
void internalEcho (char **argv, int argc, ArSocket *socket)
 The internal function for echo.
 
void internalGreeting (ArSocket *socket)
 the internal function that gives the greeting message
 
void internalHelp (ArSocket *socket)
 The internal function that does the help.
 
void internalHelp (char **argv, int argc, ArSocket *socket)
 The internal function for the help cb.
 
void internalQuit (char **argv, int argc, ArSocket *socket)
 The internal function for closing this connection.
 
void internalShutdownServer (char **argv, int argc, ArSocket *socket)
 The internal function for shutting down.
 
bool isOpen (void)
 Sees if the server is running and open.
 
int lock ()
 Lock the server.
 
bool open (ArRobot *robot, unsigned int port, const char *password, bool multipleClients=true, const char *openOnIP=NULL)
 Initializes the server. More...
 
void parseCommandOnSocket (ArArgumentBuilder *args, ArSocket *socket, bool allowLog=true)
 The internal function for parsing a command on a socket.
 
bool remCommand (const char *command)
 Removes a command. More...
 
void runOnce (void)
 the internal sync task we use for our loop
 
void sendToAllClients (const char *str,...)
 Sends the given string to all the clients. More...
 
void sendToAllClientsNextCycle (const char *str,...)
 Sends the given string to all the clients next cycle. More...
 
void sendToAllClientsNextCyclePlain (const char *str)
 Sends the given string to all the clients next cycle, no varargs, wrapper for java.
 
void sendToAllClientsPlain (const char *str)
 Sends the given string to all the clients, no varargs, wrapper for java.
 
void sendToClient (ArSocket *socket, const char *ipString, const char *str,...)
 Sends the given string to the (hopefully) the client given (this method may go away) More...
 
void sendToClientPlain (ArSocket *socket, const char *ipString, const char *str)
 Sends the given plain string to the (hopefully) the client given (this method may go away)
 
void setExtraString (const char *str)
 Sets an extra string that the server holds for passing around.
 
void setLoggingDataReceived (bool loggingData)
 Sets whether we are logging all data received or not. More...
 
void setLoggingDataSent (bool loggingData)
 Sets whether we are logging all data sent or not. More...
 
void setUseWrongEndChars (bool useWrongEndChars)
 Sets whether we're using the wrong (legacy) end chars or not. More...
 
void squelchNormal (void)
 This squelchs all the normal commands and help.
 
int tryLock ()
 Try to lock the server without blocking.
 
int unlock ()
 Unlock the server.
 
 ~ArNetServer ()
 Destructor.
 

Protected Attributes

ArSocket myAcceptingSocket
 
ArFunctorC< ArNetServermyAriaExitCB
 
std::list< ArSocket * > myConnectingConns
 
std::list< ArSocket * > myConns
 
std::list< ArSocket * > myDeleteList
 
ArFunctor3C< ArNetServer, char **, int, ArSocket * > myEchoCB
 
std::string myExtraString
 
std::map< std::string, ArFunctor3< char **, int, ArSocket * > *, ArStrCaseCmpOpmyFunctorMap
 
ArFunctor3C< ArNetServer, char **, int, ArSocket * > myHelpCB
 
std::map< std::string, std::string, ArStrCaseCmpOpmyHelpMap
 
bool myLoggingDataReceived
 
bool myLoggingDataSent
 
bool myMultipleClients
 
ArMutex myMutex
 
std::list< std::string > myNextCycleSends
 
ArMutex myNextCycleSendsMutex
 
bool myOpened
 
std::string myPassword
 
unsigned int myPort
 
ArFunctor3C< ArNetServer, char **, int, ArSocket * > myQuitCB
 
ArRobotmyRobot
 
ArSocket myServerSocket
 
ArFunctor3C< ArNetServer, char **, int, ArSocket * > myShutdownServerCB
 
bool mySquelchNormal
 
ArFunctorC< ArNetServermyTaskCB
 
bool myUseWrongEndChars
 
bool myWantToClose
 

Detailed Description

Class for running a simple net server to send/recv commands via text.

This class is for running a simple server which will have a list of commands to use and a fairly simple set of interactions... Start the server with the open() function, add commands with the addCommand() function and remove commands with remCommand(), and close the server with the close() function.

A client can connect via TCP on the port provided to open() and send a line of text where the first word is the command and the following words are extra arguments or data (space separated). The line should end with a newline ("\n") or carriage return character. The first line sent should be a password and must match the password given to open() in order to continue.

You can use the "telnet" program as a general client to any ArNetServer server.

It has a built in mutex, if you only use sendToAllClients() through the normal commands or during the robot loop you don't need to worry about locking anything and the server is locked before any of the callbacks for the commands are called so you really only need to lock the server if you're dealing with from another thread.... From another thread you can use sendToAllClientsNextCycle which takes care of all the locking itself in a threadsafe way (it puts the message in a list, then sends it in the next cycle of the loop). The only real reason to use the lock/sendToAllClients/unlock method is if you're highly concerned about synchronizing the different types of output.

Examples:
netServerExample.cpp.

Member Function Documentation

◆ addCommand()

bool ArNetServer::addCommand ( const char *  command,
ArFunctor3< char **, int, ArSocket *> *  functor,
const char *  help 
)

Adds a new command.

This adds a command to the list, when the command is given the broken up argv and argc are given along with the socket it came from (so that acks can occur)

Examples:
netServerExample.cpp.

◆ getLoggingDataReceived()

bool ArNetServer::getLoggingDataReceived ( void  )

Gets whether we are logging all data received or not.

Returns
if true data will be logged which means that all commands received from clients are logged

◆ getLoggingDataSent()

bool ArNetServer::getLoggingDataSent ( void  )

Gets whether we are logging all data sent or not.

Returns
if true data will be logged which means that all data sent out to the all the clients will be logged

◆ getUseWrongEndChars()

bool ArNetServer::getUseWrongEndChars ( void  )

Gets whether we're using the wrong (legacy) end chars or not.

Returns
if true data will be logged which means that all data sent out to the all the clients will be logged

◆ open()

bool ArNetServer::open ( ArRobot robot,
unsigned int  port,
const char *  password,
bool  multipleClients = true,
const char *  openOnIP = NULL 
)

Initializes the server.

Open the server, if you supply a robot this will run in the robots attached, if you do not supply a robot then it will be open and you'll have to call runOnce yourself (this is only recommended for advanced users)

Parameters
robotthe robot that this should be attached to and run in the sync task of or NULL not to run in any robot's task
portthe port to start up the service on
passwordthe password needed to use the service
multipleClientsif false only one client is allowed to connect, if false multiple clients are allowed to connect or just one
openOnIPIf not NULL, restrict server port to the network interface with this IP address. If NULL, accept connections from any network interface.
Returns
true if the server could be started, false otherwise
Examples:
netServerExample.cpp.

◆ remCommand()

bool ArNetServer::remCommand ( const char *  command)

Removes a command.

Parameters
commandthe command to remove
Returns
true if the command was there to remove, false otherwise

◆ sendToAllClients()

void ArNetServer::sendToAllClients ( const char *  str,
  ... 
)

Sends the given string to all the clients.

This sends the given string to all the clients, this string cannot be more than 2048 number of bytes.

See also the notes on locking in the class description.

Java and Python Wrappers: Not available in Java or Python wrapper libraries.

See also
sendToAllClientsPlain()

◆ sendToAllClientsNextCycle()

void ArNetServer::sendToAllClientsNextCycle ( const char *  str,
  ... 
)

Sends the given string to all the clients next cycle.

This sends the given string to all the clients, this string cannot be more than 2048 number of bytes.

Java and Python Wrappers: Not available in Java or Python wrapper libraries.

See also
sendToAllClientsNextCyclePlain()

◆ sendToClient()

void ArNetServer::sendToClient ( ArSocket socket,
const char *  ipString,
const char *  str,
  ... 
)

Sends the given string to the (hopefully) the client given (this method may go away)

This sends the given string to all the clients, this string cannot be more than 2048 number of bytes.

Java and Python Wrappers: Not available in Java or Python wrapper libraries.

See also
sendToClientPlain()

◆ setLoggingDataReceived()

void ArNetServer::setLoggingDataReceived ( bool  loggingData)

Sets whether we are logging all data received or not.

Parameters
loggingDataif true data will be logged which means that all commands received from clients are logged

◆ setLoggingDataSent()

void ArNetServer::setLoggingDataSent ( bool  loggingData)

Sets whether we are logging all data sent or not.

Parameters
loggingDataif true data will be logged which means that all data sent out to the all the clients will be logged

◆ setUseWrongEndChars()

void ArNetServer::setUseWrongEndChars ( bool  useWrongEndChars)

Sets whether we're using the wrong (legacy) end chars or not.

Set reverse line ending characters for compatibility with certain old clients.

Parameters
useWrongEndCharsif true the wrong end chars will be sent ('\n\r' instead of '\r\n'); a nonstandard, deprecated line ending, but may be required for certain old clients.

The documentation for this class was generated from the following files: