Example 1

The first example is the simplest. It show how to create one single connection to a FastTrack MetaMarket service and one single subscription to a given EntityClass. All received entities on this subscriptions are written on standard output and then the application terminates.

The application is very simple and small: it does not handle any errors and/or exceptions and it does not make any trace.

/*

Description
===========
  This application example open a connection with a MetaMarket service
  of a given FastTrack server and then it starts a subscription
  on FT_C_TRADING_STATE EntityClass.
  Every received Entity is written on standard output.
  The application terminates when a SubscriptionIdleEvent is received.


Example Usage
=============

  To compile this example remember to put in the classpath:
     - The path of JDK 1.1.x (or following)
     - The path of your library JFTApi.jar 
     - The path of the directory where the metamarket package reside

  To launch this example type:
     java Example1

  To obtain something like:
     eID: HDAT mID: GR sID: Primary p: NOP s: Active pd: NOP t: 73049379
     eID: HDAT mID: GR sID: Repos p: NEG s: Active pd: NEG t: 80000057
     eID: HDAT mID: GR sID: Retail p: NEG s: Active pd: NEG t: 73049404
     eID: HDAT mID: GR sID: Secondary p: NEG s: Active pd: NEG t: 80000074
     eID: GAM mID: MOT sID: 1 p: NEG s: Active pd:  t: 0
     eID: GAM mID: TON sID: 1 p: NEG s: Active pd:  t: 0
     eID: GAM mID: MOT sID: 2 p: NEG s: Active pd:  t: 0
     eID: GAM mID: TON sID: 2 p: NEG s: Active pd:  t: 0
     eID: GAM mID: MOT sID: 3 p: NEG s: Active pd:  t: 0
     eID: GAM mID: TON sID: 3 p: NEG s: Active pd:  t: 0
     eID: GAM mID: MOT sID: 4 p: NEG s: Active pd:  t: 0
     eID: GAM mID: TON sID: 4 p: NEG s: Active pd:  t: 0

Additional Classes (in metamarket package)
==========================================

  In order to profitably use this example there is need for some additional
  Java classes in the metamarket package. These classes are:
    - MetaMarket         It contains global constants (entityClassIDs, keyIDs, etc...)
                         for all data structures of FastTrack MetaMarket service.
    - FT_C_TRADING_STATE Specific EntityClass for the handled trading states.
                         You can see below a skeleton of this class.
    - FT_C_TRADING_PHASE The various enumeration values for fields of FT_C_TRADING_STATE

*/
/*

MetaMarket
==========
  MetaMarket is the Java class that contains global constants
  for all data structures of FastTrack MetaMarket service.

  This class contains
    - a lot of constants (for all entytClassIDs, keyIDs, etc...)
      that may be used to access all data handled by MetaMarket service.
    - a method registerAll() that may be used to register all
      the EntityClasses of MetaMarket.

  In this example we use only 3 EntityClasses:
  FT_C_TRADING_STATE, FT_C_ORDER and FT_C_ERROR_INFO
  so the only used members of the class MetaMarket are:
        
  public static final int FT_C_TRADING_STATE_ID = 30010; // FT_C_TRADING_STATE id
  public static final int FT_C_TRADING_STATEKey = 1;     // FT_C_TRADING_STATE prim. key
  public static void registerAll();       // to register all EntityClasses of MetaMarket


The FT_C_TRADING_STATE EntityClass
==================================
  Its entityClassID is FT_C_TRADING_STATE_ID = 30010.
  Its primary keyID is FT_C_TRADING_STATEKey = 1
      and it includes ExchangeID, MarketID and SectionID fields.
  Its structure is something like:

class FT_C_TRADING_STATE {
   String ExchangeID; // ID of the market place
   String MarketID;   // ID of the market
   String SectionID;  // ID of the section
   int    Phase;      // Phase of the security:
                      // 0 or FT_C_TRADING_PHASE.FT_C_TRADING_PHASE_UNDEF
                      // 1 or FT_C_TRADING_PHASE.FT_C_TRADING_PHASE_CLOSURE
                      // 2 or FT_C_TRADING_PHASE.FT_C_TRADING_PHASE_PRE_ISSUE
                      // 3 or FT_C_TRADING_PHASE.FT_C_TRADING_PHASE_ISSUE
                      // 4 or FT_C_TRADING_PHASE.FT_C_TRADING_PHASE_PRE_AUCTION
                      // 5 or FT_C_TRADING_PHASE.FT_C_TRADING_PHASE_AUCTION
                      // 6 or FT_C_TRADING_PHASE.FT_C_TRADING_PHASE_POST_AUCTION
                      // 7 or FT_C_TRADING_PHASE.FT_C_TRADING_PHASE_PRE_TRADING
                      // 8 or FT_C_TRADING_PHASE.FT_C_TRADING_PHASE_TRADING
                      // 9 or FT_C_TRADING_PHASE.FT_C_TRADING_PHASE_POST_TRADING
                      //10 or FT_C_TRADING_PHASE.FT_C_TRADING_PHASE_TRADING_AT_LAST
                      //11 or FT_C_TRADING_PHASE.FT_C_TRADING_PHASE_TRADING_AFTER_HOUR
                      //12 or FT_C_TRADING_PHASE.FT_C_TRADING_PHASE_FAST_MARKET
                      //13 or FT_C_TRADING_PHASE.FT_C_TRADING_PHASE_MANAGEMENT
                      //14 or FT_C_TRADING_PHASE.FT_C_TRADING_PHASE_NO_OPERATION
   int    Status;     // Status of the section
                      // 0 or FT_C_TRADING_STATUS.FT_C_TRADING_STATUS_Active
                      // 1 or FT_C_TRADING_STATUS.FT_C_TRADING_STATUS_Suspended
                      // 2 or FT_C_TRADING_STATUS.FT_C_TRADING_STATUS_Frozen
   String PhaseDescription; // Description of phase
   int    Time;       // Time (format: HHMMSSmmm) of last change
}

*/
/* 

The FT_C_TRADING_PHASE class
============================
  This java class is not an EntityClass.
  This class contains
    - all enumeration value for the trading phase,
    - a method enumAsString that returns a displayable String for a given value.
  Its structure is something like:

class FT_C_TRADING_PHASE {
  public static final int FT_C_TRADING_PHASE_UNDEF              =  0;
  public static final int FT_C_TRADING_PHASE_CLOSURE            =  1;
  public static final int FT_C_TRADING_PHASE_PRE_ISSUE          =  2;
  public static final int FT_C_TRADING_PHASE_ISSUE              =  3;
  public static final int FT_C_TRADING_PHASE_PRE_AUCTION        =  4;
  public static final int FT_C_TRADING_PHASE_AUCTION            =  5;
  public static final int FT_C_TRADING_PHASE_POST_AUCTION       =  6;
  public static final int FT_C_TRADING_PHASE_PRE_TRADING        =  7;
  public static final int FT_C_TRADING_PHASE_TRADING            =  8;
  public static final int FT_C_TRADING_PHASE_POST_TRADING       =  9;
  public static final int FT_C_TRADING_PHASE_TRADING_AT_LAST    = 10;
  public static final int FT_C_TRADING_PHASE_TRADING_AFTER_HOUR = 11;
  public static final int FT_C_TRADING_PHASE_FAST_MARKET        = 12;
  public static final int FT_C_TRADING_PHASE_MANAGEMENT         = 13;
  public static final int FT_C_TRADING_PHASE_NO_OPERATION       = 14;

  public static final String enumAsString(int value) { return "an appropriate value"; }
}

*/

// Effective source-code Example1 starts here.

import it.list.jft.*;       // to use the JFT/Api library
import it.list.jft.event.*; // to use the JFT/Api library
import metamarket.*;        // to use MetaMarket, FT_C_TRADING_STATE, etc...

abstract class Example1 {
  static final int        clientID       = 67899;
  static final String     dirPath        = ".";
  static final String     myOperatorID   = "dario";
  static final String     myOperatorPass = "*";
  static final String     host           = "194.91.195.234";
  static final int        port           = 41005;
  static       Context    context;

  public static void main(String[]args) {
    JFT.THIS.init(JFT.MODE_MULTI_THREAD);
    if(true)
      JFT.THIS.register(new FT_C_TRADING_STATE());
    else // as an expensive alternative
      MetaMarket.registerAll();
    JFT.THIS.start();
    context = JFT.THIS.makeContext();
    new ConnectionEx();
  }
}
// Class to handle the connection.

class ConnectionEx implements ConnectionListener {

  final Connection connection;

  ConnectionEx() {
    ConnectionParam cp = Example1.context.makeConnectionParam();
    cp.setHost(Example1.host);
    cp.setPort(Example1.port);
    cp.setApplRevision(new int[]{0,0,0});
    cp.setApplSignature(12345);
    cp.setClientID(Example1.clientID);
    cp.setConnType(ConnectionParam.CONN_TYPE_TCP);
    cp.setUserName(Example1.myOperatorID);
    cp.setPassword(Example1.myOperatorPass);
    cp.setUserType(ConnectionParam.USER_TYPE_VIEW);
    connection = Example1.context.makeConnection(cp, this);
    if(connection.open() != Connection.RESULT_OK)
      connection.release(); // good practice
  }

  public void onConnectionOpen(ConnectionOpenEvent ev) {
    if(ev.getResult() == ev.RESULT_OK)
      new SubscriptionEx(connection);
    else
      connection.release(); // good practice
  }

  public void onConnectionClose(ConnectionCloseEvent ev) {
    connection.release(); // good practice
  }

  public void onConnectionLost(ConnectionLostEvent ev) {
    connection.release(); // good practice
  }
}
// Class to handle the subscription.

class SubscriptionEx implements SubscriptionListener {

  final Subscription subscription;
  final Connection   connection;

  SubscriptionEx(Connection conn) {
    connection = conn;
    SubscriptionParam sp = Example1.context.makeSubscriptionParam();
    sp.setEntityClassID(MetaMarket.FT_C_TRADING_STATE_ID);
    subscription = Example1.context.makeSubscription(connection, sp, this);
    if(subscription.start() != Subscription.RESULT_OK) 
      subscription.release(); // good practice
  }

  public void onSubscriptionStart(SubscriptionStartEvent ev){
    if(ev.getResult() != ev.RESULT_OK)
      subscription.release(); // good practice
  }

  public void onSubscriptionIdle(SubscriptionIdleEvent ev){
    if(true)
      JFT.THIS.release();
    else { // as an expensive alternative
      subscription.stop();
      subscription.release(); // good practice
      connection.close();
      connection.release(); // good practice
      JFT.THIS.release();
    }
  }

  public void onSubscriptionNotify(SubscriptionNotifyEvent ev){
    switch(ev.getAction()) {
    case SubscriptionNotifyEvent.ACTION_ENTITY_ADD:
      System.out.println(entityAsString(ev.getEntity()));
      break;
    case SubscriptionNotifyEvent.ACTION_ENTITY_RWT:
    case SubscriptionNotifyEvent.ACTION_ENTITY_DEL:
    case SubscriptionNotifyEvent.ACTION_ENTITY_KIL:
    default:
      break;
    }
  }

  public void onSubscriptionStop(SubscriptionStopEvent ev){
    subscription.release(); // good practice
  }

  String entityAsString(Entity e) {
    FT_C_TRADING_STATE ts = (FT_C_TRADING_STATE) e;
    return "eID: " + ts.ExchangeID
        + " mID: " + ts.MarketID
        + " sID: " + ts.SectionID
        + " p: "   + FT_C_TRADING_PHASE.enumAsString(ts.Phase)
        + " s: "   + FT_C_TRADING_STATUS.enumAsString(ts.Status)
        + " pd: "  + ts.PhaseDescription
        + " t: "   + ts.Time;
  }
}