package mit.mitid.comm ;

public class Communications
{
	public static String COMM_TIMEOUT = "Communications has timed out" ;

	private String host = null ;
	public String getHost() { return this.host ; }
	public void setHost( String host ) { this.host = host ; }

	private Integer port = null ;
	public Integer getPort() { return this.port ; }
	public void setPort( Integer port ) { this.port = port ; }

	private java.net.Socket socket = null ;
	public java.net.Socket getSocket() { return this.socket ; }
	public void setSocket( java.net.Socket socket ) { this.socket = socket ; }

	private String result = null ;
	public String getResult() { return this.result ; }
	public void setResult( String result ) { this.result = result ; }

	public void readMessage()  
	{
		try
		{
			setResult( null ) ;
			validateSocket() ;
			long time = System.currentTimeMillis() ;
			final int SOCKETBUFFERSIZE = 6*1024 ;
			byte[] buffer = new byte[ SOCKETBUFFERSIZE ] ;
			int offset = 0 ;
			java.io.InputStream is = getSocket().getInputStream() ;
			while( true )
			{
				if( 0 != is.available() )
				{
					int length = is.available() ;
					is.read( buffer , offset , length ) ;
					offset += length ;
					setResult( new String( buffer , 0 , offset ) ) ;
					if( getResult().endsWith( mit.mitid.PROTOCOL.EOR_STRING ) )
					{
						return ;
					}
				}
				Thread currentThread = Thread.currentThread() ;
				currentThread.yield() ;
				currentThread.sleep( 500 ) ;
System.out.print( "." ) ;
				if( ( System.currentTimeMillis() - time ) > ( ( 1000 * 60 ) * 8 ) )
				{
					setResult( COMM_TIMEOUT ) ;
					return ;
				}
			}
		}
		catch( Exception ex )
		{
			ex.printStackTrace() ;
			setResult( ex.getMessage() ) ;
		}
	} 

	public void writeMessage( String message )
	{
		try
		{
			setResult( null ) ;
			validateSocket() ;
			java.io.OutputStream os = getSocket().getOutputStream() ;
			byte[] bytes = new byte[ message.getBytes().length + 1 ] ;
			System.arraycopy( message.getBytes() , 0 , bytes , 0 , message.getBytes().length ) ;
			bytes[ message.getBytes().length ] = mit.mitid.PROTOCOL.EOM_BYTE ;
			os.write( bytes , 0 , bytes.length ) ;
			os.flush() ;
		}
		catch( Exception ex )
		{
			ex.printStackTrace() ;
			setResult( ex.getMessage() ) ;
		}
	}

	public void writeMessage( String header , byte[] bytes )
	{
		try
		{
			setResult( null ) ;
			validateSocket() ;
			java.io.OutputStream os = getSocket().getOutputStream() ;
			byte[] headerBytes = header.getBytes() ;
			byte[] message = new byte[ headerBytes.length + bytes.length ] ;
			System.arraycopy( headerBytes , 0 , message , 0 , headerBytes.length ) ;
			System.arraycopy( bytes , 0 , message , headerBytes.length , bytes.length ) ;
			os.write( message , 0 , message.length ) ;
			os.flush() ;
		}
		catch( Exception ex )
		{
			ex.printStackTrace() ;
			setResult( ex.getMessage() ) ;
		}
	}

	private void validateSocket()
	throws Exception
	{
		if( null == getSocket() )
		{
			setSocket
				( new java.net.Socket( getHost() 
				, ( null != getPort() ) ? getPort().intValue() : -1
				) ;
		}
	}

}