The article explains how to log in.
Implement the IO2GSessionStatus
interface in a status listener class:
public class MySessionStatusListener implements IO2GSessionStatus { ... }
Create a session:
O2GSession session = O2GTransport.createSession();
Create an object of a status listener class:
MySessionStatusListener statusListener = new MySessionStatusListener(...);
Subscribe the status listener object to the session status. It is important to subscribe before the login:
session.subscribeSessionStatus(statusListener);
Log in using the user ID, password, URL, and connection:
session.login("userID", "password", "http://www.fxcorporate.com/Hosts.jsp", "Demo");
If you have more than one trading sessions or a pin is required for login, you have to catch the event
TRADING_SESSION_REQUESTED
in the
onSessionStatusChanged
function of your status listener.
In this case get O2GSessionDescriptorCollection
:
O2GSessionDescriptorCollection descs = mSession.getTradingSessionDescriptors();
Then process elements of this collection:
for (O2GSessionDescriptor desc : descs){...}
Finally set a trading session using the session Id and pin:
mSession.setTradingSession("sessionID", "pin");
Use the status listener onSessionStatusChanged
function to capture the CONNECTED
event:
Log out when you are done:
session.logout();
Use the status listener onSessionStatusChanged
function to capture the DISCONNECTED
event:
Unsubscribe the session from the status listener:
session.unsubscribeSessionStatus(statusListener);
Dispose the session object:
session.dispose();
Login and Logout example [hide]
package login;
import com.fxcore2.O2GSession;
import com.fxcore2.O2GTransport;
import com.fxcore2.IO2GSessionStatus;
import com.fxcore2.O2GSessionStatusCode;
import com.fxcore2.O2GSessionDescriptorCollection;
import com.fxcore2.O2GSessionDescriptor;
public class Main {
public static void main(String[] args) {
// Connection and session variables
String mUserID = "";
String mPassword = "";
String mURL = "";
String mConnection = "";
String mDBName = "";
String mPin = "";
O2GSession mSession = null;
// Check for correct number of arguments
if (args.length < 4) {
System.out.println("Not Enough Parameters!");
System.out.println("USAGE: [user ID] [password] [URL] [connection] [session ID (if needed)] [pin (if needed)]");
System.exit(1);
}
// Get command line arguments
mUserID = args[0];
mPassword = args[1];
mURL = args[2];
mConnection = args[3];
if (args.length > 4) {
mDBName = args[4];
}
if (args.length > 5) {
mPin = args[5];
}
// Create a session, subscribe to session listener, login, get accounts, logout
try {
mSession = O2GTransport.createSession();
SessionStatusListener statusListener = new SessionStatusListener(mSession, mDBName, mPin);
mSession.subscribeSessionStatus(statusListener);
mSession.login(mUserID, mPassword, mURL, mConnection);
while (!statusListener.isConnected() && !statusListener.hasError()) {
Thread.sleep(50);
}
if (!statusListener.hasError()) {
//...
mSession.logout();
while (!statusListener.isDisconnected()) {
Thread.sleep(50);
}
}
mSession.unsubscribeSessionStatus(statusListener);
mSession.dispose();
} catch (Exception e) {
System.out.println ("Exception: " + e.getMessage());
System.exit(1);
}
}
}
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 trading session ID 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;
}
}