This page explains how to use the Connection Manager to add xml protocol support to a java app
Using without Notification Acceptor
Create a command processor class
-
import metcarob.com.common.network.xmlprotocol.XMLMessage;
-
import metcarob.com.common.network.xmlprotocol.XMLMessageProcessorInterface;
-
-
public class CommandProcessor implements XMLMessageProcessorInterface {
-
private MainApp m_App = null;
-
-
super();
-
m_App = p_App;
-
}
-
-
@Override
-
public void processMessage(XMLMessage p_msg) {
-
if (!m_App.isRunning()) return; //Don't run any commands if we are quitting
-
-
-
try {
-
-
if (cmdName.equals("Quit")) {
-
m_App.cmdQuit();
-
} else {
-
p_msg.replyCmdNotRecognised();
-
}
-
try {
-
e.printStackTrace();
-
p_msg.replyException(e);
-
f.printStackTrace();
-
}
-
}
-
}
-
-
}
Main App Class
-
ConnectionManager m_comms = null;
-
int serverListenPort = 3564;
-
-
m_comms = new ConnectionManager(m_cmd,serverListenPort,null);
-
-
while(true) {
-
m_comms.LoopIteration();
-
}
-
-
m_comms.close();
Using with Notification Acceptor
Example Program to send a single command
-
import java.net.Socket;
-
import metcarob.com.common.network.xmlprotocol.Connection;
-
import metcarob.com.common.network.xmlprotocol.XMLMessage;
-
import metcarob.com.common.network.xmlprotocol.XMLMessageProcessorInterface;
-
import metcarob.com.common.network.xmlprotocol.XMLMessageReciever;
-
-
public class Main {
-
private static class Command implements XMLMessageProcessorInterface {
-
-
@Override
-
if (p_msg.isRespCmdNotRecognised()) {
-
return;
-
}
-
if (p_msg.isRespOK()) {
-
return;
-
}
-
if (p_msg.isRespException()) {
-
return;
-
}
-
}
-
-
}
-
-
try {
-
-
int port = 7854;
-
-
-
Command cmd = new Command();
-
XMLMessageReciever rec = new XMLMessageReciever(cmd);
-
conn.start();
-
-
//Execute command
-
XMLMessage msg = new XMLMessage("Quit","");
-
conn.Transmit(msg);
-
-
try {
-
if (conn!=null) {
-
conn.close(true);
-
};
-
e.printStackTrace();
-
}
-
-
-
e.printStackTrace();
-
}
-
}
-
}
Authorise a connection
Authorisation allows the verification of a client identity to the server. The connection does not necessarily have to be encrypted for this to be secure.
- When server creates the ConnectionManager it passes a Map
which contains users and public keys - Establish a normal connection
- Client Call conn.AuthConnection passing a Username and private key
- Internal three Message Authorization handshake (see diagram)
- Server Connection sends internal message to message processor to confirm the authorisation (See Code Snipit)
Three Message Handshake
Code to receive internal private message
This must check the message is internal to prevent client spoofing.
-
String cmdName = p_msg.getCmdName();
-
String cmdGUID = p_msg.getGuid();
-
String cmdREQGUID = p_msg.getReqGuid();
-
-
if (cmdName.equals(Constants.getMsgConnectionAuth()) && p_msg.isSecureInternal()) {
-
String usr = p_msg.getParams().getChildNodes().item(0).getTextContent();
-
System.out.println("Just authorised " + usr + ":" + p_msg.getSrcConnection().getAuthUser());
-
}
Encryption
Connections can be encrypted using pre-shared public and private keys. Encryption is set up in each direction independently.
To setup in one direction:
- Destination: Must call Connection.setEncryptionIdentity either directly or setup using the Connection Manager
- Source: Call Connection.EncryptFor
RJM Article Type
Work Notes