package com.eactive.eai.adapter.socket2.protocol.udp;

import com.eactive.eai.adapter.socket2.config.SocketAdapterContext;
import com.eactive.eai.adapter.socket2.protocol.UdpAdapterClient;
import com.eactive.eai.adapter.socket2.protocol.fixedheader.codec.ChunkCodecFactory;
import com.eactive.eai.adapter.socket2.protocol.fixedheader.codec.FixedHeaderCodecFactory;
import com.eactive.eai.adapter.socket2.protocol.fixedpayload.codec.FixedPayloadCodecFactory;
import org.apache.mina.filter.codec.ProtocolCodecFilter;

public class UdpClient extends UdpAdapterClient {
	public UdpClient(SocketAdapterContext context) throws Exception {
		super(context);

		this.handler = new UdpHandler(this);		

		if ("UDP_FIXED_LENGTH_HEADER".equals(context.getProtocol())) {
			if ("NONE".equals(context.getLlFieldType())) {
				config.getFilterChain().addFirst("codec", new ProtocolCodecFilter(new ChunkCodecFactory(this)));
			} else {
				config.getFilterChain().addFirst("codec", new ProtocolCodecFilter(new FixedHeaderCodecFactory(this)));
			}
		} else if ("UDP_FIXED_LENGTH_PAYLOAD".equals(context.getProtocol())) {
			config.getFilterChain().addFirst("codec", new ProtocolCodecFilter(new FixedPayloadCodecFactory(this)));
		} else {
			throw new Exception("invalid protocol in UDP category - " + context.getProtocol());
		}
	}
}
