package org.kne.cloud.network.klalb;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;

import org.kne.cloud.network.FilterSocket;
import org.kne.cloud.network.monitor.MonitorData;
import org.kne.cloud.network.monitor.SpeedAndTrafficMonitorDataImpl;

public class MonitoredSocket extends FilterSocket {

	private LongAdder inMonitor;
	private LongAdder outMonitor;
	
	
	
	public LongAdder getInMonitor() {
		return inMonitor;
	}
	public LongAdder getOutMonitor() {
		return outMonitor;
	}
	@Override
	public synchronized void close() throws IOException {
		super.close();
	}
	public MonitoredSocket(Socket socket) {
		this(socket,new LongAdder(),new LongAdder());
	}
	public MonitoredSocket(Socket socket,LongAdder inMonitor,LongAdder outMonitor) {
		super(socket);
		this.inMonitor=inMonitor;
		this.outMonitor=outMonitor;
	}
	
	

	
	private InputStream in;
	private OutputStream out;
	@Override
	public InputStream getInputStream() throws IOException {
		if(in==null)
			in=new MonitoredInputStream(socket.getInputStream(), inMonitor);
		return in;
	}

	@Override
	public OutputStream getOutputStream() throws IOException {
		if(out==null) {
			out=new MonitoredOutputStream(socket.getOutputStream(), outMonitor);
		}
		return out;
	}

}
