Commit b6dd3a99 authored by Lenard Nagy's avatar Lenard Nagy
Browse files

Added Java testport project with README.eclipse


Signed-off-by: Lenard Nagy's avatarLenard Nagy <lenard.nagy@ericsson.com>
parent bf5e87ba
<?xml version="1.0" encoding="UTF-8"?>
<TITAN_Designer_Properties>
<ActiveConfiguration>Default</ActiveConfiguration>
<ProjectProperties>
<MakefileSettings>
<targetExecutable>bin/titan.JavaTestPorts.UDP</targetExecutable>
<addSourceLineInfo>true</addSourceLineInfo>
</MakefileSettings>
<LocalBuildSettings>
<MakefileScript/>
<workingDirectory>java_src</workingDirectory>
</LocalBuildSettings>
</ProjectProperties>
</TITAN_Designer_Properties>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="java_src"/>
<classpathentry kind="src" path="user_provided"/>
<classpathentry kind="output" path="java_bin"/>
</classpath>
/java_bin/
/java_src
/logs/
*.log
temp.cfg
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>titan.JavaTestPorts.UDP</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.titan.designer.core.TITANJavaBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.ManifestBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.pde.SchemaBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.titan.designer.core.TITANNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.pde.PluginNature</nature>
</natures>
<linkedResources>
<link>
<name>src/UDPasp_PortType.ttcn</name>
<type>1</type>
<location>/home/elnrnag/Ericsson/git/titan.TestPorts.UDPasp/src/UDPasp_PortType.ttcn</location>
</link>
<link>
<name>src/UDPasp_Types.ttcn</name>
<type>1</type>
<location>/home/elnrnag/Ericsson/git/titan.TestPorts.UDPasp/src/UDPasp_Types.ttcn</location>
</link>
</linkedResources>
</projectDescription>
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: titan.JavaTestPorts.UDP
Bundle-SymbolicName: titan.JavaTestPorts.UDP; singleton:=true
Bundle-Version: 1.0.0
Require-Bundle: org.eclipse.titan.runtime;bundle-version="1.0.0",
org.antlr.runtime;bundle-version="4.3.0"
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ActivationPolicy: lazy
Export-Package: org.eclipse.titan.titan_JavaTestPorts_UDP.generated;uses:="org.eclipse.titan.runtime.core",
org.eclipse.titan.titan_JavaTestPorts_UDP.user_provided;uses:="org.eclipse.titan.titan_JavaTestPorts_UDP.generated"
This file describes how to use Titan UDP java testport from Eclipse Titan java project.
Prerequisites:
- Have this repository pulled to the target computer
The following terminology will be used:
<REPO_DIR> : The directory the titan.TestPorts.UDPasp repository is cloned
<JAVA_PORT_DIR> : The directory containing the Eclipse project of the actual java implementation of the port. It's name is titan.JavaTestPorts.UDP and is located in the root of <REPO_DIR>.
Step1:
- In Eclipse open from the menu File -> Import...
- Select "Existing Projects into Workspace"
- In the chooser dialog navigate into <REPO_DIR> and choose <JAVA_PORT_DIR> as Root directory
- Click "Finish" button. Now the Workspace must contain the titan.JavaTestPorts.UDP project.
Step2:
- In the Eclipse Project Explorer open the titan.JavaTestPorts.UDP project
- Right click on "src" directory
- Choose New -> TTCN3 Module
- Click on "Advanced >>" button at the bottom of the New TTCN3 Module window
- Click on "Link to file in the file system"
- Using the "Browse" button select <REPO_DIR>/src/UDPasp_PortType.ttcn
- Click on "Finish" button
Step3:
- Repeat the steps of Step2 with the difference that the file chosen from <REPO_DIR>/src/UDPasp_Types.ttcn
After completing these Steps the UDP java testport will be usable from other projects by adding it as Referenced project.
Referencing the UDP java testport from other Titan java project:
The following terminology will be used:
<PROJECT> : The project in which the UDP java testport is to be used.
- Right click on <PROJECT> in Project Explorer
- Select "Properties"
- In the "Project Properties" window select "Project References" in the left side list
- On the right make the checkbox of "titan.JavaTestPorts.UDP" selected
- Select "Java Build Path" in the left list
- Click "Add" button
- Select the checkbox of "titan.JavaTestPorts.UDP" in the "Required Project Selection" window and click "OK"
- Click "Apply and Close"
After completing these steps the UDP test port is usable from <PROJECT>.
Note: As per experience it may be required to close and rebuild <PROJECT> befor Eclipse realizes that it references the test port project!
source.. = java_src/,\
user_provided/
output.. = java_bin/
bin.includes = META-INF/,\
.
module HelloUDP {
import from UDPasp_PortType all;
import from UDPasp_Types all;
type component MyMTC
{
port UDPasp_PT udpport_advanced;
}
testcase tc_UDP() runs on MyMTC system MyMTC
{
map(mtc:udpport_advanced, system:udpport_advanced);
var ASP_UDP_open open := {"localhost", 2222, "localhost", 2222};
log(open);
udpport_advanced.send(open);
var ASP_UDP_open_result open_result;
udpport_advanced.receive(ASP_UDP_open_result:?) -> value open_result;
log("ELNRNAG");
log(open_result);
var ASP_UDP_message pdu := {'A1A2A3A4A5'O,omit,omit, open_result.id};
udpport_advanced.send(pdu);
log(pdu);
log("SENT");
var ASP_UDP_message inPdu;
udpport_advanced.receive(ASP_UDP_message:?) -> value inPdu;
log("RECEIVED");
log(inPdu);
log("ELNRNAG1");
unmap(mtc:udpport_advanced, system:udpport_advanced);
log("ELNRNAG2");
setverdict(pass);
log("ELNRNAG3");
}
control
{
execute(tc_UDP());
execute(tc_UDP());
}
}
[MODULE_PARAMETERS]
# This section shall contain the values of all parameters that are defined in your TTCN-3 modules.
[LOGGING]
# In this section you can specify the name of the log file and the classes of events
# you want to log into the file or display on console (standard error).
LogFile := "logs/%e.%h-%r.%s"
FileMask := LOG_ALL | DEBUG | MATCHING
ConsoleMask := ERROR | WARNING | TESTCASE | STATISTICS | PORTEVENT
AppendFile := No
TimeStampFormat := DateTime
LogEventTypes := Yes
SourceInfoFormat := Single
LogEntityName := Yes
[TESTPORT_PARAMETERS]
# In this section you can specify parameters that are passed to Test Ports.
# debugging := yes|no
*.udpport_advanced.debugging := "YES"
# *.localIPAddr := "127.0.0.1" (if not specified, all available interfaces
# will be bound for listening when opening a socket)
*.udpport_advanced.localIPAddr := "127.0.0.1"
# *.localPort := "0..65535" (0 = any port)
*.udpport_advanced.localPort := "2222"
# broadcast := enabled|disabled
# When compiling to java this option has less meaning then in C++ as
# broadcasting is enabled by default on java sockets
# *.mode := advanced
*.udpport_advanced.mode := "advanced"
[EXECUTE]
HelloUDP.control
[MAIN_CONTROLLER]
# The options herein control the behavior of MC.
TCPPort := 0
KillTimer := 10.0
# NumHCs := 0
# LocalAddress :=
package org.eclipse.titan.titan_JavaTestPorts_UDP.user_provided;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.PortUnreachableException;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.Pipe;
import java.nio.channels.SelectableChannel;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.eclipse.titan.runtime.core.Optional;
import org.eclipse.titan.runtime.core.TTCN_Logger;
import org.eclipse.titan.runtime.core.TTCN_Logger.Severity;
import org.eclipse.titan.runtime.core.TitanCharString;
import org.eclipse.titan.runtime.core.TitanInteger;
import org.eclipse.titan.runtime.core.TitanOctetString;
import org.eclipse.titan.runtime.core.TitanPort;
import org.eclipse.titan.runtime.core.TtcnError;
import org.eclipse.titan.titan_JavaTestPorts_UDP.generated.UDPasp__Types;
import org.eclipse.titan.titan_JavaTestPorts_UDP.generated.UDPasp__Types.ASP__UDP;
import org.eclipse.titan.titan_JavaTestPorts_UDP.generated.UDPasp__Types.ASP__UDP__close;
import org.eclipse.titan.titan_JavaTestPorts_UDP.generated.UDPasp__Types.ASP__UDP__message;
import org.eclipse.titan.titan_JavaTestPorts_UDP.generated.UDPasp__Types.ASP__UDP__open;
import org.eclipse.titan.titan_JavaTestPorts_UDP.generated.UDPasp__Types.ASP__UDP__open__result;
public abstract class UDPasp__PT_PROVIDER extends TitanPort {
private final String MODULE = "UDPasp__PT.";
//private final int DEFAULT_NUM_CONN = 10; unnecessary
/** port_mode
* false: basic mode. Works like R1A02
* true: advanced mode. The new features are activated
*/
private boolean port_mode = false;
private boolean debugging = false;
private int localPort = 50000;
private String localAddress = null;
DatagramChannel dc;
HashMap<Integer,SelectableChannel> conn_list;
Pipe pipe = null;
Thread thread = null;
//Constructor
public UDPasp__PT_PROVIDER(String port_name) {
super(port_name);
}
protected abstract void incoming_message(final UDPasp__Types.ASP__UDP__message incoming_par);
protected abstract void incoming_message(final UDPasp__Types.ASP__UDP incoming_par);
protected abstract void incoming_message(final UDPasp__Types.ASP__UDP__open__result incoming_par);
//Set parameters
@Override
public void set_parameter(String parameter_name, String parameter_value) {
log("Entering "+MODULE+"set_parameter: "+parameter_name+": "+parameter_value);
if (parameter_name.equals("debugging")) {
debugging = true;
} else if (parameter_name.equals("mode")) {
if (parameter_value.equals("advanced")) {
port_mode = true;
}
} else if (parameter_name.equals("localIPAddr")) {
localAddress = parameter_value;
} else if (parameter_name.equals("localPort")) {
try {
localPort = Integer.parseInt(parameter_value);
} catch (NumberFormatException nfe) {
throw new TtcnError("Invalid localPort parameter: %s" + parameter_value);
}
} else if (parameter_name.equals("broadcast")) {
TTCN_Logger.log_str(TTCN_Logger.Severity.WARNING_UNQUALIFIED, "Broadcast option is meaningless in java, the socket is broadcast-capable by defult");
}
else {
TTCN_Logger.log_str(TTCN_Logger.Severity.WARNING_UNQUALIFIED, String.format("UDPasp__PT.set_parameter: Unsupported Test Port parameter: %s ", parameter_name));
}
log("Leaving "+MODULE+"set_parameter");
}
//Map, Unmap, Configuration
@Override
protected void user_map(String system_port) {
log("Entering "+MODULE+"user_map");
if (port_mode){ //Advanced mode
if (conn_list != null) {
throw new TtcnError("UDP Test Port ("+system_port+"): Internal error: conn_list is not NULL when mapping.");
} else {
conn_list = new HashMap<Integer,SelectableChannel>();
}
} else { //Simple config mode
try {
dc = DatagramChannel.open();
dc.bind(new InetSocketAddress(localAddress, localPort));
conn_list = new HashMap<Integer,SelectableChannel>();
conn_list.put(new Integer(dc.hashCode()),(SelectableChannel)dc);
Install_Handler(collectionToSet(conn_list.values()), null, 0.0);
} catch (Exception e) {
// TODO: handle exception
throw new TtcnError("Exception in userMap: Unable to set up socket" + e.getMessage());
}
}
log("Leaving "+MODULE+"user_map");
}
@Override
protected void user_unmap(String system_port) {
log("Entering "+MODULE+"user_unmap");
if (port_mode) {
Iterator<SelectableChannel> it = conn_list.values().iterator();
while (it.hasNext()) {
dc = (DatagramChannel) it.next();
try {
if (dc.isOpen()) {
dc.close();
}
} catch (IOException ioe) {
throw new TtcnError("Exception in userUnmap: " + ioe.getMessage());
}
}
conn_list = null;
} else { //Unnecessary, since dc is added to readableSet just like in advanced mode
try {
dc.close();
dc = null;
} catch (IOException ioe) {
throw new TtcnError("Exception in userUnmap: " + ioe.getMessage());
}
super.user_unmap(system_port);
}
try {
Uninstall_Handler();
} catch (IOException e) {
throw new TtcnError("Exception in userUnmap (Uninstall_Handler failed): " + e.getMessage());
}
log("Leaving "+MODULE+"user_unmap");
}
@Override
public void Handle_Event(SelectableChannel channel, boolean is_readable, boolean is_writeable) {
log("Entering "+MODULE+"Handle_Event");
if (!(channel instanceof DatagramChannel)) {
throw new TtcnError("FATAL ERROR: incorrect channel received");
}
DatagramChannel source = ((DatagramChannel) channel);
ByteBuffer buffer = ByteBuffer.allocate(65535);
//CharBuffer cbuffer = CharBuffer.allocate(65535);
try {
//int bytesRead;
InetSocketAddress remote = null;
try {
remote = (InetSocketAddress)source.receive(buffer);
} catch (PortUnreachableException pue) {
//Ignore for now, for more info see connect() function in
//https://docs.oracle.com/javase/7/docs/api/java/net/DatagramSocket.html
}
if (remote != null) {
int received_length = buffer.position();
byte[] received = new byte[received_length];
buffer.rewind();
buffer.get(received, 0, received_length);
TitanOctetString incoming = new TitanOctetString(received);
TitanCharString remote_address = new TitanCharString(remote.getHostString());
TitanInteger remote_port = new TitanInteger(remote.getPort());
if (port_mode) { //Advanced mode
TitanInteger chId = new TitanInteger(channel.hashCode());
Optional<TitanCharString> remote_address_optional = new Optional<TitanCharString>(TitanCharString.class);
remote_address_optional.operator_assign(remote_address);
Optional<TitanInteger> remote_port_optional = new Optional<TitanInteger>(TitanInteger.class);
remote_port_optional.operator_assign(remote_port);
Optional<TitanInteger> chId_optional = new Optional<TitanInteger>(TitanInteger.class);
chId_optional.operator_assign(chId);
ASP__UDP__message msg = new ASP__UDP__message(incoming, remote_address_optional, remote_port_optional, chId_optional);
incoming_message(msg);
} else { //Simple config mode
ASP__UDP msg = new ASP__UDP(incoming, remote_address, remote_port);
incoming_message(msg);
}
}
} catch (IOException e) {
throw new TtcnError("IOException in HandleEvent: " + e.getMessage());
}
log("Leaving "+MODULE+"Handle_Event");
}
public void outgoing_send(ASP__UDP send_par) {
log("Entering "+MODULE+"outgoing_send(ASP__UDP send_par)");
InetSocketAddress address = null;
//Check if the message has a valid address to be sent to
TitanCharString addrf = send_par.constGet_field_addressf();
if (addrf != null) {
String hostname = addrf.get_value().toString();
TitanInteger portf = send_par.constGet_field_portf();
if (portf != null) {
int port = portf.get_int();
if ((hostname != null) && (hostname.length()>0) && (port > 0)){
//TODO: Add more thorough checks for address and port in this condition
//Overriding original send address & port
address = new InetSocketAddress(hostname, port);
}
}
}
try {
TitanOctetString pdu = send_par.constGet_field_data();
byte[] send_bytes = pdu.get_value();
ByteBuffer bbToSend = ByteBuffer.wrap(send_bytes);
dc.send(bbToSend,address);
} catch (IOException ioe) {
throw new TtcnError("IOException: " + ioe.getMessage());
}
log("Leaving "+MODULE+"outgoing_send(ASP__UDP send_par)");
}
public void outgoing_send(ASP__UDP__open send_par) {
log("Entering "+MODULE+"outgoing_send(ASP__UDP send_par)");
String localAddrStr;
int localPort = 0;
String remoteAddrStr;
int remotePort = 0;
InetSocketAddress localInetAddr = null;
InetSocketAddress remoteInetAddr = null;
DatagramChannel dc;
try {
dc = DatagramChannel.open();
} catch (IOException e) {
throw new TtcnError("Can not open DatagramChannel: "+e.getMessage());
}
//Set local address if specified
if (send_par.constGet_field_local__addr().is_present()) {
localAddrStr = send_par.constGet_field_local__addr().get().get_value().toString();
if (send_par.constGet_field_local__port().is_present()) {
localPort = send_par.constGet_field_local__port().get().get_int();
}
try {
localInetAddr = new InetSocketAddress(localAddrStr, localPort);
dc.bind(localInetAddr);
} catch (IOException ioe) {
throw new TtcnError("IOException: " + ioe.getMessage());
}
}
//Set remote address if specified
if (send_par.constGet_field_remote__addr().is_present()) {
remoteAddrStr = send_par.constGet_field_remote__addr().get().get_value().toString();
if (send_par.constGet_field_remote__port().is_present()) {
remotePort = send_par.constGet_field_remote__port().get().get_int();
try {
remoteInetAddr = new InetSocketAddress(InetAddress.getByName(remoteAddrStr), remotePort);
dc.connect(remoteInetAddr);
} catch (IOException ioe) {
throw new TtcnError("IOException: " + ioe.getMessage());
}
}
}
//Add to conn_list
int dcId = dc.hashCode();
conn_list.put(dcId, dc);
try {
Uninstall_Handler();
Install_Handler(collectionToSet(conn_list.values()), null, 0.0);
} catch (IOException e) {
throw new TtcnError("IOException: " + e.getMessage());
}
//Report result
try {
InetSocketAddress finalLocalAddress = (InetSocketAddress)dc.getLocalAddress();
String finalLocalAddrStr = finalLocalAddress == null ? "" : finalLocalAddress.getHostString();
int finalLocalPort = finalLocalAddress == null ? 0 : finalLocalAddress.getPort();
TitanCharString finalLocalAddrTitanStr = new TitanCharString(finalLocalAddrStr);
TitanInteger finalLocalTitanPort = new TitanInteger(finalLocalPort);
TitanInteger id = new TitanInteger(dcId);
ASP__UDP__open__result result = new ASP__UDP__open__result(finalLocalAddrTitanStr, finalLocalTitanPort, id);
incoming_message(result);
} catch (IOException e) {
throw new TtcnError("IOException: " + e.getMessage());
}
log("Leaving "+MODULE+"outgoing_send(ASP__UDP__open send_par)");
}
public void outgoing_send(ASP__UDP__close send_par) {
log("Entering "+MODULE+"outgoing_send(ASP__UDP__close send_par)");
int dcId = send_par.constGet_field_id().get_int();
DatagramChannel dc = (DatagramChannel) conn_list.remove(dcId);
try {
Uninstall_Handler();
Install_Handler(collectionToSet(conn_list.values()), null, 0.0);
} catch (IOException e1) {
throw new TtcnError("IOException: " + e1.getMessage());
}
if (dc != null) {
try {
dc.close();
dc = null;
} catch (IOException e) {
throw new TtcnError("IOException: " + e.getMessage());
}
} else {
throw new TtcnError("Channel not found in conn_list");
}
log("Leaving "+MODULE+"outgoing_send(ASP__UDP__close send_par)");