Newer
Older
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
/**
* GeoNetworking port implementation (background thread)
*
* @author ETSI / STF424
* @version $URL$
* $Id$
*
*/
package org.etsi.its.adapter.ports;
import java.util.HashMap;
import java.util.Map;
import org.etsi.adapter.TERFactory;
import org.etsi.common.ByteHelper;
import org.etsi.its.adapter.layers.EthernetLayer;
import org.etsi.its.adapter.layers.IEthernetSpecific;
import org.etsi.ttcn.tci.CharstringValue;
/**
* GeoNetworking port implementation (background thread)
*/
public class GnPort extends ProtocolPort implements Runnable, IEthernetSpecific {
/**
* GeoNetworking header type for beacon messages
*/
private static final int HT_BEACON = 1;
private static final int HT_TSB = 5;
private static final int HST_SHB = 0;
/**
* Constructor
* @param portName Name of the port
* @param componentName Name of the component owning this port instance
* @param lowerStackDesc Description of the port's lower stack in the form "Layer/Layer/Layer/..."
* @param linkLayerAddress Link-layer address to be used by this port as source address (null if not applicable)
*/
public GnPort(String portName, String componentName, String lowerStackDesc, String linkLayerAddress) {
super(portName, componentName, lowerStackDesc, linkLayerAddress);
running = true;
management.registerGnPort(this);
}
/* (non-Javadoc)
* @see org.etsi.its.adapter.ports.IEthernetSpecific#getEthernetType()
*/
@Override
public short getEthernetType() {
// Retrieve EthernetType value
Integer iutEthernetTypeValue = Integer.decode(((CharstringValue)TERFactory.getInstance().getTaParameter("IutEthernetTypeValue")).getString());
return iutEthernetTypeValue.shortValue();
}
/**
* Starts beaconning
*/
public void startBeaconning() {
running = true;
if(beaconThread == null) {
beaconThread = new Thread(this);
beaconThread.start();
}
}
/**
* Stops beaconning
*/
public void stopBeaconning() {
if(beaconThread != null) {
running = false;
try {
beaconThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
beaconThread = null;
}
}
/**
* Thread function for sending periodic beacons
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
byte[] beaconHeader = management.getGnBeacon();
if(beaconHeader != null) {
long modulo = (long)Math.pow(2,32);
Map<String, Object> params = new HashMap<String, Object>();
while(running) {
// Update timestamp. Timestamp is 1s older than current time to avoid sending beacons coming from the future (time sync between nodes)
byte[] tst = ByteHelper.intToByteArray((int)(((System.currentTimeMillis() - 1072915200000L) % modulo) - 3000), 4);
System.arraycopy(tst, 0, beaconHeader, 20, 4);
send(beaconHeader, params);
try {
Thread.sleep(management.getGnBeaconInterval());
} catch (InterruptedException e) {
// Do nothing, we do not care
}
}
}
}
/* (non-Javadoc)
* @see org.etsi.its.adapter.ports.IPort#send(byte[])
*/
@Override
public boolean send(byte[] message) {
HashMap<String, Object> params = new HashMap<String, Object>();
byte[] destMacAddress = ByteHelper.extract(message, message.length - 6, 6);
message = ByteHelper.extract(message, 0, message.length - 6);
params.put(EthernetLayer.LINK_LAYER_DESTINATION, destMacAddress);
ByteHelper.dump("GnPort.send", message);
return send(message, params);
}
/* (non-Javadoc)
* @see org.etsi.its.adapter.ports.ProtocolPort#receive(byte[])
*/
@Override
public void receive(byte[] message, Map<String, Object> lowerInfo) {
byte[] basicHdr = new byte[4];
System.arraycopy(message, 0, basicHdr, 0, 4);
byte[] commonHdr = new byte[8];
System.arraycopy(message, 4, commonHdr, 0, 8);
byte[] htHst = new byte[1];
System.arraycopy(commonHdr, 1, htHst, 0, 1);
int headerType = (int)(htHst[0] >> 4);
int headerSubType = (int)(htHst[0] & 0x000000000F);
// Update LPV table
byte[] sopv = new byte[24];
int sopvPos = 12;
if(headerType != HT_BEACON && !(headerType == HT_TSB && headerSubType == HST_SHB) ) {
sopvPos += 4;
}
System.arraycopy(message, sopvPos, sopv, 0, 24);
byte[] gn = new byte[8];
System.arraycopy(sopv, 0, gn, 0, 8);
byte[] mid = new byte[6];
System.arraycopy(gn, 2, mid, 0, 6);
byte[] tst = new byte[4];
System.arraycopy(sopv, 8, tst, 0, 4);
management.gnUpdateLocTable(mid, ByteHelper.byteArrayToInt(tst), sopv);
// Filter beacons
byte [] beaconFilter = management.getGnEnqueueBeacon();
if(headerType != HT_BEACON || beaconFilter != null) {
if(headerType == HT_BEACON) {
byte[] filterMid = new byte[6];
System.arraycopy(beaconFilter, 2, filterMid, 0, 6);
if(java.util.Arrays.equals(mid, filterMid) == false) {
// Received beacon does not match filter
return;
}
}
// Encode with GN indication header
// Extract LINK_LAYER_DESTINATION
byte[] msgInd = ByteHelper.concat(message, (byte[])lowerInfo.get(EthernetLayer.LINK_LAYER_DESTINATION));
super.receive(msgInd, lowerInfo);
}
}
/* (non-Javadoc)
* @see org.etsi.its.adapter.ports.ProtocolPort#dispose()
*/
@Override
public void dispose() {
if(running && beaconThread != null) {
running = false;
try {
beaconThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
super.dispose();
}
/**
* Indicates whether the port is still active. Setting this field to false will cause
* the beaconing thread to stop its execution.
*/
private volatile boolean running;
/**
* Beaconing thread instance.
*/
private Thread beaconThread = null;
}