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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
package protocols.implementations;
import java.util.Vector;
import core.VSInternalProcess;
import core.VSTask;
import core.time.VSVectorTime;
import events.implementations.VSLamportTimestampEvent;
import events.implementations.VSTimestampMonitorEvent;
import events.implementations.VSTimestampTriggeredEvent;
import events.implementations.VSVectorTimestampEvent;
import events.implementations.VSVectorClockMonitor;
import protocols.VSAbstractProtocol;
/**
* Demonstration protocol showing how to use timestamp-triggered events.
* This protocol sets up various timestamp conditions and monitors them.
*
* Example scenarios:
* - Log message when Lamport time reaches 10
* - Change process color when vector clock element reaches threshold
* - Trigger protocol action at specific timestamp conditions
*
* @author Paul C. Buetow
*/
public class VSTimestampDemoProtocol extends VSAbstractProtocol {
public VSTimestampDemoProtocol() {
super(HAS_ON_SERVER_START);
}
private VSTimestampMonitorEvent lamportMonitor;
private VSVectorClockMonitor vectorMonitor;
@Override
public void onServerInit() {
initInteger("lamport.target", 10, "Target Lamport time", 1, 100, "");
initString("lamport.action", "Log message", "Action to perform");
Vector<Integer> defaultVector = new Vector<>();
defaultVector.add(5);
defaultVector.add(3);
defaultVector.add(7);
initVector("vector.target", defaultVector, "Target vector clock values");
initString("vector.condition", "GREATER_EQUAL", "Comparison operator (EQUAL, GREATER_THAN, LESS_THAN, GREATER_EQUAL, LESS_EQUAL)");
}
@Override
public void onClientInit() {
initInteger("lamport.target", 15, "Target Lamport time", 1, 100, "");
initString("lamport.action", "Highlight process", "Action to perform");
Vector<Integer> defaultVector = new Vector<>();
defaultVector.add(3);
defaultVector.add(8);
defaultVector.add(2);
initVector("vector.target", defaultVector, "Target vector clock values");
initString("vector.condition", "EQUAL", "Comparison operator");
}
@Override
public void onServerStart() {
setupTimestampEvents();
}
@Override
public void onClientStart() {
setupTimestampEvents();
}
/**
* Set up timestamp-triggered events based on configuration
*/
private void setupTimestampEvents() {
VSInternalProcess internalProcess = (VSInternalProcess) process;
// Create monitors for timestamp events
lamportMonitor = new VSTimestampMonitorEvent(1); // Check every time unit
lamportMonitor.init(internalProcess);
vectorMonitor = new VSVectorClockMonitor(internalProcess);
// Set up Lamport timestamp event
setupLamportEvent();
// Set up Vector timestamp event
setupVectorEvent();
// Start Lamport monitoring by scheduling the monitor
VSTask monitorTask = new VSTask(internalProcess.getTime() + 1,
internalProcess, lamportMonitor, VSTask.LOCAL);
internalProcess.getSimulatorCanvas().getTaskManager().addTask(monitorTask);
// Vector monitoring will be triggered by clock changes, not time
log("Timestamp demo protocol started - monitoring Lamport and Vector timestamp events");
}
/**
* Configure Lamport timestamp event
*/
private void setupLamportEvent() {
long targetLamport = getLong("lamport.target");
String action = getString("lamport.action");
VSLamportTimestampEvent lamportEvent = new VSLamportTimestampEvent(
targetLamport,
VSTimestampTriggeredEvent.ComparisonOperator.GREATER_EQUAL,
action + " (Lamport >= " + targetLamport + ")",
() -> {
// Custom action when Lamport condition is met
log("Lamport timestamp condition met! Executing: " + action);
if (action.contains("Highlight") || action.contains("highlight")) {
((VSInternalProcess) process).highlightOn();
}
}
);
lamportMonitor.addLamportEvent(lamportEvent);
}
/**
* Configure Vector timestamp event
*/
private void setupVectorEvent() {
Vector<Integer> targetVectorInts = getVector("vector.target");
String conditionStr = getString("vector.condition");
// Convert Vector<Integer> to VSVectorTime
VSVectorTime targetVector = new VSVectorTime(0);
for (Integer val : targetVectorInts) {
targetVector.add(val.longValue());
}
// Parse condition string
VSTimestampTriggeredEvent.ComparisonOperator operator;
try {
operator = VSTimestampTriggeredEvent.ComparisonOperator.valueOf(conditionStr);
} catch (IllegalArgumentException e) {
operator = VSTimestampTriggeredEvent.ComparisonOperator.GREATER_EQUAL;
log("Invalid condition '" + conditionStr + "', using GREATER_EQUAL");
}
VSVectorTimestampEvent vectorEvent = new VSVectorTimestampEvent(
targetVector,
operator,
"Vector clock condition: " + targetVector + " " + operator,
() -> {
// Custom action when Vector condition is met
log("Vector timestamp condition met! Current: " +
((VSInternalProcess) process).getVectorTime());
((VSInternalProcess) process).highlightOn();
}
);
vectorMonitor.addVectorEvent(vectorEvent);
}
@Override
public void onServerRecv(core.VSMessage message) {
// Could add timestamp events based on received messages
log("Server received message - current timestamps: L=" +
((VSInternalProcess) process).getLamportTime() +
", V=" + ((VSInternalProcess) process).getVectorTime());
}
@Override
public void onClientRecv(core.VSMessage message) {
// Could add timestamp events based on received messages
log("Client received message - current timestamps: L=" +
((VSInternalProcess) process).getLamportTime() +
", V=" + ((VSInternalProcess) process).getVectorTime());
}
@Override
public String toString() {
return " [TimestampDemo]";
}
@Override
public void onServerReset() {
if (lamportMonitor != null) {
lamportMonitor.stopMonitoring();
}
if (vectorMonitor != null) {
vectorMonitor.clearVectorEvents();
}
}
@Override
public void onClientReset() {
if (lamportMonitor != null) {
lamportMonitor.stopMonitoring();
}
if (vectorMonitor != null) {
vectorMonitor.clearVectorEvents();
}
}
@Override
public void onServerSchedule() {
// No scheduled operations needed
}
@Override
public void onClientSchedule() {
// No scheduled operations needed
}
@Override
protected String createShortname(String savedShortname) {
return "TimestampDemo";
}
}
|