A command-line client for interacting with "book" and "member" servers via XMLRPC calls. Servers were created by other students.

BookstoreCaller.cpp 7.5KB

    #include "BookstoreCaller.h" #include <iostream> using namespace std; BookstoreCaller::BookstoreCaller() { } BookstoreCaller::BookstoreCaller(std::string MemberAddress, std::string MemberPort, std::string BookAddress, std::string BookPort) { _memberAddress = MemberAddress; _memberFullAddress = _memberAddress + ":" + MemberPort + "/RPC2"; _memberPort = atoi(MemberPort.c_str()); _bookAddress = BookAddress; _bookFullAddress = _bookAddress + ":" + BookPort + "/RPC2"; _bookPort = atoi(BookPort.c_str()); // connect to the Book and Member servers MemberConnect(); BookConnect(); } bool BookstoreCaller::MemberConnect() { if ( MemberAddress() == "0" || MemberAddress() == "" || MemberPort() == 0 ) { _isMemberUp = false; return false; } map<string, MemberType> members = MemberList(); if( members.size() >= 0 ) { _isMemberUp = true; return true; } _isMemberUp = false; return false; } bool BookstoreCaller::BookConnect() { if ( BookAddress() == "0" || BookAddress() == "" || BookPort() == 0 ) { _isBookUp = false; return false; } map<string, BookType> books = BookList(); if( books.size() >= 0 ) { _isBookUp = true; return true; } _isBookUp = false; return false; } /// Private Data Member Methods std::string BookstoreCaller::MemberFullAddress() { return _memberFullAddress; } std::string BookstoreCaller::MemberAddress() { return _memberAddress; } int BookstoreCaller::MemberPort() { return _memberPort; } bool BookstoreCaller::IsMemberServerUp() { return _isMemberUp; } std::string BookstoreCaller::BookFullAddress() { return _bookFullAddress; } std::string BookstoreCaller::BookAddress() { return _bookAddress; } int BookstoreCaller::BookPort() { return _bookPort; } bool BookstoreCaller::IsBookServerUp() { return _isBookUp; } bool BookstoreCaller::IsReady() { return ( IsBookServerUp() && IsMemberServerUp() ); } std::map<std::string, MemberType> BookstoreCaller::Members() { return _members; } int BookstoreCaller::MemberCount() { return _members.size(); } std::map<std::string, BookType> BookstoreCaller::Books() { return _books; } int BookstoreCaller::BookCount() { return _books.size(); } /// XMLRPC-C Server Calling Methods // --- Books --- bool BookstoreCaller::AddBook(BookType b) { if( !IsBookServerUp() ) return false; using namespace xmlrpc_c; xmlrpc_c::value result; string Method_BookAdd = "addBook"; paramList params; params.add( (value)b.ToXML() ); _client.call(BookFullAddress(), Method_BookAdd, (const paramList)params, &result); return value_boolean(result); } bool BookstoreCaller::UpdateBook(BookType b) { if( !IsBookServerUp() ) return false; using namespace xmlrpc_c; xmlrpc_c::value result; string Method_BookUpdate = "updateBook"; paramList params; params.add( (value)b.ToXML() ); _client.call(BookFullAddress(), Method_BookUpdate, (const paramList)params, &result); return value_boolean(result); } bool BookstoreCaller::DeleteBook(std::string ISBN) { if( !IsBookServerUp() ) return false; using namespace xmlrpc_c; xmlrpc_c::value result; string Method_BookDelete = "deleteBook"; paramList params; params.add( (value)( (value_string)ISBN) ); _client.call(BookFullAddress(), Method_BookDelete, (const paramList)params, &result); return value_boolean(result); } bool BookstoreCaller::DeleteBook(BookType b) { DeleteBook( b.ISBN() ); } BookType BookstoreCaller::LookupBook(std::string ISBN) { BookType book; if( !IsBookServerUp() ) return book; using namespace xmlrpc_c; xmlrpc_c::value result; string Method_BookLookup = "searchBook"; paramList params; params.add( (value)( (value_string)ISBN) ); _client.call(BookFullAddress(), Method_BookLookup, (const paramList)params, &result); book.FromXML( (value_struct)result); return book; } const std::map<std::string, BookType> BookstoreCaller::BookList() { using namespace xmlrpc_c; xmlrpc_c::value result; string Method_BookList = "listBooks"; // no parameters with call _client.call(BookFullAddress(), Method_BookList, &result); value_array ARR = (value_array)result; // convert the value_array into a vector<value> using the method vectorValueValue // this is needed since the compiler won't be able to figure out what constructor to use.. // due to the complexity of the vector structure. vector<value> ret( ARR.vectorValueValue() ); // create a map of Members to return map<string, BookType> mappy; vector<value>::iterator iter, back; iter = ret.begin(); back = ret.end(); pair<string, BookType> v; for(; iter != back; iter++) { BookType b; value_struct structure = (value_struct) *iter; b.FromXML( structure ); // check if the id is not empty.. don't want to risk inserting an empty key if( b.ISBN() == "" ) continue; v = pair<string, BookType>(b.ISBN(), b); mappy.insert(v); } _books = mappy; return mappy; } // --- Members --- bool BookstoreCaller::AddMember(MemberType m) { if( !IsMemberServerUp() ) return false; using namespace xmlrpc_c; xmlrpc_c::value result; string Method_MemberAdd = "addMember"; paramList params; params.add( (value)m.ToXML() ); _client.call(MemberFullAddress(), Method_MemberAdd, (const paramList)params, &result); return value_boolean(result); } bool BookstoreCaller::UpdateMember(MemberType m) { if( !IsMemberServerUp() ) return false; using namespace xmlrpc_c; xmlrpc_c::value result; string Method_MemberUpdate = "updateMember"; paramList params; params.add( (value)m.ToXML() ); _client.call(MemberFullAddress(), Method_MemberUpdate, (const paramList)params, &result); return value_boolean(result); } bool BookstoreCaller::DeleteMember(std::string ID) { if( !IsMemberServerUp() ) return false; using namespace xmlrpc_c; xmlrpc_c::value result; string Method_MemberDelete = "deleteMember"; paramList params; params.add( (value)(value_string)ID ); _client.call(MemberFullAddress(), Method_MemberDelete, (const paramList)params, &result); return value_boolean(result); } bool BookstoreCaller::DeleteMember(MemberType m) { DeleteMember( m.ID() ); } MemberType BookstoreCaller::LookupMember(std::string ID) { MemberType member; if( !IsMemberServerUp() ) return member; using namespace xmlrpc_c; xmlrpc_c::value result; string Method_MemberLookup = "searchMember"; paramList params; params.add( (value)( (value_string)ID) ); _client.call(MemberFullAddress(), Method_MemberLookup, (const paramList)params, &result); member.FromXML( (value_struct)result); return member; } const std::map<std::string, MemberType> BookstoreCaller::MemberList() { using namespace xmlrpc_c; xmlrpc_c::value result; string Method_MemberList = "listMembers"; // no parameters with call _client.call(MemberFullAddress(), Method_MemberList, &result); value_array ARR = (value_array)result; // convert the value_array into a vector<value> using the method vectorValueValue // this is needed since the compiler won't be able to figure out what constructor to use.. // due to the complexity of the vector structure. vector<value> ret( ARR.vectorValueValue() ); // create a map of Members to return map<string, MemberType> mappy; vector<value>::iterator iter, back; iter = ret.begin(); back = ret.end(); pair<string, MemberType> v; for(; iter != back; iter++) { MemberType m; value_struct structure = (value_struct) *iter; m.FromXML( structure ); // check if the id is not empty.. don't want to risk inserting an empty key if( m.ID() == "" ) continue; v = pair<string, MemberType>(m.ID(), m); mappy.insert(v); } _members = mappy; return mappy; }