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