std::string ForexConnectWrapper::getOfferID(std::string instrument) {
        IO2GTableManager *tableManager = getLoadedTableManager();
        IO2GOffersTable *offersTable = (IO2GOffersTable *)tableManager->getTable(::Offers);
        tableManager->release();

        IO2GOfferTableRow *offerRow = NULL;
        IO2GTableIterator tableIterator;

        std::string sOfferID;

        while (offersTable->getNextRow(tableIterator, offerRow)) {
            if (instrument == offerRow->getInstrument()) {
                break;
            }
        }
        offersTable->release();

        if (offerRow) {
            sOfferID = offerRow->getOfferID();
            offerRow->release();
        } else {
            std::stringstream ss;
            ss << "Could not find offer row for instrument " << instrument;
            log(ss.str());
            throw ss.str().c_str();
        }

        return sOfferID;
}
void TableListener::printOffers(IO2GOffersTable *offersTable, const char *sInstrument)
{
	IO2GOfferTableRow *offerRow = NULL;
	IO2GTableIterator iterator;
	while (offersTable->getNextRow(iterator, offerRow))
	{
		printOffer(offerRow, sInstrument);
		offerRow->release();
	}
}
std::map<std::string, std::string> ForexConnectClient::getOffers()
{
    std::map<std::string, std::string> offers;
    TableHandler<Offers, IO2GOffersTable, IO2GOfferTableRow> handler(mpSession);
    while (handler.getNextRow())
    {
	IO2GOfferTableRow *offerRow = handler.getRow();
        offers[offerRow->getInstrument()] = offerRow->getOfferID();
    }
    return offers;
}
double ForexConnectClient::getAsk(const std::string& instrument) {
    TableHandler<Offers, IO2GOffersTable, IO2GOfferTableRow> handler(mpSession);
    while (handler.getNextRow())
    {
	IO2GOfferTableRow* offerRow = handler.getRow();
        if (offerRow->getInstrument() == instrument)
	{
	    return offerRow->getAsk();
	}
    }
    throw std::runtime_error("Could not get offer table row.");
}
std::string ForexConnectWrapper::getOffersHashAsYAML() {
    std::string rv;
    IO2GTableManager *tableManager = getLoadedTableManager();
    IO2GOffersTable *offersTable = (IO2GOffersTable *)tableManager->getTable(::Offers);
    tableManager->release();

    IO2GOfferTableRow *offerRow = NULL;
    IO2GTableIterator tableIterator;

    while (offersTable->getNextRow(tableIterator, offerRow)) {
        rv.append(offerRow->getInstrument()).append(": ").append(offerRow->getSubscriptionStatus()).append("\n");
        //std::cout << offerRow->getInstrument() << "\t" << offerRow->getSubscriptionStatus()[0] << std::endl;
        if (offerRow)
            offerRow->release();
    }
    offersTable->release();
    return rv;
}
void ForexConnectWrapper::setSubscriptionStatus(std::string instrument, std::string status) {
    IO2GTableManager *tableManager = getLoadedTableManager();
    IO2GOffersTable *offersTable = (IO2GOffersTable *)tableManager->getTable(::Offers);
    tableManager->release();

    IO2GOfferTableRow *offerRow = NULL;
    IO2GTableIterator tableIterator;
    bool instrumentFound = false;

    while (offersTable->getNextRow(tableIterator, offerRow)) {
        if ( instrument == offerRow->getInstrument() ) {
            instrumentFound = true;
            IO2GRequestFactory *factory = session->getRequestFactory();

            IO2GValueMap *valueMap = factory->createValueMap();
            valueMap->setString(::Command, O2G2::Commands::SetSubscriptionStatus);
            valueMap->setString(::SubscriptionStatus, status.c_str());
            valueMap->setString(::OfferID, offerRow->getOfferID());

            IO2GRequest *request = factory->createOrderRequest(valueMap);
            valueMap->release();

            Listener *ll = new Listener(session);
            IO2GResponse *response = ll->sendRequest(request);
            response->release();
            request->release();
            ll->release();
            factory->release();

            break;
        }
    }
    offerRow->release();
    offersTable->release();

    if (!instrumentFound) {
        std::stringstream ss;
        ss << "Could not find offer row for instrument " << instrument;
        log(ss.str());
        throw ss.str().c_str();
    }
}