KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > encoding > BufferManagerWriteCollect


1 /*
2  * @(#)BufferManagerWriteCollect.java 1.14 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package com.sun.corba.se.impl.encoding;
8
9 import java.nio.ByteBuffer JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.NoSuchElementException JavaDoc;
12 import java.util.LinkedList JavaDoc;
13
14 import com.sun.corba.se.impl.encoding.BufferQueue;
15 import com.sun.corba.se.impl.encoding.BufferManagerWrite;
16 import com.sun.corba.se.impl.orbutil.ORBConstants;
17 import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
18 import com.sun.corba.se.impl.encoding.ByteBufferWithInfo;
19 import com.sun.corba.se.impl.protocol.giopmsgheaders.MessageBase;
20 import com.sun.corba.se.impl.protocol.giopmsgheaders.FragmentMessage;
21 import com.sun.corba.se.spi.orb.ORB;
22 import com.sun.corba.se.impl.encoding.CDROutputObject;
23 import com.sun.corba.se.impl.orbutil.ORBUtility;
24 import com.sun.corba.se.pept.transport.Connection;
25 import com.sun.corba.se.pept.transport.ByteBufferPool;
26 import com.sun.corba.se.pept.encoding.OutputObject;
27
28 /**
29  * Collect buffer manager.
30  */

31 public class BufferManagerWriteCollect extends BufferManagerWrite
32 {
33     private BufferQueue queue = new BufferQueue();
34
35     private boolean sentFragment = false;
36     private boolean debug = false;
37
38
39     BufferManagerWriteCollect(ORB orb)
40     {
41         super(orb);
42          if (orb != null)
43             debug = orb.transportDebugFlag;
44     }
45
46     public boolean sentFragment() {
47         return sentFragment;
48     }
49
50     /**
51      * Returns the correct buffer size for this type of
52      * buffer manager as set in the ORB.
53      */

54     public int getBufferSize() {
55         return orb.getORBData().getGIOPFragmentSize();
56     }
57
58     // Set the fragment's "more fragments" bit to true, put it in the
59
// queue, and allocate a new bbwi.
60
public void overflow (ByteBufferWithInfo bbwi)
61     {
62         // Set the fragment's moreFragments field to true
63
MessageBase.setFlag(bbwi.byteBuffer, Message.MORE_FRAGMENTS_BIT);
64
65         // Enqueue the previous fragment
66
queue.enqueue(bbwi);
67
68         // Create a new bbwi
69
ByteBufferWithInfo newBbwi = new ByteBufferWithInfo(orb, this);
70         newBbwi.fragmented = true;
71
72         // XREVISIT - Downcast
73
((CDROutputObject)outputObject).setByteBufferWithInfo(newBbwi);
74
75         // Now we must marshal in the fragment header/GIOP header
76

77         // REVISIT - we can optimize this by not creating the fragment message
78
// each time.
79

80         // XREVISIT - Downcast
81
FragmentMessage header =
82               ((CDROutputObject)outputObject).getMessageHeader()
83                                              .createFragmentMessage();
84
85         header.write((CDROutputObject)outputObject);
86     }
87
88     // Send all fragments
89
public void sendMessage ()
90     {
91         // Enqueue the last fragment
92
queue.enqueue(((CDROutputObject)outputObject).getByteBufferWithInfo());
93
94         Iterator JavaDoc bufs = iterator();
95
96         Connection conn =
97                           ((OutputObject)outputObject).getMessageMediator().
98                                                        getConnection();
99
100         // With the collect strategy, we must lock the connection
101
// while fragments are being sent. This is so that there are
102
// no interleved fragments in GIOP 1.1.
103
//
104
// Note that this thread must not call writeLock again in any
105
// of its send methods!
106
conn.writeLock();
107
108         try {
109
110             // Get a reference to ByteBufferPool so that the ByteBufferWithInfo
111
// ByteBuffer can be released to the ByteBufferPool
112
ByteBufferPool byteBufferPool = orb.getByteBufferPool();
113
114             while (bufs.hasNext()) {
115                 
116                 ByteBufferWithInfo bbwi = (ByteBufferWithInfo)bufs.next();
117                 ((CDROutputObject)outputObject).setByteBufferWithInfo(bbwi);
118                 
119                 conn.sendWithoutLock(((CDROutputObject)outputObject));
120
121                 sentFragment = true;
122
123                 // Release ByteBufferWithInfo's ByteBuffer back to the pool
124
// of ByteBuffers.
125
if (debug)
126                 {
127                     // print address of ByteBuffer being released
128
int bbAddress = System.identityHashCode(bbwi.byteBuffer);
129                     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(80);
130                     sb.append("sendMessage() - releasing ByteBuffer id (");
131                     sb.append(bbAddress).append(") to ByteBufferPool.");
132                     String JavaDoc msg = sb.toString();
133                     dprint(msg);
134                 }
135                 byteBufferPool.releaseByteBuffer(bbwi.byteBuffer);
136                 bbwi.byteBuffer = null;
137                 bbwi = null;
138             }
139
140             sentFullMessage = true;
141             
142         } finally {
143
144             conn.writeUnlock();
145         }
146     }
147
148     /**
149      * Close the BufferManagerWrite - do any outstanding cleanup.
150      *
151      * For a BufferManagerWriteGrow any queued ByteBufferWithInfo must
152      * have its ByteBuffer released to the ByteBufferPool.
153      */

154     public void close()
155     {
156         // iterate thru queue and release any ByteBufferWithInfo's
157
// ByteBuffer that may be remaining on the queue to the
158
// ByteBufferPool.
159

160         Iterator JavaDoc bufs = iterator();
161
162         ByteBufferPool byteBufferPool = orb.getByteBufferPool();
163
164         while (bufs.hasNext())
165         {
166             ByteBufferWithInfo bbwi = (ByteBufferWithInfo)bufs.next();
167             if (bbwi != null && bbwi.byteBuffer != null)
168             {
169                 if (debug)
170                 {
171                     // print address of ByteBuffer being released
172
int bbAddress = System.identityHashCode(bbwi.byteBuffer);
173                     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(80);
174                     sb.append("close() - releasing ByteBuffer id (");
175                     sb.append(bbAddress).append(") to ByteBufferPool.");
176                     String JavaDoc msg = sb.toString();
177                     dprint(msg);
178                 }
179                  byteBufferPool.releaseByteBuffer(bbwi.byteBuffer);
180                  bbwi.byteBuffer = null;
181                  bbwi = null;
182             }
183         }
184     }
185
186     private void dprint(String JavaDoc msg)
187     {
188         ORBUtility.dprint("BufferManagerWriteCollect", msg);
189     }
190
191     private Iterator JavaDoc iterator ()
192     {
193     return new BufferManagerWriteCollectIterator();
194     }
195
196     private class BufferManagerWriteCollectIterator implements Iterator JavaDoc
197     {
198     public boolean hasNext ()
199     {
200             return queue.size() != 0;
201     }
202
203     public Object JavaDoc next ()
204     {
205             return queue.dequeue();
206         }
207
208     public void remove ()
209     {
210         throw new UnsupportedOperationException JavaDoc();
211     }
212     }
213 }
214
Popular Tags