/**
 *  BTP port implementation
 *  
 *  @author     ETSI / STF424
 *  @version    $URL$
 *              $Id$
 *
 */
package org.etsi.its.adapter.ports;

import java.io.IOException;
import java.util.Map;

import org.etsi.its.adapter.layers.GnLayer;
import org.etsi.its.adapter.layers.Layer;
import org.etsi.its.codec.TestAdapterMessageTypeEnum;

/**
 *  BTP port implementation
 */  
public class BtpPort extends ProtocolPort {

	/**
	 * 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 BtpPort(String portName, String componentName, String lowerStackDesc, String linkLayerAddress) {
		super(portName, componentName, lowerStackDesc, linkLayerAddress);
	}

	/* (non-Javadoc)
	 * @see org.etsi.its.adapter.ports.ProtocolPort#receive(byte[])
	 */
	@Override
	public void receive(byte[] message, Map<String, Object> lowerInfo) {
		
		byte[] msgInd;

		try {
			/* Bad design in BTP which breaks layer independence */
			byte[] pseudoMsg = null;
			if((Integer)(lowerInfo.get(GnLayer.GN_NEXTHEADER)) == 1/*FIXME*/) {
				pseudoMsg = getTaCodec().Encode(TestAdapterMessageTypeEnum.BtpAHeader, message, (Long)lowerInfo.get(Layer.RECEPTION_TIMESTAMP));
			}
			else {
				pseudoMsg = getTaCodec().Encode(TestAdapterMessageTypeEnum.BtpBHeader, message, (Long)lowerInfo.get(Layer.RECEPTION_TIMESTAMP));
			}
			msgInd = getTaCodec().Encode(TestAdapterMessageTypeEnum.BtpInd, pseudoMsg, (Long)lowerInfo.get(Layer.RECEPTION_TIMESTAMP));
			super.receive(msgInd, lowerInfo);
		} catch (IOException e) {
			e.printStackTrace();
		}		
	}
}
