mirror of
https://github.com/PAMGuard/PAMGuard.git
synced 2024-11-26 00:52:29 +00:00
UDP Control
Added multiport functionality
This commit is contained in:
parent
9760c6bfc5
commit
a16d1d6eef
@ -55,6 +55,7 @@ import generalDatabase.DBControlUnit;
|
|||||||
import javafx.application.Platform;
|
import javafx.application.Platform;
|
||||||
import javafx.stage.Stage;
|
import javafx.stage.Stage;
|
||||||
import Array.ArrayManager;
|
import Array.ArrayManager;
|
||||||
|
import PamController.command.MultiportController;
|
||||||
import PamController.command.NetworkController;
|
import PamController.command.NetworkController;
|
||||||
import PamController.command.TerminalController;
|
import PamController.command.TerminalController;
|
||||||
import PamController.command.WatchdogComms;
|
import PamController.command.WatchdogComms;
|
||||||
@ -255,6 +256,9 @@ public class PamController implements PamControllerInterface, PamSettings {
|
|||||||
if (pamBuoyGlobals.getNetworkControlPort() != null) {
|
if (pamBuoyGlobals.getNetworkControlPort() != null) {
|
||||||
networkController = new NetworkController(this);
|
networkController = new NetworkController(this);
|
||||||
}
|
}
|
||||||
|
if (pamBuoyGlobals.getMultiportAddress() != null) {
|
||||||
|
new MultiportController(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// binaryStore = new BinaryStore(this);
|
// binaryStore = new BinaryStore(this);
|
||||||
|
118
src/PamController/command/MultiportController.java
Normal file
118
src/PamController/command/MultiportController.java
Normal file
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -14,7 +14,9 @@ public class pamBuoyGlobals {
|
|||||||
// public static Integer useGstreamer = 0;
|
// public static Integer useGstreamer = 0;
|
||||||
// public static boolean useNetworkCont = false;
|
// public static boolean useNetworkCont = false;
|
||||||
private static Integer networkControlPort = null;
|
private static Integer networkControlPort = null;
|
||||||
|
private static String multiportAddress;
|
||||||
// private static boolean useDSP = false;
|
// private static boolean useDSP = false;
|
||||||
|
private static int muliportPort;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the networkControlPort
|
* @return the networkControlPort
|
||||||
@ -29,6 +31,31 @@ public class pamBuoyGlobals {
|
|||||||
public static void setNetworkControlPort(Integer networkControlPort) {
|
public static void setNetworkControlPort(Integer networkControlPort) {
|
||||||
pamBuoyGlobals.networkControlPort = 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);
|
//public static HashMap pbHash = new HashMap(10,0.75);
|
||||||
|
@ -24,10 +24,23 @@ public class GlobalArguments {
|
|||||||
/**
|
/**
|
||||||
* Get a global parameter value
|
* Get a global parameter value
|
||||||
* @param name value name
|
* @param name value name
|
||||||
* @return value
|
* @return value in original String format
|
||||||
*/
|
*/
|
||||||
public static String getParam(String name) {
|
public static String getParam(String name) {
|
||||||
return globalFlags.get(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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -212,8 +212,15 @@ public class Pamguard {
|
|||||||
System.out.println("Running using settings from " + autoPsf);
|
System.out.println("Running using settings from " + autoPsf);
|
||||||
}
|
}
|
||||||
else if (anArg.equalsIgnoreCase("-port")) {
|
else if (anArg.equalsIgnoreCase("-port")) {
|
||||||
|
// port id to open a udp port to receive commands
|
||||||
pamBuoyGlobals.setNetworkControlPort(Integer.parseInt(args[iArg++]));
|
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")) {
|
else if (anArg.equalsIgnoreCase("-nolog")) {
|
||||||
System.out.println("Disabling log file from command line switch...");
|
System.out.println("Disabling log file from command line switch...");
|
||||||
ProxyPrintStream.disableLogFile();
|
ProxyPrintStream.disableLogFile();
|
||||||
|
Loading…
Reference in New Issue
Block a user