[TOC]
public static String ClientEngine.SDKVersion = "2.0.5";
public static void ClientEngine.setAutoStop(boolean auto);
public static boolean ClientEngine.isAutoStop();
Config the SDK can be cleaned up automatically.
true is the default action.
If the automatic action is disabled, ClientEngine.stop(); MUST be called for cleaning up.
public static void ClientEngine.setQuestTimeout(int timeout);
public static int ClientEngine.getQuestTimeout();
Config the default timeout in seconds for global quest. Default is 5 seconds.
public static void ClientEngine.setMaxThreadInTaskPool(int count);
public static int ClientEngine.getMaxThreadInTaskPool();
Default max limitation is 4 threads.
public static void ClientEngine.setMaxPackageLength(int length);
public static int ClientEngine.getMaxPackageLength();
The max length for FPNN Quest & FPNN Answer packages.
Default is 10MB.
public static void ClientEngine.startEngine();
public static boolean ClientEngine.started();
public static void ClientEngine.stop();
Start & stop ClientEngine.
Explicitly call ClientEngine.startEngine() is not necessary, because it will be called automatically when client connecting.
If ClientEngine.setAutoStop set to false, MUST call ClientEngine.stop() explicitly. In other cases, calling is optional.
TCPClient client = new TCPClient(String host, int port);
TCPClient client = new TCPClient(String host, int port, boolean autoConnect);
TCPClient client = TCPClient.create(String host, int port);
TCPClient client = TCPClient.create(String host, int port, boolean autoConnect);
TCPClient client = TCPClient.create(String endpoint);
TCPClient client = TCPClient.create(String endpoint, boolean autoConnect);
endpoint format: "hostname/ip" + ":" + "port".
e.g. "localhost:8000"
Note:
autoConnect is NOT keep alive, just calling connect() method automatically when sending quest without established connection.
public String endpoint();
Return current endpoint of client.
public ClientStatus getClientStatus();
Return client current status.
Values:
- TCPClient.ClientStatus.Closed
- TCPClient.ClientStatus.Connecting
- TCPClient.ClientStatus.Connected
public boolean connected();
Return client current is connected or not.
public int questTimeout();
public void setQuestTimeout(int timeout);
Get/set the quest timeout in seconds for this client instance.
Default 0, means using global settings.
public boolean isAutoConnect();
public void setAutoConnect(boolean autoConnect);
Get/set the auto connecting behavior.
Note:
Auto Connect is NOT keep alive, just calling connect() method automatically when sending quest without established connection.
public boolean enableEncryptorByDerFile(String curve, String keyFilePath);
public boolean enableEncryptorByDerData(String curve, byte[] peerPublicKey);
Enable encrypted connection.
curve is the curve name of the der key for ECC encryption.
Curve secp256k1 is recommended.
public boolean connect(boolean synchronous) throws InterruptedException;
public boolean reconnect(boolean synchronous) throws InterruptedException;
synchronous: connect/reconnect is in synchronous mode or asynchronous mode.
public void close();
Close current connection. Calling without available connection or client connecting are safe.
public void setConnectedCallback(ConnectionConnectedCallback cb);
Set the connected evnet. Prototype:
public interface ConnectionConnectedCallback {
void connectResult(InetSocketAddress peerAddress, boolean connected);
}
public void setWillCloseCallback(ConnectionWillCloseCallback cb);
public void setHasClosedCallback(ConnectionHasClosedCallback cb);
Set the will close evnet & has closed event. Prototype:
-
Will closing callback:
public interface ConnectionWillCloseCallback { void connectionWillClose(InetSocketAddress peerAddress, boolean causedByError); } -
Has closed callback:
public interface ConnectionHasClosedCallback { void connectionHasClosed(InetSocketAddress peerAddress, boolean causedByError); }
public void setQuestProcessor(Object questProcessor, String questProcessorFullClassName);
-
questProcessor:The server pushed quest processor.
Each quest processing method has the same declaration:
public Answer questMethodName(Quest quest, InetSocketAddress peer);questMethodNameis the real interface name, same with themethodin quest. -
questProcessorFullClassName:The full class name of
questProcessor, which includes the package name.
//-- synchronous
public Answer sendQuest(Quest quest) throws InterruptedException;
public Answer sendQuest(Quest quest, int timeoutInSeconds) throws InterruptedException;
//-- asynchronus
public void sendQuest(Quest quest, AnswerCallback callback);
public void sendQuest(Quest quest, AnswerCallback callback, int timeoutInSeconds);
public void sendQuest(Quest quest, FunctionalAnswerCallback callback);
public void sendQuest(Quest quest, FunctionalAnswerCallback callback, int timeoutInSeconds);
Send quest to server.
-
AnswerCallback:Prototype:
public abstract class AnswerCallback { public abstract void onAnswer(Answer answer); public abstract void onException(Answer answer, int errorCode); public long getSentTime(); public long getAnsweredTime(); }When quest successed,
onAnswer()will be called; elseonException()will be called.
TheanswerinonException()maybenull. -
FunctionalAnswerCallback:Prototype:
public interface FunctionalAnswerCallback { void onAnswer(Answer answer, int errorCode); }When the
errorCodebeyond 0 orcom.fpnn.sdk.ErrorCode.FPNN_EC_OK.value()(value ofFPNN_EC_OKis0), theanswermaybenull.
public void sendAnswer(Answer answer);
Send answer in unforeseen case.
FPNN message is the parent class of the FPNN Quest & FPNN Answer.
It offers the data access capability for FPNN Quest & FPNN Answer.
public Message();
public Message(Map body);
public Map getPayload();
public void setPayload(Map p);
Get/set the raw data map.
public void param(String key, Object value);
Add data to FPNN Message.
public Object get(String key);
public Object get(String key, Object def);
Fetch data from FPNN Messsage. If data is not exist, the null or def will be returned.
public int getInt(String key, int defaultValue);
public long getLong(String key, long defaultValue);
Fetch int type or long type data from FPNN Messsage. If data is not exist, the defaultValue will be returned.
public Object want(String key) throws NoSuchElementException;
Fetch data from FPNN Messsage. If data is not exist, a NoSuchElementException exception will be thrown.
public int wantInt(String key) throws ClassCastException, NoSuchElementException;
public long wantLong(String key) throws ClassCastException, NoSuchElementException;
Fetch int type or long type data from FPNN Messsage. If data is not exist, a NoSuchElementException exception will be thrown. If data type dose not match, a ClassCastException exception will be thrown.
public byte[] toByteArray() throws IOException;
public byte[] raw() throws IOException;
Serialize the FPNN Message instance to the byte[] data in msgpack SPEC.
public Quest(String method);
public Quest(String method, boolean isOneWay);
public Quest(String method, int seqNum, boolean isOneWay, Map payload);
-
method:
The requested interface/method name.
-
isOneWay:
Create the new FPNN Quest as an one way quest or not.
-
seqNum:
The sequence number for FPNN package (Quest & Answer).
-
payload:
The quest data in FPNN Message.
The first constructor will create a two way quest with an empty data map.
public int getSeqNum();
Fetch the sequence number of this quest.
public String method();
Fetch the quest requested interface/method.
public boolean isOneWay();
Check the quest is one way quest or not.
public boolean isTwoWay();
Check the quest is two way quest or not.
public ByteBuffer rawData();
Serialize this quest instance into a ByteBuffer instance in FPNN Packet Protocol.
public Answer(Quest quest);
public Answer(int seqNum);
public Answer(int seqNum, boolean error, Map payload);
-
quest:
The quest will be answered by this answer.
-
seqNum:
The seqNum of the quest will be answered by this answer.
-
error:
Create this answer as FPNN Standard Error Answer or not.
-
payload:
The answer data map in FPNN Message.
public int getSeqNum();
Fetch the sequence number of this answer.
public boolean isErrorAnswer();
Check this answer is the FPNN Standard Error Answer or not.
public int getErrorCode();
The error code for FPNN Standard Error Answer.
public String getErrorMessage();
Fetch the error message for FPNN Standard Error Answer.
public void fillErrorCode(int errorCode);
Fill the error code to the FPNN Standard Error Answer.
public void fillErrorInfo(int errorCode);
public void fillErrorInfo(int errorCode, String message);
Fill the error code and error message to the FPNN Standard Error Answer.
public ByteBuffer rawData() throws IOException;
Serialize this answer instance into a ByteBuffer instance in FPNN Packet Protocol.