Commit c0826263 authored by Steffen Vogel's avatar Steffen Vogel 🎅🏼
Browse files

Merge branch 'gtnet_timestamp' into 'develop'

Add RTDS drafts for GTNET + GTSYNC setup

Closes #49

See merge request !14
parents 38a2d0c5 16359fd6
POINT0 int
POINT1 int
POINT2 int
POINT3 int
POINT4 float
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellEditor;
/*
* NOTE: GTNET_SKT_TCP_GUI was made as an example TCP client/server
* The code is given as an example and is not intended to be
* part of RSCAD support. Questions will not be answered.
*/
public class GTNET_SKT_TCP_GUI extends JFrame
{
/**
*
*/
private static final long serialVersionUID = -6325275282238226823L;
private static boolean DEBUG = false;
static TCP_ListenTask serverProcess;
static TCP_ConnectTask clientProcess;
//UDP Server
static JTextField jTextFieldLocalIp;
static JTextField jTextFieldLocalPort;
static JLabel lblLocalIp;
static JLabel lblLocalPort;
public static SoftEdgeButton btnStart;
public static SoftEdgeButton btnTCP;
public static SoftEdgeButton btnQuit;
public static SoftEdgeButton btnAddIp;
public static SoftEdgeButton btnDeleteIp;
public static SoftEdgeButton btnSaveIp;
public static SoftEdgeButton btnLoadIp;
static JTable IPtable;
//UDP Client
static JTextField jTextFieldRemoteIp;
static JTextField jTextFieldRemotePort;
static JLabel lblRemoteIp;
static JLabel lblRemotePort;
public static SoftEdgeButton btnSend;
static JTable OPtable;
private static JPanel MainPnl;
final static int GTNETSKT_MAX_INPUTS = 300;
public static String[] gtnetSktInputName = new String[GTNETSKT_MAX_INPUTS];
public static String[] gtnetSktInputType = new String[GTNETSKT_MAX_INPUTS];
static int ival[];
static float fval[];
public static String[] gtnetSktOutputName = new String[GTNETSKT_MAX_INPUTS];
public static String[] gtnetSktOutputType = new String[GTNETSKT_MAX_INPUTS];
static int numInPoints = 0;
static int numOutPoints = 0;
static InetAddress host = null;
static byte[] inBuf = null;
static byte[] outBuf = null;
static String msg = null;
public static TableCellEditor editorInt;
public static TableCellEditor editorFloat;
protected static JTextArea textArea;
// private final static String newline = "\r\n";
private final static String newline = System.getProperty("line.separator");//This will retrieve line separator
private final static String delim = "\t";//","; // tab delimited file
ServerSocket serverSocket = null;
Socket connectionSocket =null;
static GTNET_SKT_TCP_GUI main_inst;
public static void main(String args[])
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
main_inst = new GTNET_SKT_TCP_GUI();
main_inst.setVisible(true);
}
});
}
public GTNET_SKT_TCP_GUI()
{
super("RTDS GTNET-SKT TCP Server and Client");
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e)
{
}
JFormattedTextField ftf[] = new JFormattedTextField[2];
ftf[0] = new JFormattedTextField(new Integer(90032221));
ftf[1] = new JFormattedTextField(java.text.NumberFormat.getInstance());
ftf[1].setValue(new Float(0.0));
editorInt = new DefaultCellEditor(ftf[0]);
editorFloat = new DefaultCellEditor(ftf[1]);
Object options[] = { "int", "float" };
JComboBox<?> comboBox = new JComboBox<Object>(options);
comboBox.setMaximumRowCount(4);
TableCellEditor editorType = new DefaultCellEditor(comboBox);
// The Server Table
String columnNames[] = {"Point #","Variable","Type","Value"};
IPtable = new JTable(new InputTableModel(columnNames));//Create JTable
IPtable.setCellSelectionEnabled(true);
IPtable.setPreferredScrollableViewportSize(new Dimension(200, 150));
IPtable.setFillsViewportHeight(true);
IPtable.getTableHeader().setReorderingAllowed(false); //no column dragging
IPtable.setRowSelectionAllowed(true);
IPtable.setColumnSelectionAllowed(false);
IPtable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JTableHeader header = IPtable.getTableHeader();
header.setBackground(Color.LIGHT_GRAY);
IPtable.getColumnModel().getColumn(2).setCellEditor(editorType);
// The Client Table
OPtable = new JTable(new OutputTableModel(columnNames));//Create JTable
OPtable.setPreferredScrollableViewportSize(new Dimension(200, 150));
OPtable.setFillsViewportHeight(true);
OPtable.getTableHeader().setReorderingAllowed(false); //no column dragging
OPtable.setRowSelectionAllowed(true);
OPtable.setColumnSelectionAllowed(false);
OPtable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
OPtable.getTableHeader().setBackground(Color.LIGHT_GRAY);
OPtable.getColumnModel().getColumn(2).setCellEditor(editorType);
setup(); // The GUI
setSize(520,745);
setLayout(new FlowLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().add(MainPnl);
setVisible(true);
}
public static void setup()
{
try {
host = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
// Main Panel
MainPnl = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
MainPnl.add(createTCP_Pnl(), gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
MainPnl.add(createInputPointPnl(), gbc);
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 1;
MainPnl.add(createOutputPointPnl(), gbc);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 1;
MainPnl.add(createMsgPnl(), gbc);
}
private static JPanel createTCP_Pnl() {
//Points from GTNET-SKT
TitledBorder borderPoints = BorderFactory.createTitledBorder(new SoftEdgeBorder(3,3, new Color(213, 223, 229)), "TCP Communication Mode");
JPanel pnlTCP = new JPanel(new GridBagLayout());
pnlTCP.setBorder(new CompoundBorder(borderPoints, new EmptyBorder(2,2,2,2)));
Object options[] = { "Client", "Server" };
final JComboBox<?> comboBox = new JComboBox<Object>(options);
btnTCP= new SoftEdgeButton("Connect");
lblLocalIp = new JLabel("Local Server, IP Address:");
jTextFieldLocalIp = new JTextField("", 10);
jTextFieldLocalIp.setText(host.getHostAddress());
jTextFieldLocalIp.setEditable(true);
lblLocalPort = new JLabel("Port");
jTextFieldLocalPort = new JTextField("", 6);
jTextFieldLocalPort.setText("12345");
jTextFieldLocalIp.setVisible(false);
jTextFieldLocalPort.setVisible(false);
lblLocalIp.setVisible(false);
lblLocalPort.setVisible(false);
lblRemoteIp = new JLabel("Remote Server, IP Address:");
jTextFieldRemoteIp = new JTextField("", 9);
jTextFieldRemoteIp.setText(host.getHostAddress());
if (DEBUG) {
jTextFieldRemoteIp.setText("172.24.9.185");
}
jTextFieldRemoteIp.setEditable(true);
lblRemotePort = new JLabel("Port");
jTextFieldRemotePort = new JTextField("", 5);
jTextFieldRemotePort.setText("12345");
comboBox.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(comboBox.getSelectedIndex()==0)
{
btnTCP.setText("Connect");
jTextFieldLocalIp.setVisible(false);
jTextFieldLocalPort.setVisible(false);
lblLocalIp.setVisible(false);
lblLocalPort.setVisible(false);
lblRemoteIp.setVisible(true);
jTextFieldRemoteIp.setVisible(true);
lblRemotePort.setVisible(true);
jTextFieldRemotePort.setVisible(true);
}
else
{
btnTCP.setText("Listen");
jTextFieldLocalIp.setVisible(true);
jTextFieldLocalPort.setVisible(true);
lblLocalIp.setVisible(true);
lblLocalPort.setVisible(true);
lblRemoteIp.setVisible(false);
jTextFieldRemoteIp.setVisible(false);
lblRemotePort.setVisible(false);
jTextFieldRemotePort.setVisible(false);
}
}
});
btnTCP.addActionListener(new ActionListener() {
@SuppressWarnings("deprecation")
@Override
public void actionPerformed(ActionEvent e) {
if(btnTCP.getText()=="Listen")
{
echo("Server socket opening...");
try {
serverProcess = new TCP_ListenTask(main_inst , numInPoints);
serverProcess.start();
}
catch (Exception e1)
{
echo(e1.getMessage());
}
return;
}
if(btnTCP.getText()=="StopServer")
{
btnTCP.setText("Listen");
btnAddIp.setEnabled(true);
btnDeleteIp.setEnabled(true);
btnSaveIp.setEnabled(true);
btnLoadIp.setEnabled(true);
jTextFieldLocalIp.setEnabled(true);
jTextFieldLocalPort.setEnabled(true);
btnSend.setEnabled(false);
echo("Server socket closed...");
serverProcess.closeTask();
serverProcess.stop();
return;
}
if(btnTCP.getText()=="Connect")
{
echo("Client socket opening...");
try {
clientProcess = new TCP_ConnectTask(main_inst , numOutPoints);
clientProcess.start();
}
catch (Exception e1)
{
echo(e1.getMessage());
}
return;
}
if(btnTCP.getText()=="StopClient")
{
clientProcess.closeTask();
echo("Client socket closed...");
return;
}
}
});
GridBagConstraints gbc = new GridBagConstraints();
JPanel TCPoptions = new JPanel(new FlowLayout(FlowLayout.CENTER));
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(1, 1, 1, 1);
TCPoptions.add(comboBox, gbc);
TCPoptions.add(btnTCP, gbc);
JPanel LocalPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(1, 1, 1, 1);
LocalPanel.add(comboBox, gbc);
LocalPanel.add(btnTCP, gbc);
LocalPanel.add(lblLocalIp, gbc);
LocalPanel.add(jTextFieldLocalIp, gbc);
LocalPanel.add(lblLocalPort, gbc);
LocalPanel.add(jTextFieldLocalPort, gbc);
JPanel RemotePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(1, 1, 1, 1);
RemotePanel.add(lblRemoteIp, gbc);
RemotePanel.add(jTextFieldRemoteIp, gbc);
RemotePanel.add(lblRemotePort, gbc);
RemotePanel.add(jTextFieldRemotePort, gbc);
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 1;
gbc.gridy = 0;
pnlTCP.add(TCPoptions, gbc);
gbc.gridx = 2;
gbc.gridy = 0;
pnlTCP.add(LocalPanel, gbc);
gbc.gridx = 3;
gbc.gridy = 0;
pnlTCP.add(RemotePanel, gbc);
return pnlTCP;
}
private static JPanel createInputPointPnl() {
//Points from GTNET-SKT
TitledBorder borderPoints = BorderFactory.createTitledBorder(new SoftEdgeBorder(3,3, new Color(213, 223, 229)), "TCP Server");
JPanel pnlPoint = new JPanel(new GridBagLayout());
pnlPoint.setBorder(new CompoundBorder(borderPoints, new EmptyBorder(5,10,5,10)));
GridBagConstraints gbc = new GridBagConstraints();
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(IPtable);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.BOTH;
pnlPoint.add(scrollPane,gbc);
btnStart= new SoftEdgeButton("Start");
btnAddIp = new SoftEdgeButton("Add") ;
btnDeleteIp = new SoftEdgeButton("Delete") ;
btnSaveIp = new SoftEdgeButton("Save Table");
btnLoadIp = new SoftEdgeButton("Load Table");
btnAddIp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
if(numInPoints< GTNETSKT_MAX_INPUTS){
int i = numInPoints++;
InputTableModel a = (InputTableModel) IPtable.getModel();
gtnetSktInputName[i] = "POINTin" + Integer.toString(i);
gtnetSktInputType[i] = "float";
Object[] values = {i, gtnetSktInputName[i], gtnetSktInputType[i], ""};
a.insertData(values);
}else{
echo("Number of Server Points is at maximum of:" + GTNETSKT_MAX_INPUTS);
}
}
});
btnDeleteIp.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(IPtable.getSelectedRow() != -1){
InputTableModel a = (InputTableModel) IPtable.getModel();
a.removeRow(IPtable.getSelectedRow());
numInPoints--;
for (int i=0; i < numInPoints; i++) {
a.setValueAt((Object) i, i, 0);
}
}
}
});
btnSaveIp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
saveIPTable();
}
});
btnLoadIp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
loadIPTable();
}
});
JPanel tableButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(1, 1, 1, 1);
tableButtonPanel.add(btnAddIp, gbc);
tableButtonPanel.add(btnDeleteIp, gbc);
tableButtonPanel.add(btnSaveIp, gbc);
tableButtonPanel.add(btnLoadIp, gbc);
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 0;
gbc.gridy = 1;
pnlPoint.add(tableButtonPanel, gbc);
return pnlPoint;
}
private static JPanel createOutputPointPnl() {
TitledBorder borderPoints = BorderFactory.createTitledBorder(new SoftEdgeBorder(3,3, new Color(213, 223, 229)), "TCP Client");
//Points to GTNET-SKT
JPanel pnlPoint = new JPanel(new GridBagLayout());
pnlPoint.setBorder(new CompoundBorder(borderPoints, new EmptyBorder(5,10,5,10)));
GridBagConstraints gbc = new GridBagConstraints();
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(OPtable);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.BOTH;
pnlPoint.add(scrollPane,gbc);
btnSend= new SoftEdgeButton("Send");
btnSend.setEnabled(false);
SoftEdgeButton btnAddOp = new SoftEdgeButton("Add") ;
SoftEdgeButton btnDeleteOp = new SoftEdgeButton("Delete") ;
SoftEdgeButton btnSaveOp = new SoftEdgeButton("Save Table");
SoftEdgeButton btnLoadOp = new SoftEdgeButton("Load Table");
btnSend.addActionListener(new ActionListener() {
int txCnt = 0;
@Override
public void actionPerformed(ActionEvent e) {
int size = numOutPoints * 4;
try {
byte[] buffer = new byte[size];
ByteBuffer outbuff = ByteBuffer.wrap(buffer);
txCnt++;
for (int i = 0; i<numOutPoints;i++)
{
if(gtnetSktOutputType[i].equalsIgnoreCase("int"))
{
//byte[] src = GTNET_SKT_UDP.convertToByteArray(Integer.valueOf((String) GTNET_SKT_UDP.OPtable.getValueAt(i, 3)));
// Note: if input is sent as float we can just round and the return will always be an INT so no exception will be thrown as with above line of code
byte[] src = convertToByteArray(Math.round(Float.valueOf((String) OPtable.getValueAt(i, 3))));
outbuff.put(src, 0, 4);
}
if(gtnetSktOutputType[i].equalsIgnoreCase("float"))
{
byte[] src = convertToByteArray(Float.valueOf((String) OPtable.getValueAt(i, 3)));
outbuff.put(src, 0, 4);
}
}
buffer = outbuff.array();
DataOutputStream outToClient = new DataOutputStream(main_inst.connectionSocket.getOutputStream());
outToClient.write(buffer, 0, size);
echo(main_inst.connectionSocket.getRemoteSocketAddress()+" - Packets Sent ="+String.valueOf(txCnt));
} catch (UnknownHostException ex) {
ex.printStackTrace();
} catch (SocketException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
catch(Exception ex) {
System.out.print("Client Error! It didn't work!\n" + ex.getMessage());
}
}
});
btnAddOp.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e)
{
if(numOutPoints< GTNETSKT_MAX_INPUTS){
int i = numOutPoints++;
OutputTableModel a = (OutputTableModel) OPtable.getModel();
gtnetSktOutputName[i] = "POINT" + Integer.toString(i);
gtnetSktOutputType[i] = "float";
Object[] values = {i, gtnetSktOutputName[i], gtnetSktOutputType[i], Float.toString(i)};
a.insertData(values);