public method IO2GResponseListener.onRequestCompleted
Brief
Processes a notification about the successful request completion.
Declaration |
C++ |
virtual void |
onRequestCompleted (const char *requestId, IO2GResponse *response = NULL) = 0 |
|
Parameters |
requestId |
The identifier of the successfully completed request. Session object may send multiple requests
(see theIO2GSession.sendRequest method).
The method receives notifications of all the requests that have been successfully completed. To make sure you are getting notification
about a particular request, you must check if the value of this parameter matches the value obtained by calling
the IO2GRequest .getRequestID method.
For details, please see the example below. |
response |
A response object. The type of the response depends on the type of the request. For example, if you request information about open
positions, the type of the response will be O2GResponseType.GetTrades. For complete list of response types,
see O2GResponseType . The response type defines a reader that can parse the response.
For example, if you request information about open positions, you must use the
createTradesTableReader method to create the
IO2GTradesTableResponseReader object.
For a complete list of the methods that create appropriate readers, see the IO2GResponseReaderFactory object. |
Details
In order to process a notification about the successful request completion, an instance of the class implementing the IO2GResponseListener interface
must be subscribed to a session object.
It is accomplished by calling the IO2GSession
.subscribeResponse
method.
Note: For the order creation request, successful completion means that an order has been validated by the trading server and accepted for execution.
It does not mean that the order has been actually inserted in the Orders table.
The order can be rejected if it has been successfully inserted in the Orders table. In case the order is deleted, the order status is \'R\'. The rejected amount is contained in the order amount
field. The rejection message is inserted to the Messages table. The rejection message has the "Market Conditions" type(feature).
The message text contains the OrderID. The order delete and the reject message notification can be received randomly.
To make sure that the order is inserted to or deleted from the Orders table or the message is inserted to the Messages table,
use the following methods:
For details of order creation, see the How to Open Position article.
Example
Proccess a notification about a request to get offers [show]
Proccess a notification about a request to get offers [hide]
#include <ForexConnect.h>
#include <threadlib.h>
class ResponseListener : public IO2GResponseListener
{
private:
// Session and request variables
IO2GSession *mSession;
std::string mRequestID;
volatile bool mRequestComplete;
volatile bool mRequestFailed;
// Reference counter
volatile long mRefCount;
public:
// Constructor
ResponseListener(IO2GSession *session)
{
mRefCount = 1;
mSession = session;
mSession->addRef();
mRequestID = "";
mRequestComplete = false;
mRequestFailed = false;
}
// Destructor
~ResponseListener()
{
mSession->release();
}
// Increase reference counter
long addRef()
{
// for non-Windows OS InterlockedIncrement is platform specific macro defined in threadlib.h
return InterlockedIncrement(&mRefCount);
}
// Decrease reference counter
long release()
{
// for non-Windows OS InterlockedDecrement is platform specific macro defined in threadlib.h
long rc = InterlockedDecrement(&mRefCount);
if (rc == 0)
delete this;
return rc;
}
//Set request
void setRequest(const char *requestID)
{
mRequestID = requestID;
mRequestComplete = false;
mRequestFailed = false;
}
// Implementation of IO2GResponseListener interface public method onRequestCompleted
void onRequestCompleted(const char *requestID, IO2GResponse *response)
{
if (mRequestID == requestID)
{
O2G2Ptr<IO2GResponseReaderFactory> readerFactory = mSession->getResponseReaderFactory();
if (readerFactory)
{
O2G2Ptr<IO2GOffersTableResponseReader> reader = readerFactory->createOffersTableReader(response);
for (int i = 0; i < reader->size(); i++)
{
O2G2Ptr<IO2GOfferRow> offer = reader->getRow(i);
std::cout << " This is a response to your request: \n Instrument = " << offer->getInstrument() <<
" Bid = " << offer->getBid() <<
" Ask = " << offer->getAsk() << std::endl;
}
mRequestComplete = true;
}
}
}
// Implementation of IO2GResponseListener interface public method onRequestFailed
void onRequestFailed(const char *requestID, const char *error)
{
if (mRequestID == requestID)
{
std::cout << "Request failed. requestID= " << requestID << "; error= " << error << std::endl;
mRequestFailed = true;
}
}
// Implementation of IO2GResponseListener interface public method onTablesUpdates
public void onTablesUpdates(IO2GResponse *response)
{
O2G2Ptr<IO2GResponseReaderFactory> factory = mSession->getResponseReaderFactory();
if (factory)
{
O2G2Ptr<IO2GTablesUpdatesReader> updatesReader = factory->createTablesUpdatesReader(response);
for (int i = 0; i < updatesReader->size(); i++)
{
if (updatesReader->getUpdateTable(i) == Offers)
{
O2G2Ptr<IO2GOfferRow> offer = updatesReader->getOfferRow(i);
std::cout << " This is a live update: \n Instrument = " << offer->getInstrument() <<
" Bid = " << offer->getBid() <<
" Ask = " << offer->getAsk() << std::endl;
}
}
}
}
};
Declared in IO2GResponseListener
back