#include <iostream>
#include <assert.h>
#include <time.h>

#include "../include/TestValues.h"
#include "../include/Mole.h"

#include "../Request.hpp"
#include "../include/XmlRpc.h"
#include "../Server.hpp"
#include "../XmlRpcppClient.hpp"
#include "../ServiceManager.hpp"
#include "../responses/HomepageResponse.hpp"
#include "../services/HomepageService.hpp"
#include "../requests/HomepageRequest.hpp"
#include "../requests/xmlrpcpp/HomepageRequest.hpp"


using namespace XmlRpc;
using namespace mole;


/* Test du service de recuperation de flux xml */
bool
testGetHomepage(ctx::Client * rpcClient, std::string ip, int port){
    serv::Service * service = rpcClient->getService(std::string(NAME_SERV_HOMEPAGE));
    if (service){
	ctx::HomepageRequest * request = static_cast <ctx::HomepageRequest *> (service->getRequest());
	if (rpcClient->execute(service)){
	    serv::HomepageResponse * resp = static_cast <serv::HomepageResponse *> (service->getResponse());
	    std::cout << resp->getContent().size() + 1;
	    if (resp && (resp->errorCode() == 0)){
		std::cout << resp->getContent() << "\n";
	    }
	}
	delete service;
    }
    return false;
}

/**
 * Instances a xmlrpc client and connects to a server,
 * and then try to invoque some services onto it.
 */
int
main(int argc, char ** argv){
    if (argc != 3){
	std::cerr << "Usage : TestClient ip port\nExiting...\n";
	return EXIT_FAILURE;  
    }

    /* Creates a RPC Client */
    ctx::xmlrpcpp::XmlRpcppClient * rpcClient = new ctx::xmlrpcpp::XmlRpcppClient(argv[1], atoi(argv[2]), "");

    /* First of all, have to registers all services purposed
     * by the server binded to. ServiceManager does that for us. */
    serv::ServiceManager * manager = new serv::ServiceManager(rpcClient);
    manager->loadServices();

    testGetHomepage(rpcClient, argv[1], atoi(argv[2]));

    delete rpcClient;
    return EXIT_SUCCESS;
}
