Hello World! in FxConnect API

Moderator: Moderator

Hello World! in FxConnect API

Postby kedulo » Tue Oct 11, 2011 6:52 pm

I'm trying to get into FxConnect Java API and based on the help file made what I could call a "Hello World!" equivalent:
Code: Select all
package forexconnect;
import com.fxcore2.*;
public class Login {
    public static class mySessionStatusListener implements IO2GSessionStatus {
        O2GSession session;
        public mySessionStatusListener(O2GSession session) {
            System.out.println("creating mySessionStatusListener("+session.toString()+")");
            this.session = session;
        }
        @Override
        public void onSessionStatusChanged(O2GSessionStatusCode ogssc) {
            System.out.println("status:"+ogssc.toString());
            if(ogssc.equals(O2GSessionStatusCode.CONNECTED)) {
                session.logout();
            }
            if(ogssc.equals(O2GSessionStatusCode.DISCONNECTED)) {
                Runtime.getRuntime().exit(0);
            }
        }
        @Override
        public void onLoginFailed(String string) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }
    public static class ResponseListener implements IO2GResponseListener {
        @Override
        public void onRequestCompleted(String string, O2GResponse ogr) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
        @Override
        public void onRequestFailed(String string, String string1) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
        @Override
        public void onTablesUpdates(O2GResponse ogr) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }
    public static void main(String[] args) {
        O2GSession session = O2GTransport.createSession();
        mySessionStatusListener statusListener = new mySessionStatusListener(session);
        session.subscribeSessionStatus(statusListener);
        ResponseListener responseListener = new ResponseListener();
        session.subscribeResponse(responseListener);
        session.login("[color=#FF0000]userID[/color]", "[color=#FF0000]password[/color]", "http://www.fxcorporate.com/Hosts.jsp", "Demo");
        System.out.println("loging in");
        try { //I've put this 10 min delay here just to keep the app from exiting before it had a...
            Thread.sleep(600000); //...chance to log in and out.
        } catch (InterruptedException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

Running this, the output should be:
Code: Select all
run:
creating mySessionStatusListener(com.fxcore2.O2GSession@1ccb029)
loging in
status:CONNECTING
status:CONNECTED
status:DISCONNECTING
status:DISCONNECTED
BUILD SUCCESSFUL (total time: 4 seconds)

The purpose is simply to test/demonstrate the ability to log in and out (saying "Hello" to the Fx server).
It's a bit clumsy so I am open to suggestions before I'll start it as a step-by-step article on the wiki. Those were ever so helpfull when I was learning scripting fx in lua. The API SDK doesn't seem to have any good example code.
Also if anybody can test the code to see if it's reliable.
kedulo
 
Posts: 4
Joined: Fri Jul 08, 2011 4:58 am

Re: Hello World! in FxConnect API

Postby sunshine » Thu Oct 13, 2011 10:42 am

You can find the examples in the Samples folder in the folder with ForexConnect. By default, the path is "C:\Program Files\Candleworks\ForexConnectAPI\samples".
The set of samples include the login sample which is similar your "Hello World!".
sunshine
 

Re: Hello World! in FxConnect API

Postby kedulo » Sun Oct 23, 2011 6:04 pm

Looks great. Thanks.
kedulo
 
Posts: 4
Joined: Fri Jul 08, 2011 4:58 am


Return to ForexConnect API

Who is online

Users browsing this forum: No registered users and 8 guests