Implementing the Virtual Methods
The following code shows the
init() method for a connector that watches a directory. The
init() method is called during Agent initialization, and the name of the directory is passed to the connector in an
rwsf::Config instance.
void
MyConnector::init(const rwsf::Config& config,
const rwsf::AgentContext& context)
{
ConnectorImp::init(config, context);
host_ = config.getInitParameter("host");
port_ = config.getInitParameter("port");
... // other needed initiation code
}
The derived
init() method should invoke the
init() method of its parent so the parent class can also process any information from the
rwsf::Config instance.
The start() method 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. The Agent invokes the start() method after it has initialized all connectors and handlers.
void
MyConnector::start()
{
address = // get address
serverSocket_ = new Socket();
serverSocket_.bind(address);
socketThread_ = new SocketThread(serverSocket);
socketThread_.start();
...
}
The stop() method should be implemented as a blocking call. The stop() method first releases any resources the connector owns and then stops the connector. The Agent invokes the stop() method when the Agent receives a signal to shut down.
void
MyConnector::stop()
{
socketThread_.shutdown();
socketThread_.join();
...
}