MCS
0.3.3-alpha7
|
Main server class for a MCS-based application. More...
#include <mcs.hh>
Public Member Functions | |
RecordSet * | getAll_ClientInfo () |
Return a RecordSet object containing informations about all clients connections. More... | |
Server & | operator= (const Server &) |
Declared to avoid using of default assignment operator. More... | |
Server (const Server &) | |
Declared to avoid using of default copy constructor. More... | |
Server (Env *lenv) | |
Constructor. More... | |
~Server () | |
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 | |
RetValue(* | cb_auth )(int &, bool &) |
Pointer to a callback function, called by hk_auth(). More... | |
RetValue(* | cb_connect )() |
Pointer to a callback function, called by hk_connect(). More... | |
void(* | cb_disconnect )() |
Pointer to a callback function, called by hk_disconnect(). More... | |
RetValue(* | cb_exec )(CommandParser *, bool &_executed) |
Pointer to a callback function, called by hk_exec(). More... | |
void(* | cb_log )(UserThread *p, Event e) |
Pointer to a callback function, called by hk_log(). More... | |
void(* | cb_newClient )(int i) |
Pointer to a void callback function, called by hk_newClient(). More... | |
void(* | cb_postexec )(CommandParser *, RetValue) |
Pointer to a callback function, called by hk_postexec(). More... | |
void(* | cbwa_auth )() |
Pointer to a void callback function without arguments, called by hk_auth(). More... | |
void(* | cbwa_connect )() |
Pointer to a void callback function without arguments, called by hk_connect(). More... | |
void(* | cbwa_exec )() |
Pointer to a void callback function without arguments, called by hk_exec(). More... | |
void(* | cbwa_log )() |
Pointer to a void callback function without arguments, called by hk_log(). More... | |
void(* | cbwa_postexec )() |
Pointer to a void callback function without arguments, called by hk_postexec(). More... | |
Record | dispatch |
Record of Data objects to be sent to various threads. More... | |
Protected Member Functions | |
virtual void | hk_newClient (int i) |
Virtual method called when a new user connects to the server. More... | |
void | killAllClients () |
Kills all UserThread and the LocalThread objects. More... | |
void | killClient (int i) |
Kill client with specified identifier. More... | |
void | newClient (int newsock) |
Search for a free identifier and creates a new UserThread object. More... | |
virtual LocalThread * | newLocalThread () |
Wrapper around LocalThread constructors. More... | |
virtual UserThread * | newUserThread (int lID, int newsock) |
Wrapper around UserThread constructors. More... | |
void | notify (int id, Thread *ref) |
Called when a child thread terminate, used to free resources. More... | |
![]() | |
RetValue | Log (Event e) |
Logging facility. More... | |
![]() | |
virtual void | final () |
Finalization method. More... | |
virtual void | initial () |
Initialization method. 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... | |
Private Member Functions | |
int | find_free_id () |
Search for a free client identifier when a new client tries to connect. See Env.max_users. More... | |
void | run () |
Server thread body. More... | |
Private Attributes | |
MCS_DEBUG_ALLOC | |
UserThread ** | pClient |
Array of pointers to UserThread objects. More... | |
Friends | |
class | LocalThread |
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... | |
![]() | |
Event * | lerror |
Last error. More... | |
![]() | |
static Env * | env |
Pointer to the actual Env object, this can be seen in all threaded object. More... | |
Main server class for a MCS-based application.
Every MCS-based application must use an object of this class, or a derived one as main server.
When started it create a LocalThread object (using the newLocalThread() method), then it acts like an usual TCP server listening on a specified port and waiting for a connection. When a new connection arrives it create a new UserThread object (using the newUserThread() method) that runs on a separate thread and handle the connection. This object will terminate spotaneously and destroy himself if a FATAL error is raised or if the LocalThread terminate and the Env.cl_root_kills_mcs variable is true. The main program can also terminate this object by calling its exit() method. Remember that you must create the Server object in the heap memory and never try to destroy it, it will destroy by himself, That is because it is an object derived from Thread (see Thread).
To customize MCS you should derive the UserThread and/or LocalThread classes, and use a server object which istantiate the derived classes instead of the standard ones. For this reason, the newLocalThread() and newUserThread() methods are virtual, so you can derive the Server class and overload these methods to use the UserThread and/or LocalThread derived classes. Suppose you implemented a class named MyClient derived from UserThread, and a class named MyLocal derived from LocalThread, here's an example of the code you should write to correctly derive the Server class:
Since overriding the newLocalThread() and newUserThread() methods are the only reason to derive the Server class, we provide an easiest way to reach the same goal, that is using the MCS_CUSTOM_SERVER macro. The code shown above can be substituted with:
To create a working server you must first create an Env object containing all information needed to run the server, then you can create the server object. A typical usage is as follows:
We provide a facility function (mcsstart()) that perform all these steps. You can substitute the code above with:
This class also provide access to the "callback functions" customizing facilities. Using callback functions you can customize the server behaviour without deriving the UserThread class (as you would do if you want to use the "hooks" methods, instead of callback). Callback functions are C++ functions (not methds!) with well determined parameters and return value, which are called automatically by the "hooks" (note that if you overload those method the callback functions won't be called anymore). In the Server class there are a number of variables whose name start with "cb_" or "cbwa_", which are pointer to functions. There are one "cb_" and one "cbwa_" variable for each "hook" method (described in the relative classes LocalThread and UserThread).
Suppose you want a function to be executed each time a line of log is written, you could of course derive the LocalThread class and overload the hk_log() method. Another way is to set a callback function for that hook, here's an example:
Note that the callback function has the same parameters and return values as the corresponding hook method. Note also that here we used the "cb_log" variable, there is also a "cbwa_log" variable but this, as well as all the other "cbwa_" variables, are pointers to void functions without arguments (the "wa" means just "without arguments").
You can use callback functions instead
mcs::Server::Server | ( | const Server & | ) |
Declared to avoid using of default copy constructor.
mcs::Server::Server | ( | Env * | lenv | ) |
|
private |
Search for a free client identifier when a new client tries to connect. See Env.max_users.
RecordSet * mcs::Server::getAll_ClientInfo | ( | ) |
|
protectedvirtual |
Virtual method called when a new user connects to the server.
This method calls the cb_newClient() callback functions if defined. If you overload this method the callback functions won't be called anymore.
This method work the same as the hk_connect() virtual method, but it is used inside the Server class instead of UserThread.
If you overload this method the callback functions won't be called anymore.
i | Index of the newly created UserThread object. |
|
protected |
Kills all UserThread and the LocalThread objects.
|
protected |
|
protected |
Search for a free identifier and creates a new UserThread object.
|
protectedvirtual |
Wrapper around LocalThread constructors.
This method can be overload to istantiate object of a LocalThread derived class.
|
protectedvirtual |
Wrapper around UserThread constructors.
Parameters are passed directly to the constructor. This method can be overload to istantiate object of a UserThread derived class.
|
protectedvirtual |
Called when a child thread terminate, used to free resources.
Reimplemented from mcs::Thread.
Declared to avoid using of default assignment operator.
|
privatevirtual |
Server thread body.
Initialize the server socket, then wait for a new connection. When a new connection arrives call the virtual newUserThread() method to istantiate a new client thread that can handle that client connection.
Reimplemented from mcs::Thread.
RetValue(* mcs::Server::cb_auth) (int &, bool &) |
RetValue(* mcs::Server::cb_connect) () |
void(* mcs::Server::cb_disconnect) () |
RetValue(* mcs::Server::cb_exec) (CommandParser *, bool &_executed) |
void(* mcs::Server::cb_log) (UserThread *p, Event e) |
void(* mcs::Server::cb_newClient) (int i) |
Pointer to a void callback function, called by hk_newClient().
void(* mcs::Server::cb_postexec) (CommandParser *, RetValue) |
void(* mcs::Server::cbwa_auth) () |
void(* mcs::Server::cbwa_connect) () |
void(* mcs::Server::cbwa_exec) () |
void(* mcs::Server::cbwa_log) () |
void(* mcs::Server::cbwa_postexec) () |
Record mcs::Server::dispatch |
|
private |
Array of pointers to UserThread objects.
![]() |
MCS (My Customizable Server) ver. 0.3.3-alpha7
|