aboutsummaryrefslogtreecommitdiff
path: root/sem5/oop/m7/src/PingMessage.java
diff options
context:
space:
mode:
Diffstat (limited to 'sem5/oop/m7/src/PingMessage.java')
-rw-r--r--sem5/oop/m7/src/PingMessage.java46
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);
+ }
+}