Using the Client Proxy
Use the proxy to implement your client application. HydraExpress creates a sample implementation you may use as a framework. This section discusses how to implement the proxy for a synchronous operation. For information on the asynchronous operation, see The Asynchronous Client API.
First, we’ll look at the generated class, and then we’ll see how we can easily edit it to implement the getDayofWeek method.
The generated sample implementation below shows the DayOfWeekPortClient.cpp file created by the rwsfgen program and located in the DayOfWeekExample\app\client directory. The sample is not a class, but a source file that includes the proxy header file and a main() function that invokes the proxy’s methods.
 
#include "DayOfWeekExample/DayOfWeekBindingProxy.h" //1
#include <iostream>
 
#include <include/webservice/DefaultLogger.h>
#include <include/webservice/HandlerManager.h>
#include <include/webservice/MessageHandler.h>
#include <include/webservice/transport/TransportManager.h>
 
void invoke_DayOfWeek(DayOfWeekBindingProxy& proxy) //2
{
std::string date_in;
std::string dayOfWeek_ret;
rwsf::CallInfo callInfo;
 
try {
dayOfWeek_ret = proxy.getDayOfWeek(callInfo, date_in);
} catch(const rwsf::SoapFaultException& e) {
std::cout << "Fault Code: " << e.getFault().getFaultcode().asString()
<< std::endl;
std::cout << "Fault String: " << e.getFault().getFaultstring() << std::endl;
}
}
 
void logError(std::string error, std::string detail) {
rwsf::HandlerManager::invokeLogger(error + ": " + detail,
rwsf::CallInfo::Error);
}
 
using rwsf::DefaultLogger;
RWSF_DEFINE_STATIC_MESSAGE_HANDLER("RWSFClientLogger",DefaultLogger)
 
int main(int argc, char* argv[])
{
 
std::string location; //3
 
if (argc < 2) {
location = "http://localhost:8090/dayofweek/DayOfWeek";
}
else if (argv[1][0]=='-' && argv[1][1]=='a') {
location = "http://localhost:8013/dayofweek/DayOfWeek";
}
else if (argv[1][0]=='-' && argv[1][1]=='l') {
location = "http://localhost:8090/dayofweek/DayOfWeek";
}
else {
location = argv[1];
}
try {
// initialize config files //4
rwsf::NamingContext::loadGlobal("../conf/client-objects.xml");
rwsf::HandlerManager::loadConfiguration("../conf/client-handlers.xml" );
rwsf::TransportManager::initialize("../conf/client-transports.xml" );
 
DayOfWeekBindingProxy proxy =
DayOfWeekBindingProxy::make(location); //5
 
if(!proxy.isValid()) {
std::cerr << "Unable to create proxy. " << "\nExiting" <<
std::endl;
return 1;
}
invoke_getDayOfWeek(proxy); //6
 
} catch (const rwsf::Exception& x) { //7
std::cerr << "rwsf::Exception thrown: "
<< x.what() << std::endl;
return 1;
} catch(const std::exception& x) {
std::cerr << "std::exception thrown:" << std::endl
<< x.what() << std::endl;
return 1;
} catch(...) {
std::cerr << "Unknown exception thrown" << std::endl;
return 1;
}
return 0;
}
//1 Includes necessary header files.
//2 Sample function that invokes the getDayOfWeek operation. The function takes a reference to a DayOfWeekPortClient proxy, creates standard string instances of the input and output variables as well as an instance of rwsf::CallInfo, and calls the getDayOfWeek operation on the proxy. The client generator implements one sample invocation method for each operation. The method uses the synchronous proxy method that takes an explicit rwsf::CallInfo parameter. The program provides a try block around the code that calls methods on the proxy. The proxy indicates errors by throwing a rwsf::SoapFaultException derived from rwsf::Exception, and the server writes a log message by default. Note that the generator makes no attempt to provide valid input for the operation.
//3 The main() function first determines the location for the service through a combination of information from the WSDL and possible command line options and arguments.
If no command line argument is used when invoking the client, the default is the location specified in the WSDL. (In this case, the location points to the simulated DayOfWeek service that ships with HydraExpress.)
//4 Registers the service handlers, naming context, and transports that are available for this message.
//5 Obtains a client proxy and ensures that it is valid.
//6 Calls the invoke_DayOfWeek() method with the proxy.
//7 Catch blocks for exceptions that might be thrown by this call. The exceptions shown should always be caught. If you generate code with the ‑SourcePro option, which uses Rogue Wave SourcePro C++ datatypes, you should also catch exceptions derived from RWxmsg.
To create a working client, simply implement the invoke_getDayOfWeek method, replacing the generated code with your own.
In this case we will use the implemented client application DayOfWeekPortClient.cpp provided in the directory <installdir>\examples\webservices\DayofWeek.
The code below shows how the shipped file implements the service operation method invoke_getDayofWeek().
 
void invoke_getDayOfWeek(DayOfWeekBindingProxy& proxy)
{
std::string date_in;
std::cout << "Enter date: " << std::flush;
std::cin >> date_in;
rwsf::DateTime dt(date_in, rwsf::DateTime::setDate);
if (!dt.isValid()) { std::cout << "Error: Invalid date" << std::endl;
return;
}
else {
rwsf::DateTime today(rwsf::DateTime::setCurrentTime);
std::string toBe;
std::string dayOfWeek_ret;
if (date_in < today) {
toBe = " was a ";
} else if (date_in == today) {
toBe = " is a ";
} else {
toBe = " will be a ";
}
rwsf::CallInfo info;
 
try {
dayOfWeek_ret = proxy.getDayOfWeek(info, date_in);
std::cout << date_in << toBe << dayOfWeek_ret << std::endl;
} catch(const rwsf::SoapFaultException& e) {
std::cout << "Fault Code: " << e.getFault().getFaultcode().asString()
<< std::endl;
std::cout << "Fault String: " << e.getFault().getFaultstring()
<< std::endl;
}
}
}
...
 
Class rwsf::DateTime correctly parses many common date formats. The client uses this class to simplify the process of converting input from the console into the ISO8601 format that the schema requires.