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

booktype.cpp 4.9KB

    #include "BookType.h" #include <iostream> using namespace std; BookType::BookType() { // init the items ISBN(""); Title(""); Price(0.0); Stock(0); Publisher(""); PublicationYear(1337); _authors.clear(); } /// XMLRPC-C Methods const xmlrpc_c::value_struct BookType::ToXML() { using namespace xmlrpc_c; // Every 'value' is actually an 'xmlrpc_c::value' due to namespace! map<string, value> mid; // add each private data member pair<string, value> isbn = pair<string, value>("isbn", value_string( ISBN() )); mid.insert(isbn); pair<string, value> title = pair<string, value>("title", value_string( Title() )); mid.insert(title); pair<string, value> stock = pair<string, value>("stock", value_int( Stock() )); mid.insert(stock); pair<string, value> price = pair<string, value>("price", value_double( Price() )); mid.insert(price); pair<string, value> publisher = pair<string, value>("publisher", value_string( Publisher() )); mid.insert(publisher); pair<string, value> pubYear = pair<string, value>("publication_year", value_int( PublicationYear() )); mid.insert(pubYear); pair<string, value> numAuth = pair<string, value>("number_of_authors", value_int( AuthorsCount() )); mid.insert(numAuth); // add the authors vector<value> auth; vector<std::string>::iterator iter, back; iter = _authors.begin(); back = _authors.end(); for(; iter != back; iter++) { auth.push_back( value_string( *iter ) ); } pair<string, value> authors = pair<string, value>("authors", value_array( auth )); mid.insert(authors); value_struct structure(mid); // return the constructed map that has been converted to a struct return structure; } void BookType::FromXML(xmlrpc_c::value_struct const& params) { // prep this object to be filled ISBN(""); Title(""); Price(0.0); Stock(0); Publisher(""); PublicationYear(1337); _authors.clear(); using namespace xmlrpc_c; // get the values of the parameters in the list // assign them to the private data members // try ... block, to catch exceptions for XMLRPC-C try { map<string, value> mappy(params); // ISBN value isbn = mappy["isbn"]; ISBN( (value_string)isbn ); // Title value title = mappy["title"]; Title( (value_string)title ); // Price value price = mappy["price"]; Price( (value_double)price ); // Stock value stock = mappy["stock"]; Stock( (value_int)stock ); // Publisher value publisher = mappy["publisher"]; Publisher( (value_string)publisher ); // Published Year value pubYear = mappy["publication_year"]; PublicationYear( (value_int)pubYear ); // Number of Authors value numAuth = mappy["number_of_authors"]; int authorCount = (value_int)numAuth; // Authors value authorsRAW = mappy["authors"]; value_array AUTHORS_ARRAY = (value_array)authorsRAW; vector<value> authors( AUTHORS_ARRAY.vectorValueValue() ); vector<value>::iterator iter, back; iter = authors.begin(); back = authors.end(); for(; iter != back; iter++) { AddAuthor( (value_string)*iter ); } } // Catch the standard error, and don't handle it. Just return and call it good. // This is a work-around for a Book Server (in Python, in theory) returning an invalid structure. catch(xmlrpc_c::fault) { } } /// Private Data Member Methods std::string BookType::ISBN() { return _isbn; } void BookType::ISBN(std::string isbn) { _isbn = isbn; } void BookType::AddAuthor(std::string author) { // Is the maximum number of authors reached? if( AuthorsCount() == BOOKTYPE_MAX_AUTHORS) { return; } // if the authors list is not empty, search it before adding the new author // no duplicates! if( AuthorsCount() != 0 ) { vector<std::string>::iterator iter, back; iter = _authors.begin(); back = _authors.end(); for(; iter != back; iter++) { // if an hour in the list is the same.. don't add it if( *iter == author ) return; } } // Add the author, as it is now unique _authors.push_back(author); } std::string BookType::Authors(int index) { // return the author if it exists, or return an empty string if(index > 0 && index <= this->AuthorsCount() && index <= BOOKTYPE_MAX_AUTHORS) return _authors[index-1]; else return ""; } const std::vector<std::string> BookType::Authors() { return _authors; } bool BookType::ClearAuthors() { // clear all the values in the internal list _authors.clear(); } int BookType::AuthorsCount() { return _authors.size(); } std::string BookType::Title() { return _title; } void BookType::Title(std::string title) { _title = title; } std::string BookType::Publisher() { return _publisher; } void BookType::Publisher(std::string publisher) { _publisher = publisher; } int BookType::PublicationYear() { return _publishYear; } void BookType::PublicationYear(int publishYear) { _publishYear = publishYear; } float BookType::Price() { return _price; } void BookType::Price(float price) { _price = price; } int BookType::Stock() { return _stock; } void BookType::Stock(int stock) { _stock = stock; }