/* BlinkenSim
 * version 0.1 date 2006-07-28
 * Copyright (C) 2004-2006: Stefan Schuermans <stefan@blinkenarea.org>
 * Copyleft: GNU public license - http://www.gnu.org/copyleft/gpl.html
 * a blinkenarea.org project
 */

package org.blinkenarea.BlinkenSim;

import java.lang.*;
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import org.blinkenarea.BlinkenLib.*;
import org.blinkenarea.BlinkenSim.*;

public class DynMcufClientRecv extends Thread
{
  private DatagramSocket sock;
  private InetAddress host;
  private int port;
  private FrameReceiver receiver;
  private boolean termReq = false;

  DynMcufClientRecv( DatagramSocket sock, InetAddress host, int port, FrameReceiver receiver )
  {
    this.sock = sock;
    this.host = host;
    this.port = port;
    this.receiver = receiver;
  }

  public void run( )
  {
    while( ! termReq )
    {
      try
      {
        byte[] data = new byte[8192];
        DatagramPacket framePacket = new DatagramPacket( data, 8192 );
        sock.receive( framePacket );
        if( framePacket.getAddress( ).equals( host ) && framePacket.getPort( ) == port )
        {
          data = framePacket.getData( );
          BlinkenFrame frame = new BlinkenFrame( 1, 1, 1, 1, 1 );
          if( data != null && frame.fromNetwork( data ) == BlinkenConstants.BlinkenProtoMcuf )
            receiver.newFrame( frame );
        }
      }
      catch( IOException e ) { }
    }
  }

  public void terminate( )
  {
    termReq = true;
    this.interrupt( );
  }
}
