- Timestamp:
- 02/02/09 04:29:31 (13 years ago)
- Location:
- chess
- Files:
-
- 4 added
- 1 deleted
- 18 edited
- 5 moved
Legend:
- Unmodified
- Added
- Removed
-
chess/chess.jad
r7 r9 1 MIDlet-Jar-Size: 71 17581 MIDlet-Jar-Size: 716270 2 2 MIDlet-Jar-URL: chess.jar 3 3 MIDlet-Name: Chess -
chess/src/chess/Util.java
r7 r9 14 14 public static final PrintStream log = System.out; 15 15 16 public static final boolean STRICT = true; 16 /** 17 * âêëþ÷àåò äîïîëíèòåëüíûå ïðîâåðêè íà ñòàäèè âûïîëíåíèÿ, 18 * ïðè ýòîì îòêëþ÷àòü èõ íà ñòàäèè ïîäãîòîâêè production 19 */ 20 public static final boolean SAFE_MODE = true; 17 21 18 22 -
chess/src/chess/control/BaseController.java
r7 r9 26 26 protected final IGame game; 27 27 protected Player player; 28 protected final I Connection conn;28 protected final IRemoteNode node; 29 29 30 30 protected int status; … … 34 34 final IGame game, 35 35 final Player player, 36 final I Connection conn) {36 final IRemoteNode node ) { 37 37 this.app = app; 38 38 this.ui = ui; 39 39 this.game = game; 40 40 this.player = player; 41 this. conn = conn;42 conn.setListener( this );41 this.node = node; 42 node.setListener( this ); 43 43 ui.setUIListener( this ); 44 44 } … … 61 61 player = sa.getPlayer(); 62 62 if( isMaster() ) { 63 throw new IllegalStateException( " master should not got suchcommands" );63 throw new IllegalStateException( "Master should not got SET_PLAYER commands" ); 64 64 } 65 65 } else if( sa.type() == SystemAction.YOUR_TURN_TYPE ) { 66 66 if( player == null ) { 67 throw new IllegalStateException( "Player == null" );67 throw new IllegalStateException( "Player == null" ); 68 68 } 69 69 setStatus( WAITING_FOR_LOCAL ); … … 71 71 } 72 72 73 public void sent( final IDatagram data ) {73 public void wasSent( final IDatagram data ) { 74 74 setStatus( WAITING_FOR_OPPONENT ); 75 75 } … … 81 81 public void post( final IDatagram data ) { 82 82 setStatus( WAITING_FOR_NETWORK ); 83 conn.send( data );83 node.send( data ); 84 84 } 85 85 -
chess/src/chess/control/ClientController.java
r4 r9 3 3 import chess.game.App; 4 4 import chess.game.IGame; 5 import chess.remote.I Connection;5 import chess.remote.IRemoteNode; 6 6 import chess.ui.GameCanvas; 7 7 … … 19 19 * 20 20 * @param game 21 * @param conn21 * @param node 22 22 */ 23 23 public ClientController( final App app, 24 24 final GameCanvas canvas, 25 25 final IGame game, 26 final I Connection conn) {27 super( app, canvas, game, null, conn);26 final IRemoteNode node ) { 27 super( app, canvas, game, null, node ); 28 28 //wait for incoming actions log 29 29 synchronized ( this ) { -
chess/src/chess/control/MasterController.java
r4 r9 1 1 package chess.control; 2 2 3 import java.util. Vector;3 import java.util.*; 4 4 5 import chess.game.Action; 6 import chess.game.App; 7 import chess.game.IGame; 8 import chess.game.Player; 9 import chess.remote.IConnection; 10 import chess.remote.SystemAction; 5 import chess.game.*; 6 import chess.remote.IRemoteNode; 11 7 import chess.ui.GameCanvas; 12 8 … … 30 26 final GameCanvas canvas, 31 27 final IGame game, 32 final I Connectionconn,28 final IRemoteNode conn, 33 29 final Player player ) { 34 30 super( app, canvas, game, player, conn ); -
chess/src/chess/control/SystemAction.java
r7 r9 1 package chess. remote;1 package chess.control; 2 2 3 3 import java.io.IOException; … … 5 5 import java.io.OutputStream; 6 6 7 import chess.Util; 7 8 import chess.game.Player; 9 import chess.remote.IDatagram; 8 10 9 11 … … 15 17 */ 16 18 public final class SystemAction implements IDatagram { 17 public static final intSET_PLAYER = 0;18 public static final intYOUR_TURN_TYPE = 1;19 public static final byte SET_PLAYER = 0; 20 public static final byte YOUR_TURN_TYPE = 1; 19 21 20 private inttype;21 22 private byte type; 23 private Player player; 22 24 23 24 25 25 public SystemAction( final InputStream is ) throws IOException { 26 read( is ); 27 } 26 28 27 public SystemAction( final int type, 28 final Player player ) { 29 this.type = type; 30 this.player = player; 31 } 29 public SystemAction( final int type, 30 final Player player ) { 31 checkType( type ); 32 if( player == null ) { 33 throw new IllegalArgumentException( "Player can't be null" ); 34 } 35 this.type = ( byte ) type; 36 this.player = player; 37 } 32 38 33 34 35 39 public int type() { 40 return type; 41 } 36 42 37 38 39 43 public Player getPlayer() { 44 return player; 45 } 40 46 41 42 43 44 47 public void write( final OutputStream os ) throws IOException { 48 os.write( type ); 49 os.write( player.ordinal() ); 50 } 45 51 46 public void read( final InputStream is ) throws IOException { 47 type = is.read(); 48 player = Player.byType( is.read() ); 49 } 52 public void read( final InputStream is ) throws IOException { 53 final int type = is.read(); 54 checkType( type ); 55 this.type = ( byte ) type; 56 player = Player.byType( is.read() ); 57 } 58 59 private static void checkType( final int type ) { 60 if( Util.SAFE_MODE ) { 61 if( type != SET_PLAYER && type != YOUR_TURN_TYPE ) { 62 throw new IllegalArgumentException( "Type " + type + " is unknown" ); 63 } 64 } 65 } 66 67 68 public boolean equals( final Object o ) { 69 if( this == o ) { 70 return true; 71 } 72 if( o == null || getClass() != o.getClass() ) { 73 return false; 74 } 75 76 final SystemAction that = ( SystemAction ) o; 77 78 return player == that.player && type == that.type; 79 80 } 81 82 public int hashCode() { 83 int result; 84 result = ( int ) type; 85 result = 31 * result + player.hashCode(); 86 return result; 87 } 50 88 } -
chess/src/chess/game/Action.java
r5 r9 15 15 */ 16 16 public class Action implements IDatagram { 17 18 17 private Player player; 18 private int duration; 19 19 20 21 20 private Location from; 21 private Location to; 22 22 23 24 25 23 public Action( final InputStream is ) throws IOException { 24 read( is ); 25 } 26 26 27 28 29 30 31 32 33 34 35 27 public Action( final Player player, 28 final int duration, 29 final Location from, 30 final Location to ) { 31 this.player = player; 32 this.duration = duration; 33 this.from = from; 34 this.to = to; 35 } 36 36 37 38 39 40 41 42 43 44 45 46 47 37 public Action( final Player player, 38 final int duration, 39 final int xFrom, 40 final int yFrom, 41 final int xTo, 42 final int yTo ) { 43 this( player, duration, 44 new Location( xFrom, yFrom ), 45 new Location( xTo, yTo ) 46 ); 47 } 48 48 49 50 51 49 public Location from() { 50 return from; 51 } 52 52 53 54 55 53 public Location to() { 54 return to; 55 } 56 56 57 58 59 57 public int duration() { 58 return duration; 59 } 60 60 61 62 63 61 public Player getPlayer() { 62 return player; 63 } 64 64 65 public void write( final OutputStream os ) throws IOException { 66 os.write( player.ordinal() ); 67 os.write( duration ); 68 os.write( from.x ); 69 os.write( from.y ); 70 os.write( to.x ); 71 os.write( to.y ); 72 } 65 public void write( final OutputStream os ) throws IOException { 66 os.write( player.ordinal() ); 73 67 74 public void read( final InputStream is ) throws IOException { 75 player = Player.byType( is.read() ); 76 duration = is.read(); 77 from = new Location( is.read(), is.read() ); 78 to = new Location( is.read(), is.read() ); 79 } 68 os.write( duration ); 69 os.write( duration >> 8 ); 70 os.write( duration >> 16 ); 71 os.write( duration >> 24 ); 72 73 os.write( from.x ); 74 os.write( from.y ); 75 os.write( to.x ); 76 os.write( to.y ); 77 } 78 79 public void read( final InputStream is ) throws IOException { 80 player = Player.byType( is.read() ); 81 duration = is.read() | is.read() << 8 | is.read() << 16 | is.read() << 24; 82 from = new Location( is.read(), is.read() ); 83 to = new Location( is.read(), is.read() ); 84 } 85 86 87 public boolean equals( final Object o ) { 88 if( this == o ) { 89 return true; 90 } 91 if( o == null || getClass() != o.getClass() ) { 92 return false; 93 } 94 95 final Action action = ( Action ) o; 96 97 if( duration != action.duration ) { 98 return false; 99 } 100 if( !from.equals( action.from ) ) { 101 return false; 102 } 103 if( !player.equals( action.player ) ) { 104 return false; 105 } 106 if( !to.equals( action.to ) ) { 107 return false; 108 } 109 110 return true; 111 } 112 113 public int hashCode() { 114 int result; 115 result = player.hashCode(); 116 result = 31 * result + duration; 117 result = 31 * result + from.hashCode(); 118 result = 31 * result + to.hashCode(); 119 return result; 120 } 80 121 } -
chess/src/chess/game/App.java
r7 r9 4 4 import chess.control.ClientController; 5 5 import chess.control.MasterController; 6 import chess.remote.I Connection;6 import chess.remote.IRemoteNode; 7 7 import chess.ui.ChessMIDlet; 8 8 import chess.ui.GameCanvas; … … 31 31 final Game game, 32 32 final GameCanvas canvas, 33 final I Connection conn) {34 this( midlet, game, canvas, conn, null );33 final IRemoteNode node ) { 34 this( midlet, game, canvas, node, null ); 35 35 } 36 36 … … 39 39 final Game game, 40 40 final GameCanvas canvas, 41 final I Connection conn,41 final IRemoteNode node, 42 42 final Player player ) { 43 43 this.midlet = midlet; … … 47 47 canvas.init( game ); 48 48 if( master ) { 49 remote = new MasterController( this, canvas, game, conn, player );49 remote = new MasterController( this, canvas, game, node, player ); 50 50 } else { 51 remote = new ClientController( this, canvas, game, conn);51 remote = new ClientController( this, canvas, game, node ); 52 52 } 53 53 } -
chess/src/chess/game/Game.java
r7 r9 209 209 private final void check( final int x, 210 210 final int y ) { 211 if( Util.S TRICT&& !isValid( x, y ) ) {211 if( Util.SAFE_MODE && !isValid( x, y ) ) { 212 212 throw new IllegalArgumentException( "Invalid location:" + x + ", " + y + " is out of bounds!" ); 213 213 } -
chess/src/chess/game/Location.java
r5 r9 9 9 */ 10 10 public final class Location { 11 12 11 public final int x; 12 public final int y; 13 13 14 15 16 17 18 14 public Location( final int x, 15 final int y ) { 16 this.x = x; 17 this.y = y; 18 } 19 19 20 21 if( this == o ) {22 23 24 if( o == null || getClass() != o.getClass() ) {25 26 20 public boolean equals( final Object o ) { 21 if( this == o ) { 22 return true; 23 } 24 if( o == null || getClass() != o.getClass() ) { 25 return false; 26 } 27 27 28 final Location l = ( Location )o;28 final Location l = ( Location ) o; 29 29 30 31 30 return x == l.x && y == l.y; 31 } 32 32 33 33 34 35 36 37 38 39 34 public int hashCode() { 35 int result; 36 result = x; 37 result = 31 * result + y; 38 return result; 39 } 40 40 41 42 43 41 public String toString() { 42 return "[" + x + ", " + y + "]"; 43 } 44 44 45 46 return ( 'A' + x) + "" + ( y + 1 );47 45 public String toChessNotation() { 46 return ( ( char ) ( 'A' + x ) ) + "" + ( y + 1 ); 47 } 48 48 49 50 51 52 49 public int ldistance( final int x, 50 final int y ) { 51 return Math.abs( this.x - x ) + Math.abs( this.y - y ); 52 } 53 53 54 55 56 54 public int ldistance( final Location l ) { 55 return ldistance( l.x, l.y ); 56 } 57 57 } -
chess/src/chess/game/Player.java
r7 r9 26 26 case 1: 27 27 return BLACK; 28 default: 29 throw new IllegalArgumentException( "Unknown player:" + type ); 28 30 } 29 throw new IllegalStateException( "Unknown player:" + type );30 31 } 31 32 -
chess/src/chess/remote/IClientListener.java
r5 r9 7 7 * Time: 19:02:46<br/> 8 8 */ 9 public interface IClientListener {10 public void finished( final IRemoteNode[] servers );9 public interface IClientListener extends IAsyncProcessListener{ 10 // public void finished( final IRemoteNode[] servers ); 11 11 } -
chess/src/chess/remote/IConnectionListener.java
r4 r9 9 9 public interface IConnectionListener { 10 10 11 11 public void received( final IDatagram data ); 12 12 13 public void sent( final IDatagram data );13 public void wasSent( final IDatagram data ); 14 14 15 15 public void closed(); 16 16 } -
chess/src/chess/remote/IDatagram.java
r4 r9 1 1 package chess.remote; 2 2 3 import java.io.OutputStream; 4 import java.io.IOException; 5 import java.io.InputStream; 3 import java.io.*; 6 4 7 5 /** … … 12 10 */ 13 11 public interface IDatagram { 14 void write(OutputStream os ) throws IOException;12 public void write( final OutputStream os ) throws IOException; 15 13 16 void read(InputStream is ) throws IOException;14 public void read( final InputStream is ) throws IOException; 17 15 } -
chess/src/chess/remote/IRemote.java
r7 r9 7 7 * Time: 15:58:43<br/> 8 8 */ 9 public interface IRemote Manager{9 public interface IRemote { 10 10 11 public voidwaitForIncoming( final IServerListener l );11 public IAsyncProcess waitForIncoming( final IServerListener l ); 12 12 13 14 13 public void getAvailable( final boolean force, 14 final IClientListener l ); 15 15 16 16 } -
chess/src/chess/remote/IRemoteNode.java
r5 r9 12 12 public String getName(); 13 13 14 public IConnection connect() throws IOException; 14 public void setListener( final IConnectionListener l ); 15 16 public void ensureConnected() throws IOException; 17 18 public void send( final IDatagram data ); 19 20 public void close(); 15 21 } -
chess/src/chess/remote/IServerListener.java
r4 r9 8 8 * Time: 16:20:45<br/> 9 9 */ 10 public interface IServerListener { 11 /** 12 * Èíôîðìàöèÿ î çàïðîñå íà ïîäêëþ÷åíèå îò êëèåíòà. 13 * 14 * @param client 15 * @return true åñëè çàïðîñ ïðèíÿò, è ïðîñëóøèâàíèå äîëæíî áûòü ïðåêðàùåíî 16 * fixme ñäåëàòü âîçìîæíîñòü ïðèíÿòü íåñêîëüêî ñîåäèíåíèé îò êëèåíòîâ? 17 */ 18 public boolean incoming( final IRemoteNode client ); 19 20 public void stopped(); 10 public interface IServerListener extends IAsyncProcessListener { 11 /** 12 * Èíôîðìàöèÿ î çàïðîñå íà ïîäêëþ÷åíèå îò êëèåíòà. 13 * <p/> 14 * fixme ñäåëàòü âîçìîæíîñòü ïðèíÿòü íåñêîëüêî ñîåäèíåíèé îò êëèåíòîâ? 15 * 16 * @return true åñëè çàïðîñ ïðèíÿò, è ïðîñëóøèâàíèå äîëæíî áûòü ïðåêðàùåíî 17 */ 18 public boolean incoming( final IRemoteNode client ); 21 19 } -
chess/src/chess/remote/impl/DummyRemote.java
r7 r9 2 2 3 3 import java.io.*; 4 import java.util.Timer; 5 import java.util.TimerTask; 4 import java.util.*; 6 5 import javax.microedition.io.StreamConnection; 7 6 8 import chess.remote.IClientListener; 9 import chess.remote.IRemoteManager; 10 import chess.remote.IRemoteNode; 11 import chess.remote.IServerListener; 7 import chess.remote.*; 12 8 13 9 … … 18 14 * Time: 16:25:10<br/> 19 15 */ 20 public class DummyRemote Manager implements IRemoteManager{21 16 public class DummyRemote implements IRemote { 17 private final Timer timer = new Timer(); 22 18 23 public void waitForIncoming( final IServerListener l ) { 24 timer.schedule( 25 new TimerTask() { 26 public void run() { 27 final RemoteNode client = new RemoteNode( 28 new DummyStreamConnection() 29 ); 30 l.incoming( client ); 31 } 32 }, 33 1000 34 ); 35 } 19 public IAsyncProcess waitForIncoming( final IServerListener l ) { 20 timer.schedule( 21 new TimerTask() { 22 public void run() { 23 try { 24 final RemoteNode client = new RemoteNode( 25 new DummyStreamConnection() 26 ); 27 l.incoming( client ); 28 } catch( IOException e ) { 29 e.printStackTrace(); 30 } 31 } 32 }, 33 1000 34 ); 35 return null; 36 } 36 37 37 38 39 40 41 42 43 44 45 46 47 38 public void getAvailable( final boolean force, 39 final IClientListener l ) { 40 timer.schedule( 41 new TimerTask() { 42 public void run() { 43 l.finished( new IRemoteNode[0] ); 44 } 45 }, 46 100 47 ); 48 } 48 49 49 50 51 return new ByteArrayInputStream( new byte[]{ } );52 50 private static class DummyStreamConnection implements StreamConnection { 51 public InputStream openInputStream() throws IOException { 52 return new ByteArrayInputStream( new byte[] { } ); 53 } 53 54 54 55 56 55 public DataInputStream openDataInputStream() throws IOException { 56 throw new RuntimeException( "Not implemented yet" ); 57 } 57 58 58 59 60 59 public OutputStream openOutputStream() throws IOException { 60 return new ByteArrayOutputStream(); 61 } 61 62 62 63 64 63 public DataOutputStream openDataOutputStream() throws IOException { 64 throw new RuntimeException( "Not implemented yet" ); 65 } 65 66 66 67 public void close() throws IOException { 67 68 68 69 69 } 70 } 70 71 } -
chess/src/chess/remote/impl/GenericDatagram.java
r7 r9 1 package chess.remote ;1 package chess.remote.impl; 2 2 3 3 import java.io.IOException; … … 6 6 7 7 import chess.game.Action; 8 import chess.remote.IDatagram; 9 import chess.control.SystemAction; 8 10 9 11 … … 15 17 */ 16 18 public class GenericDatagram implements IDatagram { 17 private static final intACTION_TYPE = 0;18 private static final intSYSTEM_ACTION_TYPE = 1;19 private static final byte ACTION_TYPE = 0; 20 private static final byte SYSTEM_ACTION_TYPE = 1; 19 21 20 private inttype;21 22 private byte type; 23 public IDatagram wrapped; 22 24 23 24 25 25 public GenericDatagram( final InputStream is ) throws IOException { 26 read( is ); 27 } 26 28 27 28 29 if( wrapped instanceof Action ) {30 31 } else if( wrapped instanceof SystemAction ) {32 33 34 35 36 29 public GenericDatagram( final IDatagram wrapped ) { 30 this.wrapped = wrapped; 31 if( wrapped instanceof Action ) { 32 type = ACTION_TYPE; 33 } else if( wrapped instanceof SystemAction ) { 34 type = SYSTEM_ACTION_TYPE; 35 } else { 36 throw new IllegalStateException( "type " + wrapped + " is unknown" ); 37 } 38 } 37 39 38 39 40 41 40 public void write( final OutputStream os ) throws IOException { 41 os.write( type ); 42 wrapped.write( os ); 43 } 42 44 43 public void read( final InputStream is ) throws IOException { 44 type = is.read(); 45 switch ( type ) { 46 case ACTION_TYPE: 47 wrapped = new Action( is ); 48 break; 49 case SYSTEM_ACTION_TYPE: 50 wrapped = new SystemAction( is ); 51 break; 52 default: 53 throw new IllegalStateException( "type " + type + " is unknown" ); 54 } 55 } 45 public void read( final InputStream is ) throws IOException { 46 type = ( byte ) is.read(); 47 switch( type ) { 48 case ACTION_TYPE: 49 wrapped = new Action( is ); 50 break; 51 case SYSTEM_ACTION_TYPE: 52 wrapped = new SystemAction( is ); 53 break; 54 default: 55 throw new IllegalStateException( "type " + type + " is unknown" ); 56 } 57 } 58 59 60 public boolean equals( final Object o ) { 61 if( this == o ) { 62 return true; 63 } 64 if( o == null || getClass() != o.getClass() ) { 65 return false; 66 } 67 68 final GenericDatagram that = ( GenericDatagram ) o; 69 70 if( type != that.type ) { 71 return false; 72 } 73 if( !wrapped.equals( that.wrapped ) ) { 74 return false; 75 } 76 77 return true; 78 } 79 80 public int hashCode() { 81 int result; 82 result = ( int ) type; 83 result = 31 * result + wrapped.hashCode(); 84 return result; 85 } 56 86 } -
chess/src/chess/remote/impl/Remote.java
r7 r9 8 8 import javax.microedition.io.StreamConnectionNotifier; 9 9 10 import chess.Util; 10 11 import chess.remote.*; 11 import chess.Util;12 12 13 13 … … 18 18 * Time: 16:25:10<br/> 19 19 */ 20 public class Remote Manager implements IRemoteManager{20 public class Remote implements IRemote { 21 21 private static final String MY_SERVICE_NUMBER = "3B9FA89520078C303355AAA694238F08;name=ChessGameServer"; 22 23 private static final IRemoteNode[] EMPTY = new IRemoteNode[0]; 24 22 25 private final LocalDevice localDevice; 23 24 26 private final Vector devices = new Vector(); 25 private static final IRemoteNode[] EMPTY = new IRemoteNode[0]; 26 27 private RemoteManager() throws Exception { 27 28 private Remote() throws Exception { 28 29 localDevice = LocalDevice.getLocalDevice(); 29 30 fillupCache(); … … 48 49 } 49 50 50 public void waitForIncoming( final IServerListener l ) { 51 try { 52 localDevice.setDiscoverable( DiscoveryAgent.GIAC ); 53 new WaitForIncomingThread( l ); 54 } catch( IOException ex ) { 55 ex.printStackTrace(); 56 } 51 public IAsyncProcess waitForIncoming( final IServerListener l ) { 52 return new WaitForIncomingThread( localDevice, l ); 57 53 } 58 54 … … 68 64 public void deviceDiscovered( final RemoteDevice btDevice, 69 65 final DeviceClass cod ) { 70 66 Util.log.println( "device found:" + btDevice.getBluetoothAddress() ); 71 67 } 72 68 … … 102 98 return EMPTY; 103 99 } 104 final IRemoteNode[] nodes = new IRemoteNode[devices.size()]; 105 for( int i = 0; i < devices.size(); i++ ) { 100 final int length = devices.size(); 101 final IRemoteNode[] nodes = new IRemoteNode[length]; 102 for( int i = 0; i < length; i++ ) { 106 103 try { 107 104 final RemoteDevice device = ( RemoteDevice ) devices.elementAt( i ); … … 115 112 } 116 113 117 public static IRemote Managercreate() throws Exception {118 // return new Remote Manager();119 return new DummyRemote Manager();120 } 121 122 private static class WaitForIncomingThread extends Thread {114 public static IRemote create() throws Exception { 115 // return new Remote(); 116 return new DummyRemote(); 117 } 118 119 private static class WaitForIncomingThread extends Thread implements IAsyncProcess { 123 120 private final IServerListener listener; 124 125 public WaitForIncomingThread( final IServerListener listener ) { 121 private final LocalDevice localDevice; 122 private volatile int status = -1; 123 124 public WaitForIncomingThread( final LocalDevice local, 125 final IServerListener listener ) { 126 this.localDevice = local; 126 127 this.listener = listener; 127 128 start(); … … 130 131 public void run() { 131 132 try { 133 localDevice.setDiscoverable( DiscoveryAgent.GIAC ); 134 status = IN_PROCESS; 132 135 while( true ) { 133 136 try { … … 147 150 //åñëè êëèåíò ïðèíÿò -- âûõîäèì (èãðà äîïóñêàåò òîëüêî îäíîãî ñîïåðíèêà) 148 151 Util.log.println( "client accepted -> close server" ); 152 status = SUCCEEDED; 149 153 return; 150 154 } … … 162 166 } catch( IOException ex ) { 163 167 ex.printStackTrace(); 168 status = FAILED; 164 169 return; 165 170 } 166 171 } 172 } catch( IOException ex ) { 173 ex.printStackTrace(); 174 status = FAILED; 167 175 } finally { 168 listener. stopped();176 listener.finished( null ); 169 177 Util.log.println( "server stopped" ); 170 178 } 171 179 } 180 181 182 public int status() { 183 return status; 184 } 185 186 public synchronized void interrupt( final boolean waitFor ) { 187 if( isAlive() ) { 188 interrupt(); 189 if( waitFor && Thread.currentThread() != this ) { 190 try { 191 join(); 192 } catch( InterruptedException e ) { 193 e.printStackTrace(); 194 } 195 } 196 } 197 } 198 199 public IAsyncProcessListener getListener() { 200 return listener; 201 } 172 202 } 173 203 } -
chess/src/chess/remote/impl/RemoteNode.java
r5 r9 8 8 import javax.microedition.io.StreamConnection; 9 9 10 import chess.remote.*; 10 import chess.remote.IConnectionListener; 11 import chess.remote.IDatagram; 12 import chess.remote.IRemoteNode; 11 13 12 14 … … 17 19 * Time: 16:33:50<br/> 18 20 */ 19 public class RemoteNode implements IRemoteNode, IConnection{21 public class RemoteNode implements IRemoteNode, Runnable { 20 22 21 23 private String name; 22 24 23 24 25 25 private RemoteDevice device; 26 private IConnectionListener listener = null; 27 private StreamConnection connection = null; 26 28 27 private transient InputStream in; 28 private transient OutputStream out; 29 private transient InputStream in; 30 private transient OutputStream out; 31 private transient Thread pump; 29 32 30 31 32 33 33 public RemoteNode( final RemoteDevice device ) throws IOException { 34 this.device = device; 35 name = device.getFriendlyName( false ); 36 } 34 37 35 public RemoteNode( final StreamConnection conn ) { 36 this( "", conn ); 37 } 38 public RemoteNode( final StreamConnection conn ) throws IOException { 39 this.connection = conn; 40 this.device = RemoteDevice.getRemoteDevice( connection ); 41 this.name = device.getFriendlyName( true ); 42 } 38 43 39 public RemoteNode( final String name, 40 final StreamConnection connection ) { 41 this.name = name; 42 this.connection = connection; 43 } 44 public RemoteNode( final String name, 45 final StreamConnection conn ) throws IOException { 46 this.name = name; 47 this.connection = conn; 48 this.device = RemoteDevice.getRemoteDevice( conn ); 49 } 44 50 45 46 47 51 public String getName() { 52 return name; 53 } 48 54 49 public IConnection connect() throws IOException {50 if ( connection == null &&device == null ) {51 52 } else if( connection == null ) {53 connection = ( StreamConnection )Connector.open( device.getBluetoothAddress() );54 55 public void ensureConnected() throws IOException { 56 if( device == null ) { 57 throw new IllegalStateException( "Already closed node" ); 58 } else if( connection == null ) { 59 connection = ( StreamConnection ) Connector.open( device.getBluetoothAddress() ); 60 } 55 61 56 try { 57 in = connection.openInputStream(); 58 } catch ( IOException ex ) { 59 ex.printStackTrace(); 60 throw new RuntimeException( "Bad:" + ex.getMessage() ); 61 } 62 try { 63 out = connection.openOutputStream(); 64 } catch ( IOException ex ) { 65 ex.printStackTrace(); 66 throw new RuntimeException( "Bad:" + ex.getMessage() ); 67 } 68 new Thread() { 69 public void run() { 70 try { 71 while ( true ) { 72 synchronized ( this ) { 73 if ( in.available() > 0 ) { 74 final GenericDatagram data = new GenericDatagram( in ); 75 if ( listener != null ) { 76 listener.received( data.wrapped ); 77 } 78 } 79 try { 80 wait( 100 ); 81 } catch ( InterruptedException e ) { 82 } 83 } 84 } 85 } catch ( IOException e ) { 86 e.printStackTrace(); 87 return; 88 } 89 } 90 }.start(); 91 return this; 92 } 62 try { 63 in = connection.openInputStream(); 64 } catch( IOException ex ) { 65 ex.printStackTrace(); 66 throw new RuntimeException( "Bad:" + ex.getMessage() ); 67 } 68 try { 69 out = connection.openOutputStream(); 70 } catch( IOException ex ) { 71 ex.printStackTrace(); 72 throw new RuntimeException( "Bad:" + ex.getMessage() ); 73 } 74 if( pump != null && pump.isAlive() ) { 75 pump.interrupt(); 76 } 77 pump = new Thread( this, "network pump" ); 78 pump.start(); 79 // return this; 80 } 93 81 94 95 96 82 public void setListener( final IConnectionListener l ) { 83 this.listener = l; 84 } 97 85 98 99 if( out != null ) {100 101 102 103 } catch( IOException ex ) {104 105 106 107 if( listener != null ) {108 listener.sent( data );109 110 111 86 public void send( final IDatagram data ) { 87 if( out != null ) { 88 try { 89 final GenericDatagram gdata = new GenericDatagram( data ); 90 gdata.write( out ); 91 } catch( IOException ex ) { 92 ex.printStackTrace(); 93 throw new RuntimeException( ex.getMessage() ); 94 } 95 if( listener != null ) { 96 listener.wasSent( data ); 97 } 98 } 99 } 112 100 113 public void close() { 114 if ( in != null ) { 115 try { 116 in.close(); 117 } catch ( IOException e ) { 118 e.printStackTrace(); 119 } 120 } 121 if ( out != null ) { 122 try { 123 out.close(); 124 } catch ( IOException e ) { 125 e.printStackTrace(); 126 } 127 } 128 try { 129 if ( connection != null ) { 130 connection.close(); 131 } 132 } catch ( IOException e ) { 133 e.printStackTrace(); 134 } 135 connection = null; 136 if ( listener != null ) { 137 listener.closed(); 138 } 139 } 101 public void close() { 102 if( in != null ) { 103 try { 104 in.close(); 105 } catch( IOException e ) { 106 e.printStackTrace(); 107 } finally { 108 in = null; 109 } 110 } 111 if( out != null ) { 112 try { 113 out.close(); 114 } catch( IOException e ) { 115 e.printStackTrace(); 116 } finally { 117 out = null; 118 } 119 } 120 if( connection != null ) { 121 try { 122 connection.close(); 123 } catch( IOException e ) { 124 e.printStackTrace(); 125 } finally { 126 connection = null; 127 } 128 } 129 if( listener != null ) { 130 listener.closed(); 131 } 132 if( pump != null && pump.isAlive() ) { 133 pump.interrupt(); 134 } 135 pump = null; 136 device = null; 137 } 138 139 140 public void run() { 141 try { 142 while( true ) { 143 synchronized( this ) { 144 if( in.available() > 0 ) { 145 final GenericDatagram data = new GenericDatagram( in ); 146 if( listener != null ) { 147 listener.received( data.wrapped ); 148 } 149 } 150 try { 151 wait( 100 ); 152 } catch( InterruptedException e ) { 153 } 154 } 155 } 156 } catch( IOException e ) { 157 e.printStackTrace(); 158 return; 159 } 160 } 161 162 163 public String toString() { 164 return getName(); 165 } 140 166 } -
chess/src/chess/ui/ChessMIDlet.java
r7 r9 11 11 import chess.game.Player; 12 12 import chess.remote.*; 13 import chess.remote.impl.Remote Manager;13 import chess.remote.impl.Remote; 14 14 15 15 … … 47 47 48 48 /* network */ 49 private final IRemote Manager remoteManager;49 private final IRemote remote; 50 50 51 51 /* current game application */ … … 54 54 public ChessMIDlet() throws Exception { 55 55 display = Display.getDisplay( this ); 56 remote Manager = RemoteManager.create();56 remote = Remote.create(); 57 57 58 58 59 59 } 60 60 61 public IRemote ManagergetRemoteManager() {62 return remote Manager;61 public IRemote getRemoteManager() { 62 return remote; 63 63 } 64 64 65 65 public void connectToServer( final IRemoteNode server ) { 66 66 try { 67 final IConnection conn = server.connect();67 server.ensureConnected();//todo may be nahren? 68 68 app = new App( 69 69 ChessMIDlet.this, 70 70 new Game(), 71 71 gameCanvas, 72 conn72 server 73 73 ); 74 74 display.setCurrent( gameCanvas ); … … 102 102 display.setCurrent( waitScreen ); 103 103 Util.log.println( "new game:" + player ); 104 remote Manager.waitForIncoming(104 remote.waitForIncoming( 105 105 new IServerListener() { 106 106 public synchronized boolean incoming( final IRemoteNode client ) { 107 107 try { 108 final IConnection conn = client.connect();108 client.ensureConnected(); 109 109 app = new App( 110 110 ChessMIDlet.this, 111 111 new Game(), 112 112 gameCanvas, 113 c onn,113 client, 114 114 player 115 115 ); 116 116 display.setCurrent( gameCanvas ); 117 Util.log.println( "joined client" );117 Util.log.println( "joined client" ); 118 118 return true; 119 119 } catch( IOException e ) { … … 123 123 } 124 124 125 public void stopped() {125 public void finished( final Object res ) { 126 126 display.setCurrent( 127 127 new Alert( "Can't connect" ) … … 135 135 searchForm.refresh( false ); 136 136 display.setCurrent( searchForm ); 137 Util.log.println( "search server" );137 Util.log.println( "search server" ); 138 138 } 139 139 140 140 protected void showHelp() { 141 141 display.setCurrent( helpScreen ); 142 Util.log.println( "show help" );142 Util.log.println( "show help" ); 143 143 } 144 144 -
chess/src/chess/ui/JoinGameScreen.java
r7 r9 14 14 */ 15 15 public class JoinGameScreen extends List implements CommandListener, IClientListener { 16 16 private final ChessMIDlet midlet; 17 17 18 19 18 private IRemoteNode[] nodes; 19 private boolean force; 20 20 21 22 23 24 25 26 21 private final Alert nothingFoundAlert = new Alert( 22 "Nothing found", 23 "No servers available", 24 null, 25 AlertType.INFO 26 ); 27 27 28 29 30 31 32 33 28 private final Alert scanningNetworkAlert = new Alert( 29 "Scanning...", 30 "Looking for game servers", 31 null, 32 AlertType.INFO 33 ); 34 34 35 36 37 35 public JoinGameScreen( final ChessMIDlet midlet ) { 36 super( MSG.getMessage( "join-game.title" ), Choice.EXCLUSIVE ); 37 this.midlet = midlet; 38 38 39 40 39 scanningNetworkAlert.setTimeout( Alert.FOREVER ); 40 //refresh( false ); 41 41 42 43 44 42 addCommand( midlet.okCommand ); 43 addCommand( midlet.refreshCommand ); 44 addCommand( midlet.backCommand ); 45 45 46 47 46 setSelectCommand( midlet.okCommand ); 47 } 48 48 49 50 51 if( force && isShown() ) {52 53 54 55 49 public void refresh( final boolean force ) { 50 this.force = force; 51 if( force && isShown() ) { 52 midlet.display.setCurrent( scanningNetworkAlert ); 53 } 54 midlet.getRemoteManager().getAvailable( this.force, this ); 55 } 56 56 57 public void finished( final IRemoteNode[] servers ) { 58 if ( !force ) { 59 if ( servers == null || servers.length == 0 ) { 60 refresh( true ); 61 return; 62 } 63 } 64 nodes = servers; 65 if ( nodes == null || nodes.length == 0 ) { 66 midlet.display.setCurrent( nothingFoundAlert ); 67 } else { 68 for ( int i = 0; i < nodes.length; i++ ) { 69 final IRemoteNode node = nodes[i]; 70 append( node.getName(), null ); 71 } 72 midlet.display.setCurrent( this ); 73 } 74 } 57 public void finished( final Object res ) { 58 final IRemoteNode[] servers = ( IRemoteNode[] ) res; 59 if( !force ) { 60 if( servers == null || servers.length == 0 ) { 61 refresh( true ); 62 return; 63 } 64 } 65 nodes = servers; 66 if( nodes == null || nodes.length == 0 ) { 67 midlet.display.setCurrent( nothingFoundAlert ); 68 } else { 69 for( int i = 0; i < nodes.length; i++ ) { 70 final IRemoteNode node = nodes[i]; 71 append( node.getName(), null ); 72 } 73 midlet.display.setCurrent( this ); 74 } 75 } 75 76 76 77 78 if( command == midlet.backCommand ) {79 80 } else if( command == midlet.okCommand ) {81 82 if( index >= 0 ) {83 84 85 86 } else if( command == midlet.refreshCommand ) {87 88 89 90 77 public void commandAction( final Command command, 78 final Displayable displayable ) { 79 if( command == midlet.backCommand ) { 80 midlet.showStartScreen(); 81 } else if( command == midlet.okCommand ) { 82 final int index = getSelectedIndex(); 83 if( index >= 0 ) { 84 final IRemoteNode server = nodes[index]; 85 midlet.connectToServer( server ); 86 } 87 } else if( command == midlet.refreshCommand ) { 88 //todo wait screen 89 refresh( true ); 90 } 91 } 91 92 }
Note: See TracChangeset
for help on using the changeset viewer.