package org.kne.cloud.network.klalb;

import java.io.IOException;
import java.net.SocketException;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;

import org.kne.cloud.network.NetworkPacket;
import org.kne.io.KNEChannels;

public abstract class AbstractKLALBPacketLink implements KLALBPacketLink {
	private static  int defaultSoTimeout=60*1000;
	
	public static int getDefaultSoTimeout() {
		return defaultSoTimeout;
	}

	public static void setDefaultSoTimeout(int defaultSoTimeout) {
		AbstractKLALBPacketLink.defaultSoTimeout = defaultSoTimeout;
	}


	
	
	
	@Override
	public void writeKLALBPacket(KLALBPacket kp) throws IOException {
		ByteBuffer dataWrite=NetworkPacket.bufferAllocator.allocate((int) kp.getTotalLength());
		dataWrite.clear();
		KLALBPacket.writeKLALBPacketToChannel(KNEChannels.newWritableChannel( dataWrite), kp);
		dataWrite.flip();
		writePacket(dataWrite);
	}
	
	protected abstract void writePacket(ByteBuffer dataWrite) throws IOException;

	@Override
	public KLALBPacket readKLALBPacket() throws IOException {
		ByteBuffer buffer=readPacket();
		if(buffer==null) {
			return null;
		}
		return KLALBPacket.readKLALBPacketFromChannel(KNEChannels.newReadableChannel(buffer));
	}

	protected abstract ByteBuffer readPacket() throws IOException;
}
