MCS  0.3.3-alpha7
mcs::UserThread Class Reference

The server side client thread. More...

#include <mcs.hh>

+ Inheritance diagram for mcs::UserThread:

Public Member Functions

ClientInfoinfo ()
 Return internal ClientInfo structure. More...
 
UserThreadoperator= (const UserThread &)
 Declared to avoid using of default assignment operator. More...
 
int userID ()
 
string userName ()
 Return the user name used to authenticate the client. More...
 
 UserThread (const UserThread &)
 Declared to avoid using of default copy constructor. More...
 
 UserThread (Thread *parent, int lID, int newsock)
 Constructor, called by a Server object when a new client is connected. More...
 
void wakeUpClient (Event *e=NULL)
 Send a message to the client. More...
 
 ~UserThread ()
 Destructor. More...
 
- Public Member Functions inherited from mcs::BaseThread
 BaseThread (const BaseThread &)
 Declared to avoid using of default copy constructor. More...
 
 BaseThread (Thread *parent, int lID)
 Constructor invoked from ClienThread objects. More...
 
 BaseThread (Thread *parent, const char *ltID)
 Constructor invoked from other objects. More...
 
int chkExt (string &s)
 Check if an external program or script is registered in the configuration file. More...
 
BaseThreadoperator= (const BaseThread &)
 Declared to avoid using of default assignment operator. More...
 
const char * tid ()
 Return the thread identifier. More...
 
 ~BaseThread ()
 Destructor. More...
 
- Public Member Functions inherited from mcs::Thread
Eventerror ()
 Return last error message. More...
 
int id ()
 Returns the Thread object identificator. More...
 
Threadparent ()
 Returns the address of the parent. More...
 
void start ()
 Start a new thread in the joinable state. More...
 
void startDetached (bool selfDelete=false)
 Start a new thread in the detached state. More...
 
int state ()
 Return the state of the thread. More...
 
void stop ()
 Stop thread execution. More...
 
 Thread (int id=0, Thread *parent=NULL)
 Declared to avoid using of default copy constructor. More...
 
virtual ~Thread ()
 Destructor. More...
 

Public Attributes

Record recv
 Record of Data objects received from the client or other threads. More...
 
Record send
 Record of Data objects to be dispatched to be sent to client. More...
 

Protected Member Functions

RetValue exec (string cmd, string pars="")
 Execution method for user's command. More...
 
virtual RetValue hk_auth (int &grants, bool &loginok)
 Virtual method called when the user issue the MCS_CMD_DBCONNECT command. More...
 
virtual RetValue hk_connect ()
 Virtual method called when a new user connects to the server. More...
 
virtual void hk_disconnect ()
 Virtual method called when the user disconnect. More...
 
virtual RetValue hk_exec (CommandParser *cmd, bool &cmd_executed)
 Virtual method called before executing a command. More...
 
virtual void hk_postexec (CommandParser *cmd, RetValue ret)
 Virtual method called after a command has been executed. More...
 
Serverparent ()
 Return the address of the Server object which generates this Thread. More...
 
void prompt (RetValue ret)
 Send a prompt to client. More...
 
void run ()
 The body of the thread. More...
 
RetValue Send (Event e, bool log=true)
 Send a message to client. More...
 
RetValue Send (Data &data)
 Send a Data object to client. More...
 
RetValue Send (Record &vec)
 Send a Record object to client. More...
 
RetValue Send (string filename, string path)
 Send a file to the client. More...
 
RetValue Send (vector< string > vec)
 Send a vector of strings to the client. More...
 
void send2OtherThread (Data &d, int destid)
 Used to send data object to other threads. More...
 
RetValue sendQueryRes ()
 Send query results as it was a file created with the Query.Result2Ascii() method. More...
 
RetValue sendStrings (string str)
 Send strings to client. More...
 
void setActiveRS (RecordSet *rs, bool delWhenFinished=true)
 
string wpath (string fn="")
 Return the path of the work directory followed by the path given in parameter. More...
 
- Protected Member Functions inherited from mcs::BaseThread
RetValue Log (Event e)
 Logging facility. More...
 
- Protected Member Functions inherited from mcs::Thread
virtual void final ()
 Finalization method. More...
 
virtual void initial ()
 Initialization method. More...
 
virtual void notify (int id, Thread *ref)
 A method called from child threads to notify their termination. More...
 
void set_cancel_state (bool cancel)
 Set cancellation state for current thread. More...
 
void test_cancel ()
 Test if a cancellation request is pending. More...
 

Protected Attributes

int batchlevel
 Nested batch level. More...
 
DBConn db
 Object to access DB. More...
 
string dbhost
 Host running database server. More...
 
string dbname
 Database name, usually the same as Env.appname. More...
 
string fnerr
 Default error file. More...
 
string fnout
 Default output file. More...
 
int grants
 User grants. More...
 
bool loginok
 Tells if client has logged in correctly. More...
 
int luserid
 The userid of current user. More...
 
string pass
 Password. More...
 
Queryquery
 Query object. More...
 
Socketsock
 Socket to client. More...
 
string user
 User name. More...
 
- Protected Attributes inherited from mcs::Thread
Eventlerror
 Last error. More...
 

Private Attributes

int csocket
 C socket descriptor, used to initialize the Socket object. More...
 
bool deleters
 
ClientInfo linfo
 Structure containing client informations. More...
 
 MCS_DEBUG_ALLOC
 
RecordSetrs
 
Synchro syn
 

Additional Inherited Members

- Static Public Member Functions inherited from mcs::BaseThread
static int fileType (string fn)
 Determine the file type of an external program or script. More...
 
static int spawn (string fn, string pars, string wpath=".", string thrID="x", string user="x", string pass="x", string dbname="x", string fout="out", string ferr="err")
 Execute an external program or script in a dedicated environment. More...
 
- Static Protected Attributes inherited from mcs::BaseThread
static Envenv
 Pointer to the actual Env object, this can be seen in all threaded object. More...
 

Detailed Description

The server side client thread.

This class provide the server functionality for a single client connected. Every time a new client is connected a new object based on this class (or a derived one) will be generated by the Server object.

The main methods are run() and exec(), the first is the body of the thread which reads command from the socket, the second is the execution unit of those commands.

This class can be derived to customize MCS behaviour. To do so simply overload one or more of the "hook" methods:

These methods are virtual, so you can overload them to customize the server, see the following code for an example. Suppose you want to implement a custom command, you should derive the UserThread class and overload the hk_exec() method:

class MyClient : public UserThread
{
//Constructors MUST have the same parameters.
MyClient(Thread* parent, int lID, int newsock) :
UserThread(parent, lID, newsock);
{}
~MyClient() {}
RetValue hk_exec(CommandParser* cmd, bool& cmd_executed)
{
cmd_executed = false;
if (cmd->cmpCmd("MyCustomCommand")) {
cmd_executed = true; //we are executing the command here
//... do the job ...
}
}
}

Once you define this class you should also derive the Server class with the MCS_CUSTOM_SERVER macro, so that it istantiate the correct class when a client connects (see the Server class description). Virtual methods aren't the only way to customize MCS, see also the callback functions in the Server class.

Definition at line 6365 of file mcs.hh.

Constructor & Destructor Documentation

◆ UserThread() [1/2]

mcs::UserThread::UserThread ( const UserThread )

Declared to avoid using of default copy constructor.

Warning
This constructor is declared but not implemented. If you try to use it you will get a compilation error.

◆ UserThread() [2/2]

mcs::UserThread::UserThread ( Thread parent,
int  lID,
int  newsock 
)

Constructor, called by a Server object when a new client is connected.

Parameters
parentAddress of the parent Server object;
lIDThread identifier;
newsockThe new C socket descriptor to be used by the new thread.

Definition at line 855 of file UserThread.cc.

◆ ~UserThread()

mcs::UserThread::~UserThread ( )

Destructor.

Definition at line 912 of file UserThread.cc.

Member Function Documentation

◆ exec()

RetValue mcs::UserThread::exec ( string  cmd,
string  pars = "" 
)
protected

Execution method for user's command.

This method execute user's command. It also call the necessary virtual methods, and even itself in case of a batch execution.

The command can be given entirely in the first parameter, or even using the second. In any case the executed command will be "cmd pars".

This method can throw several exceptions which are catched by the run() method. If a FATAL exception is raised the run() method will exit killing the thread.

Parameters
cmdString with the command;
parsOptional parameters.
Returns
value telling if the command has been executed correctly.
Note
This method will run in a critical section to be synchronized with the execution of the wakeUpClient() method.

Definition at line 316 of file UserThread.cc.

◆ hk_auth()

RetValue mcs::UserThread::hk_auth ( int &  grants,
bool &  loginok 
)
protectedvirtual

Virtual method called when the user issue the MCS_CMD_DBCONNECT command.

This method calls the cb_auth() and cbwa_auth() callback functions if defined.

If you overload this method the callback functions won't be called anymore. In this case if the return value is different from OK the authentication process will fail, and you should send an error message to the client using the Send() method.

Parameters
grantsUpon return should bear the grants for the user;
loginokUpon return is a flag saying if the user has logged in.
Returns
The return value from cb_auth(), or OK if no callback defined.

Definition at line 988 of file UserThread.cc.

◆ hk_connect()

RetValue mcs::UserThread::hk_connect ( )
protectedvirtual

Virtual method called when a new user connects to the server.

This method calls the cb_connect() and cbwa_connect() callback functions if defined.

If you overload this method the callback functions won't be called anymore. In this case if the return value is different from OK the thread would die immediately.

Returns
The return value from cb_connect(), or OK if no callback defined.

Definition at line 968 of file UserThread.cc.

◆ hk_disconnect()

void mcs::UserThread::hk_disconnect ( )
protectedvirtual

Virtual method called when the user disconnect.

This method calls the cb_disconnect callback function if defined.

If you overload this method the callback functions won't be called anymore.

Definition at line 981 of file UserThread.cc.

◆ hk_exec()

RetValue mcs::UserThread::hk_exec ( CommandParser cmd,
bool &  cmd_executed 
)
protectedvirtual

Virtual method called before executing a command.

This method calls the cb_exec() and cbwa_exec() callback functions if defined.

If you overload this method the callback functions won't be called anymore. In this case if the "cmd_executed" flag is true the exec() method will return immediately with the overloaded method's return value.

Parameters
cmdAddress of the actual CommandParser object;
cmd_executedUpon return tells if the command has been already executed or not.
Returns
The return value from cb_exec(), or OK if no callback defined.

Definition at line 945 of file UserThread.cc.

◆ hk_postexec()

void mcs::UserThread::hk_postexec ( CommandParser cmd,
RetValue  ret 
)
protectedvirtual

Virtual method called after a command has been executed.

This method calls the cb_postexec() and cbwa_postexec() callback functions if defined.

If you overload this method the callback functions won't be called anymore.

Parameters
cmdAddress of the actual CommandParser object;
retReturn value of the exec() method.

Definition at line 959 of file UserThread.cc.

◆ info()

ClientInfo & mcs::UserThread::info ( )

Return internal ClientInfo structure.

Definition at line 938 of file UserThread.cc.

◆ operator=()

UserThread& mcs::UserThread::operator= ( const UserThread )

Declared to avoid using of default assignment operator.

Warning
This operator is declared but not implemented. If you try to use it you will get a compilation error.

◆ parent()

Server * mcs::UserThread::parent ( )
protected

Return the address of the Server object which generates this Thread.

Definition at line 1004 of file UserThread.cc.

◆ prompt()

void mcs::UserThread::prompt ( RetValue  ret)
protected

Send a prompt to client.

Actually we have three type of prompt:

  • MCS_PROMPT_OK;
  • MCS_PROMPT_WARN;
  • MCS_PROMPT_ERROR.

so the user can see if the last command executed successfully or not.

Parameters
retA RetValue to determine the prompt to send.

Definition at line 190 of file UserThread.cc.

◆ run()

void mcs::UserThread::run ( )
protectedvirtual

The body of the thread.

Will send a welcome message followed by a prompt when the object is started, then read line by line commands from the socket and execute them with the exec() method, until the socket is closed, a FATAL exception is raised, or someone kills this thread.

Reimplemented from mcs::Thread.

Definition at line 205 of file UserThread.cc.

◆ Send() [1/5]

RetValue mcs::UserThread::Send ( Event  e,
bool  log = true 
)
protected

Send a message to client.

Parameters
eEvent object containing information about an event;
logIf true the message will also be logged.

Definition at line 91 of file UserThread.cc.

◆ Send() [2/5]

RetValue mcs::UserThread::Send ( Data data)
protected

Send a Data object to client.

Parameters
dataReference to a Data object data to be sent.

Definition at line 101 of file UserThread.cc.

◆ Send() [3/5]

RetValue mcs::UserThread::Send ( Record vec)
protected

Send a Record object to client.

Parameters
vecReference to a Record object data to be sent.

Definition at line 120 of file UserThread.cc.

◆ Send() [4/5]

RetValue mcs::UserThread::Send ( string  filename,
string  path 
)
protected

Send a file to the client.

The file must be present in the work directory. See wpath().

Parameters
filenameName of the file to be sent.
pathPath to the file, including working dir.

Definition at line 139 of file UserThread.cc.

◆ Send() [5/5]

RetValue mcs::UserThread::Send ( vector< string >  vec)
protected

Send a vector of strings to the client.

Strings will be sent using the MSG_OUT code.

Parameters
vecReference to a vector of strings to be sent.

Definition at line 149 of file UserThread.cc.

◆ send2OtherThread()

void mcs::UserThread::send2OtherThread ( Data d,
int  destid 
)
protected

Used to send data object to other threads.

Definition at line 1025 of file UserThread.cc.

◆ sendQueryRes()

RetValue mcs::UserThread::sendQueryRes ( )
protected

Send query results as it was a file created with the Query.Result2Ascii() method.

If an exception is raised during the execution it will be catched, then the message will be sent to the user as ane error, and the Event type will be returned. If the raised exception is of type FATAL it will be rethrown.

Returns
WARN if there was no result set, event type on a catched exception, OK otherwise.

Definition at line 160 of file UserThread.cc.

◆ sendStrings()

RetValue mcs::UserThread::sendStrings ( string  str)
protected

Send strings to client.

The parameter is splitted in at newlines, then sent to client one by one using the MSG_OUT code.

Parameters
strStrings to be sent.
Returns
OK.

Definition at line 276 of file UserThread.cc.

◆ userName()

string mcs::UserThread::userName ( )

Return the user name used to authenticate the client.

Definition at line 1010 of file UserThread.cc.

◆ wakeUpClient()

void mcs::UserThread::wakeUpClient ( Event e = NULL)

Send a message to the client.

Usually the this object is waiting to receive a request from the client on the other side of the socket. But sometime the LocalThread (or derived class) object needs to force the client to do something (see the LocalThread.run() method), in this case it can call this method to send a message to the client. The message sent will be the one contained in the Event object passd as parameter. If the parameter is null the message sent will be MSG_WAKE_UP.

Using this method makes sense only if on the other side of the socket there is a ClientThread object (see relative documentation), only in this case the client will react to this message.

Definition at line 282 of file UserThread.cc.

◆ wpath()

string mcs::UserThread::wpath ( string  fn = "")
protected

Return the path of the work directory followed by the path given in parameter.

No blanks, quotes or backslashes allowed.

Parameters
fnRelative path.
Returns
An absolute path in the "work" directory.

Definition at line 65 of file UserThread.cc.

Member Data Documentation

◆ batchlevel

int mcs::UserThread::batchlevel
protected

Nested batch level.

Definition at line 6389 of file mcs.hh.

◆ csocket

int mcs::UserThread::csocket
private

C socket descriptor, used to initialize the Socket object.

Definition at line 6370 of file mcs.hh.

◆ db

DBConn mcs::UserThread::db
protected

Object to access DB.

Definition at line 6417 of file mcs.hh.

◆ dbhost

string mcs::UserThread::dbhost
protected

Host running database server.

Definition at line 6413 of file mcs.hh.

◆ dbname

string mcs::UserThread::dbname
protected

Database name, usually the same as Env.appname.

Definition at line 6410 of file mcs.hh.

◆ fnerr

string mcs::UserThread::fnerr
protected

Default error file.

Definition at line 6401 of file mcs.hh.

◆ fnout

string mcs::UserThread::fnout
protected

Default output file.

Definition at line 6398 of file mcs.hh.

◆ grants

int mcs::UserThread::grants
protected

User grants.

Definition at line 6395 of file mcs.hh.

◆ linfo

ClientInfo mcs::UserThread::linfo
private

Structure containing client informations.

Definition at line 6373 of file mcs.hh.

◆ loginok

bool mcs::UserThread::loginok
protected

Tells if client has logged in correctly.

Definition at line 6392 of file mcs.hh.

◆ luserid

int mcs::UserThread::luserid
protected

The userid of current user.

Definition at line 6386 of file mcs.hh.

◆ pass

string mcs::UserThread::pass
protected

Password.

Definition at line 6407 of file mcs.hh.

◆ query

Query* mcs::UserThread::query
protected

Query object.

Definition at line 6420 of file mcs.hh.

◆ recv

Record mcs::UserThread::recv

Record of Data objects received from the client or other threads.

Definition at line 6713 of file mcs.hh.

◆ send

Record mcs::UserThread::send

Record of Data objects to be dispatched to be sent to client.

Definition at line 6710 of file mcs.hh.

◆ sock

Socket* mcs::UserThread::sock
protected

Socket to client.

Definition at line 6383 of file mcs.hh.

◆ user

string mcs::UserThread::user
protected

User name.

Definition at line 6404 of file mcs.hh.


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

mcslogo

MCS (My Customizable Server) ver. 0.3.3-alpha7
Documentation generated on Mon May 28 07:39:41 UTC 2018