Difference between revisions of "AMI Client Interface to Realtime Backend API"

From 3forge Documentation
Jump to navigation Jump to search
Line 22: Line 22:
 
=== Example - Java Code ===
 
=== Example - Java Code ===
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 +
package com.demo.runmaintest;
 +
 
import java.util.Map;
 
import java.util.Map;
  
Line 27: Line 29:
 
import com.f1.ami.client.AmiClientCommandDef;
 
import com.f1.ami.client.AmiClientCommandDef;
 
import com.f1.ami.client.AmiClientListener;
 
import com.f1.ami.client.AmiClientListener;
import com.f1.ami.client.RawAmiClient;
 
 
import com.f1.utils.OH;
 
import com.f1.utils.OH;
 +
import com.f1.utils.concurrent.HasherMap;
  
public class TestAmiClient implements AmiClientListener {
+
public class SampleClient implements AmiClientListener {
    public static final byte OPTION_AUTO_PROCESS_INCOMING=2;
+
public static final byte OPTION_AUTO_PROCESS_INCOMING = 2;
    public static void main(String a[]) throws Exception {
 
        AmiClient client = new AmiClient();
 
        client.addListener(new TestAmiClient(client));
 
        client.start("localhost", 3289, "demo", OPTION_AUTO_PROCESS_INCOMING);
 
        while (true)
 
            OH.sleep(1000); // Keep process alive
 
    }
 
  
    private AmiClient client;
+
public static void main(String a[]) throws Exception {
 +
AmiClient client = new AmiClient();
 +
client.addListener(new SampleClient(client));
 +
client.start("localhost", 3289, "demo", OPTION_AUTO_PROCESS_INCOMING);
 +
while (true)
 +
OH.sleep(1000); // Keep process alive
 +
}
  
    public TestAmiClient(AmiClient client) {
+
private AmiClient amiClient;
        this.client = client;
 
    }
 
  
 +
public SampleClient(AmiClient client) {
 +
this.amiClient = client;
 +
}
 
     @Override
 
     @Override
    public void onMessageReceived(AmiClient source, long now, int seqnum, int status, CharSequence message) {
+
public void onMessageReceived(AmiClient source, long now, int seqnum, int status, CharSequence message) {
        System.out.println("Message received: " + message);
+
System.out.println("Message received: " + message);
    }
+
}
 
 
 
     @Override
 
     @Override
    public void onMessageSent(AmiClient source, CharSequence message) {
+
public void onMessageSent(AmiClient source, CharSequence message) {
        System.out.println("Message sent: " + message);
+
System.out.println("Message sent: " + message);
    }
+
}
 
 
 
     @Override
 
     @Override
    public void onConnect(AmiClient source) {
+
public void onConnect(AmiClient source) {
        System.out.println("Connected");
+
System.out.println("Connected");
    }
+
}
 
 
 
     @Override
 
     @Override
    public void onDisconnect(AmiClient source) {
+
public void onDisconnect(AmiClient source) {
        System.out.println("Disconnected");
+
System.out.println("Disconnected");
    }
+
}
 
 
 
     @Override
 
     @Override
    public void onLoggedIn(AmiClient source) {
+
public void onLoggedIn(AmiClient amiClient) {
        // We’ve successfully connected an logged in, let’s register stuff.
+
// We’ve successfully connected an logged in, let’s register stuff.
        System.out.println("Logged in");
+
System.out.println("Logged in");
        // Send message
+
// Send message
        this.client.startObjectMessage("SampleOrders", "1");
+
this.amiClient.startObjectMessage("SampleOrders", "1");
        // Send as String
+
// Send as String
        // addMessageParamString(String key, String value)
+
// addMessageParamString(String key, String value)
        this.amiClient.addMessageParamString("Order", data.get("Order"));
+
this.amiClient.addMessageParamString("Order", "Order");
        // Send as int
+
// Send as int
        // addMessageParamInt(String key, int value)
+
// addMessageParamInt(String key, int value)
        this.amiClient.addMessageParamInt("Quantity", 1000);
+
this.amiClient.addMessageParamInt("Quantity", 1000);
        // Send as double
+
// Send as double
        // addMessageParamDouble(String key, double value)
+
// addMessageParamDouble(String key, double value)
        this.amiClient.addMessageParamDouble("Price", 2703.1995);
+
this.amiClient.addMessageParamDouble("Price", 2703.1995);
        // Send as long
+
// Send as long
        // addMessageParamLong(String key, long value)
+
// addMessageParamLong(String key, long value)
        this.amiClient.addMessageParamLong("Price", (long) 2703.1995);
+
this.amiClient.addMessageParamLong("Price", (long) 2703.1995);
        // Send as float
+
// Send as float
        // addMessageParamFloat(String key, float value)
+
// addMessageParamFloat(String key, float value)
        this.amiClient.addMessageParamFloat("Volume", (float) 0.45466549498);
+
this.amiClient.addMessageParamFloat("Volume", (float) 0.45466549498);
        // Send as boolean
+
// Send as boolean
        // addMessageParamBoolean(String key, boolean value)
+
// addMessageParamBoolean(String key, boolean value)
        this.amiClient.addMessageParamBoolean("GTC", false);
+
this.amiClient.addMessageParamBoolean("GTC", false);
        // Send as json object
+
// Send as json object
        // addMessageParamJson(String key, Object value)
+
// addMessageParamJson(String key, Object value)
        this.amiClient.addMessageParamJson("Table", map);
+
Map map = new HasherMap<String, String>();
        // Send as binary
+
this.amiClient.addMessageParamJson("Table", map);
        // addMessageParamBinary(String key, byte[] value)
+
// Send as binary
        this.amiClient.addMessageParamBinary("Val", val);
+
// addMessageParamBinary(String key, byte[] value)
        // Send as num
+
byte[] binary = "hello world".getBytes();
        // addMessageParamBinary(String key, CharSequence value)
+
this.amiClient.addMessageParamBinary("Val", binary);
        this.amiClient.addMessageParamEnum("Num", num)
+
// Send as num
        // Send as object
+
// addMessageParamBinary(String key, CharSequence value)
        // addMessageParamObject(String key, Object value)
+
this.amiClient.addMessageParamEnum("Num", "ENUM");
        this.amiClient.addMessageParamObject("Data", obj);
+
// Send as object
        this.client.sendMessageAndFlush();
+
// addMessageParamObject(String key, Object value)
     
+
Object obj = new Object();
        // Send command
+
this.amiClient.addMessageParamObject("Data", obj);
        AmiClientCommandDef def = new AmiClientCommandDef("sample_cmd_def");
+
this.amiClient.sendMessageAndFlush();
        def.setConditions(AmiClientCommandDef.CONDITION_USER_CLICK);
 
        this.client.sendCommandDefinition(def);
 
        this.client.flush();
 
        System.out.println("Sent command");
 
    }
 
  
    @Override
+
// Register a  command
    public void onCommand(AmiClient source, String requestId, String cmd, String userName, String type, String id, Map<String, Object> params) {
+
AmiClientCommandDef def = new AmiClientCommandDef("sample_cmd_def");
        // Do business logic triggered by callback
+
def.setConditions(AmiClientCommandDef.CONDITION_USER_CLICK);
        System.out.println("On command");
+
this.amiClient.sendCommandDefinition(def);
        client.startResponseMessage(requestId, 1, "Okay").addMessageParamLong("sample_user_callback", 45).sendMessageAndFlush();
+
this.amiClient.flush();
    }
+
System.out.println("Sent command");
 +
}
 +
        @Override
 +
public void onCommand(AmiClient source, String requestId, String cmd, String userName, String type, String id, Map<String, Object> params) {
 +
// Do business logic triggered by callback
 +
System.out.println("On command");
 +
source.startResponseMessage(requestId, 1, "Okay").addMessageParamLong("sample_user_callback", 45).sendMessageAndFlush();
 +
}
 
}
 
}
 +
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revision as of 15:44, 4 November 2022

Overview

AMI provides developers a Java library to connect to the AMI Realtime Backend API via the AMI Client.

Setup

Overview

The AMI Client Listener is used to process messages and commands sent and received by the AMI Client.

The AMI Client connects to the AMI Realtime Backend API. Below is a simple example that sends a message and a command via the AMI Client and processes the command callback.

Configuration

The hostname is the host where either AmiCenter or AmiRelay is running.

The port is configured via the property “ami.port” which typically is set to 3289.

Java interface (see javadoc for details)

com.f1.ami.client.AmiClient

com.f1.ami.client.AmiClientListener

com.f1.ami.client.AmiCommandDef

Example - Java Code

package com.demo.runmaintest;

import java.util.Map;

import com.f1.ami.client.AmiClient;
import com.f1.ami.client.AmiClientCommandDef;
import com.f1.ami.client.AmiClientListener;
import com.f1.utils.OH;
import com.f1.utils.concurrent.HasherMap;

public class SampleClient implements AmiClientListener {
	public static final byte OPTION_AUTO_PROCESS_INCOMING = 2;

	public static void main(String a[]) throws Exception {
		AmiClient client = new AmiClient();
		client.addListener(new SampleClient(client));
		client.start("localhost", 3289, "demo", OPTION_AUTO_PROCESS_INCOMING);
		while (true)
			OH.sleep(1000); // Keep process alive
	}

	private AmiClient amiClient;

	public SampleClient(AmiClient client) {
		this.amiClient = client;
	}
    @Override
	public void onMessageReceived(AmiClient source, long now, int seqnum, int status, CharSequence message) {
		System.out.println("Message received: " + message);
	}
    @Override
	public void onMessageSent(AmiClient source, CharSequence message) {
		System.out.println("Message sent: " + message);
	}
    @Override
	public void onConnect(AmiClient source) {
		System.out.println("Connected");
	}
    @Override
	public void onDisconnect(AmiClient source) {
		System.out.println("Disconnected");
	}
    @Override
	public void onLoggedIn(AmiClient amiClient) {
		// We’ve successfully connected an logged in, let’s register stuff.
		System.out.println("Logged in");
		// Send message
		this.amiClient.startObjectMessage("SampleOrders", "1");
		// Send as String
		// addMessageParamString(String key, String value)
		this.amiClient.addMessageParamString("Order", "Order");
		// Send as int
		// addMessageParamInt(String key, int value)
		this.amiClient.addMessageParamInt("Quantity", 1000);
		// Send as double
		// addMessageParamDouble(String key, double value)
		this.amiClient.addMessageParamDouble("Price", 2703.1995);
		// Send as long
		// addMessageParamLong(String key, long value)
		this.amiClient.addMessageParamLong("Price", (long) 2703.1995);
		// Send as float
		// addMessageParamFloat(String key, float value)
		this.amiClient.addMessageParamFloat("Volume", (float) 0.45466549498);
		// Send as boolean
		// addMessageParamBoolean(String key, boolean value)
		this.amiClient.addMessageParamBoolean("GTC", false);
		// Send as json object
		// addMessageParamJson(String key, Object value)
		Map map = new HasherMap<String, String>();
		this.amiClient.addMessageParamJson("Table", map);
		// Send as binary
		// addMessageParamBinary(String key, byte[] value)
		byte[] binary = "hello world".getBytes();
		this.amiClient.addMessageParamBinary("Val", binary);
		// Send as num
		// addMessageParamBinary(String key, CharSequence value)
		this.amiClient.addMessageParamEnum("Num", "ENUM");
		// Send as object
		// addMessageParamObject(String key, Object value)
		Object obj = new Object();
		this.amiClient.addMessageParamObject("Data", obj);
		this.amiClient.sendMessageAndFlush();

		// Register a  command
		AmiClientCommandDef def = new AmiClientCommandDef("sample_cmd_def");
		def.setConditions(AmiClientCommandDef.CONDITION_USER_CLICK);
		this.amiClient.sendCommandDefinition(def);
		this.amiClient.flush();
		System.out.println("Sent command");
	}
        @Override
	public void onCommand(AmiClient source, String requestId, String cmd, String userName, String type, String id, Map<String, Object> params) {
		// Do business logic triggered by callback
		System.out.println("On command");
		source.startResponseMessage(requestId, 1, "Okay").addMessageParamLong("sample_user_callback", 45).sendMessageAndFlush();
	}
}

Sending Objects

Once the AmiClient is connected to AMI Realtime Backend API, the client can start sending messages.

See Real-time Messaging API - Outbound Instruction Type - Object (O)

Class AmiClient

startObjectMessage

AmiClient startObjectMessage(String type, CharSequence id)

Starts an object (O) message. Param id is optional.


startObjectMessage

AmiClient startObjectMessage(String type, CharSequence id, long expiresOn)

Starts an object (O) message. Param id is optional. If the param expiresOn is: set to 0 the object does not expire, a positive value the object expires at an epoc absolute time, a negative value the object expires in an offset time(milliseconds) into the future.


addMessageParamObject

void addMessageParamObject(String key, Object value)


addMessageParams

AmiClient addMessageParams(Map<String, Object> params)

See com.f1.ami.client.AmiClient (javadoc for other addMessageParam[types])


sendMessage

boolean sendMessage()

Finalize and send the current message, returns true if successful


flush

void flush()

Send pending message buffer to AMI, can be called at anytime


sendMessageAndFlush

boolean sendMessageAndFlush()

Send pending message to AMI and block until the message is fully read by AMI, returns true if successful

Register Command

Commands can be created and registered to AMI.

See Real-time Messaging API - Outbound Instruction Type - Object (C)

Class AmiClientCommandDef

AmiClientCommandDef(String id)

Creates a new command

Class AmiClient

sendCommandDefinition

void sendCommandDefinition(AmiClientCommandDef def)

Send a command (C) declaration

Processing Command Callbacks

Command callbacks are processed using the AmiClientListener onCommand() method. See Real-time Messaging API - Outbound Instruction Type - Object (R)

Class AmiClient

startResponseMessage

AmiClient startResponseMessage(String origRequestId, int status, String message)

Start a response (R) message.

AmiClientAsServer

The steps to set up the interface for the AmiClientAsServer is similar to the AmiClient interface.

Configuration

To set it up, you will require the following configuration:

ami.relay.fh.active=$${ami.relay.fh.active},csocket
ami.relay.fh.csocket.start=true
ami.relay.fh.csocket.class=com.f1.ami.relay.fh.AmiClientSocketFH
ami.relay.fh.csocket.props.amiId=client_socket
ami.relay.fh.csocket.props.port=1234
ami.relay.fh.csocket.props.host=localhost

Example - Java Code

package com.f1.ami.client;

import java.io.IOException;
import java.net.Socket;
import java.util.Map;

public class AmiClientAsServerTest implements AmiClientAsServerFactory, AmiClientListener {
	public static void main(String a[]) throws IOException {
		new AmiClientAsServer(1234, null, null, new AmiClientAsServerTest());
	}

	@Override
	public void onClient(Socket socket, AmiClient client) throws IOException {
		client.start(socket, "demo", AmiClient.ENABLE_AUTO_PROCESS_INCOMING);
		client.startObjectMessage("ClientAsServer", null);
		client.addMessageParamString("key", "Hello!");
		client.addMessageParamLong("now", System.currentTimeMillis());
		client.addMessageParamDouble("now", System.currentTimeMillis());
                client.sendMessageAndFlush();
		client.addListener(this);
	}

	@Override
	public void onMessageReceived(AmiClient rawClient, long now, int seqnum, int status, CharSequence message) {
		System.out.println("On Message Received: " + message);
	}

	@Override
	public void onMessageSent(AmiClient rawClient, CharSequence message) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onConnect(AmiClient rawClient) {
		System.out.println("Connected");
	}

	@Override
	public void onDisconnect(AmiClient rawClient) {
		System.out.println("Disconnected");
	}

	@Override
	public void onCommand(AmiClient rawClient, String requestId, String cmd, String userName, String objectType, String objectId, Map<String, Object> params) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onLoggedIn(AmiClient rawClient) {
		System.out.println("Loggedin");
	}