001 /*
002 * $Id: DHTTransport.java 3210 2010-07-05 12:25:27Z kredel $
003 */
004
005 package edu.jas.util;
006
007
008 import java.io.Serializable;
009 import java.io.IOException;
010 import 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
020 public abstract class DHTTransport<K, V> implements Serializable {
021
022
023 public static long etime = 0L; // encode marshalled
024 public static long dtime = 0L; // decode marshalled
025 public static long ertime = 0L; // encode plain raw
026 public 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 /**
084 * Transport container for a distributed version of a HashTable. Immutable
085 * objects. Uses MarshalledObject to avoid deserialization on server side.
086 * @author Heinz Kredel
087 */
088
089 class DHTTransportMarshal<K, V> extends DHTTransport<K, V> {
090
091
092 protected final MarshalledObject/*<K>*/ key;
093
094
095 protected final MarshalledObject/*<V>*/ value;
096
097
098 /**
099 * Constructs a new DHTTransport Container.
100 * @param key
101 * @param value
102 */
103 public DHTTransportMarshal(K key, V value) throws IOException {
104 long t = System.currentTimeMillis();
105 this.key = new MarshalledObject/*<K>*/(key);
106 this.value = new MarshalledObject/*<V>*/(value);
107 t = System.currentTimeMillis() - t;
108 synchronized( DHTTransport.class ) {
109 etime += t;
110 }
111 //System.out.println(" marshal time = " + t);
112 }
113
114
115 /**
116 * Get the key from this DHTTransport Container.
117 */
118 public K key() throws IOException, ClassNotFoundException {
119 long t = System.currentTimeMillis();
120 K k = (K) this.key.get();
121 t = System.currentTimeMillis() - t;
122 synchronized( DHTTransport.class ) {
123 dtime += t;
124 }
125 return k;
126 }
127
128
129 /**
130 * Get the value from this DHTTransport Container.
131 */
132 public V value() throws IOException, ClassNotFoundException {
133 long t = System.currentTimeMillis();
134 V v = (V) this.value.get();
135 t = System.currentTimeMillis() - t;
136 synchronized( DHTTransport.class ) {
137 dtime += t;
138 }
139 return v;
140 }
141
142
143 /**
144 * toString.
145 */
146 @Override
147 public String toString() {
148 return super.toString() + "(" + key + "," + value + ")";
149 }
150
151
152 private void writeObject(java.io.ObjectOutputStream out) throws IOException {
153 long t = System.currentTimeMillis();
154 out.defaultWriteObject();
155 t = System.currentTimeMillis() - t;
156 synchronized( DHTTransport.class ) {
157 ertime += t;
158 }
159 }
160
161 private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
162 long t = System.currentTimeMillis();
163 in.defaultReadObject();
164 t = System.currentTimeMillis() - t; // not meaningful, includes waiting time
165 synchronized( DHTTransport.class ) {
166 drtime += t;
167 }
168 }
169
170 }
171
172
173 /**
174 * Transport container for a distributed version of a HashTable. Immutable
175 * objects. Uses plain objects.
176 * @author Heinz Kredel
177 */
178
179 class DHTTransportPlain<K, V> extends DHTTransport<K, V> {
180
181
182 protected final K key;
183
184
185 protected final V value;
186
187
188 /**
189 * Constructs a new DHTTransport Container.
190 * @param key
191 * @param value
192 */
193 public DHTTransportPlain(K key, V value) throws IOException {
194 this.key = key;
195 this.value = value;
196 }
197
198
199 /**
200 * Get the key from this DHTTransport Container.
201 */
202 public K key() throws IOException, ClassNotFoundException {
203 return this.key;
204 }
205
206
207 /**
208 * Get the value from this DHTTransport Container.
209 */
210 public V value() throws IOException, ClassNotFoundException {
211 return this.value;
212 }
213
214
215 /**
216 * toString.
217 */
218 @Override
219 public String toString() {
220 return super.toString() + "(" + key + "," + value + ")";
221 }
222
223
224 private void writeObject(java.io.ObjectOutputStream out) throws IOException {
225 long t = System.currentTimeMillis();
226 out.defaultWriteObject();
227 t = System.currentTimeMillis() - t;
228 synchronized( DHTTransport.class ) {
229 ertime += t;
230 }
231 }
232
233 private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
234 long t = System.currentTimeMillis();
235 in.defaultReadObject();
236 t = System.currentTimeMillis() - t;
237 synchronized( DHTTransport.class ) {
238 drtime += t;
239 }
240 }
241
242 }