package oracle.forms.fd;

import oracle.forms.handler.IHandler;
import oracle.forms.ui.VBean;
import oracle.forms.properties.ID;
import oracle.forms.ui.CustomEvent;

  /**
   * A Bean to transform the Forms module
   * into a socket server
   *
   * @author Francois Degrelle
   * @version 1.1
   * 
   * tried only with the Sun Java Plugin
   */
   
public class SocketServer extends VBean  implements Runnable 
{
  // variables
  public final static ID initServer     = ID.registerProperty("INIT_SERVER"); 
  public final static ID stopServer     = ID.registerProperty("STOP_SERVER");          
  private static final ID SEND_MSG      = ID.registerProperty("SENDMSG");        
  private static final ID MESSAGEVALUE  = ID.registerProperty("MESSAGEVALUE");     
  
  static Thread              runner ;
  public static boolean      bMsg = false ;
  public static String       sMessage = "" ;
  private static IHandler    mHandler;  
  private Server             server ; // special class that init the socker server

      
  public void init(IHandler handler)
  {
    mHandler = handler;
    super.init(handler);
  }  
  

      // we start the thread here
      private void startThread()
      {
        if (runner == null )
        {
          runner = new Thread(this);
          runner.start();
        }
      }
      
      // we stop the thread here
      private static void stopThread()
      {
        if (runner != null )
        {
          runner = null;
        }
      }    

      // Start the thread
      public void run()
      {
        Thread theThread = Thread.currentThread();
        while (runner == theThread)
        {
            // well we need sleep to release processor for do any thing else
            try {
                theThread.sleep(1000);
            } catch (InterruptedException e) {
                System.err.println("Error : " + e);
                e.printStackTrace();
            }
            // tell Forms that a message is incoming
            if( bMsg )
            {
              System.out.println("Sendmessage="+sMessage);
              SendMessage(sMessage) ;
              bMsg = false ;
              if( sMessage.equalsIgnoreCase("BYE."))
                stopThread() ;
            }
        }
      }      
  
  /*
   *  Set some properties 
   */
  public boolean setProperty(ID property, Object value)
  {
    // Start the socket server on the given port
    if( property == initServer )
    {
      int iPort = Integer.parseInt((String)value) ;
      server = new Server(iPort);
      server.start();
      startThread() ;
      return true ;
    }
    // Stop the socket server
    else if( property == stopServer )
    {
        stopThread() ;
      return true ;
    }    
    else
    {
     return super.setProperty(property, value);
    }
  }  
  
  // send message back to Forms
  public void SendMessage( String sMessage )
  {
    try{
      CustomEvent ce = new CustomEvent(mHandler, SEND_MSG);
      mHandler.setProperty( MESSAGEVALUE, sMessage );
      dispatchCustomEvent(ce);  
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }    
  }

}
