- Timestamp:
- 02/06/09 09:38:29 (13 years ago)
- Location:
- chess
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
chess/chess.jad
r26 r27 1 1 MIDlet-1: Chess Game, , chess.ui.ChessMIDlet 2 MIDlet-Jar-Size: 845402 MIDlet-Jar-Size: 310674 3 3 MIDlet-Jar-URL: chess.jar 4 4 MIDlet-Name: Chess -
chess/src/chess/MSG.properties
r25 r27 1 1 command.ok=OK 2 command.yes=\u0414\u0430 3 command.no=\u041d\u0435\u0442 2 4 command.exit=\u0412\u044b\u0445\u043e\u0434 3 5 command.back=\u041d\u0430\u0437\u0430\u0434 -
chess/src/chess/Util.java
r23 r27 10 10 import chess.remote.IRemote; 11 11 import chess.remote.impl.BTRemote; 12 import chess.remote.impl.debug.DebugRemote; 12 13 13 14 … … 24 25 /** 25 26 * âêëþ÷àåò äîïîëíèòåëüíûå ïðîâåðêè íà ñòàäèè âûïîëíåíèÿ, 26 * ïðè ýòîì îòêëþ÷àòü èõ íà ñòàäèè ïîäãîòîâêè production27 * ïðè ýòîì ìîæíî îòêëþ÷àòü èõ íà ñòàäèè ïîäãîòîâêè production 27 28 */ 28 29 public static final boolean SAFE_MODE = true; … … 31 32 public static IRemote create() throws Exception { 32 33 return new BTRemote(); 33 //return new DebugRemote( 100, 100 );34 //return new DebugRemote( 500, 500 ); 34 35 } 35 36 -
chess/src/chess/remote/IAsyncProcess.java
r9 r27 9 9 public static final int SUCCEEDED = 1; 10 10 public static final int FAILED = 2; 11 public static final int INTERRUPTED = 3; 11 12 12 13 public int status(); -
chess/src/chess/remote/impl/BTRemote.java
r25 r27 20 20 */ 21 21 public class BTRemote implements IRemote { 22 public static final String BTSPP_SCHEME = "btspp://"; 23 public static final String MY_SERVICE_NUMBER = "3B9FA89520078C303355AAA694238F08;name=ChessGameServer"; 24 25 public static final IRemoteNode[] EMPTY = new IRemoteNode[0]; 26 27 private final LocalDevice localDevice; 28 private final Vector devices = new Vector(); 29 30 public BTRemote() throws Exception { 31 localDevice = LocalDevice.getLocalDevice(); 32 fillupCache(); 33 } 34 35 private synchronized void fillupCache() { 36 final DiscoveryAgent agent = localDevice.getDiscoveryAgent(); 37 final RemoteDevice[] preknownDevices = agent.retrieveDevices( DiscoveryAgent.PREKNOWN ); 38 if( preknownDevices != null ) { 39 for( int i = 0; i < preknownDevices.length; i++ ) { 40 final RemoteDevice device = preknownDevices[i]; 41 devices.addElement( device ); 42 } 43 } 44 final RemoteDevice[] cachedDevices = agent.retrieveDevices( DiscoveryAgent.CACHED ); 45 if( cachedDevices != null ) { 46 for( int i = 0; i < cachedDevices.length; i++ ) { 47 final RemoteDevice device = cachedDevices[i]; 48 devices.addElement( device ); 49 } 50 } 51 } 52 53 private synchronized void refreshCache() { 54 devices.removeAllElements(); 55 fillupCache(); 56 } 57 58 public IAsyncProcess waitForIncoming( final IServerListener l ) { 59 return new WaitForIncomingThread( localDevice, l ); 60 } 61 62 public IAsyncProcess getAvailable( final boolean refresh, 63 final IClientListener l ) { 64 if( refresh ) { 65 try { 66 final DiscoveryAgent agent = localDevice.getDiscoveryAgent(); 67 return new QueryDeviceProcess( this, agent, l ); 68 } catch( Exception e ) { 69 e.printStackTrace(); 70 l.finished( null ); 71 return null; 72 } 73 } else { 74 final IRemoteNode[] nodes = getNodes(); 75 l.finished( nodes ); 76 return null; 77 } 78 } 79 80 private synchronized IRemoteNode[] getNodes() { 81 if( devices.isEmpty() ) { 82 return EMPTY; 83 } 84 final int length = devices.size(); 85 final IRemoteNode[] nodes = new IRemoteNode[length]; 86 for( int i = 0; i < length; i++ ) { 87 try { 88 final RemoteDevice device = ( RemoteDevice ) devices.elementAt( i ); 89 final BTNode node = new BTNode( device ); 90 nodes[i] = node; 91 } catch( IOException e ) { 92 e.printStackTrace(); 93 } 94 } 95 return nodes; 96 } 97 98 private static class WaitForIncomingThread extends BaseAsyncProcess { 99 private final LocalDevice localDevice; 100 private StreamConnectionNotifier server; 101 102 public WaitForIncomingThread( final LocalDevice local, 103 final IServerListener listener ) { 104 super( listener ); 105 this.localDevice = local; 106 start(); 107 } 108 109 public void run() { 110 try { 111 localDevice.setDiscoverable( DiscoveryAgent.GIAC ); 112 setStatus( IN_PROCESS ); 113 while( true ) { 114 server = ( StreamConnectionNotifier ) Connector.open( 115 BTSPP_SCHEME + "localhost:" + MY_SERVICE_NUMBER, 116 Connector.READ_WRITE, 117 true 118 ); 119 Util.log.println( "creating server and waiting for incoming" ); 120 try { 121 while( true ) { 122 // Îæèäàåì âõîäÿùåå ïîòîêîâîå ñîåäèíåíèå 123 final StreamConnection conn = server.acceptAndOpen(); 124 Util.log.println( "accepted incoming:" + conn ); 125 final BTNode client = new BTNode( conn ); 126 if( listener.incoming( client ) ) { 127 //åñëè êëèåíò ïðèíÿò -- âûõîäèì (èãðà äîïóñêàåò òîëüêî îäíîãî ñîïåðíèêà) 128 Util.log.println( "client accepted -> close server" ); 129 setStatus( SUCCEEDED ); 130 return; 131 } 132 client.close(); 133 } 134 } catch( InterruptedIOException ex ) { 135 Util.log.println( "server was closed:" + ex.getMessage() ); 136 return; 137 } catch( IOException ex ) { 138 Util.log.println( "server got error -- trying to recreate server connection" ); 139 ex.printStackTrace(); 140 } finally { 141 if( server != null ) { 142 try { 143 server.close(); 144 server = null; 145 } catch( IOException e ) { 146 e.printStackTrace(); 147 } 148 } 149 } 150 151 } 152 } catch( IOException ex ) { 153 //åñëè íå óäàëîñü îòêðûòü ñîåäèíåíèå -- ýòî ïèçäåö. óõîäèì 154 //...íå óäàëîñü îïóáëèêîâàòü óñòðîéñòâî -- ïèçäåö, óõîäèì 155 ex.printStackTrace(); 156 setStatus( FAILED ); 157 } finally { 158 listener.finished( null ); 159 Util.log.println( "server stopped" ); 160 } 161 } 162 163 164 public void interrupt( final boolean waitFor ) { 165 if( isAlive() ) { 166 // interrupt(); 167 if( server != null ) { 168 try { 169 server.close(); 170 server = null; 171 } catch( IOException e ) { 172 e.printStackTrace(); 173 } 174 } 175 if( waitFor && Thread.currentThread() != this ) { 176 try { 177 join(); 178 } catch( InterruptedException e ) { 179 e.printStackTrace(); 180 } 181 } 182 } 183 } 184 } 185 186 private static class QueryDeviceProcess implements IAsyncProcess, DiscoveryListener { 187 private final DiscoveryAgent agent; 188 private final IClientListener l; 189 private final BTRemote remote; 190 191 private volatile int status = IN_PROCESS; 192 193 public QueryDeviceProcess( final BTRemote remote, 194 final DiscoveryAgent agent, 195 final IClientListener l ) throws Exception { 196 this.remote = remote; 197 this.agent = agent; 198 this.l = l; 199 agent.startInquiry( DiscoveryAgent.GIAC, this ); 200 } 201 202 public int status() { 203 return status; 204 } 205 206 public void interrupt( final boolean waitFor ) { 207 agent.cancelInquiry( this ); 208 } 209 210 public IAsyncProcessListener getListener() { 211 return l; 212 } 213 214 public void deviceDiscovered( final RemoteDevice btDevice, 215 final DeviceClass cod ) { 216 Util.log.println( "device found:" + btDevice.getBluetoothAddress() ); 217 } 218 219 public void servicesDiscovered( final int transID, 220 final ServiceRecord[] servRecord ) { 221 Util.log.println( "service found:" + transID ); 222 } 223 224 public void serviceSearchCompleted( final int transID, 225 final int respCode ) { 226 227 } 228 229 public void inquiryCompleted( final int discType ) { 230 switch( discType ) { 231 case INQUIRY_COMPLETED: 232 remote.refreshCache(); 233 status = SUCCEEDED; 234 l.finished( remote.getNodes() ); 235 break; 236 case INQUIRY_TERMINATED: 237 Util.log.println( "query interrupted" ); 238 case INQUIRY_ERROR: 239 status = FAILED; 240 l.finished( null ); 241 } 242 } 243 } 22 public static final String BTSPP_SCHEME = "btspp://"; 23 public static final String MY_SERVICE_NUMBER = "3B9FA89520078C303355AAA694238F08;name=ChessGameServer"; 24 25 public static final IRemoteNode[] EMPTY = new IRemoteNode[0]; 26 27 private final LocalDevice localDevice; 28 private final Vector devices = new Vector(); 29 30 public BTRemote() throws Exception { 31 localDevice = LocalDevice.getLocalDevice(); 32 fillupCache(); 33 } 34 35 private synchronized void fillupCache() { 36 final DiscoveryAgent agent = localDevice.getDiscoveryAgent(); 37 final RemoteDevice[] preknownDevices = agent.retrieveDevices( DiscoveryAgent.PREKNOWN ); 38 if ( preknownDevices != null ) { 39 for ( int i = 0; i < preknownDevices.length; i++ ) { 40 final RemoteDevice device = preknownDevices[i]; 41 devices.addElement( device ); 42 } 43 } 44 final RemoteDevice[] cachedDevices = agent.retrieveDevices( DiscoveryAgent.CACHED ); 45 if ( cachedDevices != null ) { 46 for ( int i = 0; i < cachedDevices.length; i++ ) { 47 final RemoteDevice device = cachedDevices[i]; 48 devices.addElement( device ); 49 } 50 } 51 } 52 53 private synchronized void refreshCache() { 54 devices.removeAllElements(); 55 fillupCache(); 56 } 57 58 public IAsyncProcess waitForIncoming( final IServerListener l ) { 59 return new WaitForIncomingThread( localDevice, l ); 60 } 61 62 public IAsyncProcess getAvailable( final boolean refresh, 63 final IClientListener l ) { 64 if ( refresh ) { 65 try { 66 final DiscoveryAgent agent = localDevice.getDiscoveryAgent(); 67 return new QueryDeviceProcess( this, agent, l ); 68 } catch ( Exception e ) { 69 e.printStackTrace(); 70 l.finished( null ); 71 return null; 72 } 73 } else { 74 final IRemoteNode[] nodes = getNodes(); 75 l.finished( nodes ); 76 return null; 77 } 78 } 79 80 private synchronized IRemoteNode[] getNodes() { 81 if ( devices.isEmpty() ) { 82 return EMPTY; 83 } 84 final int length = devices.size(); 85 final IRemoteNode[] nodes = new IRemoteNode[length]; 86 for ( int i = 0; i < length; i++ ) { 87 try { 88 final RemoteDevice device = ( RemoteDevice )devices.elementAt( i ); 89 final BTNode node = new BTNode( device ); 90 nodes[i] = node; 91 } catch ( IOException e ) { 92 e.printStackTrace(); 93 } 94 } 95 return nodes; 96 } 97 98 private static class WaitForIncomingThread extends BaseAsyncProcess { 99 private final LocalDevice localDevice; 100 private StreamConnectionNotifier server; 101 102 public WaitForIncomingThread( final LocalDevice local, 103 final IServerListener listener ) { 104 super( listener ); 105 this.localDevice = local; 106 start(); 107 } 108 109 public void run() { 110 try { 111 localDevice.setDiscoverable( DiscoveryAgent.GIAC ); 112 setStatus( IN_PROCESS ); 113 while ( true ) { 114 server = ( StreamConnectionNotifier )Connector.open( 115 BTSPP_SCHEME + "localhost:" + MY_SERVICE_NUMBER, 116 Connector.READ_WRITE, 117 true 118 ); 119 Util.log.println( "creating server and waiting for incoming" ); 120 try { 121 final IServerListener _listener = ( IServerListener )listener; 122 while ( true ) { 123 // Îæèäàåì âõîäÿùåå ïîòîêîâîå ñîåäèíåíèå 124 final StreamConnection conn = server.acceptAndOpen(); 125 Util.log.println( "accepted incoming: " + conn ); 126 final BTNode client = new BTNode( conn ); 127 if ( _listener.incoming( client ) ) { 128 //åñëè êëèåíò ïðèíÿò -- âûõîäèì (èãðà äîïóñêàåò òîëüêî îäíîãî ñîïåðíèêà) 129 Util.log.println( "client accepted -> close server" ); 130 setStatus( SUCCEEDED ); 131 return; 132 } 133 client.close(); 134 } 135 } catch ( InterruptedIOException ex ) { 136 setStatus( INTERRUPTED ); 137 Util.log.println( "server was closed: " + ex.getMessage() ); 138 return; 139 } catch ( IOException ex ) { 140 Util.log.println( "server got error -- trying to recreate server connection" ); 141 ex.printStackTrace(); 142 } finally { 143 if ( server != null ) { 144 try { 145 server.close(); 146 server = null; 147 } catch ( IOException e ) { 148 e.printStackTrace(); 149 } 150 } 151 } 152 } 153 } catch ( IOException ex ) { 154 //åñëè íå óäàëîñü îòêðûòü ñîåäèíåíèå -- ýòî ïèçäåö. óõîäèì 155 //...íå óäàëîñü îïóáëèêîâàòü óñòðîéñòâî -- ïèçäåö, óõîäèì 156 ex.printStackTrace(); 157 setStatus( FAILED ); 158 } finally { 159 listener.finished( null ); 160 Util.log.println( "server stopped" ); 161 } 162 } 163 164 public void interrupt( final boolean waitFor ) { 165 if ( isAlive() ) { 166 if ( server != null ) { 167 try { 168 server.close(); 169 server = null; 170 } catch ( IOException e ) { 171 e.printStackTrace(); 172 } 173 } 174 waitForThread( waitFor ); 175 } 176 } 177 178 } 179 180 private static class QueryDeviceProcess extends BaseAsyncProcess implements DiscoveryListener { 181 private final DiscoveryAgent agent; 182 private final BTRemote remote; 183 184 public QueryDeviceProcess( final BTRemote remote, 185 final DiscoveryAgent agent, 186 final IClientListener listener ) { 187 super( listener ); 188 this.remote = remote; 189 this.agent = agent; 190 start(); 191 } 192 193 public void run() { 194 /* Íåïîíÿòíî, ÿâëÿåòñÿ ëè ýòîò çàïðîñ ñèíõðîííûì, èëè àñèíõðîííûì -- 195 * äîêóìåíòàöèÿ íà ýòîò ñ÷åò ìîë÷èò, à ïðàêòèêà ïîêàçûâàåò, ÷òî 196 * áûâàåò è òàê è ýäàê. Ïîýòîìó ñòðàõóåìñÿ íà âñÿêèé ñëó÷àé îò 197 * íåâåäîìîé õóéíè è äåëàåì ýòî â îòäåëüíîì ïîòîêå 198 */ 199 setStatus( IN_PROCESS ); 200 Util.log.println( "starting query device list" ); 201 try { 202 agent.startInquiry( DiscoveryAgent.GIAC, this ); 203 } catch ( BluetoothStateException e ) { 204 e.printStackTrace(); 205 setStatus( FAILED ); 206 listener.finished( null ); 207 } 208 } 209 210 211 public void interrupt( final boolean waitFor ) { 212 if ( status() == IN_PROCESS ) { 213 agent.cancelInquiry( this ); 214 waitForThread( waitFor ); 215 } 216 } 217 218 public void deviceDiscovered( final RemoteDevice btDevice, 219 final DeviceClass cod ) { 220 Util.log.println( "device found:" + btDevice.getBluetoothAddress() ); 221 } 222 223 public void servicesDiscovered( final int transID, 224 final ServiceRecord[] servRecord ) { 225 Util.log.println( "service found:" + transID ); 226 } 227 228 public void serviceSearchCompleted( final int transID, 229 final int respCode ) { 230 231 } 232 233 public void inquiryCompleted( final int discType ) { 234 switch ( discType ) { 235 case INQUIRY_COMPLETED: 236 Util.log.println( "device list query finished" ); 237 remote.refreshCache(); 238 setStatus( SUCCEEDED ); 239 listener.finished( remote.getNodes() ); 240 break; 241 case INQUIRY_TERMINATED: 242 Util.log.println( "device list query interrupted" ); 243 setStatus( INTERRUPTED ); 244 listener.finished( null ); 245 break; 246 case INQUIRY_ERROR: 247 Util.log.println( "device list query failed" ); 248 setStatus( FAILED ); 249 listener.finished( null ); 250 break; 251 } 252 } 253 } 244 254 } -
chess/src/chess/remote/impl/BaseAsyncProcess.java
r14 r27 3 3 import chess.remote.IAsyncProcess; 4 4 import chess.remote.IAsyncProcessListener; 5 import chess. remote.IServerListener;5 import chess.Util; 6 6 7 7 /** … … 12 12 */ 13 13 public class BaseAsyncProcess extends Thread implements IAsyncProcess { 14 protected final IServerListener listener; 15 private volatile int status = IN_PROCESS; 14 protected final IAsyncProcessListener listener; 16 15 17 public BaseAsyncProcess( final IServerListener listener ) { 16 private int status = IN_PROCESS; 17 18 public BaseAsyncProcess( final IAsyncProcessListener listener ) { 18 19 this.listener = listener; 19 20 } 20 21 21 public int status() {22 public final int status() { 22 23 return status; 23 24 } 24 25 25 protected void setStatus( final int status ) {26 protected final synchronized void setStatus( final int status ) { 26 27 switch ( status ) { 27 28 case IN_PROCESS: 28 29 case SUCCEEDED: 29 30 case FAILED: 31 case INTERRUPTED: 30 32 this.status = status; 31 33 break; … … 38 40 if ( isAlive() ) { 39 41 interrupt(); 40 if ( waitFor && Thread.currentThread() != this ) { 41 try { 42 join(); 43 } catch ( InterruptedException e ) { 44 e.printStackTrace(); 45 } 42 waitForThread( waitFor ); 43 } 44 } 45 46 protected void waitForThread( final boolean waitFor ) { 47 if ( waitFor 48 && isAlive() 49 && Thread.currentThread() != this ) { 50 Util.log.println( "waiting for thread to finish" ); 51 try { 52 join(); 53 } catch ( InterruptedException e ) { 54 e.printStackTrace(); 46 55 } 47 56 } 48 57 } 49 58 50 public IAsyncProcessListener getListener() {59 public final IAsyncProcessListener getListener() { 51 60 return listener; 52 61 } -
chess/src/chess/remote/impl/debug/DebugRemote.java
r23 r27 113 113 try { 114 114 setStatus( IN_PROCESS ); 115 final IServerListener _listener = ( IServerListener )listener; 115 116 while ( true ) { 116 try { 117 synchronized ( this ) { 118 wait( 4 * networkLatency ); 119 } 120 } catch ( InterruptedException e ) { 121 e.printStackTrace(); 117 synchronized ( this ) { 118 wait( 4 * networkLatency ); 122 119 } 123 if ( listener.incoming( new ClientRemoteNode() ) ) {120 if ( _listener.incoming( new ClientRemoteNode() ) ) { 124 121 Util.log.println( "emulate incoming connection: OK" ); 125 122 setStatus( SUCCEEDED ); … … 127 124 } 128 125 } 126 } catch ( InterruptedException ex ) { 127 setStatus( INTERRUPTED ); 129 128 } catch ( Exception ex ) { 130 129 setStatus( FAILED ); -
chess/src/chess/ui/ChessMIDlet.java
r25 r27 26 26 /* commands */ 27 27 public final Command okCommand = new Command( MSG.getMessage( "command.ok" ), Command.OK, 1 ); 28 public final Command yesCommand = new Command( MSG.getMessage( "command.yes" ), Command.OK, 1 ); 29 public final Command noCommand = new Command( MSG.getMessage( "command.no" ), Command.CANCEL, 1 ); 28 30 public final Command exitCommand = new Command( MSG.getMessage( "command.exit" ), Command.EXIT, 1 ); 29 public final Command closeCommand = new Command( MSG.getMessage( "command.close" ), Command.S CREEN, 1 );31 public final Command closeCommand = new Command( MSG.getMessage( "command.close" ), Command.STOP, 1 ); 30 32 public final Command cancelCommand = new Command( MSG.getMessage( "command.cancel" ), Command.CANCEL, 1 ); 31 public final Command backCommand = new Command( MSG.getMessage( "command.back" ), Command. CANCEL, 1 );33 public final Command backCommand = new Command( MSG.getMessage( "command.back" ), Command.BACK, 1 ); 32 34 public final Command refreshCommand = new Command( MSG.getMessage( "command.refresh" ), Command.SCREEN, 1 ); 33 35 public final Command newCommand = new Command( MSG.getMessage( "command.new" ), Command.SCREEN, 1 ); … … 136 138 ); 137 139 dlg.setTimeout( Alert.FOREVER ); 138 dlg.addCommand( okCommand ); 139 dlg.addCommand( Alert.DISMISS_COMMAND ); 140 dlg.addCommand( yesCommand ); 141 dlg.addCommand( noCommand ); 142 //dlg.removeCommand( Alert.DISMISS_COMMAND ); 140 143 dlg.setCommandListener( 141 144 new CommandListener() { … … 143 146 final Displayable displayable ) { 144 147 try { 145 if ( command == okCommand ) {146 Util.log.println( command );148 //Util.log.println( command ); 149 if ( command == yesCommand ) { 147 150 retValue = true; 148 151 } else { … … 153 156 ChessMIDlet.this.notifyAll(); 154 157 } 158 display.setCurrent( waitScreen ); 155 159 } 156 160 } … … 162 166 ChessMIDlet.this.wait(); 163 167 } catch ( InterruptedException e ) { 164 e.printStackTrace();168 //e.printStackTrace(); 165 169 } 166 170 } … … 183 187 184 188 public void finished( final Object res ) { 185 //todo ask for status and display error message if failed 186 if ( waitForIncomingProcess.status() == IAsyncProcess.SUCCEEDED ) { 187 waitScreen.finished( res ); 188 } else { 189 display.setCurrent( startForm ); 189 //todo display error message if failed 190 final int status = waitForIncomingProcess.status(); 191 switch ( status ) { 192 case IAsyncProcess.SUCCEEDED: 193 waitScreen.finished( res ); 194 break; 195 case IAsyncProcess.FAILED: 196 final Alert alert = new Alert( "Error", "Error in starting server", null, AlertType.ERROR ); 197 display.setCurrent( alert, startForm ); 198 break; 199 case IAsyncProcess.INTERRUPTED: 200 display.setCurrent( startForm ); 201 break; 190 202 } 191 203 waitForIncomingProcess = null;
Note: See TracChangeset
for help on using the changeset viewer.