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 { 00029 00031 protected TicTacMain parent; 00033 protected int role; 00034 00035 protected static final int ROLE_SERVEUR=0; 00036 protected static final int ROLE_CLIENT=1; 00037 00039 protected static final UUID RFCOMM_UUID = new UUID(0x0003); 00040 00041 00042 //Données partagées 00043 protected StreamConnection conn; 00044 protected LocalDevice localDevice; 00045 protected InputStream input; 00046 protected OutputStream output; 00047 00048 00049 private boolean stopThread=false; 00050 00051 Thread thread = new Thread(this) ; 00052 00053 boolean initOK=false; 00054 00055 boolean newData=false; 00056 int toSendData[]=new int[4]; 00057 00058 boolean readRequest=false; 00059 int readData[]=new int[4]; 00060 00061 00062 boolean newReceivedData=false; 00063 00064 00066 public int [] getReceivedData(){ 00067 return readData; 00068 } 00069 00077 public void send(int player, int x, int y, int z){ 00078 synchronized(this){ 00079 toSendData[0]=player; 00080 toSendData[1]=x; 00081 toSendData[2]=y; 00082 toSendData[3]=z; 00083 00084 newData=true; 00085 } 00086 } 00087 00089 public void receive(){ 00090 synchronized(this){ 00091 newReceivedData=false; 00092 readRequest=true; 00093 } 00094 } 00095 00096 void initBeforeThread(){} 00097 void initAfterThread(){} 00098 void initCom() throws IOException{} 00099 00105 public TicTacCom(TicTacMain p, int r) { 00106 parent = p; 00107 role=r; 00108 00109 initBeforeThread(); 00110 00111 //On thread 00112 thread.start() ; 00113 } 00114 00115 00119 public void run() { 00120 00121 try { 00122 initAfterThread(); 00123 00124 while(!initOK) 00125 ; 00126 00127 00128 initCom(); 00129 00130 output = conn.openOutputStream(); 00131 input = conn.openInputStream(); 00132 00133 while(!stopThread){ 00134 synchronized(this){ 00135 00136 if(newData){ 00137 00138 for(int i=0; i<toSendData.length; i++) 00139 output.write(toSendData[i]); 00140 newData=false; 00141 } 00142 00143 if(readRequest){ 00144 for(int i=0; i<readData.length; i++) 00145 readData[i]=input.read(); 00146 readRequest=false; 00147 newReceivedData=true; 00148 } 00149 } 00150 } 00151 00152 }catch (Exception ex) { 00153 System.err.println("Bluetooth Running Error: " + ex +" on "+role); 00154 } finally { 00155 try { 00156 00157 if (input != null) { 00158 input.close(); 00159 } 00160 if (output != null) { 00161 output.close(); 00162 } 00163 if (conn != null) { 00164 conn.close(); 00165 } 00166 00167 } catch (IOException ioe) { 00168 System.err.println( "Error Closing connection "+role+": IOException: " + ioe ); 00169 } 00170 } 00171 } 00172 00173 public void debug(String st) { 00174 System.out.println ("DEBUG TicTacCom : "+st) ; 00175 } 00176 00177 00178 00179 00180 00181 00182 00183 00184 }