package org.kne.cloud.network.tcp;

import java.io.IOException;

import org.kne.cloud.network.ipv6.IPv6Address;
import org.kne.cloud.network.ipv6.IPv6Packet;
import org.kne.cloud.network.ipv6.IPv6Packet.IPv6Payload;
import org.kne.cloud.network.ipv6.IPv6ProtocolRegister;
import org.kne.cloud.network.klalb.BindableConsumer;
import org.kne.cloud.network.klalb.KLALBController;
import org.kne.cloud.network.klalb.PortBinder;
import org.kne.cloud.network.klalb.PortPacket;
import org.kne.cloud.network.srv6.IPv6PacketConsumer;

public class UDPProtocolRegister extends PortBinder<IPv6Packet> implements IPv6ProtocolRegister {
	private static final boolean showpacket=false;
	
		private KLALBController controller;
		
		public UDPProtocolRegister(KLALBController controller) {
			super(controller.getSelf().getAddress());
			this.controller = controller;
		}

		@Override
		public boolean onaccept(IPv6Packet packx) throws IOException {

			IPv6Payload pl = packx.getPayload();
			if (pl instanceof UDPPacket) {
				UDPPacket rec = (UDPPacket) pl;
				if (showpacket)
					System.out.println("UDP_RX:" + rec);
				IPv6Address srcA = packx.getSourceAddress();
				PortPacket pt = (PortPacket) rec;
				BindableConsumer<IPv6Packet> cons;
				if((cons=distributePacketToConsumer(srcA, pt))!=null) {
					cons.accept(packx);
					return true;
				}
			}
			return false;
		}

	}