diff --git a/src/PamController/PamController.java b/src/PamController/PamController.java index 227dbdaf..611dd9a2 100644 --- a/src/PamController/PamController.java +++ b/src/PamController/PamController.java @@ -55,6 +55,7 @@ import generalDatabase.DBControlUnit; import javafx.application.Platform; import javafx.stage.Stage; import Array.ArrayManager; +import PamController.command.MultiportController; import PamController.command.NetworkController; import PamController.command.TerminalController; import PamController.command.WatchdogComms; @@ -255,6 +256,9 @@ public class PamController implements PamControllerInterface, PamSettings { if (pamBuoyGlobals.getNetworkControlPort() != null) { networkController = new NetworkController(this); } + if (pamBuoyGlobals.getMultiportAddress() != null) { + new MultiportController(this); + } // binaryStore = new BinaryStore(this); diff --git a/src/PamController/command/MultiportController.java b/src/PamController/command/MultiportController.java new file mode 100644 index 00000000..ead9e397 --- /dev/null +++ b/src/PamController/command/MultiportController.java @@ -0,0 +1,118 @@ +package PamController.command; + +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.MulticastSocket; +import java.net.NetworkInterface; +import java.net.SocketAddress; +import java.nio.charset.StandardCharsets; + +import PamController.PamController; +import PamController.pamBuoyGlobals; + +public class MultiportController extends CommandManager { + + //The multicast addresses are in the range 224.0.0.0 through 239.255.255.255 + + private static String unitName = "Multiport Controller"; + private PamController pamController; + private String mAddress; + private int mPort; + private MulticastSocket socket; + InetAddress inetAddr; + + static private final int MAX_COMMAND_LENGTH = 4096; + + private byte[] byteBuffer = new byte[MAX_COMMAND_LENGTH]; + private DatagramPacket lastDatagram; + + public MultiportController(PamController pamController) { + super(pamController, unitName); + this.pamController = pamController; + this.mAddress = pamBuoyGlobals.getMultiportAddress(); + this.mPort = pamBuoyGlobals.getMuliportPort(); + + Thread t = new Thread(new ListenerThread()); + t.start(); + } + + public void runListenerLoop() { + try { + // inetAddr = InetAddress.getByName(mAddress); + // SocketAddress sockAddr = new SocketAddress() + // socket = new MulticastSocket(mPort); + // socket.joinGroup(inetAddr, null); + // socket.joinG + + // open port + InetAddress mcastaddr = InetAddress.getByName(mAddress); + InetSocketAddress group = new InetSocketAddress(mcastaddr, mPort); + NetworkInterface netIf = NetworkInterface.getByName("bge0"); + socket = new MulticastSocket(mPort); + socket.joinGroup(group, netIf); + socket.setSoTimeout(0); + + System.out.printf("Waiting for multicast messages at %s port %d\n", mAddress, mPort); + + // sit in loop + while (true) { + try { + DatagramPacket datagram = new DatagramPacket(byteBuffer, MAX_COMMAND_LENGTH); + socket.receive(datagram); + processDatagram(datagram); + } + catch (IOException ioE) { + ioE.printStackTrace(); + break; + } + } + + // close port. + + socket.leaveGroup(group, netIf); + } + catch(Exception e) + { + e.printStackTrace(); + } + } + + private void processDatagram(DatagramPacket datagram) { + lastDatagram = datagram; + String str = new String(datagram.getData(), 0, datagram.getLength()); +// str = str.substring(0, datagram.getLength()); + System.out.println("Datagram received \"" + str + "\""); + interpretCommand(str); + } + + @Override + public boolean sendData(String dataString) { + DatagramPacket senderInfo = lastDatagram; + System.out.printf("Send back data \"%s\" to %s port %d\n", dataString, senderInfo.getAddress(), senderInfo.getPort()); +// dataString += "\n"; + DatagramPacket packet = new DatagramPacket(dataString.getBytes(), dataString.length()); + packet.setAddress(senderInfo.getAddress()); + packet.setPort(senderInfo.getPort()); + try { + socket.send(packet); +// receiveSocket. + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + return false; + } + return true; + } + + private class ListenerThread implements Runnable { + + @Override + public void run() { + runListenerLoop(); + } + + } +} diff --git a/src/PamController/pamBuoyGlobals.java b/src/PamController/pamBuoyGlobals.java index 26cc5a90..dee3a1db 100644 --- a/src/PamController/pamBuoyGlobals.java +++ b/src/PamController/pamBuoyGlobals.java @@ -14,7 +14,9 @@ public class pamBuoyGlobals { // public static Integer useGstreamer = 0; // public static boolean useNetworkCont = false; private static Integer networkControlPort = null; +private static String multiportAddress; // private static boolean useDSP = false; +private static int muliportPort; /** * @return the networkControlPort @@ -29,6 +31,31 @@ public class pamBuoyGlobals { public static void setNetworkControlPort(Integer networkControlPort) { pamBuoyGlobals.networkControlPort = networkControlPort; } + + /** + * Set parameters for mulitport configutation. + * @param mAddr + * @param mPort + */ + public static void setMultiportConfig(String mAddr, int mPort) { + multiportAddress = mAddr; + muliportPort = mPort; + + } + + /** + * @return the multiportAddress + */ + public static String getMultiportAddress() { + return multiportAddress; + } + + /** + * @return the muliportPort + */ + public static int getMuliportPort() { + return muliportPort; + } //public static HashMap pbHash = new HashMap(10,0.75); diff --git a/src/pamguard/GlobalArguments.java b/src/pamguard/GlobalArguments.java index c8c6599a..4f601911 100644 --- a/src/pamguard/GlobalArguments.java +++ b/src/pamguard/GlobalArguments.java @@ -24,10 +24,23 @@ public class GlobalArguments { /** * Get a global parameter value * @param name value name - * @return value + * @return value in original String format */ public static String getParam(String name) { return globalFlags.get(name); } + /** + * Get a param read as an integer + * @param name + * @return value as integer or null if not set. Throws exception if invalid integer. + */ + public static Integer getParamI(String name) { + String val = getParam(name); + if (val == null) { + return null; + } + return Integer.valueOf(val); + } + } diff --git a/src/pamguard/Pamguard.java b/src/pamguard/Pamguard.java index f1f7aaa1..c955192a 100644 --- a/src/pamguard/Pamguard.java +++ b/src/pamguard/Pamguard.java @@ -212,8 +212,15 @@ public class Pamguard { System.out.println("Running using settings from " + autoPsf); } else if (anArg.equalsIgnoreCase("-port")) { + // port id to open a udp port to receive commands pamBuoyGlobals.setNetworkControlPort(Integer.parseInt(args[iArg++])); } + else if (anArg.equalsIgnoreCase("-mport")) { + // multicast control (for multiple PAMGuards) + String mAddr = args[iArg++]; + int mPort = Integer.parseInt(args[iArg++]); + pamBuoyGlobals.setMultiportConfig(mAddr, mPort); + } else if (anArg.equalsIgnoreCase("-nolog")) { System.out.println("Disabling log file from command line switch..."); ProxyPrintStream.disableLogFile();