/Users/detro/workspace-nb/JAEBI-BlueToothJ2MEClient/src/ServerMidlet.java

Vai alla documentazione di questo file.
00001 /*
00002  * ServerMidlet.java
00003  *
00004  * Created on 28 maggio 2005, 18.45
00005  */
00006 
00007 import java.io.IOException;
00008 import java.rmi.RemoteException;
00009 import java.util.Vector;
00010 import javax.bluetooth.RemoteDevice;
00011 import javax.bluetooth.UUID;
00012 import javax.microedition.io.StreamConnection;
00013 import javax.microedition.midlet.*;
00014 import org.jaebi.midlet.bt.btException.BtInitException;
00015 import org.jaebi.midlet.bt.*;
00016 
00017 
00022 public class ServerMidlet extends MIDlet implements javax.microedition.lcdui.CommandListener, ConnectionEventHandler {
00023     
00024     private StringBuffer url;
00025     private BtServer btServer;
00026     
00027     private String serviceURL = "http://192.168.1.11:8080/JAEBI/JAEBIWS";
00028 
00029     
00030     //definisco una costante che memorizza l'UUID (puramente esemplificativo) della device bluetooth
00031     private static final UUID JAEBI_MIDLET_UUID = new UUID(0x12345);
00032     
00033     /*
00034      * vettore di RemoteDevice. Contiene gli handle alle device connesse a
00035      * questo server, indicizzate nello stesso ordine in cui sono
00036      * rappresentate nel componente List connectdeDevice
00037      */
00038     private Vector connected;
00039     
00040     /*
00041      * Vector di StreamConnections. Contiene gli handle alle connessioni attive
00042      * su questo server. Una connessione x presente in questo container avrà
00043      * indice uguale all'indice con cui la relativa remotedevice è memorizzata
00044      * in connected
00045      */
00046     private Vector connections;
00047     
00056     private Vector connectionProcessors;
00057     
00059     public ServerMidlet() {
00060         
00061             /*
00062              *******************************************************************
00063              * costruisco la stringa contenete l'url del server e che verrà
00064              * passata al costruttore di BtServer
00065              *******************************************************************
00066              */
00067         
00068         //usiamo il profilo serial Port Profile per le connessioni
00069         url = new StringBuffer("btspp://");
00070         
00071         // indico nell'url che si tratta di un server
00072         url.append("localhost").append(':');
00073         
00074         // aggiungo l'UUID per identificare il servizio
00075         url.append(JAEBI_MIDLET_UUID.toString());
00076         
00077         // Aggiungo il nome per il nostro servizio
00078         url.append(";name=Midlet_Server");
00079         
00080         // richiedo che i client siano autorizzati
00081         //TODO in che senso autorizzati!?!?
00082         url.append(";authorize=true");
00083         
00084         //TODO debug
00085         System.out.println("UUID=" + JAEBI_MIDLET_UUID.toString());
00086         
00087         this.connected = new Vector();
00088         this.connections = new Vector();
00089         this.connectionProcessors = new Vector();
00090         
00091     }
00092     
00093 // --- This section is auto-generated by NetBeans IDE. Do not modify or you may lose your changes.//<editor-fold id="MVDMethods" defaultstate="collapsed" desc="This section is auto-generated by NetBeans IDE.">//GEN-BEGIN:MVDMethods
00097     private void initialize() {
00098 // For adding user code into this block, select "Design" item in the inspector and invoke property editor on Action property in Properties window.
00099         javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_alertSplashScreen());
00100     }
00101     
00107     public void commandAction(javax.microedition.lcdui.Command command, javax.microedition.lcdui.Displayable displayable) {
00108         if (displayable == connectedDevice) {
00109             if (command == info) {
00110 // For adding user code into this block, select "Design | Screens | connectedDevice [List] | Assigned Commands | info" item in the inspector and invoke property editor on Action property in Properties window.
00111                 javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_deviceInfo(), get_connectedDevice());
00112             }
00113         } else if (displayable == initAlert) {
00114             if (command == exitCommand1) {
00115 // For adding user code into this block, select "Design | Screens | initAlert [Alert] | Assigned Commands | exitCommand1" item in the inspector and invoke property editor on Action property in Properties window.
00116                 javax.microedition.lcdui.Display.getDisplay(this).setCurrent(null);
00117                 destroyApp(true);
00118                 notifyDestroyed();
00119             }
00120         }
00121     }
00122     
00127     private javax.microedition.lcdui.Alert get_initAlert() {
00128         if (initAlert == null) {
00129             initAlert = new javax.microedition.lcdui.Alert(null, "Si \u00E8 verificato un errore durante l\'inizializzazione della periferica bluetooth", null, javax.microedition.lcdui.AlertType.INFO);
00130             initAlert.addCommand(get_exitCommand1());
00131             initAlert.setCommandListener(this);
00132             initAlert.setTimeout(-2);
00133         }
00134         return initAlert;
00135     }
00136     
00141     private javax.microedition.lcdui.Command get_exitCommand1() {
00142         if (exitCommand1 == null) {
00143             exitCommand1 = new javax.microedition.lcdui.Command("Exit", javax.microedition.lcdui.Command.EXIT, 1);
00144         }
00145         return exitCommand1;
00146     }
00147     
00152     private javax.microedition.lcdui.Ticker get_ticker1() {
00153         if (ticker1 == null) {
00154             ticker1 = new javax.microedition.lcdui.Ticker("In attesa di connesioni da parte di altre device");
00155         }
00156         return ticker1;
00157     }
00158     
00163     private javax.microedition.lcdui.Alert get_alertSplashScreen() {
00164         if (alertSplashScreen == null) {
00165             alertSplashScreen = new javax.microedition.lcdui.Alert(null, "", get_logo(), javax.microedition.lcdui.AlertType.INFO);
00166             alertSplashScreen.setTimeout(3000);
00167             try{
00168                 btServer = new BtServer(this.url, this);
00169             }catch(BtInitException e){
00170                 javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_initAlert());
00171                 e.printStackTrace();
00172             }
00173             javax.microedition.lcdui.Display.getDisplay(this).setCurrent(get_connectedDevice());
00174         }
00175         return alertSplashScreen;
00176     }
00177     
00182     private javax.microedition.lcdui.Image get_logo() {
00183         if (logo == null) {
00184             try {
00185                 logo = javax.microedition.lcdui.Image.createImage("/images/jaebi LogoFinal.jpg");
00186             } catch (java.io.IOException exception) {
00187             }
00188         }
00189         return logo;
00190     }
00191     
00196     private javax.microedition.lcdui.List get_connectedDevice() {
00197         if (connectedDevice == null) {
00198             connectedDevice = new javax.microedition.lcdui.List(null, javax.microedition.lcdui.Choice.IMPLICIT, new java.lang.String[0], new javax.microedition.lcdui.Image[0]);
00199             connectedDevice.addCommand(get_Disconnect());
00200             connectedDevice.addCommand(get_info());
00201             connectedDevice.setCommandListener(this);
00202             connectedDevice.setTicker(get_tickerConnectedDevice());
00203             connectedDevice.setSelectedFlags(new boolean[0]);
00204             connectedDevice.setSelectCommand(get_Disconnect());
00205         }
00206         return connectedDevice;
00207     }
00208     
00213     private javax.microedition.lcdui.Command get_Disconnect() {
00214         if (Disconnect == null) {
00215             Disconnect = new javax.microedition.lcdui.Command("Disconnect", javax.microedition.lcdui.Command.ITEM, 1);
00216         }
00217         return Disconnect;
00218     }
00219     
00224     private javax.microedition.lcdui.Command get_info() {
00225         if (info == null) {
00226             info = new javax.microedition.lcdui.Command("Info", javax.microedition.lcdui.Command.SCREEN, 1);
00227         }
00228         return info;
00229     }
00230     
00235     private javax.microedition.lcdui.Alert get_deviceInfo() {
00236         if (deviceInfo == null) {
00237             deviceInfo = new javax.microedition.lcdui.Alert(null, "<Enter Text>", null, javax.microedition.lcdui.AlertType.INFO);
00238             deviceInfo.setTimeout(-2);
00239         }
00240         return deviceInfo;
00241     }
00242     
00247     private javax.microedition.lcdui.Ticker get_tickerConnectedDevice() {
00248         if (tickerConnectedDevice == null) {
00249             tickerConnectedDevice = new javax.microedition.lcdui.Ticker("Al momento ci sono 0 connessioni attive");
00250         }
00251         return tickerConnectedDevice;
00252     }
00253     
00254     javax.microedition.lcdui.Alert initAlert;
00255     javax.microedition.lcdui.Command exitCommand1;
00256     javax.microedition.lcdui.Ticker ticker1;
00257     javax.microedition.lcdui.Alert alertSplashScreen;
00258     javax.microedition.lcdui.Image logo;
00259     javax.microedition.lcdui.List connectedDevice;
00260     javax.microedition.lcdui.Command Disconnect;
00261     javax.microedition.lcdui.Command info;
00262     javax.microedition.lcdui.Alert deviceInfo;
00263     javax.microedition.lcdui.Ticker tickerConnectedDevice;
00264 // --- This is the end of auto-generated section.//</editor-fold>//GEN-END:MVDMethods
00265     
00266     public void startApp() {
00267         initialize();
00268     }
00269     
00270     public void pauseApp() {
00271     }
00272     
00273     public void destroyApp(boolean unconditional) {
00274         //TODO in questo metodo devono essere distrutte tutte le connessioni del listner
00275     }
00276     
00277     
00278     public void connectionAdded(StreamConnection conn){
00282         String nameOrAddress;
00283         RemoteDevice rd = null;
00284         ConnectionProcessor cp = null;
00285         
00286         try{
00287             rd = RemoteDevice.getRemoteDevice(conn);
00288         } catch (IOException  ex){
00289             
00290             /*
00291              * qualcosa non va: non riesco a recuperare l'handle alla
00292              * RemoteDevice cui si riferisce la connessione. Rimuovo il
00293              * riferimento alla connessione in bTServer e non faccio nient'altro
00294              */
00295             btServer.removeConnection(conn);
00296             return;
00297         }
00298         
00299         try{
00300             nameOrAddress = rd.getFriendlyName(true);
00301         } catch(IOException e){
00302             nameOrAddress = rd.getBluetoothAddress();
00303         }
00304         
00305         /*
00306          * Aggiorno il componente List connectedDevice
00307          */
00308         get_connectedDevice().append(nameOrAddress, null);
00309         
00310         /*
00311          * aggiungo in connected l'handle alla remotedevice connessa
00312          */
00313         connected.addElement(rd);
00314         
00315         /*
00316          * aggiungo l'handle alla connessione a connections
00317          */
00318         connections.addElement(conn);
00319         
00320         /*
00321          * faccio partire il thread di gestione della connessione
00322          */
00323        cp = new ConnectionProcessor(conn, this.btServer, this.serviceURL);
00324         
00325         /*
00326          * aggiungo l'handle all'istanza di ConnectionProcessor a connectionProcessors
00327          */
00328         connectionProcessors.addElement(cp);
00329         
00330         get_tickerConnectedDevice().setString("Al momento ci sono " + connections.size() + "connessioni attive");
00331     }
00332     
00333     
00334     public void connectionRemoved(StreamConnection conn){
00335         
00336         //recupero l'indice della connessione in connections
00337         int index = connections.indexOf(conn);
00338         
00339         /*
00340          * Verifico se è necessario aggiornare conneced, connections e
00341          * connectionProcessors (potrebbe non esserlo a causa del primo
00342          * try..catch all'interno del metodo connectionAdded()
00343          */
00344         if (index != -1){
00345             
00346             //rimuovo la connessione da connections
00347             connections.removeElementAt(index);
00348             
00349             /* rimuovo da connectionProcessors l'istanza di ConnecionProcessor che
00350              * gestiva la connessione rimossa
00351              */
00352             connectionProcessors.removeElementAt(index);
00353             
00354             /*
00355              * rimuovo da connected l'handle alla RemoteDevice relativa alla
00356              * connessione rimossa
00357              */
00358             connected.removeElementAt(index);
00359         }
00360         
00361         get_tickerConnectedDevice().setString("Al momento ci sono " + connections.size() + "connessioni attive");
00362     }
00363     
00364 }

Generato il Thu Jun 23 00:03:00 2005 per JAEBI - BlueTooth J2ME Midlet Client da  doxygen 1.4.3