001/*
002 * $Id: DHTTransport.java 4259 2012-10-21 10:20:37Z kredel $
003 */
004
005package edu.jas.util;
006
007
008import java.io.Serializable;
009import java.io.IOException;
010import java.rmi.MarshalledObject;
011
012
013/**
014 * Transport container for a distributed version of a HashTable. 
015 * <b>Note:</b> Contains code for timing of marshalled versus plain 
016 * object serialization which can be removed later.
017 * @author Heinz Kredel
018 */
019
020public abstract class DHTTransport<K, V> implements Serializable {
021
022
023    static long etime  = 0L; // encode marshalled
024    static long dtime  = 0L; // decode marshalled
025    static long ertime = 0L; // encode plain raw
026    static long drtime = 0L; // decode plain raw
027
028
029    public static enum Stor { // storage and transport class
030        marshal, plain
031    };
032
033
034    public static final Stor stor = Stor.marshal; //Stor.plain; 
035
036
037    /**
038     * protected constructor.
039     */
040    protected DHTTransport() {
041    }
042
043
044    /**
045     * Create a new DHTTransport Container.
046     * @param key
047     * @param value
048     */
049    public static <K,V> DHTTransport<K,V> create(K key, V value) throws IOException {
050        switch (stor) {
051        case marshal: return new DHTTransportMarshal<K,V>(key,value);
052        case plain:   return new DHTTransportPlain<K,V>(key,value);
053        default: throw new IllegalArgumentException("this should not happen");
054        }
055    }
056
057
058    /**
059     * Get the key from this DHTTransport Container.
060     */
061    public abstract K key() throws IOException, ClassNotFoundException;
062
063
064    /**
065     * Get the value from this DHTTransport Container.
066     */
067    public abstract V value() throws IOException, ClassNotFoundException;
068
069
070    /**
071     * toString.
072     */
073    @Override
074    public String toString() {
075        return this.getClass().getName();
076
077    }
078
079}
080
081
082/**
083 * Transport container to signal termination for a distributed version
084 * of a HashTable. Contains no objects.
085 * @author Heinz Kredel
086 */
087
088class DHTTransportTerminate<K, V> extends DHTTransport<K, V> {
089
090    /**
091     * Get the key from this DHTTransport Container.
092     */
093    public K key() throws IOException, ClassNotFoundException {
094        throw new UnsupportedOperationException("this should not happen");
095    }
096
097
098    /**
099     * Get the value from this DHTTransport Container.
100     */
101    public V value() throws IOException, ClassNotFoundException {
102        throw new UnsupportedOperationException("this should not happen");
103    }
104
105}
106
107
108/**
109 * Transport container for a distributed version of a HashTable. Immutable
110 * objects. Uses MarshalledObject to avoid deserialization on server side.
111 * @author Heinz Kredel
112 */
113
114class DHTTransportMarshal<K, V> extends DHTTransport<K, V> {
115
116
117    protected final MarshalledObject/*<K>*/ key;
118
119
120    protected final MarshalledObject/*<V>*/ value;
121
122
123    /**
124     * Constructs a new DHTTransport Container.
125     * @param key
126     * @param value
127     */
128    public DHTTransportMarshal(K key, V value) throws IOException {
129        long t = System.currentTimeMillis();
130        this.key = new MarshalledObject/*<K>*/(key);
131        this.value = new MarshalledObject/*<V>*/(value);
132        t = System.currentTimeMillis() - t;
133        synchronized( DHTTransport.class ) {
134            etime += t;
135        }
136        //System.out.println("         marshal time = " + t);
137    }
138
139
140    /**
141     * Get the key from this DHTTransport Container.
142     */
143    public K key() throws IOException, ClassNotFoundException {
144        long t = System.currentTimeMillis();
145        K k = (K) this.key.get();
146        t = System.currentTimeMillis() - t;
147        synchronized( DHTTransport.class ) {
148            dtime += t;
149        }
150        return k;
151    }
152
153
154    /**
155     * Get the value from this DHTTransport Container.
156     */
157    public V value() throws IOException, ClassNotFoundException {
158        long t = System.currentTimeMillis();
159        V v = (V) this.value.get();
160        t = System.currentTimeMillis() - t;
161        synchronized( DHTTransport.class ) {
162            dtime += t;
163        }
164        return v;
165    }
166
167
168    /**
169     * toString.
170     */
171    @Override
172    public String toString() {
173        return super.toString() + "(" + key + "," + value + ")";
174    }
175
176
177    private void writeObject(java.io.ObjectOutputStream out) throws IOException {
178        long t = System.currentTimeMillis();
179        out.defaultWriteObject();
180        t = System.currentTimeMillis() - t;
181        synchronized( DHTTransport.class ) {
182            ertime += t;
183        }
184    }
185
186    private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
187        long t = System.currentTimeMillis();
188        in.defaultReadObject();
189        t = System.currentTimeMillis() - t; // not meaningful, includes waiting time
190        synchronized( DHTTransport.class ) {
191            drtime += t;
192        }
193    }
194
195}
196
197
198/**
199 * Transport container for a distributed version of a HashTable. Immutable
200 * objects. Uses plain objects.
201 * @author Heinz Kredel
202 */
203
204class DHTTransportPlain<K, V> extends DHTTransport<K, V> {
205
206
207    protected final K key;
208
209
210    protected final V value;
211
212
213    /**
214     * Constructs a new DHTTransport Container.
215     * @param key
216     * @param value
217     */
218    public DHTTransportPlain(K key, V value) throws IOException {
219        this.key = key;
220        this.value = value;
221    }
222
223
224    /**
225     * Get the key from this DHTTransport Container.
226     */
227    public K key() throws IOException, ClassNotFoundException {
228        return this.key;
229    }
230
231
232    /**
233     * Get the value from this DHTTransport Container.
234     */
235    public V value() throws IOException, ClassNotFoundException {
236        return this.value;
237    }
238
239
240    /**
241     * toString.
242     */
243    @Override
244    public String toString() {
245        return super.toString() + "(" + key + "," + value + ")";
246    }
247
248
249    private void writeObject(java.io.ObjectOutputStream out) throws IOException {
250        long t = System.currentTimeMillis();
251        out.defaultWriteObject();
252        t = System.currentTimeMillis() - t;
253        synchronized( DHTTransport.class ) {
254            ertime += t;
255        }
256    }
257
258    private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
259        long t = System.currentTimeMillis();
260        in.defaultReadObject();
261        t = System.currentTimeMillis() - t;
262        synchronized( DHTTransport.class ) {
263            drtime += t;
264        }
265    }
266
267}