/* Richard A. DeVenezia * SUGI 29 - "Greetings from the Edge" * www.devenezia.com/papers */ import java.rmi.*; import java.rmi.server.*; import java.util.*; import java.io.*; public class HashManager extends UnicastRemoteObject implements HashInterface { private Hashtable hashes; // handle --> Hash private int numberOfHandles; java.text.DecimalFormat nf = new java.text.DecimalFormat ("########"); //--------------------------------------------------------------------------- public HashManager (int numberOfHandlesToManage) throws RemoteException { this.numberOfHandles = numberOfHandlesToManage; this.hashes = new Hashtable (numberOfHandlesToManage); } //--------------------------------------------------------------------------- private Integer getNewHandle () { Integer handle; // has to be an object so that it can be used in a Hashtable do { handle = new Integer ( (int) Math.floor (Math.random() * 1e8)); } while (hashes.containsKey (handle)); return handle; } //--------------------------------------------------------------------------- private Object getObjectOfKey(Hashtable H, Object key) throws Exception { Object mv = H.get (key); if (mv == null) { Thread.currentThread().sleep(500); throw new Exception ("Invalid handle."); } return mv; } //--------------------------------------------------------------------------- private Hashtable getHashOfHandle(int handle) throws Exception { Integer key = new Integer(handle); return (Hashtable) getObjectOfKey (hashes, key); } //--------------------------------------------------------------------------- public int getHashHandle (int size, float load) throws RemoteException { Integer hhandle = getNewHandle(); Hashtable hash; if (hashes.size() == numberOfHandles) { System.out.println ("No more handles available."); throw new RemoteException ("No more handles available."); } try { hash = new Hashtable (size, load); System.out.println ("Hashtable allocated, handle="+hhandle); } catch (Exception e) { System.out.println (e); throw new RemoteException ("Problem allocating a Hashtable", e); } hashes.put (hhandle, hash); return hhandle.intValue(); } //--------------------------------------------------------------------------- public void freeHashHandle(int handle) throws RemoteException { try { getHashOfHandle(handle).clear(); hashes.remove (new Integer(handle)); } catch (Exception e) { throw new RemoteException("", e); } System.out.println ("Hashtable deallocated, handle was " + nf.format(handle)); } //--------------------------------------------------------------------------- public double getDouble(int handle, double key) throws RemoteException { try { Object value = getHashOfHandle(handle).get(new Double(key)); return (value instanceof Double) ? ((Double)value).doubleValue() : Double.NaN; } catch (Exception e) { throw new RemoteException("", e); } } //--------------------------------------------------------------------------- public double getDouble(int handle, String key) throws RemoteException { try { Object value = getHashOfHandle(handle).get(key); return (value instanceof Double) ? ((Double)value).doubleValue() : Double.NaN; } catch (Exception e) { throw new RemoteException("", e); } } //--------------------------------------------------------------------------- public String getString(int handle, double key) throws RemoteException { try { Object value = getHashOfHandle(handle).get(new Double(key)); return (value instanceof String) ? (String)value : ""; } catch (Exception e) { throw new RemoteException("", e); } } //--------------------------------------------------------------------------- public String getString(int handle, String key) throws RemoteException { try { Object value = getHashOfHandle(handle).get(key); return (value instanceof String) ? (String)value : ""; } catch (Exception e) { throw new RemoteException("", e); } } //--------------------------------------------------------------------------- public void put(int handle, double key, String value) throws RemoteException { try { getHashOfHandle(handle).put(new Double(key), value); } catch (Exception e) { throw new RemoteException("", e); } } //--------------------------------------------------------------------------- public void put(int handle, String key, String value) throws RemoteException { try { getHashOfHandle(handle).put(key, value); } catch (Exception e) { throw new RemoteException("", e); } } //--------------------------------------------------------------------------- public void put(int handle, double key, double value) throws RemoteException { try { getHashOfHandle(handle).put(new Double(key), new Double(value)); } catch (Exception e) { throw new RemoteException("", e); } } //--------------------------------------------------------------------------- public void put(int handle, String key, double value) throws RemoteException { try { getHashOfHandle(handle).put(key, new Double(value)); } catch (Exception e) { throw new RemoteException("", e); } } }