001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.internal.cs.messages;
022:
023: import com.db4o.foundation.network.*;
024: import com.db4o.internal.*;
025:
026: /**
027: * Messages with Data for Client/Server Communication
028: */
029: public class MsgD extends Msg {
030:
031: StatefulBuffer _payLoad;
032:
033: MsgD() {
034: super ();
035: }
036:
037: MsgD(String aName) {
038: super (aName);
039: }
040:
041: public Buffer getByteLoad() {
042: return _payLoad;
043: }
044:
045: public final StatefulBuffer payLoad() {
046: return _payLoad;
047: }
048:
049: public void payLoad(StatefulBuffer writer) {
050: _payLoad = writer;
051: }
052:
053: public final MsgD getWriterForByte(Transaction trans, byte b) {
054: MsgD msg = getWriterForLength(trans, 1);
055: msg._payLoad.writeByte(b);
056: return msg;
057: }
058:
059: public final MsgD getWriterForLength(Transaction trans, int length) {
060: MsgD message = (MsgD) publicClone();
061: message.setTransaction(trans);
062: message._payLoad = new StatefulBuffer(trans, length
063: + Const4.MESSAGE_LENGTH);
064: message.writeInt(_msgID);
065: message.writeInt(length);
066: if (trans.parentTransaction() == null) {
067: message._payLoad.writeByte(Const4.SYSTEM_TRANS);
068: } else {
069: message._payLoad.writeByte(Const4.USER_TRANS);
070: }
071: return message;
072: }
073:
074: public final MsgD getWriter(Transaction trans) {
075: return getWriterForLength(trans, 0);
076: }
077:
078: public final MsgD getWriterForInts(Transaction trans, int[] ints) {
079: MsgD message = getWriterForLength(trans, Const4.INT_LENGTH
080: * ints.length);
081: for (int i = 0; i < ints.length; i++) {
082: message.writeInt(ints[i]);
083: }
084: return message;
085: }
086:
087: public final MsgD getWriterForIntArray(Transaction a_trans,
088: int[] ints, int length) {
089: MsgD message = getWriterForLength(a_trans, Const4.INT_LENGTH
090: * (length + 1));
091: message.writeInt(length);
092: for (int i = 0; i < length; i++) {
093: message.writeInt(ints[i]);
094: }
095: return message;
096: }
097:
098: public final MsgD getWriterForInt(Transaction a_trans, int id) {
099: MsgD message = getWriterForLength(a_trans, Const4.INT_LENGTH);
100: message.writeInt(id);
101: return message;
102: }
103:
104: public final MsgD getWriterForIntString(Transaction a_trans,
105: int anInt, String str) {
106: MsgD message = getWriterForLength(a_trans, Const4.stringIO
107: .length(str)
108: + Const4.INT_LENGTH * 2);
109: message.writeInt(anInt);
110: message.writeString(str);
111: return message;
112: }
113:
114: public final MsgD getWriterForLong(Transaction a_trans, long a_long) {
115: MsgD message = getWriterForLength(a_trans, Const4.LONG_LENGTH);
116: message.writeLong(a_long);
117: return message;
118: }
119:
120: public MsgD getWriterForSingleObject(Transaction trans, Object obj) {
121: SerializedGraph serialized = Serializer.marshall(trans
122: .container(), obj);
123: MsgD msg = getWriterForLength(trans, serialized
124: .marshalledLength());
125: serialized.write(msg._payLoad);
126: return msg;
127: }
128:
129: public final MsgD getWriterForString(Transaction a_trans, String str) {
130: MsgD message = getWriterForLength(a_trans, Const4.stringIO
131: .length(str)
132: + Const4.INT_LENGTH);
133: message.writeString(str);
134: return message;
135: }
136:
137: public MsgD getWriter(StatefulBuffer bytes) {
138: MsgD message = getWriterForLength(bytes.getTransaction(), bytes
139: .length());
140: message._payLoad.append(bytes._buffer);
141: return message;
142: }
143:
144: public byte[] readBytes() {
145: return _payLoad.readBytes(readInt());
146: }
147:
148: public final int readInt() {
149: return _payLoad.readInt();
150: }
151:
152: public final long readLong() {
153: return _payLoad.readLong();
154: }
155:
156: public final boolean readBoolean() {
157: return _payLoad.readByte() != 0;
158: }
159:
160: public Object readObjectFromPayLoad() {
161: return Serializer.unmarshall(stream(), _payLoad);
162: }
163:
164: final Msg readPayLoad(MessageDispatcher messageDispatcher,
165: Transaction a_trans, Socket4 sock, Buffer reader) {
166: int length = reader.readInt();
167: a_trans = checkParentTransaction(a_trans, reader);
168: final MsgD command = (MsgD) publicClone();
169: command.setTransaction(a_trans);
170: command.setMessageDispatcher(messageDispatcher);
171: command._payLoad = readMessageBuffer(a_trans, sock, length);
172: return command;
173: }
174:
175: public final String readString() {
176: int length = readInt();
177: return Const4.stringIO.read(_payLoad, length);
178: }
179:
180: public Object readSingleObject() {
181: return Serializer.unmarshall(stream(), SerializedGraph
182: .read(_payLoad));
183: }
184:
185: public final void writeBytes(byte[] aBytes) {
186: writeInt(aBytes.length);
187: _payLoad.append(aBytes);
188: }
189:
190: public final void writeInt(int aInt) {
191: _payLoad.writeInt(aInt);
192: }
193:
194: public final void writeLong(long l) {
195: _payLoad.writeLong(l);
196: }
197:
198: public final void writeString(String aStr) {
199: _payLoad.writeInt(aStr.length());
200: Const4.stringIO.write(_payLoad, aStr);
201: }
202:
203: }
|