www.pudn.com > Bluegammon蓝牙的应用编程.rar > BluetoothServerWorkflow.java


// Copyright (c) 2005 Sony Ericsson Mobile Communications AB 
// 
// This software is provided "AS IS," without a warranty of any kind.  
// ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,  
// INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A  
// PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.  
// 
// THIS SOFTWARE IS COMPLEMENTARY OF JAYWAY AB (www.jayway.se) 
 
package bluegammon.gui; 
 
import java.io.IOException; 
 
import javax.bluetooth.LocalDevice; 
 
import bluegammon.Bluegammon; 
import bluegammon.Resources; 
import bluegammon.gui.menu.ItemAction; 
import bluegammon.gui.menu.MenuPage; 
import bluegammon.gui.menu.PageItem; 
import bluegammon.gui.popup.Popup; 
import bluegammon.gui.popup.PopupListener; 
import bluegammon.io.BackgammonBTConnection; 
 
/** 
 * Handles the different steps of setting up a server 
 * and awaiting a client.  
 * @author Peter Andersson 
 */ 
public class BluetoothServerWorkflow implements PopupListener, ItemAction 
{ 
  /** The connection */ 
  protected BackgammonBTConnection m_serverConnection = null; 
  /** Reference to popup we need to close on successful connection */ 
  protected Popup m_popup; 
   
  /** 
   * Called when user sets up a server. 
   */ 
  public void itemAction(MenuPage page, PageItem item) 
  { 
    synchronized (this) 
    { 
      m_serverConnection = new BackgammonBTConnection(); 
    } 
    m_popup = Bluegammon.showPopup(Resources.getChars(Resources.TXT_BT_SERVER_OPENED), 
        Popup.ALT_CANCEL, 0, 0, 0, this); 
    try 
    { 
      // If there already is a workflow waiting for client, just show popup. 
      // Another thread is still waiting for a client somewhere in the dark, 
      // and will handle any possible client connection. 
      if (m_serverConnection.isAwaitingClient()) 
      { 
        // Just tell the server that we will accept a client if it arrives. 
        // Non blocking call. 
        m_serverConnection.waitForClient(); 
      } 
      else 
      { 
        // Wait for client, blocking call. 
        m_serverConnection.waitForClient(); 
        if (m_popup != null) 
        { 
          m_popup.dispose(); 
          m_popup = null; 
        } 
        char[] localName = Resources.getChars(Resources.TXT_BT_UNKNOWN); 
        try 
        { 
          localName = LocalDevice.getLocalDevice().getFriendlyName().toCharArray(); 
        } catch (Throwable t) {} 
        if (!m_serverConnection.isClosed()) 
        { 
          Bluegammon.setBackgammonConnection(m_serverConnection); 
          Bluegammon.startRemoteGame(true, localName); 
        } 
      } 
    } 
    catch (IOException ioe) 
    { 
      m_popup = null; 
      Bluegammon.showPopup((Resources.getString(Resources.TXT_BT_SERVER_FAILURE) + "\n" +  
          ioe.getMessage()).toCharArray(), 
          Popup.ALT_OK, 60, 0, 0, null); 
      try 
      { 
        m_serverConnection.close(); 
      } catch (IOException e) {} 
      ioe.printStackTrace(); 
    } 
  } 
   
  /** 
   * Called when user cancels a server setup. 
   */ 
  public void selectedChoice(byte choice, boolean timeOut) 
  { 
    if (m_popup != null) 
    { 
      m_popup.dispose(); 
      m_popup = null; 
    } 
    synchronized (this) 
    { 
      if (m_serverConnection != null) 
      { 
        m_serverConnection.pretendServerClose(); 
      } 
    } 
  } 
}