MCS
0.3.3-alpha7
|
The server side client thread. More...
#include <mcs.hh>
Public Member Functions | |
ClientInfo & | info () |
Return internal ClientInfo structure. More... | |
UserThread & | operator= (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... | |
![]() | |
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... | |
BaseThread & | operator= (const BaseThread &) |
Declared to avoid using of default assignment operator. More... | |
const char * | tid () |
Return the thread identifier. More... | |
~BaseThread () | |
Destructor. More... | |
![]() | |
Event * | error () |
Return last error message. More... | |
int | id () |
Returns the Thread object identificator. More... | |
Thread * | parent () |
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... | |
Server * | parent () |
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... | |
![]() | |
RetValue | Log (Event e) |
Logging facility. More... | |
![]() | |
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... | |
Query * | query |
Query object. More... | |
Socket * | sock |
Socket to client. More... | |
string | user |
User name. More... | |
![]() | |
Event * | lerror |
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 | |
RecordSet * | rs |
Synchro | syn |
Additional Inherited Members | |
![]() | |
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 Env * | env |
Pointer to the actual Env object, this can be seen in all threaded object. More... | |
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:
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.
mcs::UserThread::UserThread | ( | const UserThread & | ) |
Declared to avoid using of default copy constructor.
mcs::UserThread::UserThread | ( | Thread * | parent, |
int | lID, | ||
int | newsock | ||
) |
Constructor, called by a Server object when a new client is connected.
parent | Address of the parent Server object; |
lID | Thread identifier; |
newsock | The new C socket descriptor to be used by the new thread. |
Definition at line 855 of file UserThread.cc.
mcs::UserThread::~UserThread | ( | ) |
Destructor.
Definition at line 912 of file UserThread.cc.
|
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.
cmd | String with the command; |
pars | Optional parameters. |
Definition at line 316 of file UserThread.cc.
|
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.
grants | Upon return should bear the grants for the user; |
loginok | Upon return is a flag saying if the user has logged in. |
Definition at line 988 of file UserThread.cc.
|
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.
Definition at line 968 of file UserThread.cc.
|
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.
|
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.
cmd | Address of the actual CommandParser object; |
cmd_executed | Upon return tells if the command has been already executed or not. |
Definition at line 945 of file UserThread.cc.
|
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.
cmd | Address of the actual CommandParser object; |
ret | Return value of the exec() method. |
Definition at line 959 of file UserThread.cc.
ClientInfo & mcs::UserThread::info | ( | ) |
Return internal ClientInfo structure.
Definition at line 938 of file UserThread.cc.
UserThread& mcs::UserThread::operator= | ( | const UserThread & | ) |
Declared to avoid using of default assignment operator.
|
protected |
Return the address of the Server object which generates this Thread.
Definition at line 1004 of file UserThread.cc.
|
protected |
Send a prompt to client.
Actually we have three type of prompt:
so the user can see if the last command executed successfully or not.
ret | A RetValue to determine the prompt to send. |
Definition at line 190 of file UserThread.cc.
|
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 a message to client.
e | Event object containing information about an event; |
log | If true the message will also be logged. |
Definition at line 91 of file UserThread.cc.
Send a Data object to client.
data | Reference to a Data object data to be sent. |
Definition at line 101 of file UserThread.cc.
Send a Record object to client.
vec | Reference to a Record object data to be sent. |
Definition at line 120 of file UserThread.cc.
|
protected |
Send a file to the client.
The file must be present in the work directory. See wpath().
filename | Name of the file to be sent. |
path | Path to the file, including working dir. |
Definition at line 139 of file UserThread.cc.
|
protected |
Send a vector of strings to the client.
Strings will be sent using the MSG_OUT code.
vec | Reference to a vector of strings to be sent. |
Definition at line 149 of file UserThread.cc.
|
protected |
Used to send data object to other threads.
Definition at line 1025 of file UserThread.cc.
|
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.
Definition at line 160 of file UserThread.cc.
|
protected |
Send strings to client.
The parameter is splitted in at newlines, then sent to client one by one using the MSG_OUT code.
str | Strings to be sent. |
Definition at line 276 of file UserThread.cc.
string mcs::UserThread::userName | ( | ) |
Return the user name used to authenticate the client.
Definition at line 1010 of file UserThread.cc.
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.
|
protected |
Return the path of the work directory followed by the path given in parameter.
No blanks, quotes or backslashes allowed.
fn | Relative path. |
Definition at line 65 of file UserThread.cc.
|
protected |
|
private |
|
protected |
|
protected |
Database name, usually the same as Env.appname.
|
protected |
|
private |
|
protected |
|
protected |
Record mcs::UserThread::recv |
Record mcs::UserThread::send |
![]() |
MCS (My Customizable Server) ver. 0.3.3-alpha7
|