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
/**
* Upper Tester port implementation. This port is used to trigger IUT's upper interface
*
* @author ETSI / STF424
* @version $URL$
* $Id$
*
*/
package org.etsi.its.adapter.ports;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.etsi.adapter.TERFactory;
import org.etsi.common.ByteHelper;
import org.etsi.ttcn.tci.CharstringValue;
/** This class implements behaviour for Upper Tester port
* The Upper tester entity in the SUT enables triggering Protocol functionalities by simulating primitives from
* application or LDM entities
* It is required to trigger the Protocol layer in the SUT to send Protocol specific messages, which are
* resulting from upper layer primitives
*/
public class UpperTesterPort extends AdapterPort implements IPort, IObservable {
private static final String SETTINGS_PATTERN = "(\\S+)\\:(\\d+)";
/**
* Constructor
* @param portName Name of the port
* @param componentName Name of the component owning this port instance
* @param localPortNumber Local port number for the UDP listener
* @param remotePortNumber UDP port listener of remote UT application
*/
public UpperTesterPort(final String portName, final String componentName) {
super(portName, componentName);
// UDP connection parameters
String settings = ((CharstringValue)TERFactory.getInstance().getTaParameter("UpperTesterSettings")).getString();
Matcher matcher = settingsPattern.matcher(settings);
if (matcher.find()) {
try {
utPeerAddress = InetAddress.getByName(matcher.group(1));
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
utPeerPort = Integer.parseInt(matcher.group(2));
} else {
}
running = true;
// UDP socket for communication with UT
try {
utSocket = new DatagramSocket();
utThread = new UdpThread(utSocket);
utThread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean send(final byte[] message) {
/* FIXME: For debug only. Uncomment if no UT
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// Do nothing, we do not care
}
setChanged();
byte[] rsp;
switch (message[0]) {
case 0x00:
rsp = new byte[]{(byte)0x01, (byte)0x01};
break;
default:
rsp = new byte[]{(byte)0x41, (byte)0x01};
break;
}
notifyObservers(new PortEvent(rsp, getPortName(), getComponentName()));
if(true)
return true;
*/
DatagramPacket packet = new DatagramPacket(message, message.length, utPeerAddress, utPeerPort);
try {
utSocket.send(packet);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
@Override
public void dispose() {
if(running) {
running = false;
if(utThread != null) {
try {
utSocket.close();
utThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private DatagramSocket utSocket;
private Thread utThread;
private InetAddress utPeerAddress = null;
private int utPeerPort = 0;
private Pattern settingsPattern = Pattern.compile(SETTINGS_PATTERN);
/**
* Indicates whether the port is still active. Setting this field to false will cause
* the UDP communication with Upper Tester to be stopped
*/
private volatile boolean running;
private class UdpThread extends Thread {
private DatagramSocket taSocket;
public UdpThread(DatagramSocket taSocket) throws IOException {
this.taSocket = taSocket;
}
@Override
public void run() {
while(running) {
try {
byte[] buf = new byte[4096];
// receive packet
DatagramPacket packet = new DatagramPacket(buf, buf.length);
taSocket.receive(packet);
setChanged();
notifyObservers(new PortEvent(ByteHelper.extract(packet.getData(), packet.getOffset(), packet.getLength()), getPortName(), getComponentName()));
} catch (IOException e) {
running = false;
}
}
taSocket.close();
}
}
}