| // ================================================================================== |
| // Copyright (c) 2000-2019 Ericsson Telecom AB AB |
| // All rights reserved. This program and the accompanying materials |
| // are made available under the terms of the Eclipse Public License v2.0 |
| // which accompanies this distribution, and is available at |
| // https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html |
| // ================================================================================== |
| // Contributors: |
| // Krisztian Gulyas - initial implementation and initial documentation |
| // |
| // File: SimpleTCP.ttcn |
| // Rev: |
| // Prodnr: |
| // ================================================================================== |
| |
| // ============================================================================== |
| // |
| // Helper module for simple TCP/IP communication using the IPL4asp library |
| // |
| // ============================================================================== |
| module SimpleTCP |
| { |
| // import basic definitions of TCP/IP communication |
| import from IPL4asp_Types all; |
| import from IPL4asp_PortType all; |
| |
| // basic TCP connection data type |
| type record ConnectionData { |
| charstring host, // host name [string] |
| integer portNumber // port number [integer] |
| } |
| |
| // ------------------------------------------------------------------------------ |
| // Helper function for simple TCP message send and receive |
| // |
| // 0) Register message lenght callback (function) |
| // 1) Initalize TCP connection |
| // 2) Sending out TCP message with given PDU |
| // 3) if necessary: catching reply message, get PDU |
| // |
| // parameters: |
| // - testPort // IPL4asp_PT type TCP/IP testport [IPL4asp_PT] |
| // - msgLenCallback // message length function callback |
| // - local // local host/port [ConnectionData] |
| // - remote // remote host/port [ConnectionData] |
| // - pduOut // these octets will be sent out as the part of TCP message [octets] |
| // - pduIn // these octets are received from incomming TCP message [octets] |
| // - expectResponse // expected response message from host/port [true/false] |
| // return: |
| // - TCP communication was success [true/false] |
| // |
| // ------------------------------------------------------------------------------ |
| |
| function sendReceiveMsg ( |
| IPL4asp_PT testPort, // IPL4asp_PT type TCP/IP testport [IPL4asp_PT] |
| f_IPL4_getMsgLen msgLenCallback, |
| ConnectionData local, |
| ConnectionData remote, |
| in octetstring pduOut, |
| inout octetstring pduIn, |
| boolean expectResponse |
| ) return boolean // TCP commution result |
| { |
| var boolean res := false; // send/receive result, default = false |
| |
| var Result connectionResult, closingResult; |
| |
| // register message lenght callback |
| f_IPL4_setGetMsgLen(testPort, -1, msgLenCallback, {}); |
| |
| // connect to the socket and check result |
| connectionResult := f_IPL4_connect( |
| testPort, |
| remote.host, remote.portNumber, |
| local.host, local.portNumber, |
| -1, |
| { tcp := {}}, |
| { |
| { reuseAddress := {enable := true} } |
| }); |
| |
| if (ispresent(connectionResult.errorCode)) { |
| log(" [!!] TCP port connection error: ", connectionResult); |
| } |
| else { |
| // successfully connected to the given host/port |
| log(" [::] TCP port [", remote.host, "/", remote.portNumber, "] connected."); |
| |
| // send and receive TCP message |
| var ASP_SendTo tcp_msg_send; |
| var ASP_RecvFrom tcp_msg_received; |
| |
| // Timer with 0.1 s duration |
| timer T := 0.1; |
| |
| // set up TCP message |
| tcp_msg_send := { connectionResult.connId, remote.host, remote.portNumber, { tcp := {} }, pduOut } |
| |
| // send out TCP message to the given port |
| testPort.send(tcp_msg_send); |
| log(" [=>] TCP message sent..."); |
| |
| // process received message if needed |
| if (expectResponse) { |
| |
| // start timer with 0.1 s |
| T.start; |
| |
| alt |
| { |
| [] testPort.receive(ASP_RecvFrom : ?) -> value tcp_msg_received |
| { |
| log(" [<=] TCP message received "); |
| pduIn := tcp_msg_received.msg; |
| res := true; |
| } |
| [] T.timeout { |
| log(" [!!] Timeout (no response) "); |
| } |
| } |
| } |
| else { |
| log(" [::] Expecting no response"); |
| res := true; |
| } |
| |
| // close the connection and check result |
| closingResult := f_IPL4_close(testPort, connectionResult.connId, { unspecified := {} }); |
| |
| if (ispresent(closingResult.errorCode)) { |
| log(" [!!] TCP port closing error: ", connectionResult.errorCode); |
| } |
| else { |
| log(" [::] TCP port [", remote.host, "/", remote.portNumber, "] closed"); |
| } |
| |
| // 0.1 second delay (proper closing of TCP operations) |
| T.start; T.timeout; |
| |
| } |
| |
| return res; |
| } |
| |
| } |