This project is read-only.

Connecting and sending commands to the simulator

This section describes how to build an application that can connect via TCP/IP to Thales Simulator (or to a real Thales HSM) and send host commands.

The most basic thing to understand before trying to connect to the simulator is the format in which host commands are expected. Host commands are comprised of text of variable length. Each host command is preceded by a 2-byte software header. The software header defines the length of the host command that is send to the simulator.

As an example, consider the command 0000NO00 which asks the simulator to return status information. Once you connect to the simulator's listening port and establish a data socket, you would need to send a message starting with two bytes (x00 x08) and then append the actual message. The simulator responds in the same manner to your request (unless it doesn't understand it).

The easiest way to connect to the simulator is to use the ThalesCore.dll, specifically the ThalesSim.Core.TCP.WorkerClient class. The WorkerClient class handles the fuss of calculating the software header for you. First declare an instance of WorkerClient.
Dim WithEvents thales As ThalesSim.Core.TCP.WorkerClient
After that, declare code for the event handlers that will be called to handle data arriving from the simulator or a lost connection.
Private Sub thales_MessageArrived(ByVal sender As ThalesSim.Core.TCP.WorkerClient, ByRef b() As Byte, ByVal len As Integer) Handles thales.MessageArrived
''Code to handle the message from the simulator.
End Sub

Private Sub thales_Disconnected(ByVal sender As ThalesSim.Core.TCP.WorkerClient) Handles thales.Disconnected
''Code to handle a disconnect.
End Sub
(Note: you may obviously choose to manually attach the event handlers in case you want to declare thales without using the WithEvents keyword).

To connect to the simulator, use the following:
thales = New TCP.WorkerClient(New Net.Sockets.TcpClient(thalesIPAddressOrHostName, thalesPort))
thales.InitOps()
You may get a SocketException if you specified an incorrect IP address/host name, an incorrect port number or you haven't started the simulator. Otherwise, you may send a command to the simulator using the following:
thales.send(hostCommandString)
Remember that WorkerClient calculates the software header for you, so you need only send the actual host command text. After you're done, use the following code to disconnect from the simulator:
thales.TermClient()
You can have a look at the PVV Clashing Demo application that is included in the Thales Simulator source code to see how it connects to the simulator.

Last edited Jul 9, 2010 at 9:26 AM by nickntg, version 5

Comments

taourarte Mar 14 at 12:47 PM 
how many commands are supported on thales simulator ??

taourarte Mar 14 at 10:40 AM 
please we want more examples about sending commands from java client to thales simulator

RCMCCART Feb 25, 2016 at 6:13 PM 
Hello! Do you happen to have a C# example with regards to connecting to the simulator?

snowch Jun 11, 2014 at 12:33 PM 
This is probably the most basic example you can get:


import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.ByteBuffer;
public class Main {

public static void main(String[] args) throws Exception {

Socket socket = new Socket("localhost", 9998);

String command = "0006303030304e43";

// the following line converts the hex command string to a byte array
byte[] bytes = ByteBuffer.allocate(8).putLong(Long.parseLong(command, 16)).array();

OutputStream out = socket.getOutputStream();
BufferedOutputStream bufferedOut = new BufferedOutputStream(out, 1024);
bufferedOut.write(bytes);
bufferedOut.flush();

InputStream in = socket.getInputStream();
int result;
while ((result = in.read()) != -1) {
System.out.print((char)result);
}
socket.close();
}
}


To understand the meaning of the string 0006303030304e43, it can be broken down as follows (in reverse order):

- 4e43 is the 2 byte command NC as hex
- 30303030 adds a 4 byte header 0000 as hex
- 0006 represents the length in hex of the comamnd and header (i.e. the length of 0000NC)

sagamagus Jul 8, 2013 at 4:47 PM 
Hi, good day, i have some questions, can you help me a little? i want to connect to the emulator but im using java, im wondering som things, can i comunicate with the emulator with a socket using the port number?