Implementing the Virtual Methods
The following code shows the
init() method for a listener that watches a directory. The
init() method is called during Agent initialization, and the name of the directory is passed to the listener in an
rwsf::Config instance.
These are the implemented virtual functions:
The
doInit() method initializes the listener based on initialization values it gets from the
rwsf::Config instance.
void MyListener::doInit(const rwsf::Config& initParams)
{
port_ = initParams.getInitParamater("port");
// get more initialization parameters...
...
}
The initServer() method needs to perform any setup needed by the listener before it is started. In the example below, note that the Socket class is for illustration only; it is not a HydraExpress class.
void MyListener::initServer()
{
// Instantiate and bind the socket...
Socket socket = new Socket();
socket.bind(port_);
mainThread_ = new SocketThread(socket, this);
}
The method doStart() kicks off the thread to start the listener. It should be implemented as a non-blocking method, and should prepare the system that waits for events and accepts requests for the rest of the system. In most cases, the start() method spawns at least one thread that listens for requests.
void MyListener::doStart()
{
mainThread_.start();
}
The method doStop() shuts down the thread, stopping the listener. The stop() method first releases any resources the listener owns and then stops the listener.
void MyListener::doStop()
{
mainThread_.shutdown();
mainThread_.join();
}
Use doReset() to reset the listener to the state it was in before it started.
void MyListener::doReset()
{
if( mainThread_.isRunning() )
doStop();
delete mainThread_;
}