// WsClientGenericRepository.cxx #include "dataset_util/Text.h" #include "dataset_xml/XmlParser.h" #include "dial_ws_repository/WsClientGenericRepository.h" #include "../gsoap/dial_ws_repositoryH.h" using std::string; using std::cerr; using std::endl; using dset::GenericRepository; using dial::WsClientGenericRepository; using namespace dial_ws_repository; typedef GenericRepository::size_type size_type; typedef GenericRepository::IdList IdList; //********************************************************************** // Local definitions. //********************************************************************** namespace { //********************************************************************** // Creator. // We expect connection of the form // @WS: // Also allowed are extra accessors, e.g. // @@WS: GenericRepository* create(string type, string conn) { // Strip off the last accessor. string::size_type ipos = conn.rfind("@"); string accessor = conn.substr(ipos+1); if ( accessor.size() < 5 ) return 0; // Extract address (server:port) from accessor. if ( accessor.substr(0,3) != "WS:" ) return 0; string url = accessor.substr(3); // Keep the remaining prefix as the new connection. string newconn; if ( ipos > 1 ) { newconn = conn.substr(0, ipos); } // Create the client repository. GenericRepository* pgen = new WsClientGenericRepository(url, type, newconn, true); return pgen; } //********************************************************************** // Register the creator. int wsgr_creator_stat = GenericRepository::register_creator("WS", create); //********************************************************************** } // end unnamed namespace //********************************************************************** // Constructors and destructor. //********************************************************************** // Constructor. WsClientGenericRepository:: WsClientGenericRepository(string url, string type, string conn, bool usegsi) : m_client(url, usegsi), m_type(type), m_conn(conn) { } //********************************************************************** // Repository methods. //********************************************************************** // Validity. bool WsClientGenericRepository::is_valid() const { bool valid; int sstat = soap_call_dial__repository_is_valid( psoap(), curl(), "", type(), conn(), valid ); if ( sstat != SOAP_OK ) { soap_print_fault(psoap(), stderr); clean_soap(); return false; } clean_soap(); return valid; } //********************************************************************** // Error code. int WsClientGenericRepository::error() const { int errout; int sstat = soap_call_dial__repository_error( psoap(), curl(), "", type(), conn(), errout ); if ( sstat != SOAP_OK ) { soap_print_fault(psoap(), stderr); clean_soap(); return 102; } clean_soap(); return errout; } //********************************************************************** // Error string. string WsClientGenericRepository::error_string(int err) const { if ( err == 101 ) { clean_soap(); return "Server unable to resolve type/connection"; } else if ( err == 102 ) { clean_soap(); return "Unable to contact server"; } else { string msg; int sstat = soap_call_dial__repository_error_string( psoap(), curl(), "", type(), conn(), err, msg ); if ( sstat != SOAP_OK ) { soap_print_fault(psoap(), stderr); clean_soap(); return "Unable to retrieve error string"; } clean_soap(); return msg; } clean_soap(); return "Unknown error"; } //********************************************************************** // Repository type. string WsClientGenericRepository::repository_type() const { return m_type; } //********************************************************************** // Insert. int WsClientGenericRepository::insert(string sid, string sdesc) const { int stat; int sstat = soap_call_dial__repository_insert( psoap(), curl(), "", type(), conn(), sid, sdesc, stat ); if ( sstat != SOAP_OK ) { soap_print_fault(psoap(), stderr); clean_soap(); return 102; } clean_soap(); return stat; } //********************************************************************** // Remove. int WsClientGenericRepository::remove(string sid) const { int stat; int sstat = soap_call_dial__repository_remove( psoap(), curl(), "", type(), conn(), sid, stat ); if ( sstat != SOAP_OK ) { soap_print_fault(psoap(), stderr); clean_soap(); return 102; } clean_soap(); return stat; } //********************************************************************** // Has. bool WsClientGenericRepository::has(string sid) const { bool stat; int sstat = soap_call_dial__repository_has( psoap(), curl(), "", type(), conn(), sid, stat ); if ( sstat != SOAP_OK ) { soap_print_fault(psoap(), stderr); clean_soap(); return false; } clean_soap(); return stat; } //********************************************************************** // Retrieve. string WsClientGenericRepository::get(string sid) const { dial__StringResponse res; int sstat = soap_call_dial__repository_get( psoap(), curl(), "", type(), conn(), sid, res ); if ( sstat != SOAP_OK ) { soap_print_fault(psoap(), stderr); clean_soap(); return ""; } string desc = res.Response; clean_soap(); return desc; } //********************************************************************** // Multiple retrieve. int WsClientGenericRepository:: mget(const IdList& sids, ElementList& eles) const { dial__StringVector vids; vids.pvec = const_cast(&sids); ElementList neweles; dial__StringVector res; res.pvec = &neweles; int sstat = soap_call_dial__repository_mget( psoap(), curl(), "", type(), conn(), vids, res ); if ( sstat != SOAP_OK ) { soap_print_fault(psoap(), stderr); clean_soap(); return 99; } // Apparently pvec may be null if no elements are returned. if ( res.pvec != 0 ) { eles.insert(eles.end(), res.pvec->begin(), res.pvec->end()); } clean_soap(); return 0; } //********************************************************************** // Time. time_t WsClientGenericRepository::time(string sid) const { Time time = 0; int sstat = soap_call_dial__repository_time( psoap(), curl(), "", type(), conn(), sid, time ); if ( sstat != SOAP_OK ) { soap_print_fault(psoap(), stderr); clean_soap(); return 0; } clean_soap(); return time; } //********************************************************************** // Size. size_type WsClientGenericRepository::size() const { unsigned int count; int sstat = soap_call_dial__repository_size( psoap(), curl(), "", type(), conn(), count ); if ( sstat != SOAP_OK ) { soap_print_fault(psoap(), stderr); clean_soap(); return 0; } clean_soap(); return count; } //********************************************************************** // Size since. size_type WsClientGenericRepository::size_since(time_t time) const { unsigned int count; int sstat = soap_call_dial__repository_size_since( psoap(), curl(), "", type(), conn(), time, count ); if ( sstat != SOAP_OK ) { soap_print_fault(psoap(), stderr); clean_soap(); return 0; } clean_soap(); return count; } //********************************************************************** // Get ID's. IdList WsClientGenericRepository::get_ids(size_type maxent) const { IdList ids; char* cids; int sstat = soap_call_dial__repository_get_ids( psoap(), curl(), "", type(), conn(), maxent, cids ); if ( sstat != SOAP_OK ) { soap_print_fault(psoap(), stderr); clean_soap(); return ids; } string sids = cids; Text::WordList wids = Text::split(sids, " ", false); for ( Text::WordList::const_iterator iwid=wids.begin(); iwid!=wids.end(); ++iwid ) { ids.push_back(*iwid); } clean_soap(); return ids; } //********************************************************************** // Get ID's since. IdList WsClientGenericRepository:: get_ids_since(time_t time, size_type maxent) const { IdList ids; char* cids; int sstat = soap_call_dial__repository_get_ids_since( psoap(), curl(), "", type(), conn(), maxent, time, cids ); if ( sstat != SOAP_OK ) { soap_print_fault(psoap(), stderr); clean_soap(); return ids; } string sids = cids; Text::WordList wids = Text::split(sids, " ", false); for ( Text::WordList::const_iterator iwid=wids.begin(); iwid!=wids.end(); ++iwid ) { ids.push_back(*iwid); } clean_soap(); return ids; } //********************************************************************** // Service methods. //********************************************************************** // Return the gsoap structure. soap* WsClientGenericRepository::psoap() const { return m_client.psoap(); } //********************************************************************** // Return the URL. const char* WsClientGenericRepository::curl() const { return m_client.curl(); } //********************************************************************** // Return the repository type.. const char* WsClientGenericRepository::type() const { return m_type.c_str(); } //********************************************************************** // Return the connection string. const char* WsClientGenericRepository::conn() const { return m_conn.c_str(); } //********************************************************************** // Terminate. void WsClientGenericRepository::terminate_service() const { return m_client.terminate_service(); } //********************************************************************** // Cleanup gsoap. void WsClientGenericRepository::clean_soap() const { m_client.clean_soap(); } //**********************************************************************