- Timestamp:
- 02/03/09 14:29:18 (13 years ago)
- Location:
- chess
- Files:
-
- 2 added
- 4 deleted
- 15 edited
Legend:
- Unmodified
- Added
- Removed
-
chess/chess.jad
r13 r14 1 MIDlet-Jar-Size: 693461 MIDlet-Jar-Size: 849333 2 2 MIDlet-Jar-URL: chess.jar 3 3 MIDlet-Name: chess -
chess/chess.xml
r13 r14 154 154 memorymaximumsize = "${compiler.max.memory}" 155 155 fork = "true" 156 target = "1.3" 156 157 executable = "${module.jdk.bin.chess}/javac"> 157 158 <compilerarg line = "${compiler.args.chess}" /> … … 217 218 <arg line = "-d ${tmp.dir.chess}" /> 218 219 <arg line = "-classpath ${jdk.home.SJWTK_2.5.2_01}/lib/midpapi21.jar;${jdk.home.SJWTK_2.5.2_01}/lib/cldcapi11.jar;${basedir}/libs/bluecove-2.1.0.jar" /> 219 <!--<arg line = "-classpath ${module.jdk.classpath.chess}" />-->220 <!--<arg line = "-classpath ${module.jdk.classpath.chess}" />--> 220 221 <!--\D:\JAVA\LIBS\jmock-1.1.0.jar\D:\JAVA\LIBS\junit-3.8.1.jar--> 221 222 <arg line = "${chess.output.dir}" /> … … 226 227 description = "Build mobile suite for module 'chess'"> 227 228 <property name = "mobile.path.jar" value = "${basedir}/chess.jar" /> 229 <!-- copy resources --> 230 <copy todir = "${tmp.dir.chess}"> 231 <fileset dir = "${basedir}/src"> 232 <type type = "file" /> 233 <patternset refid = "compiler.resources" /> 234 </fileset> 235 </copy> 228 236 <jar destfile = "${mobile.path.jar}" duplicate = "preserve"> 229 237 <zipfileset dir = "${tmp.dir.chess}" prefix = "" /> -
chess/src/chess/MSG.properties
r7 r14 2 2 command.exit=\u0412\u044b\u0445\u043e\u0434 3 3 command.back=\u041d\u0430\u0437\u0430\u0434 4 command.cancel=\u041e\u0442\u043c\u0435\u043d\u0438\u0442\u044c 4 5 command.refresh=\u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c 5 6 command.close=\u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c … … 12 13 start-menu.join-game= \u041f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c\u0441\u044f 13 14 start-menu.help= \u0421\u043f\u0440\u0430\u0432\u043a\u0430 15 start-menu.exit= \u0412\u044b\u0439\u0442\u0438 14 16 15 17 join-game.title=\u0412\u044b\u0431\u0435\u0440\u0438\u0442\u0435 18 join-game.no-server-found.title =\u041d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e :( 19 join-game.no-server-found.message= \u041f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u0438\u0433\u0440 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e 20 join-game.scanning-network.title = \u041f\u043e\u0438\u0441\u043a \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u0432... 21 join-game.scanning-network.message = \u0418\u0449\u0435\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 22 23 new-game.incoming-client = \u0421\u043e\u0433\u043b\u0430\u0441\u043d\u044b \u0438\u0433\u0440\u0430\u0442\u044c? 24 new-game.incoming-client.description = \u0412\u0445\u043e\u0434\u044f\u0449\u0435\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043e\u0442 {0}. \u0418\u0433\u0440\u0430\u0442\u044c \u0441 \u043d\u0438\u043c? 16 25 17 26 wait-for-bluetooth.title = \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f bluetooth. \u041f\u043e\u0434\u043e\u0436\u0434\u0438\u0442\u0435... 18 27 19 wait-for-opponent.title = \u041e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 \u0445\u043e\u0434\u0430 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u0438\u043a\u0430... 28 wait.title = \u041e\u0436\u0438\u0434\u0430\u043d\u0438\u0435... 29 wait.for-opponent = \u041e\u0436\u0438\u0434\u0430\u0435\u043c \u0445\u043e\u0434 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u0438\u043a\u0430... 30 wait.for-network = \u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0434\u0430\u043d\u043d\u044b\u0445... 20 31 21 32 choose-player.title = \u0412\u044b\u0431\u0435\u0440\u0438\u0442\u0435 \u0441\u0442\u043e\u0440\u043e\u043d\u0443: -
chess/src/chess/control/BaseController.java
r11 r14 1 1 package chess.control; 2 2 3 import java.util.Vector; 4 import java.io.IOException; 3 5 import javax.microedition.lcdui.Command; 4 6 import javax.microedition.lcdui.Displayable; … … 16 18 * Time: 13:24:44<br/> 17 19 */ 18 public abstractclass BaseController implements IConnectionListener, IUIListener {20 public class BaseController implements IConnectionListener, IUIListener { 19 21 public static final int WAITING_FOR_LOCAL = 0; 20 22 public static final int WAITING_FOR_NETWORK = 1; … … 28 30 protected final IRemoteNode node; 29 31 32 private final boolean master; 33 30 34 protected int status; 31 35 32 p ublicBaseController( final App app,33 final GameCanvas ui,34 final IGame game,35 final Player player,36 final IRemoteNode node ){36 private BaseController( final App app, 37 final GameCanvas ui, 38 final IGame game, 39 final Player player, 40 final IRemoteNode node ) throws IOException { 37 41 this.app = app; 38 42 this.ui = ui; … … 40 44 this.player = player; 41 45 this.node = node; 46 47 master = player != null; 48 42 49 node.setListener( this ); 50 node.ensureConnected(); 51 43 52 ui.setUIListener( this ); 44 } 45 46 public void received( final IDatagram data ) { 53 if ( master ) { 54 sendLog( game, player, node ); 55 } else { 56 //wait for incoming actions log 57 synchronized ( this ) { 58 try { 59 wait(); 60 } catch ( InterruptedException e ) { 61 e.printStackTrace(); 62 } 63 } 64 } 65 } 66 67 private static void sendLog( final IGame game, 68 final Player player, 69 final IRemoteNode node ) { 70 //ïðè ñîåäèíåíèè â ïåðâóþ î÷åðåäü ïåðåäàåì ïàðòíåðó ñîñòîÿíèå èãðû 71 final Vector log = game.getActionsLog(); 72 for ( int i = 0; i < log.size(); i++ ) { 73 final Action a = ( Action )log.elementAt( i ); 74 node.send( a ); 75 } 76 //çàòåì êåì îí èãðàåò 77 node.send( new SystemAction( SystemAction.SET_PLAYER, player.opponent() ) ); 78 79 /*ðåøàåì, êòî õîäèò*/ 80 if ( log.isEmpty() ) {//íà÷àëî èãðû - õîäÿò áåëûå 81 if ( player.isWhite() ) { 82 //todo enable ui 83 } else { 84 node.send( new SystemAction( SystemAction.YOUR_TURN, player.opponent() ) ); 85 } 86 } else { 87 final Action lastAction = ( Action )log.elementAt( log.size() - 1 ); 88 if ( lastAction.getPlayer() == player ) { 89 //todo enable ui 90 } else { 91 node.send( new SystemAction( SystemAction.YOUR_TURN, player.opponent() ) ); 92 } 93 } 94 } 95 96 public synchronized void received( final IDatagram data ) { 47 97 if ( data instanceof Action ) { 48 98 processAction( ( Action )data ); … … 50 100 final SystemAction sa = ( SystemAction )data; 51 101 processSystemAction( sa ); 102 } else { 103 throw new IllegalArgumentException( "Datagram " + data + " is in unknown format" ); 52 104 } 53 105 } … … 67 119 throw new IllegalStateException( "Player == null" ); 68 120 } 121 if ( !master ) { 122 synchronized ( this ) { 123 notifyAll(); 124 } 125 } 69 126 setStatus( WAITING_FOR_LOCAL ); 70 127 } 71 128 } 72 129 73 public void wasSent( final IDatagram data ) {130 public synchronized void wasSent( final IDatagram data ) { 74 131 setStatus( WAITING_FOR_OPPONENT ); 75 132 } … … 84 141 } 85 142 86 public void setStatus( final int status ) {143 public synchronized void setStatus( final int status ) { 87 144 //todo 88 145 this.status = status; … … 105 162 } 106 163 107 public abstract boolean isMaster(); 164 public boolean isMaster() { 165 return master; 166 } 108 167 109 168 public void keyPressed( final int code ) { … … 176 235 ui.setSelectedCell( null ); 177 236 ui.clearHighlightedCells(); 178 post( action ); 179 post( new SystemAction( SystemAction.YOUR_TURN, player ) ); 237 postAction( action ); 180 238 } 181 239 } else { … … 184 242 185 243 } 244 } 245 246 private void postAction( final Action action ) { 247 post( action ); 248 post( new SystemAction( SystemAction.YOUR_TURN, player ) ); 186 249 } 187 250 … … 222 285 return available; 223 286 } 287 288 public static BaseController createClient( final App app, 289 final GameCanvas ui, 290 final IGame game, 291 final IRemoteNode node ) throws IOException { 292 return new BaseController( app, ui, game, null, node ); 293 } 294 295 public static BaseController createMaster( final App app, 296 final GameCanvas ui, 297 final IGame game, 298 final Player player, 299 final IRemoteNode node ) throws IOException { 300 return new BaseController( app, ui, game, player, node ); 301 } 224 302 } -
chess/src/chess/control/SystemAction.java
r11 r14 19 19 public static final byte SET_PLAYER = 0; 20 20 public static final byte YOUR_TURN = 1; 21 public static final byte SURRENDER = 2; 22 public static final byte ASK_FOR_DRAW = 3; 21 23 22 24 private byte type; … … 59 61 private static void checkType( final int type ) { 60 62 if ( Util.SAFE_MODE ) { 61 if ( type != SET_PLAYER && type != YOUR_TURN ) { 62 throw new IllegalArgumentException( "Type " + type + " is unknown" ); 63 switch ( type ) { 64 case SET_PLAYER: 65 case YOUR_TURN: 66 case SURRENDER: 67 case ASK_FOR_DRAW: 68 break; 69 default: 70 throw new IllegalArgumentException( "Type " + type + " is unknown" ); 63 71 } 64 72 } … … 93 101 case YOUR_TURN: 94 102 return "YOUR_TURN[" + player + "]"; 103 case SURRENDER: 104 return "SURRENDER[" + player + "]"; 105 case ASK_FOR_DRAW: 106 return "ASK_FOR_DRAW[" + player + "]"; 95 107 default: 96 108 throw new IllegalArgumentException( "type is unknown:" + type ); -
chess/src/chess/game/App.java
r9 r14 2 2 3 3 import chess.control.BaseController; 4 import chess.control.ClientController;5 import chess.control.MasterController;6 4 import chess.remote.IRemoteNode; 7 5 import chess.ui.ChessMIDlet; … … 16 14 */ 17 15 public class App { 18 16 private BaseController remote; 19 17 20 21 18 private boolean master; 19 private Player player; 22 20 23 24 21 private Game game; 22 private GameCanvas canvas; 25 23 26 24 private ChessMIDlet midlet; 27 25 28 26 29 /** client mode */ 30 public App( final ChessMIDlet midlet, 31 final Game game, 32 final GameCanvas canvas, 33 final IRemoteNode node ) { 34 this( midlet, game, canvas, node, null ); 35 } 27 /** 28 * client mode 29 */ 30 public App( final ChessMIDlet midlet, 31 final Game game, 32 final GameCanvas canvas, 33 final IRemoteNode node ) throws Exception { 34 this( midlet, game, canvas, node, null ); 35 } 36 36 37 /** server mode */ 38 public App( final ChessMIDlet midlet, 39 final Game game, 40 final GameCanvas canvas, 41 final IRemoteNode node, 42 final Player player ) { 43 this.midlet = midlet; 44 this.game = game; 45 this.player = player; 46 master = player != null; 47 canvas.init( game ); 48 if( master ) { 49 remote = new MasterController( this, canvas, game, node, player ); 50 } else { 51 remote = new ClientController( this, canvas, game, node ); 52 } 53 } 37 /** 38 * server mode 39 */ 40 public App( final ChessMIDlet midlet, 41 final Game game, 42 final GameCanvas canvas, 43 final IRemoteNode node, 44 final Player player ) throws Exception { 45 this.midlet = midlet; 46 this.game = game; 47 this.player = player; 48 master = player != null; 49 canvas.init( game ); 50 if ( master ) { 51 remote = BaseController.createMaster( this, canvas, game, player, node ); 52 } else { 53 remote = BaseController.createClient( this, canvas, game, node ); 54 } 55 } 54 56 55 57 56 57 58 58 public BaseController getRemote() { 59 return remote; 60 } 59 61 60 61 62 62 public boolean isMaster() { 63 return master; 64 } 63 65 64 65 66 66 public Player getPlayer() { 67 return player; 68 } 67 69 68 69 70 70 public Game getGame() { 71 return game; 72 } 71 73 72 73 74 74 public GameCanvas getCanvas() { 75 return canvas; 76 } 75 77 76 77 78 78 public ChessMIDlet getMidlet() { 79 return midlet; 80 } 79 81 } -
chess/src/chess/remote/IRemote.java
r9 r14 9 9 public interface IRemote { 10 10 11 public IAsyncProcess waitForIncoming( final IServerListener l ); 11 /** 12 * Ïîäïèñûâàåìñÿ íà âõîäÿùèå ñîåäèíåíèÿ. Äðóãèìè ñëîâàìè, 13 * ðåæèì ñåðâåðà -- æäåì êëèåíòîâ 14 * @param l 15 * @return 16 */ 17 public IAsyncProcess waitForIncoming( final IServerListener l ); 12 18 13 public void getAvailable( final boolean force, 14 final IClientListener l ); 19 /** 20 * Èùåì ïîäõîäÿùèå ñåðâåðà -- ðåæèì êëèåíòà. Ïîèñê ìîæåò èäòè â êýøå 21 * óæå èçâåñòíûõ (÷àñòî èñïîëüçóåìûõ) ñåðâåðîâ, èëè ïðèíóäèòåëüíûì 22 * çàïðîñîì âíå êýøà. 23 * 24 *  ïåðâîì ñëó÷àå (force = false) çàïðîñ áóäåò ñèíõðîííûì, è ìåòîä 25 * âîçâðàùàåò null 26 * 27 * Âî âòîðîì ñëó÷àå (force = true) çàïðîñ áóäåò àñèíõðîííûì, è ìåòîä 28 * âîçâðàùàåò äåñêðèïòîð àñèíõðîííîãî ïðîöåññà ïîèñêà óñòðîéñòâ 29 * 30 * @param l 31 * @return 32 */ 33 public IAsyncProcess getAvailable( final boolean force, 34 final IClientListener l ); 15 35 16 36 } -
chess/src/chess/remote/impl/BaseAsyncProcess.java
r11 r14 29 29 case FAILED: 30 30 this.status = status; 31 break; 31 32 default: 32 33 throw new IllegalArgumentException( "Status " + status + " is unknown" ); -
chess/src/chess/remote/impl/Remote.java
r12 r14 10 10 import chess.Util; 11 11 import chess.remote.*; 12 import chess.remote.impl.debug.DebugRemote;13 12 14 13 … … 22 21 private static final String MY_SERVICE_NUMBER = "3B9FA89520078C303355AAA694238F08;name=ChessGameServer"; 23 22 24 p rivatestatic final IRemoteNode[] EMPTY = new IRemoteNode[0];23 public static final IRemoteNode[] EMPTY = new IRemoteNode[0]; 25 24 26 25 private final LocalDevice localDevice; … … 32 31 } 33 32 34 private void fillupCache() {33 private synchronized void fillupCache() { 35 34 final DiscoveryAgent agent = localDevice.getDiscoveryAgent(); 36 35 final RemoteDevice[] preknownDevices = agent.retrieveDevices( DiscoveryAgent.PREKNOWN ); … … 50 49 } 51 50 51 private synchronized void refreshCache() { 52 devices.removeAllElements(); 53 fillupCache(); 54 } 55 52 56 public IAsyncProcess waitForIncoming( final IServerListener l ) { 53 57 return new WaitForIncomingThread( localDevice, l ); 54 58 } 55 59 56 public voidgetAvailable( final boolean refresh,57 final IClientListener l ) {60 public IAsyncProcess getAvailable( final boolean refresh, 61 final IClientListener l ) { 58 62 if ( refresh ) { 59 63 //todo 60 64 try { 61 65 final DiscoveryAgent agent = localDevice.getDiscoveryAgent(); 62 agent.startInquiry( 63 DiscoveryAgent.GIAC, 64 new DiscoveryListener() { 65 public void deviceDiscovered( final RemoteDevice btDevice, 66 final DeviceClass cod ) { 67 Util.log.println( "device found:" + btDevice.getBluetoothAddress() ); 68 } 69 70 public void servicesDiscovered( final int transID, 71 final ServiceRecord[] servRecord ) { 72 73 } 74 75 public void serviceSearchCompleted( final int transID, 76 final int respCode ) { 77 78 } 79 80 public void inquiryCompleted( final int discType ) { 81 devices.removeAllElements(); 82 fillupCache(); 83 l.finished( getNodes() ); 84 } 85 } 86 ); 87 } catch ( BluetoothStateException e ) { 66 return new QueryDeviceProcess( this, agent, l ); 67 } catch ( Exception e ) { 88 68 e.printStackTrace(); 89 69 l.finished( null ); 70 return null; 90 71 } 91 72 } else { 92 73 final IRemoteNode[] nodes = getNodes(); 93 74 l.finished( nodes ); 94 } 95 } 96 97 private IRemoteNode[] getNodes() { 75 return null; 76 } 77 } 78 79 private synchronized IRemoteNode[] getNodes() { 98 80 if ( devices.isEmpty() ) { 99 81 return EMPTY; … … 114 96 115 97 public static IRemote create() throws Exception { 116 //return new Remote();98 return new Remote(); 117 99 // return new DummyRemote(); 118 return new DebugRemote( 500, 500 );100 // return new DebugRemote( 500, 500 ); 119 101 } 120 102 … … 179 161 } 180 162 } 163 164 private static class QueryDeviceProcess implements IAsyncProcess, DiscoveryListener { 165 private final DiscoveryAgent agent; 166 private final IClientListener l; 167 private final Remote remote; 168 169 private volatile int status = IN_PROCESS; 170 171 public QueryDeviceProcess( final Remote remote, 172 final DiscoveryAgent agent, 173 final IClientListener l ) throws Exception { 174 this.remote = remote; 175 this.agent = agent; 176 this.l = l; 177 agent.startInquiry( DiscoveryAgent.GIAC, this ); 178 } 179 180 public int status() { 181 return status; 182 } 183 184 public void interrupt( final boolean waitFor ) { 185 agent.cancelInquiry( this ); 186 } 187 188 public IAsyncProcessListener getListener() { 189 return l; 190 } 191 192 public void deviceDiscovered( final RemoteDevice btDevice, 193 final DeviceClass cod ) { 194 Util.log.println( "device found:" + btDevice.getBluetoothAddress() ); 195 } 196 197 public void servicesDiscovered( final int transID, 198 final ServiceRecord[] servRecord ) { 199 Util.log.println( "service found:" + transID ); 200 } 201 202 public void serviceSearchCompleted( final int transID, 203 final int respCode ) { 204 205 } 206 207 public void inquiryCompleted( final int discType ) { 208 if ( discType == INQUIRY_COMPLETED ) { 209 remote.refreshCache(); 210 status = SUCCEEDED; 211 l.finished( remote.getNodes() ); 212 } else { 213 status = FAILED; 214 l.finished( null ); 215 } 216 } 217 } 181 218 } -
chess/src/chess/remote/impl/debug/DebugRemote.java
r12 r14 4 4 import java.util.Timer; 5 5 import java.util.TimerTask; 6 import java.util.Random; 6 7 7 8 import chess.remote.*; 8 9 import chess.remote.impl.BaseAsyncProcess; 9 10 import chess.remote.impl.GenericDatagram; 11 import chess.remote.impl.Remote; 10 12 import chess.game.Action; 11 13 import chess.game.Player; … … 22 24 public class DebugRemote implements IRemote { 23 25 private static final Timer TIMER = new Timer(); 26 27 private static final Random random = new Random(); 24 28 25 29 private final int networkLatency; … … 73 77 74 78 public IAsyncProcess waitForIncoming( final IServerListener l ) { 75 //final InputStream is = getClass().getResourceAsStream( "incoming" );76 //final InputStream is = prepareLog( Player.BLACK );77 79 return new AsyncProcess( l ); 78 80 } 79 81 80 public void getAvailable( final boolean force, 81 final IClientListener l ) { 82 TIMER.schedule( 83 new TimerTask() { 84 public void run() { 85 l.finished( new IRemoteNode[0] ); 86 } 87 }, 88 100 89 ); 90 82 public IAsyncProcess getAvailable( final boolean force, 83 final IClientListener l ) { 84 if ( !force ) { 85 try { 86 Thread.sleep( networkLatency ); 87 } catch ( InterruptedException e ) { 88 e.printStackTrace(); 89 } 90 l.finished( Remote.EMPTY ); 91 return null; 92 } else { 93 final QueryDeviceProcess process = new QueryDeviceProcess( this, l ); 94 TIMER.schedule( process, networkLatency ); 95 return process; 96 } 91 97 } 92 98 … … 107 113 e.printStackTrace(); 108 114 } 109 if ( listener.incoming( new RemoteNode() ) ) {115 if ( listener.incoming( new ClientRemoteNode() ) ) { 110 116 Util.log.println( "emulate incoming connection: OK" ); 111 117 return; … … 119 125 } 120 126 121 private class RemoteNode implements IRemoteNode {127 private class ClientRemoteNode implements IRemoteNode { 122 128 private InputStream is; 123 129 private IConnectionListener listener; … … 144 150 Util.log.println( "emulate wasSent: " + data ); 145 151 listener.wasSent( data ); 152 if ( data instanceof SystemAction ) { 153 final SystemAction sa = ( SystemAction )data; 154 if ( sa.type() == SystemAction.SET_PLAYER ) { 155 player = sa.getPlayer(); 156 is = prepareLog( player ); 157 } 158 if ( sa.type() == SystemAction.YOUR_TURN ) { 159 try { 160 final GenericDatagram aPacket = new GenericDatagram( ClientRemoteNode.this.is ); 161 final GenericDatagram saPacket = new GenericDatagram( ClientRemoteNode.this.is ); 162 Thread.sleep( 5 * networkLatency ); 163 TIMER.schedule( 164 new TimerTask() { 165 public void run() { 166 Util.log.println( "recv: emulate" ); 167 168 Util.log.println( "recv: " + aPacket.wrapped ); 169 listener.received( aPacket.wrapped ); 170 171 Util.log.println( "recv: " + saPacket.wrapped ); 172 listener.received( saPacket.wrapped ); 173 } 174 }, 175 ( ( Action )aPacket.wrapped ).duration() 176 ); 177 } catch ( Exception e ) { 178 e.printStackTrace(); 179 } 180 181 } 182 } 146 183 } 147 184 }, 148 185 2 * networkLatency 149 186 ); 150 if ( data instanceof SystemAction ) { 151 final SystemAction sa = ( SystemAction )data; 152 if ( sa.type() == SystemAction.SET_PLAYER ) { 153 this.player = sa.getPlayer(); 154 is = prepareLog( player ); 155 } 156 if ( sa.type() == SystemAction.YOUR_TURN ) { 157 try { 158 final GenericDatagram aPacket = new GenericDatagram( RemoteNode.this.is ); 159 final GenericDatagram saPacket = new GenericDatagram( RemoteNode.this.is ); 160 Thread.sleep( 5 * networkLatency ); 161 TIMER.schedule( 162 new TimerTask() { 163 public void run() { 164 Util.log.println( "recv: emulate" ); 165 166 Util.log.println( "recv: " + aPacket.wrapped ); 167 listener.received( aPacket.wrapped ); 168 169 Util.log.println( "recv: " + saPacket.wrapped ); 170 listener.received( saPacket.wrapped ); 171 } 172 }, 173 ( ( Action )aPacket.wrapped ).duration() 174 ); 175 } catch ( Exception e ) { 176 e.printStackTrace(); 177 } 178 179 } 180 } 187 181 188 } 182 189 } … … 186 193 } 187 194 } 195 196 private class ServerRemoteNode implements IRemoteNode { 197 private InputStream is; 198 private IConnectionListener listener; 199 private final Player player; 200 private final String name; 201 202 private ServerRemoteNode( final int n ) { 203 player = Player.byType( n % 2 ); 204 name = "debug server #" + n + "[" + player + "]"; 205 } 206 207 public String getName() { 208 return name; 209 } 210 211 public void setListener( final IConnectionListener l ) { 212 this.listener = l; 213 } 214 215 public void ensureConnected() throws IOException { 216 if ( listener == null ) { 217 throw new IllegalStateException( "listener can't be null in ensureConnected!" ); 218 } 219 //ïîñûëàåì íà÷àëüíóþ ïîñëåäîâàòåëüíîñòü -- 220 //restore logged actions, SET_PLAYER, YOUR_TURN 221 Util.log.println( "server emulation started: " + name ); 222 is = prepareLog( player ); 223 TIMER.schedule( 224 new TimerTask() { 225 public void run() { 226 Util.log.println( name + ": sending SET_PLAYER" ); 227 listener.received( 228 new SystemAction( 229 SystemAction.SET_PLAYER, 230 player.opponent() 231 ) 232 ); 233 } 234 }, 235 2 * networkLatency 236 ); 237 if ( player == Player.WHITE ) { 238 packet(); 239 } else { 240 //èíà÷å æäåì YOUR_TURN îò áåëûõ 241 } 242 } 243 244 public void send( final IDatagram data ) { 245 if ( listener != null ) { 246 Util.log.println( name + ": send data " + data ); 247 TIMER.schedule( 248 new TimerTask() { 249 public void run() { 250 Util.log.println( name + ": wasSent " + data ); 251 listener.wasSent( data ); 252 if ( data instanceof SystemAction ) { 253 final SystemAction sa = ( SystemAction )data; 254 if ( sa.type() == SystemAction.SET_PLAYER ) { 255 throw new IllegalStateException( "Can't be SET_PLAYER to server" ); 256 } 257 if ( sa.type() == SystemAction.YOUR_TURN ) { 258 packet(); 259 } 260 } 261 } 262 }, 263 2 * networkLatency 264 ); 265 } 266 } 267 268 private void packet() { 269 try { 270 final GenericDatagram aPacket = new GenericDatagram( is ); 271 final GenericDatagram saPacket = new GenericDatagram( is ); 272 Thread.sleep( 5 * networkLatency ); 273 TIMER.schedule( 274 new TimerTask() { 275 public void run() { 276 Util.log.println( name + ":recv emulation..." ); 277 278 Util.log.println( name + ":recv " + aPacket.wrapped ); 279 listener.received( aPacket.wrapped ); 280 281 Util.log.println( name + ":recv " + saPacket.wrapped ); 282 listener.received( saPacket.wrapped ); 283 } 284 }, 285 ( ( Action )aPacket.wrapped ).duration() 286 ); 287 } catch ( Exception e ) { 288 e.printStackTrace(); 289 } 290 } 291 292 public void close() { 293 listener.closed(); 294 } 295 } 296 297 private static class QueryDeviceProcess extends TimerTask implements IAsyncProcess { 298 private final IClientListener l; 299 private final DebugRemote remote; 300 301 public QueryDeviceProcess( final DebugRemote remote, 302 final IClientListener l ) { 303 this.remote = remote; 304 this.l = l; 305 } 306 307 public void run() { 308 final int rnd = random.nextInt( 5 ); 309 final IRemoteNode[] servers = new IRemoteNode[rnd]; 310 for ( int i = 0; i < servers.length; i++ ) { 311 servers[i] = remote.new ServerRemoteNode( i + 1 ); 312 } 313 l.finished( servers ); 314 } 315 316 public int status() { 317 return 0; 318 } 319 320 public void interrupt( final boolean waitFor ) { 321 cancel(); 322 } 323 324 public IAsyncProcessListener getListener() { 325 return l; 326 } 327 } 188 328 } -
chess/src/chess/ui/ChessMIDlet.java
r12 r14 30 30 public final Command exitCommand = new Command( MSG.getMessage( "command.exit" ), Command.EXIT, 1 ); 31 31 public final Command closeCommand = new Command( MSG.getMessage( "command.close" ), Command.SCREEN, 1 ); 32 public final Command cancelCommand = new Command( MSG.getMessage( "command.cancel" ), Command.CANCEL, 1 ); 32 33 public final Command backCommand = new Command( MSG.getMessage( "command.back" ), Command.CANCEL, 1 ); 33 34 public final Command refreshCommand = new Command( MSG.getMessage( "command.refresh" ), Command.SCREEN, 1 ); … … 39 40 public final Display display; 40 41 41 private final Screen startForm = new StartScreen( this ); 42 private final ChoosePlayerForm choosePlayerForm = new ChoosePlayerForm( this ); 43 //private final Screen waitScreen = new WaitScreen( this, MSG.getMessage( "wait-for-bluetooth.title" ), ); 42 private final Screen startForm = new StartMenuScreen2( this ); 43 private final Screen choosePlayerForm = new ChoosePlayerForm( this ); 44 private final GameCanvas gameCanvas = new GameCanvas( this ); 45 private final WaitScreen waitScreen = new WaitScreen( 46 this, 47 MSG.getMessage( "wait-for-bluetooth.title" ), 48 gameCanvas 49 ); 44 50 private final JoinGameScreen searchForm = new JoinGameScreen( this ); 45 private final GameCanvas gameCanvas = new GameCanvas( this );46 51 private final Screen helpScreen = new HelpScreen( this ); 47 52 … … 51 56 /* current game application */ 52 57 private App app; 58 private IAsyncProcess waitForIncomingProcess; 53 59 54 60 public ChessMIDlet() throws Exception { … … 56 62 remote = Remote.create(); 57 63 58 64 waitScreen.addCommand( cancelCommand ); 65 waitScreen.setCommandListener( this ); 59 66 } 60 67 … … 65 72 public void connectToServer( final IRemoteNode server ) { 66 73 try { 67 server.ensureConnected();//todo may be nahren?68 74 app = new App( 69 75 ChessMIDlet.this, … … 73 79 ); 74 80 display.setCurrent( gameCanvas ); 75 } catch ( IOException e ) {81 } catch ( Exception e ) { 76 82 e.printStackTrace(); 77 83 } … … 100 106 101 107 protected void newGame( final Player player ) { 102 final WaitScreen waitScreen = new WaitScreen( 103 this, 104 MSG.getMessage( "wait-for-bluetooth.title" ), 105 gameCanvas 106 ); 108 if ( waitForIncomingProcess != null ) { 109 waitForIncomingProcess.interrupt( true ); 110 waitForIncomingProcess = null; 111 } 107 112 display.setCurrent( waitScreen ); 108 113 Util.log.println( "new game:" + player ); 109 remote.waitForIncoming(114 waitForIncomingProcess = remote.waitForIncoming( 110 115 new IServerListener() { 111 116 private boolean retValue = false; 112 117 113 118 public synchronized boolean incoming( final IRemoteNode client ) { 114 final Alert dlg = new Alert( "Connect?", client.getName(), null, AlertType.CONFIRMATION ); 119 final Alert dlg = new Alert( 120 MSG.getMessage( "new-game.incoming-client" ), 121 MSG.getMessage( 122 "new-game.incoming-client.description", 123 new Object[]{ client.getName() } 124 ), 125 null, 126 AlertType.CONFIRMATION 127 ); 115 128 dlg.setTimeout( Alert.FOREVER ); 116 129 dlg.addCommand( okCommand ); … … 124 137 Util.log.println( command ); 125 138 try { 126 client.ensureConnected();127 139 app = new App( 128 140 ChessMIDlet.this, … … 132 144 player 133 145 ); 134 display.setCurrent( gameCanvas );146 //display.setCurrent( gameCanvas ); 135 147 Util.log.println( "joined client" ); 136 148 retValue = true; 137 149 //return true; 138 } catch ( IOException e ) {150 } catch ( Exception e ) { 139 151 e.printStackTrace(); 140 152 retValue = false; … … 164 176 165 177 public void finished( final Object res ) { 166 waitScreen.finished( res ); 167 //display.setCurrent( 168 // new Alert( "Can't connect" ) 169 //); 178 //todo ask for status and display error message if failed 179 if ( waitForIncomingProcess.status() == IAsyncProcess.SUCCEEDED ) { 180 waitScreen.finished( res ); 181 } else { 182 display.setCurrent( startForm ); 183 } 184 waitForIncomingProcess = null; 170 185 } 171 186 } … … 184 199 } 185 200 186 public void exit () {201 public void exitGame() { 187 202 destroyApp( true ); 188 203 notifyDestroyed(); … … 191 206 public void commandAction( final Command c, 192 207 final Displayable d ) { 208 if ( d == waitScreen ) { 209 if ( c == cancelCommand ) { 210 if ( waitForIncomingProcess != null ) { 211 waitForIncomingProcess.interrupt( true ); 212 waitForIncomingProcess = null; 213 } 214 } 215 } 193 216 if ( c == exitCommand ) { 194 exit ();217 exitGame(); 195 218 } else if ( c == newCommand ) { 196 219 choosePlayer(); -
chess/src/chess/ui/ChoosePlayerForm.java
r7 r14 11 11 * created 31.01.2009 at 19:29:43 12 12 */ 13 public class ChoosePlayerForm extends List implements CommandListener { 14 private final ChessMIDlet midlet; 13 public class ChoosePlayerForm extends Form implements CommandListener, ItemCommandListener { 14 private final ChessMIDlet midlet; 15 16 private final StringItem whiteItem = new StringItem( 17 Player.WHITE.name(), 18 "", 19 StringItem.BUTTON 20 ); 21 private final StringItem blackItem = new StringItem( 22 Player.BLACK.name(), 23 "", 24 StringItem.BUTTON 25 ); 26 27 public ChoosePlayerForm( final ChessMIDlet midlet ) { 28 super( MSG.getMessage( "choose-player.title" ) ); 29 this.midlet = midlet; 15 30 16 31 17 public ChoosePlayerForm( final ChessMIDlet midlet ) { 18 super( MSG.getMessage( "choose-player.title" ), Choice.IMPLICIT);19 this.midlet = midlet;32 whiteItem.setDefaultCommand( midlet.okCommand ); 33 whiteItem.setItemCommandListener( this ); 34 whiteItem.setLayout( Item.LAYOUT_EXPAND | Item.LAYOUT_VEXPAND ); 20 35 21 append( Player.WHITE.name(), null ); 22 append( Player.BLACK.name(), null ); 36 blackItem.setDefaultCommand( midlet.okCommand ); 37 blackItem.setItemCommandListener( this ); 38 blackItem.setLayout( Item.LAYOUT_EXPAND | Item.LAYOUT_VEXPAND ); 23 39 24 addCommand( midlet.okCommand);25 addCommand( midlet.backCommand);40 append( whiteItem ); 41 append( blackItem ); 26 42 27 setSelectCommand( midlet.okCommand ); 43 addCommand( midlet.okCommand ); 44 addCommand( midlet.backCommand ); 28 45 29 30 46 setCommandListener( this ); 47 } 31 48 32 49 33 public void commandAction( final Command command, 34 final Displayable displayable ) { 35 if( command == midlet.okCommand ) { 36 final int index = getSelectedIndex(); 37 if( index >= 0 ) { 38 midlet.newGame( Player.byType( index ) ); 39 } 40 } else if( command == midlet.backCommand ) { 41 midlet.showStartScreen(); 42 } 43 } 50 public void commandAction( final Command command, 51 final Displayable displayable ) { 52 /*if ( command == midlet.okCommand ) { 53 final int index = getSelectedIndex(); 54 if ( index >= 0 ) { 55 midlet.newGame( Player.byType( index ) ); 56 } 57 } else */ 58 if ( command == midlet.backCommand ) { 59 midlet.showStartScreen(); 60 } 61 } 62 63 public void commandAction( final Command command, 64 final Item item ) { 65 if ( command == midlet.okCommand ) { 66 if ( item == whiteItem ) { 67 midlet.newGame( Player.WHITE ); 68 } else if ( item == blackItem ) { 69 midlet.newGame( Player.BLACK ); 70 } 71 } 72 } 44 73 } -
chess/src/chess/ui/GameCanvas.java
r10 r14 58 58 private static final int LABELS_COLOR = 0; 59 59 60 private final Screen waitScreen;60 private final WaitScreen waitScreen; 61 61 62 62 public GameCanvas( final ChessMIDlet midlet ) { … … 70 70 waitScreen = new WaitScreen( 71 71 midlet, 72 MSG.getMessage( "wait -for-opponent.title" ),72 MSG.getMessage( "wait.title" ), 73 73 this 74 74 ); … … 344 344 //todo move it to ChessMIDlet? 345 345 if ( status != ACTIVE ) { 346 midlet.display.setCurrent( waitScreen ); 346 if ( midlet.display.getCurrent() != waitScreen ) { 347 midlet.display.setCurrent( waitScreen ); 348 } 349 switch ( status ) { 350 case WAITING_FOR_NETWORK: 351 waitScreen.setText( MSG.getMessage( "wait.for-network" ) ); 352 case WAITING_FOR_OPPONENT: 353 waitScreen.setText( MSG.getMessage( "wait.for-opponent" ) ); 354 } 347 355 } else { 348 356 midlet.display.setCurrent( this ); -
chess/src/chess/ui/JoinGameScreen.java
r9 r14 6 6 import chess.remote.IClientListener; 7 7 import chess.remote.IRemoteNode; 8 import chess.remote.IAsyncProcess; 8 9 9 10 /** … … 14 15 */ 15 16 public class JoinGameScreen extends List implements CommandListener, IClientListener { 16 17 private final ChessMIDlet midlet; 17 18 18 19 19 private IRemoteNode[] nodes; 20 private boolean force; 20 21 21 22 "Nothing found",23 "No servers available",24 25 26 22 private final Alert nothingFoundAlert = new Alert( 23 MSG.getMessage( "join-game.no-server-found.title" ), 24 MSG.getMessage( "join-game.no-server-found.message" ), 25 null, 26 AlertType.INFO 27 ); 27 28 28 29 "Scanning...",30 "Looking for game servers",31 32 33 29 private final Alert scanningNetworkAlert = new Alert( 30 MSG.getMessage( "join-game.scanning-network.title" ), 31 MSG.getMessage( "join-game.scanning-network.message" ), 32 null, 33 AlertType.INFO 34 ); 34 35 35 public JoinGameScreen( final ChessMIDlet midlet ) { 36 super( MSG.getMessage( "join-game.title" ), Choice.EXCLUSIVE ); 37 this.midlet = midlet; 36 private IAsyncProcess process; 38 37 39 scanningNetworkAlert.setTimeout( Alert.FOREVER ); 40 //refresh( false ); 38 public JoinGameScreen( final ChessMIDlet midlet ) { 39 super( MSG.getMessage( "join-game.title" ), Choice.IMPLICIT ); 40 this.midlet = midlet; 41 41 42 addCommand( midlet.okCommand ); 43 addCommand( midlet.refreshCommand ); 44 addCommand( midlet.backCommand ); 42 scanningNetworkAlert.setTimeout( Alert.FOREVER ); 43 //scanningNetworkAlert.setTicker( 44 // new Ticker( "Looking for game servers" ) 45 //); 46 scanningNetworkAlert.removeCommand( Alert.DISMISS_COMMAND ); 47 scanningNetworkAlert.addCommand( midlet.cancelCommand ); 48 scanningNetworkAlert.setCommandListener( this ); 45 49 46 setSelectCommand( midlet.okCommand ); 47 } 50 addCommand( midlet.okCommand ); 51 addCommand( midlet.refreshCommand ); 52 addCommand( midlet.backCommand ); 48 53 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 } 54 setSelectCommand( midlet.okCommand ); 56 55 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 } 56 setCommandListener( this ); 57 } 76 58 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 } 59 public void refresh( final boolean force ) { 60 this.force = force; 61 if ( force && isShown() ) { 62 midlet.display.setCurrent( scanningNetworkAlert ); 63 } 64 if ( process != null ) { 65 process.interrupt( true ); 66 process = null; 67 } 68 process = midlet.getRemoteManager().getAvailable( this.force, this ); 69 } 70 71 public void finished( final Object res ) { 72 final IRemoteNode[] servers = ( IRemoteNode[] )res; 73 if ( !force ) { 74 if ( servers == null || servers.length == 0 ) { 75 refresh( true ); 76 return; 77 } 78 } 79 //todo check process.status() and display error message 80 81 nodes = servers; 82 if ( nodes == null || nodes.length == 0 ) { 83 midlet.display.setCurrent( nothingFoundAlert, this ); 84 } else { 85 for ( int i = 0; i < nodes.length; i++ ) { 86 final IRemoteNode node = nodes[i]; 87 append( node.getName(), null ); 88 } 89 midlet.display.setCurrent( this ); 90 } 91 } 92 93 public void commandAction( final Command command, 94 final Displayable displayable ) { 95 if ( displayable == scanningNetworkAlert ) { 96 if ( command == midlet.cancelCommand ) { 97 if ( process != null ) { 98 process.interrupt( true ); 99 process = null; 100 } 101 } 102 } else if ( command == midlet.backCommand ) { 103 midlet.showStartScreen(); 104 } else if ( command == midlet.okCommand ) { 105 final int index = getSelectedIndex(); 106 if ( index >= 0 ) { 107 final IRemoteNode server = nodes[index]; 108 midlet.connectToServer( server ); 109 } 110 } else if ( command == midlet.refreshCommand ) { 111 //todo wait screen 112 refresh( true ); 113 } 114 } 92 115 } -
chess/src/chess/ui/WaitScreen.java
r10 r14 18 18 private final Displayable nextScreen; 19 19 20 private final StringItem text = new StringItem( "", "" ); 21 20 22 public WaitScreen( final ChessMIDlet midlet, 21 23 final String message, … … 24 26 this.midlet = midlet; 25 27 this.nextScreen = nextScreen; 28 29 text.setLayout( Item.LAYOUT_EXPAND | Item.LAYOUT_VCENTER ); 30 append( text ); 31 32 //append( new Gauge( "test-1", true, 100, 35 ) ); 33 //append( new Gauge( "test-2", true, 100, 45 ) ); 34 } 35 36 public void setText( final String text ) { 37 this.text.setText( text ); 38 } 39 40 public String getText() { 41 return text.getText(); 26 42 } 27 43
Note: See TracChangeset
for help on using the changeset viewer.