KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > cs > messages > Msg


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

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 /**
30  * Messages for Client/Server Communication
31  */

32 public class Msg implements Cloneable JavaDoc {
33
34     static int _idGenerator = 1;
35     private static Msg[] _messages = new Msg[60];
36
37     int _msgID;
38     String JavaDoc _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 JavaDoc 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 JavaDoc e) {
112             // shouldn't happen
113
}
114         return msg;
115     }
116     
117     public final boolean equals(Object JavaDoc 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         // do nothing
131
}
132
133     /**
134      * dummy method to allow clean override handling
135      * without casting
136      */

137     public YapReader getByteLoad() {
138         return null;
139     }
140
141     final String JavaDoc 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 JavaDoc streamLock(){
161         return stream().lock();
162     }
163     
164     protected Config4Impl config(){
165         return stream().config();
166     }
167
168     /**
169      * server side execution
170      * @param serverThread TODO
171      */

172     public boolean processAtServer(YapServerThread serverThread) {
173         // default: do nothing
174
return false; // since not processed
175
}
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 JavaDoc 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 JavaDoc finalThis = this;
223                 try{
224                     DebugCS.clientMessageQueueLock.run(new Closure4() {
225                         public Object JavaDoc run() {
226                             DebugCS.clientMessageQueue.add(finalThis);
227                             return null;
228                         }
229                     });
230                 }catch(Exception JavaDoc ex){
231                     
232                     // TODO: notify client app about problems and try to fix here
233

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 JavaDoc e) {
247                     
248                     // Note: It is not sufficient to catch IoException only
249
// here. .NET will throw different exceptions if a socket
250
// is disconnected.
251

252                     // TODO: handle more softly in YapClient, maybe
253
// try to reconnect
254

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