diff options
author | Julian T <julian@jtle.dk> | 2020-09-29 15:10:59 +0200 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2020-09-29 15:10:59 +0200 |
commit | 2764a232788e6d820ebe95419730a319eea1bd3c (patch) | |
tree | 7b331f2eb5a7db0fa40e67427117c3c79f1279df /sem5/oop/m7/src/PingMessage.java | |
parent | fef26c3cd47c1e6d3a9dc7592db5721b996ead6b (diff) |
Added java and net assignments
Diffstat (limited to 'sem5/oop/m7/src/PingMessage.java')
-rw-r--r-- | sem5/oop/m7/src/PingMessage.java | 46 |
1 files changed, 46 insertions, 0 deletions
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<String> 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); + } +} |