summaryrefslogtreecommitdiff
path: root/assignments/assignment2/Bounded Retransmission Protocol Tester/Source/basiclearner/SocketSUL.java
diff options
context:
space:
mode:
authorRené den Hertog2017-10-26 17:34:51 +0200
committerRené den Hertog2017-10-26 17:34:51 +0200
commitac914c719d401c44cb2633127952b022bf563ff3 (patch)
treef7d2399b2ab6cddb86aa996096581a397ff407b9 /assignments/assignment2/Bounded Retransmission Protocol Tester/Source/basiclearner/SocketSUL.java
parentFinish assignment 2, part 1 (diff)
'Bounded Retransmission Protocol' Blob
Diffstat (limited to 'assignments/assignment2/Bounded Retransmission Protocol Tester/Source/basiclearner/SocketSUL.java')
-rw-r--r--assignments/assignment2/Bounded Retransmission Protocol Tester/Source/basiclearner/SocketSUL.java80
1 files changed, 80 insertions, 0 deletions
diff --git a/assignments/assignment2/Bounded Retransmission Protocol Tester/Source/basiclearner/SocketSUL.java b/assignments/assignment2/Bounded Retransmission Protocol Tester/Source/basiclearner/SocketSUL.java
new file mode 100644
index 0000000..f7f0ed5
--- /dev/null
+++ b/assignments/assignment2/Bounded Retransmission Protocol Tester/Source/basiclearner/SocketSUL.java
@@ -0,0 +1,80 @@
+package basiclearner;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.UnknownHostException;
+
+import de.learnlib.api.SUL;
+import de.learnlib.api.SULException;
+
+/**
+ * Socket interface to connect to an SUT/test adapter over TCP.
+ *
+ * As an example, type into a unix terminal "nc -vl {ip} {port}" (where {ip} and
+ * {port} are the chosen values), and run this socketSUL. You can now control the
+ * SUL through the terminal.
+ * @author Ramon Janssen
+ */
+public class SocketSUL implements SUL<String, String>, AutoCloseable {
+ private final BufferedReader SULoutput;
+ private final PrintWriter SULinput;
+ private final Socket socket;
+ private final boolean extraNewLine;
+ private final String resetCmd;
+
+ /**
+ * Socket-interface for SUTs. Connects to a SUT (or test-adapter)
+ * @param ip the ip-address, for example InetAddress.getLoopbackAddress() for localhost
+ * @param port the tcp-port
+ * @param extraNewLine whether to print a newline after every input to the SUT
+ * @param resetCmd the command to send for resetting the SUT
+ * @throws UnknownHostException
+ * @throws IOException
+ */
+ public SocketSUL(InetAddress ip, int port, boolean extraNewLine, String resetCmd) throws UnknownHostException, IOException {
+ this.socket = new Socket(ip, port);
+ this.SULoutput = new BufferedReader(new InputStreamReader(socket.getInputStream()));
+ this.SULinput = new PrintWriter(socket.getOutputStream(), true);
+ this.extraNewLine = extraNewLine;
+ this.resetCmd = resetCmd;
+ }
+
+ @Override
+ public void post() {
+ if (extraNewLine) {
+ this.SULinput.write(this.resetCmd + System.lineSeparator());
+ } else {
+ this.SULinput.write(this.resetCmd);
+ }
+ this.SULinput.flush();
+ }
+
+ @Override
+ public void pre() {
+
+ }
+
+ @Override
+ public String step(String input) throws SULException {
+ if (extraNewLine) {
+ this.SULinput.write(input + System.lineSeparator());
+ } else {
+ this.SULinput.write(input);
+ }
+ this.SULinput.flush();
+ try {
+ return this.SULoutput.readLine();
+ } catch (IOException e) {
+ throw new SULException(e);
+ }
+ }
+
+ @Override
+ public void close() throws Exception {
+ this.socket.close();
+ }
+}