1 21 package com.db4o.cs.messages; 22 23 import java.io.*; 24 import com.db4o.*; 25 import com.db4o.cs.*; 26 import com.db4o.foundation.*; 27 import com.db4o.foundation.network.*; 28 29 32 public class Msg implements Cloneable { 33 34 static int _idGenerator = 1; 35 private static Msg[] _messages = new Msg[60]; 36 37 int _msgID; 38 String _name; 39 Transaction _trans; 40 41 public static final MsgD CLASS_NAME_FOR_ID = new MClassNameForID(); 42 public static final Msg CLOSE = new Msg("CLOSE"); 43 public static final Msg COMMIT = new MCommit(); 44 public static final Msg COMMIT_OK = new MCommitOK(); 45 public static final MsgD CREATE_CLASS = new MCreateClass(); 46 public static final MsgObject CLASS_META = new MClassMeta(); 47 public static final Msg CURRENT_VERSION = new Msg("VERSION"); 48 public static final MsgD DELETE = new MDelete(); 49 public static final Msg ERROR = new Msg("ERROR"); 50 public static final Msg FAILED = new Msg("FAILED"); 51 public static final MsgD GET_ALL = new MGetAll(); 52 public static final MsgD GET_CLASSES = new MGetClasses(); 53 public static final MsgD GET_INTERNAL_IDS = new MGetInternalIDs(); 54 public static final Msg GET_THREAD_ID = new Msg("GET_THREAD_ID"); 55 public static final MsgD ID_LIST = new MsgD("ID_LIST"); 56 public static final Msg IDENTITY = new Msg("IDENTITY"); 57 public static final MsgD LENGTH = new MsgD("LENGTH"); 58 public static final MsgD LOGIN = new MsgD("LOGIN"); 59 public static final MsgD LOGIN_OK = new MsgD("LOGIN_OK"); 60 public static final Msg NULL = new Msg("NULL"); 61 public static final MsgD OBJECT_BY_UUID = new MObjectByUuid(); 62 public static final MsgObject OBJECT_TO_CLIENT = new MsgObject(); 63 public static final MsgD OBJECTSET_FETCH = new MObjectSetFetch(); 64 public static final MsgD OBJECTSET_FINALIZED = new MsgD("OBJECTSET_FINALIZED"); 65 public static final MsgD OBJECTSET_GET_ID = new MObjectSetGetId(); 66 public static final MsgD OBJECTSET_INDEXOF = new MObjectSetIndexOf(); 67 public static final MsgD OBJECTSET_RESET = new MObjectSetReset(); 68 public static final MsgD OBJECTSET_SIZE = new MObjectSetSize(); 69 public static final Msg OK = new Msg("OK"); 70 public static final Msg PING = new Msg("PING"); 71 public static final MsgD PREFETCH_IDS = new MPrefetchIDs(); 72 public static final Msg PROCESS_DELETES = new MProcessDeletes(); 73 public static final MsgObject QUERY_EXECUTE = new MQueryExecute(); 74 public static final MsgD QUERY_RESULT = new MsgD("QUERY_RESULT"); 75 public static final MsgD RAISE_VERSION = new MsgD("RAISE_VERSION"); 76 public static final MsgBlob READ_BLOB = new MReadBlob(); 77 public static final MsgD READ_BYTES = new MReadBytes(); 78 public static final MsgD READ_MULTIPLE_OBJECTS = new MReadMultipleObjects(); 79 public static final MsgD READ_OBJECT = new MReadObject(); 80 public static final MsgD RELEASE_SEMAPHORE = new MReleaseSemaphore(); 81 public static final Msg ROLLBACK = new MRollback(); 82 public static final MsgD SET_SEMAPHORE = new MSetSemaphore(); 83 public static final Msg SUCCESS = new Msg("SUCCESS"); 84 public static final MsgD SWITCH_TO_FILE = new MsgD("SWITCH_F"); 85 public static final Msg SWITCH_TO_MAIN_FILE = new Msg("SWITCH_M"); 86 public static final MsgD TA_DELETE = new MTaDelete(); 87 public static final MsgD TA_IS_DELETED = new MTaIsDeleted(); 88 public static final MsgD USER_MESSAGE = new MUserMessage(); 89 public static final MsgD USE_TRANSACTION = new MUseTransaction(); 90 public static final MsgBlob WRITE_BLOB = new MWriteBlob(); 91 public static final MWriteNew WRITE_NEW = new MWriteNew(); 92 public static final MsgObject WRITE_UPDATE = new MWriteUpdate(); 93 public static final MsgD WRITE_UPDATE_DELETE_MEMBERS = new MWriteUpdateDeleteMembers(); 94 95 96 Msg() { 97 _msgID = _idGenerator++; 98 _messages[_msgID] = this; 99 } 100 101 Msg(String aName) { 102 this(); 103 _name = aName; 104 } 105 106 public final Msg clone(Transaction a_trans) { 107 Msg msg = null; 108 try { 109 msg=(Msg) clone(); 110 msg._trans = a_trans; 111 } catch (CloneNotSupportedException e) { 112 } 114 return msg; 115 } 116 117 public final boolean equals(Object obj) { 118 if(this==obj) { 119 return true; 120 } 121 if(obj==null||obj.getClass()!=this.getClass()) { 122 return false; 123 } 124 return _msgID == ((Msg) obj)._msgID; 125 } 126 127 128 void fakePayLoad(Transaction a_trans) { 129 _trans = a_trans; 130 } 132 133 137 public YapReader getByteLoad() { 138 return null; 139 } 140 141 final String getName() { 142 if (_name == null) { 143 return getClass().getName(); 144 } 145 return _name; 146 } 147 148 protected Transaction transaction() { 149 return _trans; 150 } 151 152 protected YapFile file(){ 153 return (YapFile)stream(); 154 } 155 156 protected YapStream stream(){ 157 return transaction().stream(); 158 } 159 160 protected Object streamLock(){ 161 return stream().lock(); 162 } 163 164 protected Config4Impl config(){ 165 return stream().config(); 166 } 167 168 172 public boolean processAtServer(YapServerThread serverThread) { 173 return false; } 176 177 public static final Msg readMessage(Transaction a_trans, YapSocket sock) throws IOException { 178 YapWriter reader = new YapWriter(a_trans, YapConst.MESSAGE_LENGTH); 179 if(!reader.read(sock)) { 180 return null; 181 } 182 Msg message = _messages[reader.readInt()].readPayLoad(a_trans, sock, reader); 183 if (Debug.messages) { 184 System.out.println(message + " arrived at " + a_trans.stream()); 185 } 186 return message; 187 } 188 189 Msg readPayLoad(Transaction a_trans, YapSocket sock, YapReader reader) 190 throws IOException { 191 a_trans = checkParentTransaction(a_trans, reader); 192 return clone(a_trans); 193 } 194 195 protected Transaction checkParentTransaction(Transaction a_trans, YapReader reader) { 196 if(reader.readByte() == YapConst.SYSTEM_TRANS && a_trans.parentTransaction() != null){ 197 return a_trans.parentTransaction(); 198 } 199 return a_trans; 200 } 201 202 final void setTransaction(Transaction aTrans) { 203 _trans = aTrans; 204 } 205 206 final public String toString() { 207 return getName(); 208 } 209 210 211 public final void write(YapStream stream, YapSocket sock) { 212 if (Debug.fakeServer) { 213 YapStream i_stream = null; 214 if (stream == DebugCS.serverStream) { 215 i_stream = DebugCS.clientStream; 216 } else { 217 i_stream = DebugCS.serverStream; 218 } 219 setTransaction(i_stream.getTransaction()); 220 fakePayLoad(i_stream.getTransaction()); 221 if (stream == DebugCS.serverStream) { 222 final Object finalThis = this; 223 try{ 224 DebugCS.clientMessageQueueLock.run(new Closure4() { 225 public Object run() { 226 DebugCS.clientMessageQueue.add(finalThis); 227 return null; 228 } 229 }); 230 }catch(Exception ex){ 231 232 234 } 235 } else { 236 processAtServer(null); 237 } 238 } else { 239 synchronized (sock) { 240 try { 241 if (Debug.messages) { 242 System.out.println(this +" sent by " + stream); 243 } 244 sock.write(payLoad()._buffer); 245 sock.flush(); 246 } catch (Exception e) { 247 248 252 255 } 256 } 257 } 258 } 259 260 public YapWriter payLoad() { 261 YapWriter writer = new YapWriter(transaction(), YapConst.MESSAGE_LENGTH); 262 writer.writeInt(_msgID); 263 return writer; 264 } 265 266 } | Popular Tags |