forked from RoboCup-SPL/GameController
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathSPLCoachMessage.java
95 lines (83 loc) · 3.53 KB
/
SPLCoachMessage.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
package data.spl;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import controller.EventHandler;
import controller.SystemClock;
public class SPLCoachMessage implements Serializable
{
private static final long serialVersionUID = -1586200167710301614L;
/** Some constants from the C-structure. */
public static final int SPL_COACH_MESSAGE_PORT = 3839;
public static final String SPL_COACH_MESSAGE_STRUCT_HEADER = "SPLC";
public static final int SPL_COACH_MESSAGE_STRUCT_VERSION = 4;
public static final int SPL_COACH_MESSAGE_SIZE = 253;
public static final long SPL_COACH_MESSAGE_RECEIVE_INTERVALL = 1000; // in ms
public static final long SPL_COACH_MESSAGE_MIN_SEND_INTERVALL = 0; // in ms
public static final long SPL_COACH_MESSAGE_MAX_SEND_INTERVALL = 0; // in ms
public static final int SIZE = 4 // header size
+ 1 // byte for the version
+ 1 // team number
+ 1 // sequence number
+ SPL_COACH_MESSAGE_SIZE;
public String header; // header to identify the structure
public byte version; // version of the data structure
public byte team; // unique team number
public byte sequence; // sequence number of this message (set by the coach)
public byte[] message; // what the coach says
private long sendTime; // delay in ms that the message will be held back
public SPLCoachMessage()
{
sendTime = generateSendIntervallForSPLCoachMessage() + SystemClock.getInstance().getCurrentTimeMillis();
}
public byte[] toByteArray()
{
ByteBuffer buffer = ByteBuffer.allocate(SIZE);
buffer.order(ByteOrder.LITTLE_ENDIAN);
buffer.put(header.getBytes());
buffer.put(version);
buffer.put(team);
buffer.put(sequence);
buffer.put(message);
return buffer.array();
}
public boolean fromByteArray(ByteBuffer buffer)
{
try {
buffer.order(ByteOrder.LITTLE_ENDIAN);
byte[] header = new byte[4];
buffer.get(header);
this.header = new String(header);
if (!this.header.equals(SPL_COACH_MESSAGE_STRUCT_HEADER)) {
return false;
} else {
version = buffer.get();
if (version != SPL_COACH_MESSAGE_STRUCT_VERSION) {
return false;
} else {
team = buffer.get();
if ((team != EventHandler.getInstance().data.team[0].teamNumber) && (team != EventHandler.getInstance().data.team[1].teamNumber)) {
return false;
}
sequence = buffer.get();
message = new byte[SPLCoachMessage.SPL_COACH_MESSAGE_SIZE];
buffer.get(message);
return true;
}
}
} catch (RuntimeException e) {
return false;
}
}
public long getRemainingTimeToSend()
{
long remainingTime = sendTime - SystemClock.getInstance().getCurrentTimeMillis();
return remainingTime > 0 ? remainingTime : 0;
}
private long generateSendIntervallForSPLCoachMessage()
{
return (long) (Math.random() * (SPLCoachMessage.SPL_COACH_MESSAGE_MAX_SEND_INTERVALL
- SPLCoachMessage.SPL_COACH_MESSAGE_MIN_SEND_INTERVALL))
+ SPLCoachMessage.SPL_COACH_MESSAGE_MIN_SEND_INTERVALL;
}
}