interface IO2GSessionStatus
Brief
The interface provides method signatures to process notifications about session status changes and login failures.
Details
The interface must be implemented by an application in order to process status changes of a session object.
When a user tries to login, the session status changes to CONNECTING.
If a login fails, the status changes to DISCONNECTED and the server
returns an error message explaining why the login has failed. If you want to capture what has gone wrong, use the
onLoginFailed method of the interface.
If a login is successful, the session status changes to CONNECTED.
If you want to capture and process every status change of the session object, use the
onSessionStatusChanged method of the interface.
For a complete list of the session statuses, see O2GSessionStatusCode.
For a detailed explanation of the session statuses, refer to the Session Statuses section.
If you want to use the methods of the IO2GSessionStatus
interface, you must create a class that implements the interface. For example,
public class SessionStatusListener implements IO2GSessionStatus { }
An instance of the class implementing the IO2GSessionStatus
interface must be subscribed to the session object before calling the
O2GSession.login method. It is accomplished by calling the
O2GSession
.subscribeSessionStatus
method. For example,
mSession = O2GTransport.createSession();
SessionStatusListener statusListener = new SessionStatusListener(mSession, mDBName, mPin);
mSession.subscribeSessionStatus(statusListener);
The subscribed instance must be unsubscribed from the session object after calling the
O2GSession.logout method, but before calling the
O2GSession.dispose method. It is accomplished by calling the
O2GSession
.unsubscribeSessionStatus
method.
For example,
mSession.logout();
mSession.unsubscribeSessionStatus(statusListener);
mSession.dispose();
For the interface implementation details, see the example below.
Example
Process notifications about session status changes and a login failure [show]
Process notifications about session status changes and a login failure [hide]
package getclosedtrades;
import com.fxcore2.IO2GSessionStatus;
import com.fxcore2.O2GSession;
import com.fxcore2.O2GSessionStatusCode;
import com.fxcore2.O2GSessionDescriptorCollection;
import com.fxcore2.O2GSessionDescriptor;
public class SessionStatusListener implements IO2GSessionStatus {
// Connection , session and status variables
private boolean mConnected = false;
private boolean mDisconnected = false;
private boolean mError = false;
private String mDBName = "";
private String mPin = "";
private O2GSession mSession = null;
private O2GSessionStatusCode mStatus = null;
// Constructor
public SessionStatusListener(O2GSession session, String dbName, String pin) {
mSession = session;
mDBName = dbName;
mPin = pin;
}
// Shows if session is connected
public final boolean isConnected() {
return mConnected;
}
// Shows if session is disconnected
public final boolean isDisconnected() {
return mDisconnected;
}
// Shows if there was an error during the logn process
public final boolean hasError() {
return mError;
}
// Returns current session status
public final O2GSessionStatusCode getStatus() {
return mStatus;
}
// Implementation of IO2GSessionStatus interface public method onSessionStatusChanged
public final void onSessionStatusChanged(O2GSessionStatusCode status) {
mStatus = status;
System.out.println("Status: " + mStatus.toString());
if (mStatus == O2GSessionStatusCode.CONNECTED) {
mConnected = true;
}
else {
mConnected = false;
}
if (status == O2GSessionStatusCode.DISCONNECTED) {
mDisconnected = true;
}
else {
mDisconnected = false;
}
if (mStatus == O2GSessionStatusCode.TRADING_SESSION_REQUESTED) {
O2GSessionDescriptorCollection descs = mSession.getTradingSessionDescriptors();
System.out.println("\nSession descriptors");
System.out.println("id, name, description, requires pin");
for (O2GSessionDescriptor desc : descs) {
System.out.println(desc.getId() + " " + desc.getName() + " " + desc.getDescription() + " " + desc.isPinRequired());
}
if (mDBName.equals("")) {
System.out.println("Argument for database is missing");
}
else {
mSession.setTradingSession(mDBName, mPin);
}
}
}
// Implementation of IO2GSessionStatus interface public method onLoginFailed
public final void onLoginFailed(String error) {
System.out.println("Login error: " + error);
mError = true;
}
}
The namespace is com.fxcore2
.
Public Methods |
onLoginFailed
|
Processes a notification about a login failure.
|
onSessionStatusChanged
|
Processes notifications about session status changes.
|
back