-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.java
More file actions
78 lines (62 loc) · 2.4 KB
/
Message.java
File metadata and controls
78 lines (62 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* event class called Message that extends Event
* implement first the abstract methods from Event and later the additional methods from instructions
* also make sure it is compatible with the methods in Host!! */
public class Message extends Event{
public static final String PING_RESPONSE = "ping response";
public static final String PING_REQUEST = "ping request";
private String stringMessage;
private int srcAddress;
private int destAddress;
private Host nextHop;
private int distance;
public Message(String stringMessage, int srcAddress, int destAddress){
this.stringMessage = stringMessage;
this.srcAddress = srcAddress;
this.destAddress = destAddress;
this.nextHop = null; // next hop initially set to null
this.distance = 0;
}
public String getMessage(){
return stringMessage;
}
public int getSrcAddress(){
return srcAddress;
}
public int getDestAddress(){
return destAddress;
}
public void setNextHop(Host nextHop, int distance){
this.nextHop = nextHop;
// set insertion time based on distance (1 distance = 1 simualtion time)
setInsertionTime(getInsertionTime() + distance);
}
@Override
public void setInsertionTime(int currentTime) {
this.insertionTime = currentTime;
}
@Override
public void cancel() {
// messages cant be cancelled so u can leave alone
}
@Override
public void handle() {
// check if the current time is accessible from the Host object
if(nextHop != null && nextHop instanceof Host){
int currentTime = ((Host) nextHop).getCurrentTime();
if(stringMessage.equals("ping request")){
// process ping request message
// like send a ping response back to source
Message pingResponse = new Message("ping response", destAddress, srcAddress);
pingResponse.setNextHop(this.nextHop, distance);
nextHop.sendToNeighbor(pingResponse);
} else if(stringMessage.equals("ping response")){
// process ping response message
// compute RTT
int rtt = currentTime - insertionTime;
System.out.println("RTT for message from Host " + srcAddress + " to Host " + destAddress + ": " + rtt);
}
} else {
System.out.println("Error: could not access current simulation time");
}
}
}