- Timestamp:
- 02/05/09 13:44:58 (13 years ago)
- Location:
- chess
- Files:
-
- 2 added
- 10 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
chess/chess.jad
r21 r22 1 1 MIDlet-1: Chess Game, , chess.ui.ChessMIDlet 2 MIDlet-Jar-Size: 8 11062 MIDlet-Jar-Size: 83914 3 3 MIDlet-Jar-URL: chess.jar 4 4 MIDlet-Name: chess -
chess/src/chess/MSG.properties
r14 r22 28 28 wait.title = \u041e\u0436\u0438\u0434\u0430\u043d\u0438\u0435... 29 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-connect = \u041e\u0436\u0438\u0434\u0430\u0435\u043c \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f... 30 31 wait.for-network = \u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0434\u0430\u043d\u043d\u044b\u0445... 31 32 -
chess/src/chess/control/BaseController.java
r19 r22 56 56 57 57 ui.setUIListener( this ); 58 ui.setStatus( GameCanvas.WAITING_FOR_NETWORK);58 setStatus( WAITING_CONNECT ); 59 59 if ( master ) { 60 60 sendLog( game, player, node ); … … 104 104 } 105 105 106 public void connected() { 107 //todo 108 setStatus( WAITING_FOR_OPPONENT ); 109 } 110 106 111 public synchronized void received( final IDatagram data ) { 107 112 if ( data instanceof Action ) { … … 126 131 throw new IllegalStateException( "Master should not got SET_PLAYER commands" ); 127 132 } 133 setStatus( WAITING_FOR_OPPONENT ); 128 134 } else if ( sa.type() == SystemAction.YOUR_TURN ) { 129 135 if ( player == null ) { 130 136 throw new IllegalStateException( "Player == null" ); 131 137 } 132 if ( !master ) {133 synchronized ( this ) {134 notifyAll();135 }136 }138 //if ( !master ) { 139 // synchronized ( this ) { 140 // notifyAll(); 141 // } 142 //} 137 143 lastTimeStamp = System.currentTimeMillis(); 138 144 setStatus( WAITING_FOR_LOCAL ); 139 145 } else if ( sa.type() == SystemAction.SURRENDER ) { 140 146 final ChessMIDlet midlet = app.getMidlet(); 147 setStatus( WAITING_FOR_LOCAL ); 141 148 midlet.display.setCurrent( 142 149 new Alert( "Opponent surrenders. You win." ), -
chess/src/chess/remote/IConnectionListener.java
r9 r22 9 9 public interface IConnectionListener { 10 10 11 public void received( final IDatagram data);11 public void connected(); 12 12 13 public void wasSent( final IDatagram data );13 public void received( final IDatagram data ); 14 14 15 public void closed(); 15 public void wasSent( final IDatagram data ); 16 17 public void closed(); 16 18 } -
chess/src/chess/remote/impl/BTNode.java
r21 r22 14 14 15 15 /** 16 * Class 16 * Bluetooth-ñîåäèíåíèå 17 * <p/> 17 18 * User: BegemoT<br/> 18 19 * Date: 28.01.2009<br/> 19 20 * Time: 16:33:50<br/> 20 21 */ 21 public class RemoteNode implements IRemoteNode, Runnable { 22 public class BTNode implements IRemoteNode, Runnable { 23 /** 24 * êàíàë BT, èñïîëüçóåìûé íàøèì ïðèëîæåíèåì 25 */ 26 private static final String BT_STREAM = "1"; 22 27 23 28 private String name; … … 31 36 private transient Thread pump; 32 37 38 /** 39 * ìîíèòîð îæèäàíèÿ îòêðûòèÿ ñîåäèíåíèÿ 40 */ 33 41 private final Object connectLock = new Object(); 34 42 35 public RemoteNode( final RemoteDevice device ) throws IOException { 43 private final Object queueOverloadedLock = new Object(); 44 private final IDatagram[] postQueue = new IDatagram[2]; 45 46 47 public BTNode( final RemoteDevice device ) throws IOException { 36 48 this( device, null ); 37 49 } 38 50 39 public RemoteNode( final StreamConnection conn ) throws IOException {51 public BTNode( final StreamConnection conn ) throws IOException { 40 52 this( null, null, conn ); 41 53 } 42 54 43 public RemoteNode( final RemoteDevice device,44 55 public BTNode( final RemoteDevice device, 56 final StreamConnection conn ) throws IOException { 45 57 this( null, device, conn ); 46 58 } 47 59 48 public RemoteNode( final String name,49 50 60 public BTNode( final String name, 61 final RemoteDevice device, 62 final StreamConnection conn ) throws IOException { 51 63 if ( device == null && conn == null ) { 52 throw new IllegalArgumentException( "No-no-no, David Bl ein! device =null && conn =null" );64 throw new IllegalArgumentException( "No-no-no, David Blain! device=null && conn=null" ); 53 65 } 54 66 … … 66 78 } 67 79 68 private static String getBluetoothURL( final RemoteDevice device ) { 69 return Remote.BTSPP_SCHEME + device.getBluetoothAddress() + ":1"; 70 } 71 72 public RemoteNode( final String name, 73 final StreamConnection conn ) throws IOException { 80 public BTNode( final String name, 81 final StreamConnection conn ) throws IOException { 74 82 this( name, null, conn ); 75 83 } … … 80 88 81 89 public synchronized void start() { 82 check Status();83 if ( pump == null) {90 checkNotClosed(); 91 if ( !isStartCalled() ) { 84 92 pump = new Thread( this, "network pump" ); 85 93 pump.start(); … … 87 95 } 88 96 89 private void checkStatus() {90 if ( isClosed() ) {91 throw new IllegalStateException( "Already closed node" );92 }93 }94 95 private boolean isClosed() {96 return device == null;97 }98 99 97 public synchronized void setListener( final IConnectionListener l ) { 100 //start(); 98 if ( l != null && isClosed() ) { 99 throw new IllegalStateException( "Node is closed - lsitener is useless" ); 100 } 101 101 this.listener = l; 102 102 } 103 103 104 104 public void send( final IDatagram data ) { 105 //start(); 106 checkStatus(); 105 checkPumpActive(); 107 106 synchronized ( connectLock ) { 108 while ( out == null) {109 try { 110 connectLock.wait( );107 while ( !isConnected() && !isClosed() ) { 108 try { 109 connectLock.wait( 250 ); 111 110 } catch ( InterruptedException e ) { 112 111 e.printStackTrace(); … … 114 113 } 115 114 } 116 synchronized ( this ) { 117 try { 118 final GenericDatagram gdata = new GenericDatagram( data ); 119 gdata.write( out ); 120 } catch ( IOException ex ) { 121 ex.printStackTrace(); 122 throw new RuntimeException( ex.getMessage() ); 115 //áëîêèðóåì this ÷òîáû íàñ íå çàêðûëè íåâîâðåìÿ 116 while ( true ) { 117 synchronized ( this ) { 118 checkConnected(); 119 try { 120 synchronized ( postQueue ) { 121 //áëîêèðóåì î÷åðåäü, ÷òîáû ïðåäóïðåäèòü åå èçìåíåíèÿ 122 for ( int i = 0; i < postQueue.length; i++ ) { 123 if ( postQueue[i] == null ) { 124 postQueue[i] = data; 125 return; 126 } 127 } 128 } 129 } finally { 130 notifyAll();//push pump to run/ïèíàåì íàøåãî ïî÷òàëüîíà 131 } 132 } 133 //åñëè î÷åðåäü ïåðåïîëíåíà -- âûíóæäåíû æäàòü 134 synchronized ( queueOverloadedLock ) { 135 try { 136 queueOverloadedLock.wait( 250 ); 137 } catch ( InterruptedException e ) { 138 e.printStackTrace(); 139 } 140 } 141 } 142 //todo ýòî áû òîæå íåïëîõî áû âíåñòè â run() -- à òî áóäåò áëîêèðîâàòüñÿ EDT 143 //try { 144 // final GenericDatagram gdata = new GenericDatagram( data ); 145 // gdata.write( out ); 146 //} catch ( IOException ex ) { 147 // ex.printStackTrace(); 148 // throw new RuntimeException( ex.getMessage() ); 149 //} 150 //if ( listener != null ) { 151 // listener.wasSent( data ); 152 //} 153 } 154 155 public void run() { 156 try { 157 synchronized ( this ) { 158 if ( isClosed() ) { 159 return; 160 } 161 openPipeline(); 123 162 } 124 163 if ( listener != null ) { 125 listener.wasSent( data ); 126 } 164 listener.connected(); 165 } 166 synchronized ( connectLock ) { 167 connectLock.notifyAll(); 168 } 169 final GenericDatagram data = new GenericDatagram(); 170 while ( !isClosed() ) { 171 synchronized ( this ) { 172 final int timeout; 173 //îòïðàâëÿåì íåîòïðàâëåííîå 174 synchronized ( postQueue ) { 175 for ( int i = 0; i < postQueue.length; i++ ) { 176 final IDatagram packet = postQueue[i]; 177 if ( packet != null ) { 178 data.setWrapped( packet ); 179 data.write( out ); 180 postQueue[i] = null; 181 if ( listener != null ) { 182 listener.wasSent( packet ); 183 } 184 } 185 } 186 } 187 synchronized ( queueOverloadedLock ) { 188 //îïîâåùàåì, ÷òî î÷åðåäü îñâîáîäèëàñü 189 queueOverloadedLock.notifyAll(); 190 } 191 //÷èòàåì íåäî÷èòàííîå 192 if ( in.available() > 0 ) { 193 data.read( in ); 194 if ( listener != null ) { 195 listener.received( data.wrapped() ); 196 } 197 timeout = 50; 198 } else { 199 timeout = 250; 200 } 201 try { 202 wait( timeout ); 203 } catch ( InterruptedException e ) { 204 } 205 } 206 } 207 } catch ( Throwable e ) { 208 e.printStackTrace(); 209 } finally { 210 pump = null; 211 close(); 127 212 } 128 213 } … … 180 265 } 181 266 182 183 public void run() { 184 try { 185 synchronized ( this ) { 186 if ( isClosed() ) { 187 return; 188 } 189 ensurePipelineOpened(); 190 } 191 synchronized ( connectLock ) { 192 connectLock.notifyAll(); 193 } 194 while ( !isClosed() ) { 195 synchronized ( this ) { 196 final int timeout; 197 if ( in.available() > 0 ) { 198 final GenericDatagram data = new GenericDatagram( in ); 199 if ( listener != null ) { 200 listener.received( data.wrapped ); 201 } 202 timeout = 50; 203 } else { 204 timeout = 250; 205 } 206 try { 207 wait( timeout ); 208 } catch ( InterruptedException e ) { 209 } 210 } 211 } 212 } catch ( Throwable e ) { 213 e.printStackTrace(); 214 return; 215 } finally { 216 pump = null; 217 close(); 218 } 219 } 220 221 private void ensurePipelineOpened() throws IOException { 267 private void openPipeline() throws IOException { 222 268 if ( connection == null ) { 223 269 final String addr = getBluetoothURL( device ); … … 242 288 } 243 289 244 245 290 public String toString() { 246 291 return getName(); 247 292 } 293 294 private void checkNotClosed() { 295 if ( isClosed() ) { 296 throw new IllegalStateException( "Node already closed" ); 297 } 298 } 299 300 private void checkPumpActive() { 301 checkNotClosed(); 302 if ( !isStartCalled() ) { 303 throw new IllegalStateException( ".start() not called" ); 304 } 305 } 306 307 private void checkConnected() { 308 checkNotClosed(); 309 checkPumpActive(); 310 if ( !isConnected() ) { 311 throw new IllegalStateException( "Node not connected" ); 312 } 313 } 314 315 private boolean isClosed() { 316 return device == null; 317 } 318 319 private boolean isStartCalled() { 320 return pump != null; 321 } 322 323 private boolean isConnected() { 324 return connection != null && in != null && out != null; 325 } 326 327 328 private static String getBluetoothURL( final RemoteDevice device ) { 329 return Remote.BTSPP_SCHEME + device.getBluetoothAddress() + ":" + BT_STREAM; 330 } 248 331 } -
chess/src/chess/remote/impl/GenericDatagram.java
r9 r22 17 17 */ 18 18 public class GenericDatagram implements IDatagram { 19 20 19 private static final byte ACTION_TYPE = 0; 20 private static final byte SYSTEM_ACTION_TYPE = 1; 21 21 22 23 publicIDatagram wrapped;22 private byte type; 23 private IDatagram wrapped; 24 24 25 public GenericDatagram( final InputStream is ) throws IOException { 26 read( is ); 27 } 25 public GenericDatagram() { 26 type = -1; 27 wrapped = null; 28 } 28 29 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 } 30 public GenericDatagram( final InputStream is ) throws IOException { 31 read( is ); 32 } 39 33 40 public void write( final OutputStream os ) throws IOException { 41 os.write( type ); 42 wrapped.write( os ); 43 } 34 public GenericDatagram( final IDatagram wrapped ) { 35 setWrapped( wrapped ); 36 } 44 37 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 } 38 public void write( final OutputStream os ) throws IOException { 39 os.write( type ); 40 wrapped.write( os ); 41 } 58 42 43 public void read( final InputStream is ) throws IOException { 44 type = ( byte )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 } 59 56 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 } 57 public void setWrapped( final IDatagram d ) { 58 this.wrapped = d; 59 if ( wrapped instanceof Action ) { 60 type = ACTION_TYPE; 61 } else if ( wrapped instanceof SystemAction ) { 62 type = SYSTEM_ACTION_TYPE; 63 } else { 64 throw new IllegalStateException( "type " + wrapped + " is unknown" ); 65 } 66 } 67 67 68 final GenericDatagram that = ( GenericDatagram ) o; 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 } 69 75 70 if( type != that.type ) { 71 return false; 72 } 73 if( !wrapped.equals( that.wrapped ) ) { 74 return false; 75 } 76 final GenericDatagram that = ( GenericDatagram )o; 76 77 77 return true; 78 } 78 if ( type != that.type ) { 79 return false; 80 } 81 if ( !wrapped.equals( that.wrapped ) ) { 82 return false; 83 } 79 84 80 public int hashCode() { 81 int result; 82 result = ( int ) type; 83 result = 31 * result + wrapped.hashCode(); 84 return result; 85 } 85 return true; 86 } 87 88 public int hashCode() { 89 int result; 90 result = ( int )type; 91 result = 31 * result + wrapped.hashCode(); 92 return result; 93 } 94 95 public IDatagram wrapped() { 96 return wrapped; 97 } 86 98 } -
chess/src/chess/remote/impl/Remote.java
r19 r22 10 10 import chess.Util; 11 11 import chess.remote.*; 12 import chess.remote.impl.debug.DebugRemote;13 12 14 13 … … 87 86 try { 88 87 final RemoteDevice device = ( RemoteDevice )devices.elementAt( i ); 89 final RemoteNode node = new RemoteNode( device );88 final BTNode node = new BTNode( device ); 90 89 nodes[i] = node; 91 90 } catch ( IOException e ) { … … 123 122 final StreamConnection conn = server.acceptAndOpen(); 124 123 Util.log.println( "accepted incoming:" + conn ); 125 final RemoteNode client = new RemoteNode( conn );124 final BTNode client = new BTNode( conn ); 126 125 if ( listener.incoming( client ) ) { 127 126 //åñëè êëèåíò ïðèíÿò -- âûõîäèì (èãðà äîïóñêàåò òîëüêî îäíîãî ñîïåðíèêà) -
chess/src/chess/remote/impl/debug/DebugRemote.java
r19 r22 150 150 151 151 public void start() { 152 152 TIMER.schedule( 153 new TimerTask() { 154 public void run() { 155 listener.connected(); 156 } 157 }, networkLatency 158 ); 153 159 } 154 160 … … 177 183 Util.log.println( "recv: emulate" ); 178 184 179 Util.log.println( "recv: " + aPacket.wrapped );180 listener.received( aPacket.wrapped );181 182 Util.log.println( "recv: " + saPacket.wrapped );183 listener.received( saPacket.wrapped );185 Util.log.println( "recv: " + aPacket.wrapped() ); 186 listener.received( aPacket.wrapped() ); 187 188 Util.log.println( "recv: " + saPacket.wrapped() ); 189 listener.received( saPacket.wrapped() ); 184 190 } 185 191 }, 186 ( ( Action )aPacket.wrapped ).duration()192 ( ( Action )aPacket.wrapped() ).duration() 187 193 ); 188 194 } else { … … 233 239 Util.log.println( "server emulation started: " + name ); 234 240 is = prepareLog( player ); 241 final TimerTask handshakeTask = new TimerTask() { 242 public void run() { 243 Util.log.println( name + ": sending SET_PLAYER" ); 244 listener.received( 245 new SystemAction( 246 SystemAction.SET_PLAYER, 247 player.opponent() 248 ) 249 ); 250 if ( player == Player.WHITE ) { 251 packet(); 252 } else { 253 listener.received( 254 new SystemAction( 255 SystemAction.YOUR_TURN, 256 player 257 ) 258 ); 259 //èíà÷å æäåì YOUR_TURN îò áåëûõ 260 } 261 } 262 }; 263 235 264 TIMER.schedule( 236 265 new TimerTask() { 237 266 public void run() { 238 Util.log.println( name + ": sending SET_PLAYER" ); 239 listener.received( 240 new SystemAction( 241 SystemAction.SET_PLAYER, 242 player.opponent() 243 ) 244 ); 245 if ( player == Player.WHITE ) { 246 packet(); 247 } else { 248 listener.received( 249 new SystemAction( 250 SystemAction.YOUR_TURN, 251 player 252 ) 253 ); 254 //èíà÷å æäåì YOUR_TURN îò áåëûõ 255 } 267 Util.log.println( name + ":sending connect" ); 268 listener.connected(); 269 TIMER.schedule( handshakeTask, networkLatency ); 256 270 } 257 271 }, 258 2 *networkLatency272 networkLatency 259 273 ); 260 274 } … … 295 309 Util.log.println( name + ":recv emulation..." ); 296 310 297 Util.log.println( name + ":recv " + aPacket.wrapped );298 listener.received( aPacket.wrapped );299 300 Util.log.println( name + ":recv " + saPacket.wrapped );301 listener.received( saPacket.wrapped );311 Util.log.println( name + ":recv " + aPacket.wrapped() ); 312 listener.received( aPacket.wrapped() ); 313 314 Util.log.println( name + ":recv " + saPacket.wrapped() ); 315 listener.received( saPacket.wrapped() ); 302 316 } 303 317 }, 304 ( ( Action )aPacket.wrapped ).duration()318 ( ( Action )aPacket.wrapped() ).duration() 305 319 ); 306 320 } else { -
chess/src/chess/ui/ChessMIDlet.java
r19 r22 84 84 85 85 protected void startApp() { 86 showStartScreen(); 86 if ( display.getCurrent() == null ) { 87 showStartScreen(); 88 } 87 89 } 88 90 89 91 protected void pauseApp() { 92 //todo pause threads? 90 93 } 91 94 92 95 protected void destroyApp( boolean unconditional ) { 96 //todo remote.close() 97 ensureServerClosed(); 98 app = null; 99 notifyDestroyed(); 100 } 101 102 private void ensureServerClosed() { 103 if ( waitForIncomingProcess != null ) { 104 waitForIncomingProcess.interrupt( true ); 105 waitForIncomingProcess = null; 106 } 93 107 } 94 108 … … 104 118 105 119 protected void newGame( final Player player ) { 106 if ( waitForIncomingProcess != null ) { 107 waitForIncomingProcess.interrupt( true ); 108 waitForIncomingProcess = null; 109 } 120 ensureServerClosed(); 110 121 display.setCurrent( waitScreen ); 111 122 Util.log.println( "new game:" + player ); … … 134 145 if ( command == okCommand ) { 135 146 Util.log.println( command ); 136 try { 137 app = new App( 138 ChessMIDlet.this, 139 new Game(), 140 gameCanvas, 141 client, 142 player 143 ); 144 //display.setCurrent( gameCanvas ); 145 Util.log.println( "joined client" ); 146 retValue = true; 147 //return true; 148 } catch ( Exception e ) { 149 e.printStackTrace(); 150 retValue = false; 151 //return false; 152 } 147 retValue = true; 153 148 } else { 154 149 retValue = false; … … 170 165 } 171 166 } 167 if ( retValue ) { 168 try { 169 app = new App( 170 ChessMIDlet.this, 171 new Game(), 172 gameCanvas, 173 client, 174 player 175 ); 176 Util.log.println( "joined client" ); 177 } catch ( Exception e ) { 178 e.printStackTrace(); 179 } 180 } 172 181 return retValue; 173 182 } … … 206 215 if ( d == waitScreen ) { 207 216 if ( c == cancelCommand ) { 208 if ( waitForIncomingProcess != null ) { 209 waitForIncomingProcess.interrupt( true ); 210 waitForIncomingProcess = null; 211 } 217 ensureServerClosed(); 212 218 } 213 219 } -
chess/src/chess/ui/GameCanvas.java
r19 r22 438 438 break; 439 439 case WAITING_FOR_NETWORK: 440 if ( midlet.display.getCurrent() != waitScreen ) { 441 midlet.display.setCurrent( waitScreen ); 442 } 443 waitScreen.setText( MSG.getMessage( "wait.for-network" ) ); 440 showWaitMessage( MSG.getMessage( "wait.for-network" ) ); 444 441 Util.log.println( "status:WAIT_FOR_NETWORK" ); 445 442 break; 446 443 case WAITING_FOR_OPPONENT: 447 if ( midlet.display.getCurrent() != waitScreen ) { 448 midlet.display.setCurrent( waitScreen ); 449 } 450 waitScreen.setText( MSG.getMessage( "wait.for-opponent" ) ); 444 showWaitMessage( MSG.getMessage( "wait.for-opponent" ) ); 451 445 Util.log.println( "status:WAIT_FOR_OPPONENT" ); 452 446 break; 453 447 case DISCONNECTED: 448 showWaitMessage( MSG.getMessage( "wait.for-connect" ) ); 454 449 Util.log.println( "status:DISCONNECTED" ); 455 450 //todo implement 451 break; 452 } 453 } 454 455 private void showWaitMessage( final String message ) { 456 waitScreen.setText( message ); 457 if ( midlet.display.getCurrent() != waitScreen ) { 458 midlet.display.setCurrent( waitScreen ); 456 459 } 457 460 } -
chess/src/chess/ui/JoinGameScreen.java
r19 r22 14 14 * Time: 15:30:43<br/> 15 15 */ 16 public class JoinGameScreen extends BaseListScreen implements IChoiceListener, IClientListener { 16 public class JoinGameScreen extends BaseListScreen 17 implements IChoiceListener, IClientListener, Runnable { 17 18 //private final ChessMIDlet midlet; 18 19 … … 34 35 ); 35 36 36 private IAsyncProcess process;37 private IAsyncProcess asyncProcess; 37 38 38 39 public JoinGameScreen( final ChessMIDlet midlet ) { 39 40 super( midlet, MSG.getMessage( "join-game.title" ) ); 40 //this.midlet = midlet;41 41 42 42 scanningNetworkAlert.setTimeout( Alert.FOREVER ); 43 //scanningNetworkAlert.setTicker(44 // new Ticker( "Looking for game servers" )45 //);46 43 scanningNetworkAlert.removeCommand( Alert.DISMISS_COMMAND ); 47 44 scanningNetworkAlert.addCommand( midlet.cancelCommand ); 48 45 scanningNetworkAlert.setCommandListener( this ); 49 46 50 //addCommand( midlet.okCommand );51 47 addCommand( midlet.refreshCommand ); 52 48 addCommand( midlet.backCommand ); 53 49 54 //setSelectCommand( midlet.okCommand ); 50 setChoiceListener( this ); 51 } 55 52 56 //setCommandListener( this );57 setChoiceListener( this);53 public void run() { 54 refresh( true ); 58 55 } 59 56 60 57 public void refresh( final boolean force ) { 61 58 this.force = force; 62 if ( force && isShown()) {59 if ( force && midlet.display.getCurrent() == this ) { 63 60 midlet.display.setCurrent( scanningNetworkAlert ); 64 61 } 65 if ( process != null ) { 66 process.interrupt( true ); 67 process = null; 68 } 69 process = midlet.getRemoteManager().getAvailable( this.force, this ); 62 ensureSearcherClosed(); 63 asyncProcess = midlet.getRemoteManager().getAvailable( this.force, this ); 70 64 } 71 65 72 66 public void finished( final Object res ) { 73 67 final IRemoteNode[] servers = ( IRemoteNode[] )res; 74 if ( !force ) {75 if ( servers == null || servers.length == 0 ) {76 refresh( true );77 return;78 }79 }80 //todo check process.status() and display error message81 82 68 removeAllItems(); 83 69 nodes = servers; 84 if ( nodes == null || nodes.length == 0 ) { 85 midlet.display.setCurrent( nothingFoundAlert, this ); 70 71 //todo check asyncProcess.status() and display error message 72 73 final boolean nothingFound = ( servers == null || servers.length == 0 ); 74 if ( nothingFound ) { 75 if ( force ) { 76 //åñëè èñêàëè â ñåòè è íè÷åãî íå íàøëè -- íå ñóäüáà 77 midlet.display.setCurrent( nothingFoundAlert, this ); 78 } else { 79 //åñëè èñêàëè â êýøå... 80 //...è íèõåðà íå íàøëè -- ïðèäåòñÿ îïðîñèòü ñåòü è îáíîâèòü êýø 81 /** 82 * îïðîñ äåëàåì ïðèíóäèòåëüíî â EDT, ÷òîáû èçáåæàòü deadlock'îâ 83 * è, çàîäíî, äàòü îáíîâèòüñÿ ýêðàíó 84 */ 85 midlet.display.callSerially( this ); 86 } 86 87 } else { 87 88 for ( int i = 0; i < nodes.length; i++ ) { … … 97 98 if ( displayable == scanningNetworkAlert ) { 98 99 if ( command == midlet.cancelCommand ) { 99 if ( process != null ) { 100 process.interrupt( true ); 101 process = null; 102 } 100 ensureSearcherClosed(); 103 101 } 104 } 102 } 105 103 super.commandAction( command, displayable ); 106 104 } … … 114 112 } 115 113 } else if ( c == midlet.backCommand ) { 114 ensureSearcherClosed(); 116 115 midlet.showStartScreen(); 117 116 } else if ( c == midlet.refreshCommand ) { … … 119 118 } 120 119 } 120 121 private void ensureSearcherClosed() { 122 if ( asyncProcess != null ) { 123 asyncProcess.interrupt( true ); 124 asyncProcess = null; 125 } 126 } 121 127 }
Note: See TracChangeset
for help on using the changeset viewer.