From 2764a232788e6d820ebe95419730a319eea1bd3c Mon Sep 17 00:00:00 2001 From: Julian T Date: Tue, 29 Sep 2020 15:10:59 +0200 Subject: Added java and net assignments --- sem5/oop/m7/src/PingMessage.java | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 sem5/oop/m7/src/PingMessage.java (limited to 'sem5/oop/m7/src/PingMessage.java') diff --git a/sem5/oop/m7/src/PingMessage.java b/sem5/oop/m7/src/PingMessage.java new file mode 100644 index 0000000..39a9386 --- /dev/null +++ b/sem5/oop/m7/src/PingMessage.java @@ -0,0 +1,46 @@ +import java.io.Serializable; +import java.util.ArrayList; + +public class PingMessage extends Message implements Serializable { + private boolean is_response; + private boolean do_trace; + public ArrayList trace; + + public PingMessage(String from, String to, boolean trace) { + super(from, to); + this.is_response = false; + this.do_trace = trace; + if (trace) { + this.trace = new ArrayList<>(); + } + } + + public void trace(String node) { + if (!this.do_trace || this.is_response) { + return; + } + System.out.println("DOING TRACE"); + + this.trace.add(node); + } + + public void handle(Node node) throws Exception { + if (this.is_response) { + System.out.printf("%s >> Ping response%n", this.from); + if (this.do_trace) { + System.out.print(" Trace: "); + for (String t : this.trace) { + System.out.printf("%s -> ", t); + } + System.out.print(System.lineSeparator()); + } + return; + } + + this.is_response = true; + this.to = this.from; + this.from = node.name; + + node.sendMsgLocal(this); + } +} -- cgit v1.2.3