The article explains how to log in.
Implement the IO2GSessionStatus
interface in a status listener class:
public class MySessionStatusListener : IO2GSessionStatus{ ... }
Create a session:
O2GSession mSession = 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:
mSession.subscribeSessionStatus(statusListener);
Log in using the user ID, password, URL, and connection:
mSession.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
TradingSessionRequested
in the
onSessionStatusChanged
function of your status listener.
In this case get O2GSessionDescriptorCollection
:
O2GSessionDescriptorCollection descs = mSession.getTradingSessionDescriptors();
Then process elements of this collection:
foreach (O2GSessionDescriptor desc in 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:
mSession.logout();
Use the status listener onSessionStatusChanged
function to capture the Disconnected
event:
Unsubscribe the session from the status listener:
mSession.unsubscribeSessionStatus(statusListener);
Dispose the session object:
mSession.Dispose();
Login and logout example: [hide]
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
using fxcore2;
namespace Login
{
class Program
{
private static O2GSession mSession;
public static O2GSession Session
{
get { return mSession; }
}
private static string sSessionID = "";
public static string SessionID
{
get { return sSessionID; }
}
private static string sPin = "";
public static string Pin
{
get {return sPin;}
}
static void Main(string[] args)
{
if (args.Length < 4)
{
Console.WriteLine("Not Enough Parameters!");
Console.WriteLine("USAGE: [application].exe [user ID] [password] [URL] [connection] [session ID (if needed)] [pin (if needed)]");
Console.WriteLine("\nPress enter to close the program");
Console.ReadLine();
return;
}
string sUserID = args[0];
string sPassword = args[1];
string sURL = args[2];
string sConnection = args[3];
if (args.Length > 4)
sSessionID = args[4];
if (args.Length > 5)
sPin = args[5];
try
{
mSession = O2GTransport.createSession();
SessionStatusListener statusListener = new SessionStatusListener(mSession);
mSession.subscribeSessionStatus(statusListener);
statusListener.login(sUserID, sPassword, sURL, sConnection);
if (statusListener.Connected)
{
//...
if (!statusListener.Error)
{
session.logout();
while (!statusListener.Disconnected)
Thread.Sleep(50);
}
}
mSession.unsubscribeSessionStatus(statusListener);
Console.WriteLine("Done! Press enter to exit");
Console.ReadLine();
mSession.Dispose();
}
catch (Exception e)
{
Console.WriteLine("Exception: {0}", e.ToString());
}
}
}
public class SessionStatusListener : IO2GSessionStatus
{
private bool mConnected = false;
private bool mDisconnected = false;
private bool mError = false;
private O2GSession mSession;
private object mEvent = new object();
public SessionStatusListener(O2GSession session)
{
mSession = session;
}
public bool Connected
{
get
{
return mConnected;
}
}
public bool Disconnected
{
get
{
return mDisconnected;
}
}
public bool Error
{
get
{
return mError;
}
}
public void onSessionStatusChanged(O2GSessionStatusCode status)
{
Console.WriteLine("Status: " + status.ToString());
if (status == O2GSessionStatusCode.Connected)
mConnected = true;
else
mConnected = false;
if (status == O2GSessionStatusCode.Disconnected)
mDisconnected = true;
else
mDisconnected = false;
if (status == O2GSessionStatusCode.TradingSessionRequested)
{
if (Program.SessionID == "")
Console.WriteLine("Argument for trading session ID is missing");
else
mSession.setTradingSession(Program.SessionID, Program.Pin);
}
else if (status == O2GSessionStatusCode.Connected)
{
lock (mEvent)
Monitor.PulseAll(mEvent);
}
}
public void onLoginFailed(string error)
{
Console.WriteLine("Login error: " + error);
mError = true;
lock (mEvent)
Monitor.PulseAll(mEvent);
}
public void login(string sUserID, string sPassword, string sURL, string sConnection)
{
Program.Session.login(sUserID, sPassword, sURL, sConnection);
lock (mEvent)
Monitor.Wait(mEvent, 60000);
}
}
}