Mercurial > hg > blitz_condensed
comparison src/org/dancres/blitz/remote/nio/CommandFactory.java @ 0:3dc0c5604566
Initial checkin of blitz 2.0 fcs - no installer yet.
author | Dan Creswell <dan.creswell@gmail.com> |
---|---|
date | Sat, 21 Mar 2009 11:00:06 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3dc0c5604566 |
---|---|
1 package org.dancres.blitz.remote.nio; | |
2 | |
3 import org.dancres.blitz.mangler.MangledEntry; | |
4 import org.dancres.io.AnnotatingObjectOutputStream; | |
5 import org.dancres.io.AnnotatingObjectInputStream; | |
6 import net.jini.core.transaction.Transaction; | |
7 import net.jini.core.transaction.server.TransactionManager; | |
8 | |
9 import java.io.*; | |
10 | |
11 /** | |
12 * Yields a space operation that can be rendered into a byte array for | |
13 * passing across to the server over a socket. Also performs the conversion | |
14 * in the other direction | |
15 */ | |
16 public class CommandFactory { | |
17 public Operation newWrite(MangledEntry anEntry, Transaction aTxn, | |
18 long aLease) { | |
19 return new GenericSpaceOp(GenericSpaceOp.WRITE, anEntry, aTxn, aLease); | |
20 } | |
21 | |
22 public Operation newRead(MangledEntry anEntry, Transaction aTxn, | |
23 long aWaitTime) { | |
24 return new GenericSpaceOp(GenericSpaceOp.READ, anEntry, aTxn, aWaitTime); | |
25 } | |
26 | |
27 public Operation newTake(MangledEntry anEntry, Transaction aTxn, | |
28 long aWaitTime) { | |
29 return new GenericSpaceOp(GenericSpaceOp.TAKE, anEntry, aTxn, aWaitTime); | |
30 } | |
31 | |
32 public Operation newReadExists(MangledEntry anEntry, Transaction aTxn, | |
33 long aWaitTime) { | |
34 return new GenericSpaceOp(GenericSpaceOp.READ_EXISTS, anEntry, aTxn, aWaitTime); | |
35 } | |
36 | |
37 public Operation newTakeExists(MangledEntry anEntry, Transaction aTxn, | |
38 long aWaitTime) { | |
39 return new GenericSpaceOp(GenericSpaceOp.TAKE_EXISTS, anEntry, aTxn, aWaitTime); | |
40 } | |
41 | |
42 public Operation newPrepare(TransactionManager aMgr, long anId) { | |
43 return new TransactionOp(TransactionOp.PREPARE, aMgr, anId); | |
44 } | |
45 | |
46 public Operation newAbort(TransactionManager aMgr, long anId) { | |
47 return new TransactionOp(TransactionOp.ABORT, aMgr, anId); | |
48 } | |
49 | |
50 public Operation newCommit(TransactionManager aMgr, long anId) { | |
51 return new TransactionOp(TransactionOp.COMMIT, aMgr, anId); | |
52 } | |
53 | |
54 public Operation newPrepareCommit(TransactionManager aMgr, long anId) { | |
55 return new TransactionOp(TransactionOp.PREPARE_COMMIT, aMgr, anId); | |
56 } | |
57 | |
58 public byte[] pack(Operation anOp) throws IOException { | |
59 ByteArrayOutputStream myBAOS = new ByteArrayOutputStream(); | |
60 | |
61 if (anOp instanceof GenericSpaceOp) { | |
62 GenericSpaceOp myOp = (GenericSpaceOp) anOp; | |
63 | |
64 myBAOS.write(myOp.getOperation()); | |
65 | |
66 AnnotatingObjectOutputStream myOOS = | |
67 new AnnotatingObjectOutputStream(myBAOS, myBAOS); | |
68 | |
69 myOOS.writeObject(myOp.getEntry()); | |
70 myOOS.writeObject(myOp.getTxn()); | |
71 myOOS.writeLong(myOp.getLease()); | |
72 | |
73 myOOS.close(); | |
74 } else if (anOp instanceof TransactionOp) { | |
75 TransactionOp myOp = (TransactionOp) anOp; | |
76 | |
77 myBAOS.write(myOp.getOperation()); | |
78 | |
79 AnnotatingObjectOutputStream myOOS = | |
80 new AnnotatingObjectOutputStream(myBAOS, myBAOS); | |
81 | |
82 myOOS.writeObject(myOp.getMgr()); | |
83 myOOS.writeLong(myOp.getId()); | |
84 | |
85 myOOS.close(); | |
86 } else | |
87 throw new IOException("Don't know how to marshal: " + | |
88 anOp.getClass()); | |
89 | |
90 byte[] myResult = myBAOS.toByteArray(); | |
91 | |
92 // System.err.println("Pack hash: " + hash(myResult)); | |
93 | |
94 return myResult; | |
95 } | |
96 | |
97 private int hash(byte[] anArray) { | |
98 int myHash = 0; | |
99 | |
100 for (int i = 0; i < anArray.length; i++) { | |
101 myHash += anArray[i]; | |
102 myHash += (myHash << 10); | |
103 myHash ^= (myHash >> 6); | |
104 } | |
105 | |
106 myHash += (myHash << 3); | |
107 myHash ^= (myHash >> 11); | |
108 myHash += (myHash << 15); | |
109 | |
110 return myHash; | |
111 } | |
112 | |
113 public Operation unpack(byte[] aFlattenedOp) throws IOException { | |
114 | |
115 // System.err.println("Unpack hash: " + hash(aFlattenedOp)); | |
116 | |
117 ByteArrayInputStream myBAIS = new ByteArrayInputStream(aFlattenedOp); | |
118 | |
119 int myOpCode = myBAIS.read(); | |
120 | |
121 switch (myOpCode) { | |
122 case GenericSpaceOp.WRITE : | |
123 case GenericSpaceOp.TAKE_EXISTS : | |
124 case GenericSpaceOp.READ_EXISTS : | |
125 case GenericSpaceOp.TAKE : | |
126 case GenericSpaceOp.READ : { | |
127 | |
128 try { | |
129 AnnotatingObjectInputStream myOIS = | |
130 new AnnotatingObjectInputStream(null, myBAIS, | |
131 myBAIS, false); | |
132 | |
133 MangledEntry myEntry = (MangledEntry) myOIS.readObject(); | |
134 Transaction myTxn = (Transaction) myOIS.readObject(); | |
135 long myLease = myOIS.readLong(); | |
136 | |
137 myOIS.close(); | |
138 | |
139 return new GenericSpaceOp(myOpCode, myEntry, | |
140 myTxn, myLease); | |
141 | |
142 } catch (ClassNotFoundException aCNFE) { | |
143 IOException myIOE = | |
144 new IOException("Failed to unpack operation"); | |
145 | |
146 myIOE.initCause(aCNFE); | |
147 throw myIOE; | |
148 } | |
149 } | |
150 | |
151 case TransactionOp.ABORT : | |
152 case TransactionOp.COMMIT : | |
153 case TransactionOp.PREPARE : | |
154 case TransactionOp.PREPARE_COMMIT : { | |
155 | |
156 try { | |
157 AnnotatingObjectInputStream myOIS = | |
158 new AnnotatingObjectInputStream(null, myBAIS, | |
159 myBAIS, false); | |
160 | |
161 TransactionManager myMgr = | |
162 (TransactionManager) myOIS.readObject(); | |
163 long myId = myOIS.readLong(); | |
164 | |
165 myOIS.close(); | |
166 | |
167 return new TransactionOp(myOpCode, myMgr, myId); | |
168 | |
169 } catch (ClassNotFoundException aCNFE) { | |
170 IOException myIOE = | |
171 new IOException("Failed to unpack operation"); | |
172 | |
173 myIOE.initCause(aCNFE); | |
174 throw myIOE; | |
175 } | |
176 } | |
177 | |
178 default : throw new IOException("Bad Op: " + myOpCode); | |
179 } | |
180 } | |
181 } |