package org.kne.cloud.network.tun;

import java.io.IOException;
import java.nio.ByteBuffer;

import one.papachi.tuntap4j.TunDevice;

public class LinuxTUNChannel implements TUNChannel {
	private TunDevice device;
	protected LinuxTUNChannel(TunDevice device) throws IOException{
		this.device=device;
		device.setStatus(true);
	}
	@Override
	public int write(ByteBuffer src) throws IOException {
		return device.write(src);
	}

	@Override
	public int read(ByteBuffer dst) throws IOException {
		return device.read(dst);
	}

	@Override
	public boolean isOpen() {
		return device!=null;
	}

	@Override
	public void close() throws IOException {
		if(device!=null) {
			device=null;
			device.setStatus(false);
		}
	}

}
