Main Page | Packages | Class List | Directories | File List | Class Members

TicTacCom.java

Go to the documentation of this file.
00001 import javax.microedition.lcdui.CommandListener;
00002 
00003 /*
00004  * Created on 22 mai 2005
00005  *
00006  * TODO To change the template for this generated file go to
00007  * Window - Preferences - Java - Code Style - Code Templates
00008  */
00009 
00017 import javax.bluetooth.*;
00018 import javax.microedition.io.*;
00019 
00020 import java.io.*;
00021 
00028 public class TicTacCom implements Runnable, DiscoveryListener {
00029 
00031         TicTacMain parent;
00033         int role;
00034         
00035         public static final int ROLE_SERVEUR=0;
00036         public static final int ROLE_CLIENT=1;
00037         
00039         public static final UUID RFCOMM_UUID = new UUID(0x0003);        
00040         
00042         private DiscoveryAgent discoveryAgent;
00044         private RemoteDevice[] remoteDevices;
00045         
00046         private UUID[] uuidSet;
00047         
00049         private String serviceUrl;
00050         int clientResponseCode = -1 ;
00051           
00052         //Serveur
00053         private StreamConnectionNotifier notifier;
00054         private StreamConnection conn;
00055         private LocalDevice localDevice;
00056         private ServiceRecord serviceRecord;
00057         private InputStream input;
00058         private OutputStream output;
00059           
00060         private static String serverUrl = "btspp://localhost:" + RFCOMM_UUID + ";name=rfcommtest;authorize=true";
00061           
00062         private boolean stopThread=false;
00063          
00064         Thread thread = new Thread(this) ;
00065         
00066         boolean initOK=false;
00067         
00068         boolean newData=false;
00069         int toSendData[]=new int[4];
00070         
00071         boolean readRequest=false;
00072         int readData[]=new int[4];
00073         
00074         
00075         boolean newReceivedData=false;
00076         
00077         
00079         public int [] getReceivedData(){
00080                 return readData;
00081         }
00082         
00090         public void send(int player, int x, int y, int z){
00091                 synchronized(this){
00092                 toSendData[0]=player;
00093                 toSendData[1]=x;
00094                 toSendData[2]=y;
00095                 toSendData[3]=z;
00096                 
00097                 newData=true;
00098                 }
00099         }
00100         
00102         public void receive(){
00103                 synchronized(this){
00104                 newReceivedData=false;
00105                 readRequest=true;
00106                 }
00107         }
00108         
00114         public TicTacCom(TicTacMain p, int r) {
00115                 parent = p;
00116                 role=r;
00117                 
00118                 init(2);
00119                 
00120                 //On thread
00121             thread.start() ;
00122         }
00123         
00124         
00128         public void run() {
00129                 
00130                 try {
00131                         do{
00132                                 init(1);
00133                         }while(role!=ROLE_SERVEUR && serviceUrl==null);
00134                                 
00135                         while(!initOK)
00136                                 ;
00137                         
00138                         
00139                         
00140                         if(role==ROLE_SERVEUR)
00141                                 conn = notifier.acceptAndOpen(); 
00142                         else
00143                                 conn = (StreamConnection)Connector.open(serviceUrl);
00144                         
00145                         output = conn.openOutputStream(); 
00146                         input = conn.openInputStream();
00147                                 
00148                         while(!stopThread){
00149                                 synchronized(this){
00150                                 
00151                                 if(newData){
00152 
00153                                         for(int i=0; i<toSendData.length; i++)
00154                                                 output.write(toSendData[i]);    
00155                                         newData=false;
00156                                 }
00157                                 
00158                                 if(readRequest){
00159                                         for(int i=0; i<readData.length; i++)
00160                                                 readData[i]=input.read();
00161                                         readRequest=false;
00162                                         newReceivedData=true;
00163                                 }
00164                                 }
00165                         }
00166                         
00167                 }catch (Exception ex) {
00168                         System.err.println("Bluetooth Running Error: " + ex +" on "+role);      
00169             } finally {
00170               try {
00171                 
00172                 if (input != null) {
00173                         input.close();
00174                 }
00175                 if (output != null) {
00176                         output.close();
00177                 }
00178                 if (conn != null) {
00179                         conn.close();
00180                 }
00181                 
00182               } catch (IOException ioe) {
00183                 System.err.println( "Error Closing connection "+role+": IOException: " + ioe );
00184               }
00185             }
00186         }
00187         
00188         public void debug(String st) {
00189             System.out.println ("DEBUG TicTacCom : "+st) ;
00190         }
00191         
00193         void init(int r){
00194                 try{
00195                         if(r==1)
00196                                 initServer();
00197                         else
00198                                 initClient();
00199                         
00200                 }catch (BluetoothStateException e) {        
00201                 System.err.println( "Init "+role+": BluetoothStateException: " + e.getMessage() );    
00202             } catch (IOException e) {        
00203                 System.err.println( "Init "+role+": IOException: " + e.getMessage() );      
00204             }catch (Exception e) {
00205                 System.err.println( "Init "+role+": Exception: " + e.getMessage() );
00206             }
00207                 
00208         }
00209         
00211         void initServer() throws BluetoothStateException, IOException, Exception{
00212         conn = null;
00213         localDevice = LocalDevice.getLocalDevice();
00214         localDevice.setDiscoverable( DiscoveryAgent.GIAC );
00215         
00216         notifier = (StreamConnectionNotifier)Connector.open(serverUrl);  
00217         
00218         initOK=true;
00219         }
00220         
00222         void initClient() throws Exception {
00223                 localDevice = LocalDevice.getLocalDevice();
00224                 discoveryAgent = localDevice.getDiscoveryAgent();
00225                 debug ("startInquiry(DiscoveryAgent.GIAC, this);") ;
00226       
00227                 discoveryAgent.startInquiry(DiscoveryAgent.GIAC, this);
00228                 
00229         }
00230         
00232         
00234          public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
00235             debug("deviceDiscovered") ;
00236             try {
00237               // Get Device Info
00238               System.out.println("Device Discovered");
00239               System.out.println("Major Device Class: " + cod.getMajorDeviceClass() + " Minor Device Class: " + cod.getMinorDeviceClass());
00240               System.out.println("Bluetooth Address: " + btDevice.getBluetoothAddress());
00241               System.out.println("Bluetooth Friendly Name: " + btDevice.getFriendlyName(true));     
00242               
00243               // Search for Services
00244               uuidSet = new UUID[1];     
00245               uuidSet[0] = RFCOMM_UUID;      
00246               int searchID = discoveryAgent.searchServices(null,uuidSet,btDevice,this);      
00247             } catch (Exception e) {
00248               System.out.println("Device Discovered Error: " + e);      
00249             }
00250           }
00251           
00252           public void inquiryCompleted(int discType) {
00253             debug("inquiryCompleted") ;
00254            // System.out.println("InquiryCompleted");
00255           }
00256           
00258           public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
00259             debug("servicesDiscovered") ;
00260             
00261             // in this example there is only one service
00262             for(int i=0;i<servRecord.length;i++) {       
00263               serviceUrl = servRecord[i].getConnectionURL(0,false);
00264             }    
00265           }  
00266           
00268           public void serviceSearchCompleted(int transID, int responseCode) {    
00269             debug("serviceSearchCompleted") ;
00270             
00271             
00272                
00273             if(responseCode == SERVICE_SEARCH_COMPLETED) {
00274               System.out.println("SERVICE_SEARCH_COMPLETED\n");
00275               System.out.println("Service URL: " + serviceUrl);
00276               clientResponseCode = responseCode ;
00277                   
00278                   initOK=true;
00279             }else{
00280                     if(responseCode == SERVICE_SEARCH_ERROR)
00281                               System.out.println("SERVICE_SEARCH_ERROR\n");
00282                     
00283                     if(responseCode == SERVICE_SEARCH_TERMINATED)
00284                       System.out.println("SERVICE_SEARCH_TERMINATED\n");
00285                     
00286                     if(responseCode == SERVICE_SEARCH_DEVICE_NOT_REACHABLE)
00287                       System.out.println("SERVICE_SEARCH_DEVICE_NOT_REACHABLE\n");
00288                     
00289                     if(responseCode == SERVICE_SEARCH_NO_RECORDS){
00290                       System.out.println("SERVICE_SEARCH_NO_RECORDS\n");
00291         
00292                     }
00293             }
00294 
00295            
00296           }
00297           
00298           
00299 }

Generated on Sat Jun 11 20:13:59 2005 for TicTacToe3D by  doxygen 1.4.3