bluegammon.io
Class BackgammonBTConnection

java.lang.Object
  extended bybluegammon.io.BackgammonBTConnection
All Implemented Interfaces:
BackgammonConnection

public class BackgammonBTConnection
extends java.lang.Object
implements BackgammonConnection

Implementation of the interface BackgammonConnection for bluetooth bearer.

Author:
Peter Andersson

Constructor Summary
BackgammonBTConnection()
           
 
Method Summary
 void close()
          Closes all resources opened in this connection.
 boolean connectClient(java.lang.Object remote)
          Connects a client to a server.
 javax.microedition.io.StreamConnection getConnection()
          If server has got a client or client is connected to a server, this method returns the connection.
 java.io.DataInputStream getInput()
          If server has got a client or client is connected to a server, this method returns the input stream.
 java.io.DataOutputStream getOutput()
          If server has got a client or client is connected to a server, this method returns the output stream.
 boolean isAwaitingClient()
          Returns true if this connection is awaiting a client already.
 boolean isClosed()
          Returns if this connection has been closed or not.
 void pretendServerClose()
          The BT server cannot be interrupted, so lets just pretend we close the server.
 void waitForClient()
           Waits for a client, the method blocks until client arrives or IOException is thrown.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BackgammonBTConnection

public BackgammonBTConnection()
Method Detail

waitForClient

public void waitForClient()
                   throws java.io.IOException

Waits for a client, the method blocks until client arrives or IOException is thrown. The BT server cannot be interrupted - call pretendServerClose to emulate this. Any client that connects after a call to pretendServerClose will immediately be shutdown.

Disable this state by calling waitForClient again, which will make the client accepted when it connects. Any call to waitForClient during an already active client wait will just set flags and return directly, as there must be another thread still waiting a client generated by the first call to waitForClient.

Specified by:
waitForClient in interface BackgammonConnection
Throws:
java.io.IOException

connectClient

public boolean connectClient(java.lang.Object remote)
                      throws java.io.IOException
Description copied from interface: BackgammonConnection
Connects a client to a server.

Specified by:
connectClient in interface BackgammonConnection
Parameters:
remote - Any info needed to connect to the server.
Returns:
True if client connects successfully, false otherwise.
Throws:
java.io.IOException

getConnection

public javax.microedition.io.StreamConnection getConnection()
                                                     throws java.io.IOException
Description copied from interface: BackgammonConnection
If server has got a client or client is connected to a server, this method returns the connection. Otherwise it returns null.

Specified by:
getConnection in interface BackgammonConnection
Returns:
The stream connection or null.
Throws:
java.io.IOException

getInput

public java.io.DataInputStream getInput()
Description copied from interface: BackgammonConnection
If server has got a client or client is connected to a server, this method returns the input stream. Otherwise it returns null.

Specified by:
getInput in interface BackgammonConnection
Returns:
the input stream or null.

getOutput

public java.io.DataOutputStream getOutput()
Description copied from interface: BackgammonConnection
If server has got a client or client is connected to a server, this method returns the output stream. Otherwise it returns null.

Specified by:
getOutput in interface BackgammonConnection
Returns:
the output stream or null.

pretendServerClose

public void pretendServerClose()
The BT server cannot be interrupted, so lets just pretend we close the server. If the server gets a client while in pretended closed mode, or when it time out, we close connections for real.


isAwaitingClient

public boolean isAwaitingClient()
Description copied from interface: BackgammonConnection
Returns true if this connection is awaiting a client already.

Specified by:
isAwaitingClient in interface BackgammonConnection
Returns:
True if awaiting client, false otherwise.

close

public void close()
           throws java.io.IOException
Description copied from interface: BackgammonConnection
Closes all resources opened in this connection.

Specified by:
close in interface BackgammonConnection
Throws:
java.io.IOException

isClosed

public boolean isClosed()
Description copied from interface: BackgammonConnection
Returns if this connection has been closed or not.

Specified by:
isClosed in interface BackgammonConnection
Returns:
true if closed, false otherwise.