-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestFederate.java
276 lines (224 loc) · 10.4 KB
/
TestFederate.java
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* NOTE: The TestFederate.java file was modified from the original TestFederate.java file (/ucef-gateway/test-federation/GatewayTest_generated/GatewayTest-java-federates/GatewayTest-impl-java/TestFederate/src/main/java/GatewayTest) that was downloaded as part of the ucef-gateway repository.
The ucef-gateway repository was cloned from here: https://github.com/usnistgov/ucef-gateway.git
The code on the feature/refactor branch of the ucef-gateway repository was used.
The License of the ucef-gateway repository has been copied in the "Third Party License" directory of this repo (file name is "LICENSE").
*/
/*
Copyright (c) 2017-2018,
Alliance for Sustainable Energy, LLC
All rights reserved.
*/
package GatewayTest;
import org.cpswt.config.FederateConfig;
import org.cpswt.config.FederateConfigParser;
import org.cpswt.hla.base.ObjectReflector;
import org.cpswt.hla.ObjectRoot;
import org.cpswt.hla.InteractionRoot;
import org.cpswt.hla.base.AdvanceTimeRequest;
import java.util.concurrent.ThreadLocalRandom;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
* The TestFederate type of federate for the federation designed in WebGME.
*
*/
public class TestFederate extends TestFederateBase {
private final static Logger log = LogManager.getLogger(TestFederate.class);
private static final String VALID_CHARACTERS = "abcdefghijklmnopqrstuvwxyz0123456789";
double currentTime = 0;
TestObject testObjectInstance = new TestObject();
boolean booleanValue;
double doubleValue;
int intValue;
int sequenceNumber = 0;
int lastInteraction = 0;
int lastObjectUpdate = 0;
String stringValue;
public TestFederate(FederateConfig params) throws Exception {
super(params);
/* HELICS-HLA Modifications Begin */
testObjectInstance.registerObject(getLRC(),"HelicsHlaObject");
/* HELICS-HLA Modifications End */
}
private void CheckReceivedSubscriptions(String s) {
InteractionRoot interaction = null;
while ((interaction = getNextInteractionNoWait()) != null) {
if (interaction instanceof TestInteraction) {
handleInteractionClass((TestInteraction) interaction);
}
}
ObjectReflector reflector = null;
while ((reflector = getNextObjectReflectorNoWait()) != null) {
reflector.reflect();
ObjectRoot object = reflector.getObjectRoot();
/* HELICS-HLA Modifications Begin */
//if (object instanceof TestObject) {
// handleObjectClass((TestObject) object);
if (object instanceof HelicsPublications) {
handleObjectClass((HelicsPublications) object);
/* HELICS-HLA Modifications End */
}
}
}
private void execute() throws Exception {
if(super.isLateJoiner()) {
currentTime = super.getLBTS() - super.getLookAhead();
super.disableTimeRegulation();
}
AdvanceTimeRequest atr = new AdvanceTimeRequest(currentTime);
putAdvanceTimeRequest(atr);
// check if parameter handles match for child and parent interactions
int interactionRoot = getLRC().getInteractionClassHandle("InteractionRoot.C2WInteractionRoot");
int federateJoin = getLRC().getInteractionClassHandle(
"InteractionRoot.C2WInteractionRoot.FederateJoinInteraction");
int sourceFedR = getLRC().getParameterHandle("sourceFed", interactionRoot);
int sourceFedJ = getLRC().getParameterHandle("sourceFed", federateJoin);
log.debug(String.format("C2WInteractionRoot.sourceFed=%d : FederateJoin.sourceFed=%d", sourceFedR, sourceFedJ));
if(!super.isLateJoiner()) {
readyToPopulate();
}
if(!super.isLateJoiner()) {
readyToRun();
}
startAdvanceTimeThread();
// this is the exit condition of the following while loop
// it is used to break the loop so that latejoiner federates can
// notify the federation manager that they left the federation
boolean exitCondition = false;
while (true) {
atr.requestSyncStart();
enteredTimeGrantedState();
log.info("t = " + currentTime);
CheckReceivedSubscriptions("Main Loop");
/* HELICS-HLA Modifications Begin */
//if (lastInteraction == sequenceNumber) { // && lastObjectUpdate == sequenceNumber) {
sendNextValues();
//}
/* HELICS-HLA Modifications End */
currentTime += super.getStepSize();
AdvanceTimeRequest newATR = new AdvanceTimeRequest(currentTime);
putAdvanceTimeRequest(newATR);
atr.requestSyncEnd();
atr = newATR;
if(exitCondition) {
break;
}
}
// while loop finished, notify FederationManager about resign
super.notifyFederationOfResign();
}
private void handleInteractionClass(TestInteraction interaction) {
log.info("received TestInteraction");
checkSequenceNumber(interaction.get_sequenceNumber());
checkBooleanValue(interaction.get_booleanValue());
checkDoubleValue(interaction.get_doubleValue());
checkIntValue(interaction.get_intValue());
checkStringValue(interaction.get_stringValue());
lastInteraction = interaction.get_sequenceNumber();
// see what happens to the value when the parameter handles don't match
log.debug("sourceFed received as " + interaction.get_sourceFed());
}
/* HELICS-HLA Modifications Begin */
/*
private void handleObjectClass(TestObject object) {
log.info("received TestObject reflection");
checkSequenceNumber(object.get_sequenceNumber());
checkBooleanValue(object.get_booleanValue());
checkDoubleValue(object.get_doubleValue());
checkIntValue(object.get_intValue());
checkStringValue(object.get_stringValue());
lastObjectUpdate = object.get_sequenceNumber();
}*/
private void handleObjectClass(HelicsPublications object) {
log.info("received HelicsPublications reflection \n");
log.info("\treceived double from HELICS at time " + currentTime + " seconds is: "+ object.get_doubleValue()+"\n");
log.info("\treceived string from HELICS at time " + currentTime + " seconds is: "+ object.get_stringValue()+"\n");
//checkDoubleValue(object.get_doubleValue());
//checkStringValue(object.get_stringValue());
}
/* HELICS-HLA Modifications End */
private String generateStringValue() {
StringBuffer buffer = new StringBuffer(64);
for (int i = 0; i < 64; i++) {
buffer.append(VALID_CHARACTERS.charAt(ThreadLocalRandom.current().nextInt(VALID_CHARACTERS.length())));
}
return buffer.toString();
}
private void sendNextValues()
throws Exception {
booleanValue = ThreadLocalRandom.current().nextBoolean();
doubleValue = ThreadLocalRandom.current().nextDouble(1000);
intValue = ThreadLocalRandom.current().nextInt(10000);
stringValue = generateStringValue();
sequenceNumber = sequenceNumber + 1;
TestInteraction testInteraction = create_TestInteraction();
testInteraction.set_sequenceNumber(sequenceNumber);
testInteraction.set_booleanValue(booleanValue);
testInteraction.set_doubleValue(doubleValue);
testInteraction.set_intValue(intValue);
testInteraction.set_stringValue(stringValue);
testInteraction.sendInteraction(getLRC(), currentTime + super.getLookAhead());
log.info("sent " + testInteraction.toString());
testObjectInstance.set_sequenceNumber(sequenceNumber);
testObjectInstance.set_booleanValue(booleanValue);
testObjectInstance.set_doubleValue(doubleValue);
testObjectInstance.set_intValue(intValue);
testObjectInstance.set_stringValue(stringValue);
testObjectInstance.updateAttributeValues(getLRC(), currentTime + super.getLookAhead());
log.info("sent " + testObjectInstance.toString());
}
private void checkBooleanValue(boolean value) {
log.info("\treceived boolean " + value);
log.info("\texpected boolean " + booleanValue);
if (value != booleanValue) {
log.error("FAILED - boolean value incorrect");
//throw new RuntimeException("boolean value incorrect");
}
}
private void checkDoubleValue(double value) {
log.info("\treceived double " + value);
log.info("\texpected double " + doubleValue);
if (value != doubleValue) {
log.error("FAILED - double value incorrect");
//throw new RuntimeException("double value incorrect");
}
}
private void checkIntValue(int value) {
log.info("\treceived int " + value);
log.info("\texpected int " + intValue);
if (value != intValue) {
log.error("FAILED - int value incorrect");
//throw new RuntimeException("int value incorrect");
}
}
private void checkStringValue(String value) {
log.info(String.format("\treceived string %s (%d)", value, value.length()));
log.info(String.format("\texpected string %s (%d)", stringValue, stringValue.length()));
if (!value.equals(stringValue)) {
log.error("FAILED - string value incorrect");
//throw new RuntimeException("string value incorrect");
}
}
private void checkSequenceNumber(int value) {
log.info("\treceived sequence number " + value);
log.info("\texpected sequence number " + sequenceNumber);
if (value != sequenceNumber) {
log.error("FAILED - sequence number incorrect");
//throw new RuntimeException("sequence number incorrect");
}
}
public static void main(String[] args) {
log.info(args);
try {
FederateConfigParser federateConfigParser = new FederateConfigParser();
FederateConfig federateConfig = federateConfigParser.parseArgs(args, FederateConfig.class);
TestFederate federate = new TestFederate(federateConfig);
federate.execute();
System.exit(0);
} catch (Exception e) {
log.error("There was a problem executing the TestFederate federate: {}", e.getMessage());
log.error(e);
System.exit(1);
}
}
}