Go to the documentation of this file.00001
00002
00003
00004
00005 package com.phidgets;
00006
00007 import com.phidgets.event.*;
00008
00009 import java.util.Iterator;
00010 import java.util.LinkedList;
00011 import java.util.Vector;
00012
00031 public class Dictionary
00032 {
00033 static
00034 {
00035 try
00036 {
00037 System.loadLibrary("phidget21");
00038 }
00039 catch(UnsatisfiedLinkError ex)
00040 {
00041 String os = System.getProperty("os.name");
00042 if(os.startsWith("Linux"))
00043 {
00044 throw new ExceptionInInitializerError(ex.getMessage()
00045 +"\nCould not locate the Phidget C library (libphidget21.so)."
00046 +"\nMake sure it is installed, and add it's path to LD_LIBRARY_PATH.");
00047 }
00048 else if(os.startsWith("Windows"))
00049 {
00050 throw new ExceptionInInitializerError(ex.getMessage()
00051 +"\nCould not locate the Phidget C library."
00052 +"\nThe Windows Phidget21 MSI must be installed.");
00053 }
00054 else if(os.startsWith("Mac"))
00055 {
00056 throw new ExceptionInInitializerError(ex.getMessage()
00057 +"\nCould not locate the Phidget C library."
00058 +"\nThe Mac Phidget21 DMG must be installed.");
00059 }
00060 else
00061 {
00062 throw new ExceptionInInitializerError(ex.getMessage()
00063 +"\nCould not locate the Phidget C library.");
00064 }
00065 }
00066 }
00067
00068 public long handle = 0;
00074 public Dictionary() throws PhidgetException
00075 {
00076 handle = create();
00077 }
00078 private final native long create() throws PhidgetException;
00079
00080 private final native void nativeClose() throws PhidgetException;
00081 private final native void nativeDelete() throws PhidgetException;
00082 private final native void nativeOpenRemote(String serverID, String pass) throws PhidgetException;
00083 private final native void nativeOpenRemoteIP(String ipAddress, int port, String pass) throws PhidgetException;
00084 private final native void nativeAddKey(String key, String val, int persistent) throws PhidgetException;
00085 private final native void nativeRemoveKey(String keyPattern) throws PhidgetException;
00086 private final native String nativeGetKey(String key) throws PhidgetException;
00087
00088
00095 public final native String getServerAddress() throws PhidgetException;
00103 public final native String getServerID() throws PhidgetException;
00110 public final native int getServerPort() throws PhidgetException;
00117 public final native boolean isAttached() throws PhidgetException;
00118
00127 public final native boolean isAttachedToServer() throws PhidgetException;
00128
00140 public final void add(String key, String val) throws PhidgetException
00141 {
00142 nativeAddKey(key, val, 1);
00143 }
00144
00157 public final void add(String key, String val, boolean persistent) throws PhidgetException
00158 {
00159 if (persistent) nativeAddKey(key, val, 1);
00160 else nativeAddKey(key, val, 0);
00161 }
00167 public final void remove(String pattern) throws PhidgetException
00168 {
00169 nativeRemoveKey(pattern);
00170 }
00171
00175 public final String get(String key) throws PhidgetException
00176 {
00177 return nativeGetKey(key);
00178 }
00179
00186 public final void open(String ipAddress, int port, String password) throws PhidgetException
00187 {
00188 enableEvents(true);
00189 nativeOpenRemoteIP(ipAddress, port, password);
00190 }
00201 public final void open(String ipAddress, int port) throws PhidgetException
00202 {
00203 enableEvents(true);
00204 nativeOpenRemoteIP(ipAddress, port, "");
00205 }
00212 public final void open(String serverID, String password) throws PhidgetException
00213 {
00214 enableEvents(true);
00215 nativeOpenRemote(serverID, password);
00216 }
00224 public final void open(String serverID) throws PhidgetException
00225 {
00226 enableEvents(true);
00227 nativeOpenRemote(serverID, "");
00228 }
00235 public final void close() throws PhidgetException
00236 {
00237 enableEvents(false);
00238 nativeClose();
00239 }
00240
00241 private void enableEvents(boolean b)
00242 {
00243 enableServerConnectEvents(b && serverConnectListeners.size() > 0);
00244 enableServerDisconnectEvents(b && serverDisconnectListeners.size() > 0);
00245 }
00246
00247 private LinkedList serverConnectListeners = new LinkedList();
00248 private long nativeServerConnectHandler = 0;
00249
00257 public final void addServerConnectListener(ServerConnectListener l)
00258 {
00259 synchronized (serverConnectListeners)
00260 {
00261 serverConnectListeners.add(l);
00262 enableServerConnectEvents(true);
00263 }
00264 }
00265
00269 public final void removeServerConnectListener(ServerConnectListener l)
00270 {
00271 synchronized (serverConnectListeners)
00272 {
00273 serverConnectListeners.remove(l);
00274 enableServerConnectEvents(serverConnectListeners.size() > 0);
00275 }
00276 }
00277 private void fireServerConnect(ServerConnectEvent e)
00278 {
00279 synchronized (serverConnectListeners)
00280 {
00281 for (Iterator it = serverConnectListeners.iterator();
00282 it.hasNext(); )
00283 ((ServerConnectListener)it.next()).serverConnected(e);
00284 }
00285 }
00286 private native void enableServerConnectEvents(boolean b);
00287
00288
00289 private LinkedList serverDisconnectListeners = new LinkedList();
00290 private long nativeServerDisconnectHandler = 0;
00291
00299 public final void addServerDisconnectListener(ServerDisconnectListener l)
00300 {
00301 synchronized (serverDisconnectListeners)
00302 {
00303 serverDisconnectListeners.add(l);
00304 enableServerDisconnectEvents(true);
00305 }
00306 }
00307
00311 public final void removeServerDisconnectListener(ServerDisconnectListener l)
00312 {
00313 synchronized (serverDisconnectListeners)
00314 {
00315 serverDisconnectListeners.remove(l);
00316 enableServerDisconnectEvents(serverDisconnectListeners.size() > 0);
00317 }
00318 }
00319 private void fireServerDisconnect(ServerDisconnectEvent e)
00320 {
00321 synchronized (serverDisconnectListeners)
00322 {
00323 for (Iterator it = serverDisconnectListeners.iterator();
00324 it.hasNext(); )
00325 ((ServerDisconnectListener)it.next()).serverDisconnected(e);
00326 }
00327 }
00328 private native void enableServerDisconnectEvents(boolean b);
00329
00333 public String toString()
00334 {
00335 return "PhidgetDictionary: ";
00336 }
00337
00338 protected void finalize() {
00339 try
00340 {
00341 close();
00342 nativeDelete();
00343 handle = 0;
00344 } catch (Exception e) {
00345 ;
00346 }
00347 }
00348 }