module TRXC_CodecPort {

import from IPL4asp_PortType all;
import from IPL4asp_Types all;
import from TRXC_Types all;

type record TRXC_RecvFrom {
	ConnectionId	connId,
	HostName	remName,
	PortNumber	remPort,
	HostName	locName,
	PortNumber	locPort,
	TrxcMessage	msg
}

template TRXC_RecvFrom tr_TRXC_RecvFrom(template ConnectionId cid, template TrxcMessage msg) := {
	connId := cid,
	remName := ?,
	remPort := ?,
	locName := ?,
	locPort := ?,
	msg := msg
}

type record TRXC_Send {
	ConnectionId	connId,
	TrxcMessage	msg
}

private function IPL4_to_TRXC_RecvFrom(in ASP_RecvFrom pin, out TRXC_RecvFrom pout) {
	pout.connId := pin.connId;
	pout.remName := pin.remName;
	pout.remPort := pin.remPort;
	pout.locName := pin.locName;
	pout.locPort := pin.locPort;
	pout.msg := dec_TrxcMessage(oct2char(pin.msg));
} with { extension "prototype(fast)" };

private function TRXC_to_IPL4_Send(in TRXC_Send pin, out ASP_Send pout) {
	pout.connId := pin.connId;
	pout.proto := { udp := {} };
	pout.msg := char2oct(enc_TrxcMessage(pin.msg));
} with { extension "prototype(fast)" };

type port TRXC_CODEC_PT message {
	out	TRXC_Send;
	in	TRXC_RecvFrom,
		ASP_ConnId_ReadyToRelease,
		ASP_Event;
} with { extension "user IPL4asp_PT
	out(TRXC_Send -> ASP_Send: function(TRXC_to_IPL4_Send))
	in(ASP_RecvFrom -> TRXC_RecvFrom: function(IPL4_to_TRXC_RecvFrom);
	   ASP_ConnId_ReadyToRelease -> ASP_ConnId_ReadyToRelease: simple;
	   ASP_Event -> ASP_Event: simple)"
}

template (value) TRXC_Send ts_TRXC_Send(ConnectionId cid, template (value) TrxcMessage msg) := {
	connId := cid,
	msg := msg
}

function f_TRXC_transceive(TRXC_CODEC_PT pt, ConnectionId conn_id, TrxcMessage tx,
			   template TrxcMessage tr := ?) return TrxcMessage {
	var TRXC_RecvFrom rf;
	timer T := 3.0;
	/* build better default template */
	if (istemplatekind(tr, "?")) {
		tr := {
			rsp := {
				verb := tx.cmd.verb,
				status := ?,
				params := *
			}
		};
	}
	pt.send(ts_TRXC_Send(conn_id, tx));
	T.start;
	alt {
	[] pt.receive(tr_TRXC_RecvFrom(conn_id, tr)) -> value rf {
		return rf.msg;
		}
	[] T.timeout {
		setverdict(fail, "Timeout waiting for ", tr, " on ", pt);
		mtc.stop;
		}
	}
	return rf.msg;
}

}
