We find ourselves writing the same code snippets over and over again.
Perhaps this is useful to others.
For clients:
template<class xxxClient>
xxxClient*
getClient(const char* sname, const char* host="127.0.0.1")
{
int port = applianceUtils::getPortforService(sname); /// @TODO:
XXX your "portmapper" here.
shared_ptr<TSocket> socket(new TSocket(host, port));
shared_ptr<TBufferedTransport> bufferedSocket(new
TBufferedTransport(socket));
bufferedSocket->open();
shared_ptr<TBinaryProtocol> protocol(new
TBinaryProtocol(bufferedSocket));
return new xxxClient(protocol);
}
Usage:
shared_ptr<userManagerClient>
c(getClient<userManagerClient>("usermanager"));
c->login(name, password);
c->logout();
... connection closed when "c" goes out of scope
For servers we use this: Obviously you can add more common prolog/epilog
(like logging), make TSimpleServer be a template parameter, etc.
template<class xxxManager, class xxxManagerProcessor>
static void
RunServer(int port, xxxManager* impl)
{
shared_ptr<xxxManager> s(impl);
shared_ptr<TProcessor> processor(new xxxManagerProcessor(s));
shared_ptr<TServerTransport> transport(new TServerSocket(port));
shared_ptr<TTransportFactory> tfactory(new
TBufferedTransportFactory());
shared_ptr<TProtocolFactory> pfactory(new
TBinaryProtocolFactory());
TSimpleServer server(processor, transport, tfactory, pfactory);
server.serve();
}
Usage:
RunServer<hardwareManager, hardwareManagerProcessor>(
applianceUtils::getPortforService("hardwaremanager");
new hardwareManagerServer());
Hopefully this will save *dozens* of lines of code world-wide. :)
/r$
--
STSM, WebSphere Appliance Architect
https://www.ibm.com/developerworks/mydeveloperworks/blogs/soma/