Request Handling and Threading
This code creates a custom listener class:
#include <rwsf/core/Config.h> //1
#include <rwsf/webservice/listener/MessageListenerImp.h>
class MyListener : public rwsf::MessageListenerImp {
public:
MyListener();
protected:
virtual void doInit(const rwsf::Config& initParams);
virtual void initServer();
virtual void doStart();
virtual void doStop();
virtual void doReset();
private:
int port_;
client_; //2
example::SocketThread mainThread_;
};
NOTE: For threading and network capabilities in HydraExpress services, you can use the network and threading classes of Rogue Wave’s SourcePro.
The following lines are pseudo code for creating a threading framework. This framework is needed so the doStart() method can be implemented as non-blocking.
namespace example {
class SocketThread : public Thread {
public:
SocketThread(Socket socket, MyListener listener); //1
void run(); //2
private:
Socket socket_;
MyListener listener_;
};
}
void SocketThread::run()
{
while( not end ) { //3
client = socket.accept(); //4
RequestHandler(client, listener).start(); //5
}
}
A listener needs to do the following work:
Collect incoming requests off the wire and place data into an
rwsf::CallInfo instance. See the description of this class for information on the data structures and methods it provides.
Invoke the service implementation for handling the request.
This sample code declares the RequestHandler class:
class RequestHandler : public Thread {
public:
RequestHandler(client, MyListener listener);
void run();
private:
client_;
MyListener listener_;
};
void RequestHandler::run()
{
listener_.handleRequest(client_);
}
The RequestHandler’s run() method invokes the listener’s handleRequest() method, which actually processes the request:
void MyListener::handleRequest(client)
{
std::string payload = client.getPayload(); //1
rwsf::CallInfo callInfo;
callInfo.setRequest(payload); //2
invoke(callInfo); //3
std::string response = callInfo.getResponse(); //4
client.write(response); //5
}
When the invoke has completed, the
rwsf::CallInfo should contain any information needed to send back a response. This information might include a response payload, headers, a status code, and so on. You need to implement code for writing that information to a location where the client can retrieve it.
Finally, use the following convenience macro that generates code to create a listener instance.
RWSF_DEFINE_RWSF_LISTENER(MyListener)