Skip to content
Snippets Groups Projects
Commit 51845dc6 authored by Terry D. Norbraten's avatar Terry D. Norbraten
Browse files

[Terry N.] use constants to avoid hardcoded arguments

parent 2ec0a252
No related branches found
No related tags found
No related merge requests found
...@@ -71,11 +71,10 @@ ...@@ -71,11 +71,10 @@
--> -->
<!-- TODO fix --> <target name="run.default" depends="init">
<target name="run.default">
<echo message="run.classpath=${run.classpath}"/> <echo message="run.classpath=${run.classpath}"/>
<java classname="edu.nps.moves.cot.dis.CotMsgReceiver" <java classname="${main.class}"
classpath=".;${run.classpath}"> classpath="${run.classpath}">
<arg line="--performanceTestSendCoT udp localhost 9999 1 1"/> <arg line="--performanceTestSendCoT udp localhost 9999 1 1"/>
<jvmarg value="-Djava.net.preferIPv4Stack=true"/> <jvmarg value="-Djava.net.preferIPv4Stack=true"/>
</java> </java>
......
...@@ -98,11 +98,11 @@ public class CoTMsgReceiver { ...@@ -98,11 +98,11 @@ public class CoTMsgReceiver {
System.out.println("\n\nStarting listener pool\n"); System.out.println("\n\nStarting listener pool\n");
if (isTcp) { if (isTcp) {
connectors.addTcpListener(9998); connectors.addTcpListener(CoTtcpListener.DEFAULT_COT_TCP_PORT);
connectors.startListener(9998); connectors.startListener(CoTtcpListener.DEFAULT_COT_TCP_PORT);
} else { } else {
connectors.addUdpListener(9999); // default packet size 1024 connectors.addUdpListener(CoTudpListener.DEFAULT_COT_UDP_PORT); // default packet size 1024
connectors.startListener(9999); connectors.startListener(CoTudpListener.DEFAULT_COT_UDP_PORT);
} }
PipedOutputStream out = null; PipedOutputStream out = null;
...@@ -122,14 +122,15 @@ public class CoTMsgReceiver { ...@@ -122,14 +122,15 @@ public class CoTMsgReceiver {
t.setDaemon(true); t.setDaemon(true);
t.start(); t.start();
sleep(5L); // TODO: can make this another input argument
sleep(5L); // default to 5 seconds of operation
System.out.println("\n\nStopping listener pool\n"); System.out.println("\n\nStopping listener pool\n");
if (isTcp) if (isTcp)
connectors.stopListener(9998); connectors.stopListener(CoTtcpListener.DEFAULT_COT_TCP_PORT);
else else
connectors.stopListener(9999); connectors.stopListener(CoTudpListener.DEFAULT_COT_UDP_PORT);
// Issue the CoT message generator/server quit/exit command // Issue the CoT message generator/server quit/exit command
try { try {
......
...@@ -88,7 +88,7 @@ public class CoTconnectors { ...@@ -88,7 +88,7 @@ public class CoTconnectors {
* @param debug a boolean indicating whether to print out debug statements * @param debug a boolean indicating whether to print out debug statements
*/ */
public void addUdpListener(int port, boolean debug) { public void addUdpListener(int port, boolean debug) {
addUdpListener(port, 1024, debug); addUdpListener(port, CoTudpListener.DEFAULT_PACKET_SIZE, debug);
}//addUdpListener }//addUdpListener
/** /**
...@@ -107,7 +107,7 @@ public class CoTconnectors { ...@@ -107,7 +107,7 @@ public class CoTconnectors {
cotTcpListener.addCoTparser(p); cotTcpListener.addCoTparser(p);
} }
cotListeners.put(port, cotTcpListener); cotListeners.put(port, cotTcpListener);
System.out.println("CoT TCP Listener created, port: " + cotTcpListener.getPort()); System.out.println(CoTtcpListener.TCP_LISTENER + " created, port: " + cotTcpListener.getPort());
} else { } else {
// Todo: handle existing listener // Todo: handle existing listener
System.out.println(port + " already exists, finish this method"); System.out.println(port + " already exists, finish this method");
...@@ -140,7 +140,7 @@ public class CoTconnectors { ...@@ -140,7 +140,7 @@ public class CoTconnectors {
cotUdpListener.addCoTparser(p); cotUdpListener.addCoTparser(p);
} }
cotListeners.put(port, cotUdpListener); cotListeners.put(port, cotUdpListener);
System.out.println("CoT UDP Listener created, port: " + cotUdpListener.getPort()); System.out.println(CoTudpListener.UDP_LISTENER + "created, port: " + cotUdpListener.getPort());
} else { } else {
// Todo: handle existing listener // Todo: handle existing listener
System.out.println(port + " already exists, finish this method"); System.out.println(port + " already exists, finish this method");
...@@ -192,12 +192,12 @@ public class CoTconnectors { ...@@ -192,12 +192,12 @@ public class CoTconnectors {
Object listener = cotListeners.get(port); Object listener = cotListeners.get(port);
switch (listener) { switch (listener) {
case CoTudpListener coTudpListener -> { case CoTudpListener coTudpListener -> {
System.out.println("Starting UDP listener, port: " + port); System.out.println("Starting " + CoTudpListener.UDP_LISTENER + ", port: " + port);
CoTudpListener udp = coTudpListener; CoTudpListener udp = coTudpListener;
udp.start(); udp.start();
} }
case CoTtcpListener coTtcpListener -> { case CoTtcpListener coTtcpListener -> {
System.out.println("Starting TCP listener, port: " + port); System.out.println("Starting " + CoTtcpListener.TCP_LISTENER + ", port: " + port);
CoTtcpListener tcp = coTtcpListener; CoTtcpListener tcp = coTtcpListener;
tcp.start(); tcp.start();
} }
...@@ -244,13 +244,13 @@ public class CoTconnectors { ...@@ -244,13 +244,13 @@ public class CoTconnectors {
Object listener = cotListeners.get(port); Object listener = cotListeners.get(port);
switch (listener) { switch (listener) {
case CoTudpListener coTudpListener -> { case CoTudpListener coTudpListener -> {
System.out.println("Stopping UDP listener, port: " + port); System.out.println("Stopping " + CoTudpListener.UDP_LISTENER + ", port: " + port);
CoTudpListener udp = coTudpListener; CoTudpListener udp = coTudpListener;
udp.stopThread(); udp.stopThread();
cotListeners.remove(port); cotListeners.remove(port);
} }
case CoTtcpListener coTtcpListener -> { case CoTtcpListener coTtcpListener -> {
System.out.println("Stopping TCP listener, port: " + port); System.out.println("Stopping " + CoTtcpListener.TCP_LISTENER + ", port: " + port);
CoTtcpListener tcp = coTtcpListener; CoTtcpListener tcp = coTtcpListener;
tcp.stopThread(); tcp.stopThread();
cotListeners.remove(port); cotListeners.remove(port);
......
...@@ -40,10 +40,11 @@ import java.util.logging.Logger; ...@@ -40,10 +40,11 @@ import java.util.logging.Logger;
*/ */
public class CoTtcpListener extends Thread { public class CoTtcpListener extends Thread {
private static final String TCP_LISTENER = "CoT TCP Listener"; public static final String TCP_LISTENER = "CoT TCP Listener";
public static final int DEFAULT_COT_TCP_PORT = 9998;
private ServerSocket cotSocket; private ServerSocket cotSocket;
private int cotPort = 9998; private int cotTcpPort;
private boolean debug = false; private boolean debug = false;
private volatile boolean runFlag = true; private volatile boolean runFlag = true;
private final List<CoTparser> cotParsers; private final List<CoTparser> cotParsers;
...@@ -54,7 +55,7 @@ public class CoTtcpListener extends Thread { ...@@ -54,7 +55,7 @@ public class CoTtcpListener extends Thread {
* Create a CoT TCP socket listener on the default port of 9998 * Create a CoT TCP socket listener on the default port of 9998
*/ */
public CoTtcpListener() { public CoTtcpListener() {
this(9998); this(DEFAULT_COT_TCP_PORT);
}//CotTcpListener }//CotTcpListener
/** /**
...@@ -74,11 +75,11 @@ public class CoTtcpListener extends Thread { ...@@ -74,11 +75,11 @@ public class CoTtcpListener extends Thread {
* @param debug a boolean indicating whether to print out debug statements * @param debug a boolean indicating whether to print out debug statements
*/ */
public CoTtcpListener(int port, boolean debug) { public CoTtcpListener(int port, boolean debug) {
cotPort = port; cotTcpPort = port;
this.debug = debug; this.debug = debug;
debugToConsole(TCP_LISTENER + " port: " + cotPort); debugToConsole(TCP_LISTENER + " port: " + cotTcpPort);
try { try {
cotSocket = new ServerSocket(cotPort); cotSocket = new ServerSocket(cotTcpPort);
} catch (IOException ex) { } catch (IOException ex) {
Logger.getLogger(CoTtcpListener.class.getName()).log(Level.SEVERE, null, ex); Logger.getLogger(CoTtcpListener.class.getName()).log(Level.SEVERE, null, ex);
} }
...@@ -108,7 +109,7 @@ public class CoTtcpListener extends Thread { ...@@ -108,7 +109,7 @@ public class CoTtcpListener extends Thread {
* not be restarted. The listener object must be reconstructed. * not be restarted. The listener object must be reconstructed.
*/ */
public void stopThread() { public void stopThread() {
debugToConsole(TCP_LISTENER + " received stop request, port: " + cotPort); debugToConsole(TCP_LISTENER + " received stop request, port: " + cotTcpPort);
this.runFlag = false; this.runFlag = false;
try { try {
cotSocket.close(); cotSocket.close();
...@@ -122,7 +123,7 @@ public class CoTtcpListener extends Thread { ...@@ -122,7 +123,7 @@ public class CoTtcpListener extends Thread {
* @return an integer representing the port the socket listener uses * @return an integer representing the port the socket listener uses
*/ */
public int getPort() { public int getPort() {
return this.cotPort; return this.cotTcpPort;
}//getPort }//getPort
@Override @Override
...@@ -130,7 +131,7 @@ public class CoTtcpListener extends Thread { ...@@ -130,7 +131,7 @@ public class CoTtcpListener extends Thread {
Socket accept = null; Socket accept = null;
String cotLine; String cotLine;
StringBuilder cotMessage = new StringBuilder(); StringBuilder cotMessage = new StringBuilder();
debugToConsole(TCP_LISTENER + " thread started, port: " + cotPort); debugToConsole(TCP_LISTENER + " thread started, port: " + cotTcpPort);
if (!this.customParsersSet) { if (!this.customParsersSet) {
cotParsers.add(new CoTparser()); // Create a default Cursor on Target parser cotParsers.add(new CoTparser()); // Create a default Cursor on Target parser
} }
...@@ -167,7 +168,7 @@ public class CoTtcpListener extends Thread { ...@@ -167,7 +168,7 @@ public class CoTtcpListener extends Thread {
} catch (IOException e) { } catch (IOException e) {
this.runFlag = false; this.runFlag = false;
System.err.println(e.getMessage() + "\n" + e.toString()); System.err.println(e.getMessage() + "\n" + e.toString());
System.err.println(TCP_LISTENER + " thread stopped, port: " + cotPort); System.err.println(TCP_LISTENER + " thread stopped, port: " + cotTcpPort);
} finally { } finally {
try { try {
if (accept != null) if (accept != null)
......
...@@ -39,9 +39,11 @@ import java.util.logging.Logger; ...@@ -39,9 +39,11 @@ import java.util.logging.Logger;
*/ */
public class CoTudpListener extends Thread { public class CoTudpListener extends Thread {
private static final String UDP_LISTENER = "CoT UDP Listener"; public static final String UDP_LISTENER = "CoT UDP Listener";
public static final int DEFAULT_COT_UDP_PORT = 9999;
private int cotUdpPort = 9999; public static final int DEFAULT_PACKET_SIZE = 1024;
private int cotUdpPort;
private boolean debug = false; private boolean debug = false;
private int packetSize = 1024; private int packetSize = 1024;
private volatile boolean runFlag = true; private volatile boolean runFlag = true;
...@@ -55,7 +57,7 @@ public class CoTudpListener extends Thread { ...@@ -55,7 +57,7 @@ public class CoTudpListener extends Thread {
*/ */
public CoTudpListener() { public CoTudpListener() {
// Constructor using default port of 9999 // Constructor using default port of 9999
this(9999); this(DEFAULT_COT_UDP_PORT);
}//CotUdpListener }//CotUdpListener
/** /**
...@@ -66,7 +68,7 @@ public class CoTudpListener extends Thread { ...@@ -66,7 +68,7 @@ public class CoTudpListener extends Thread {
*/ */
public CoTudpListener(int port) { public CoTudpListener(int port) {
// Constructor that sets a custom UDP listening port // Constructor that sets a custom UDP listening port
this(port, 1024); this(port, DEFAULT_PACKET_SIZE);
}//CotUdpListener(int port) }//CotUdpListener(int port)
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment