package org.kne.cloud.network;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ProtocolDetectorServerSocket extends FilterServerSocket {
	public ProtocolDetectorServerSocket(ServerSocket serversocket) throws IOException {
		super(serversocket);
		this.protocolDetector=ProtocolDetector.getNewDefaultDetector();
	}
	public ProtocolDetectorServerSocket(ServerSocket serversocket,ProtocolDetector protocolDetector) throws IOException {
		super(serversocket);
		this.protocolDetector=protocolDetector;
	}
	private ProtocolDetector protocolDetector;
	
	public void setProtocolDetector(ProtocolDetector protocolDetector) {
		this.protocolDetector = protocolDetector;
	}
	public ProtocolDetector getProtocolDetector() {
		return protocolDetector;
	}
	@Override
	public ProtocolDetectorSocket accept() throws IOException {
		ProtocolDetectorSocket s=(ProtocolDetectorSocket) super.accept();
		int sot=s.getSoTimeout();
		s.setSoTimeout(200);
		InputStream ins=s.getInputStream();
		ProtocolStack psd= protocolDetector.detect(ins);
		s.setSoTimeout(sot);
		s.getProtocolStack().addAll(psd);
		return s;
	}

	@Override
	protected ProtocolDetectorSocket socketFilterFunction(Socket accept) {
		// TODO 自动生成的方法存根
		return new ProtocolDetectorSocket(accept);
	}
	
}
