class IO2GSessionStatus
Brief
The interface provides method signatures to process notifications about session status changes and login failure.
Details
The interface must be implemented by the application in order to process the status change of the session object.
When the user tries to login, the session status changes to Connecting.
If login failed, the status changes to Disconnected and server
returns an error message explaining why login has failed. If you want to capture what went wrong, use
onLoginFailed method of the interface.
If login was successful, the session status changes to Connected.
If you want to capture and process every status change of the session object, use
onSessionStatusChanged method of the interface.
For complete list of session statuses, please see O2GSessionStatus.
For detailed explanation of session statuses, please refer to Session Statuses section.
If you want to use methods of IO2GSessionStatus
interface, you must create a class which implements the interface. For example,
class SessionStatusListener : public IO2GSessionStatus { };
An instance of the class implementing IO2GSessionStatus
interface must be subscribed to the session object before calling
IO2GSession::login method. It is accomplished by calling
IO2GSession
::subscribeSessionStatus
method. For example,
mSession = CO2GTransport::createSession();
SessionStatusListener *statusListener = new SessionStatusListener(mSession, mSubSessionID, mPin);
mSession->subscribeSessionStatus(statusListener);
An instance of the class implementing IO2GSessionStatus
interface must be unsubscribed from the session object after calling
IO2GSession::logout method. It is accomplished by calling
IO2GSession
::unsubscribeSessionStatus
method.
For example,
mSession->logout();
mSession->unsubscribeSessionStatus(statusListener);
For the interface implementation details, please see the example below:
Process notifications about session status changes and login failure [show]
Process notifications about session status changes and login failure [hide]
#include <ForexConnect.h>
#include <threadlib.h>
class SessionStatusListener : public IO2GSessionStatus
{
private:
// Connection, session and status variables
volatile bool mConnected;
volatile bool mDisconnected;
volatile bool mError;
std:string mSubSessionID;
std:string mPin;
IO2GSession *mSession;
IO2GSessionStatus::O2GSessionStatus mStatus;
// Reference counter
long mRefCount;
public:
// Constructor
SessionStatusListener(IO2GSession *session, const char *subSessionID, const char *pin)
{
mSession = session;
mSession->addRef();
if (mSubSessionID)
mSubSessionID = subSessionID;
else
mSubSessionID = "";
if (pin)
mPin = pin;
else
mPin = "";
mConnected = false;
mDisconnected = false;
mError = false;
mStatus = IO2GSessionStatus::Disconnected;
mRefCount = 1;
}
// Destructor
~SessionStatusListener()
{
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;
}
// Shows if session is connected
bool isConnected()
{
return mConnected;
}
// Shows if session is disconnected
bool isDisconnected()
{
return mDisconnected;
}
// Shows if there was an error during the logn process
bool hasError()
{
return mError;
}
// Returns current session status
IO2GSessionStatus::O2GSessionStatus getStatus()
{
return mStatus;
}
// Implementation of IO2GSessionStatus interface public method onSessionStatusChanged
void onSessionStatusChanged(O2GSessionStatus status)
{
mStatusCode = status;
switch (mStatusCode)
{
case IO2GSessionStatus::Disconnected:
std::cout << "Status::disconnected" << std::endl;
break;
case IO2GSessionStatus::Connecting:
std::cout << "Status::connecting" << std::endl;
break;
case IO2GSessionStatus::TradingSessionRequested:
std::cout << "Status::trading session requested" << std::endl;
break;
case IO2GSessionStatus::Connected:
std::cout << "Status::connected" << std::endl;
break;
case IO2GSessionStatus::Reconnecting:
std::cout << "Status::reconnecting" << std::endl;
break;
case IO2GSessionStatus::Disconnecting:
std::cout << "Status::disconnecting" << std::endl;
break;
case IO2GSessionStatus::SessionLost:
std::cout << "Status::session lost" << std::endl;
break;
}
if (mStatusCode == IO2GSessionStatus::TradingSessionRequested)
{
IO2GSessionDescriptorCollection *descriptors = mSession->getTradingSessionDescriptors();
bool found = false;
if (descriptors)
{
std::cout << "Available descriptors:" << std::endl;
for (int i = 0; i < descriptors->size(); i++)
{
IO2GSessionDescriptor *descriptor = descriptors->get(i);
std::cout << " id='" << descriptor->getID() << "' " <<
"name='" << descriptor->getName() << "' " <<
"description='" << descriptor->getDescription() << "' " <<
<< descriptor->requiresPin() ? "requires pin" : "" << std::endl;
if (mSubSessionID == descriptor->getID())
found = true;
descriptor->release();
}
descriptors->release();
}
if (!found)
onLoginFailed("The specified sub session identifier is not found");
else
mSession->setTradingSession(mSubSessionID.c_str(), mPin.c_str());
}
}
// Implementation of IO2GSessionStatus interface public method onLoginFailed
void onLoginFailed(const char *error)
{
std::cout << "Login error: " << error << std::endl;
mError = true;
}
};
Public Methods |
onLoginFailed
|
Processes notification about the login failure.
|
onSessionStatusChanged
|
Processes notifications about the session status change.
|
back