00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 package org.jaebi.midlet.bt;
00012
00013 import java.io.IOException;
00014 import java.util.Enumeration;
00015 import java.util.Vector;
00016 import javax.bluetooth.RemoteDevice;
00017 import javax.microedition.io.StreamConnection;
00018 import org.jaebi.midlet.bt.btException.BtIOException;
00019 import org.jaebi.midlet.util.Observable;
00020 import org.jaebi.midlet.util.Observer;
00021
00035 public class ConnectionSet extends Observable{
00036
00037 private Vector connections;
00038 private int event;
00039
00041 public ConnectionSet(ConnectionObserver observer) {
00042 connections = new Vector();
00043 this.addObserver((Observer)observer);
00044 }
00045
00046
00047 public synchronized void addConnection(StreamConnection conn){
00048 ConnectionEvent event = new ConnectionEvent(ConnectionEvent.CONNECTION_ADDED, conn);
00049
00050 connections.addElement(conn);
00051 this.setChanged();
00052 notifyObservers(event);
00053 }
00054
00055
00056 public synchronized void removeConnection(StreamConnection conn){
00057 ConnectionEvent event = new ConnectionEvent(ConnectionEvent.CONNECTION_REMOVED, conn);
00058
00059 try{
00060 conn.close();
00061 } catch (IOException e){
00062 System.err.println("Errore durante la chiusura della connessione");
00063 }
00064
00065 connections.removeElement(conn);
00066 this.setChanged();
00067 notifyObservers(event);
00068
00069 }
00070
00071 public synchronized StreamConnection getConnection(int index){
00072 return (StreamConnection)connections.elementAt(index);
00073 }
00074
00078 public synchronized StreamConnection[] getConnection(RemoteDevice rd){
00079 Vector outTemp = new Vector();
00080 StreamConnection out[];
00081 Enumeration en = connections.elements();
00082
00083 while (en.hasMoreElements()){
00084 StreamConnection conn = (StreamConnection)en.nextElement();
00085 RemoteDevice temp = null;
00086
00087 try{
00088 temp = RemoteDevice.getRemoteDevice(conn);
00089 if (temp.equals(conn))
00090 outTemp.addElement(conn);
00091 }
00092 catch(IOException e){
00093 ;
00094
00095
00096
00097
00098 }
00099
00100 }
00101
00102 out = new StreamConnection[outTemp.size()];
00103
00104 for (int i=0; i<outTemp.size(); i++){
00105 out[i] = (StreamConnection)outTemp.elementAt(i);
00106 }
00107
00108 outTemp.removeAllElements();
00109 return out;
00110 }
00111
00112
00116 public synchronized int connectionsCount(){
00117 return connections.size();
00118 }
00119
00123 public synchronized void closeAll() throws BtIOException{
00124 Enumeration en;
00125 en = connections.elements();
00126 while(en.hasMoreElements()){
00127 try{
00128 ((StreamConnection)en.nextElement()).close();
00129 } catch(IOException e){
00130 throw new BtIOException();
00131 }
00132 }
00133 connections.removeAllElements();
00134 }
00135
00136
00137 }