package prototype.net;

import java.util.Vector;


public class ServerSocketManager extends Thread {
	
	ServerSocketThread serveThread;
	int port;
	
	Vector<Object> buffer;
	
	String name;
	
	public ServerSocketManager(){
		super();
		
		serveThread = new ServerSocketThread();
		port = -1;		
		
		buffer = new Vector<Object>();
		
	}
	
	// DEBUGGING ..
	public void setSensorName( String name_ ){
		
		name = name_;
		
	}
	
	public void run(){
		
		Object data;
		
		if( serveThread != null ){
			
			if( port > 0 )
				serveThread.startServer( port );
			else
				serveThread.startServer();

			while(true){
				
				if( ( buffer != null ) && ( buffer.size() > 0 ) ){
					
					data = buffer.firstElement();
					buffer.removeElementAt(0);
				
					serveThread.writeData( data );
					
					//System.out.println("ServerSocketThread("+name+"), writeData: " + data.toString() );
					
					data = null;
					
				}
				
			}	
			
		}
		
	}
	
	public void setData( Object data ){
		
		if( ( buffer != null ) && ( data != null ) )
			buffer.add( data );
		
	}

	public int getPort() {
		
		return port;
		
	}

	public void setPort( int port_ ) {
		
		port = port_;
		
	}
	
}
