1 7 package com.sun.corba.se.impl.encoding; 8 9 import java.nio.ByteBuffer ; 10 import java.util.Iterator ; 11 import java.util.NoSuchElementException ; 12 import java.util.LinkedList ; 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 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 54 public int getBufferSize() { 55 return orb.getORBData().getGIOPFragmentSize(); 56 } 57 58 public void overflow (ByteBufferWithInfo bbwi) 61 { 62 MessageBase.setFlag(bbwi.byteBuffer, Message.MORE_FRAGMENTS_BIT); 64 65 queue.enqueue(bbwi); 67 68 ByteBufferWithInfo newBbwi = new ByteBufferWithInfo(orb, this); 70 newBbwi.fragmented = true; 71 72 ((CDROutputObject)outputObject).setByteBufferWithInfo(newBbwi); 74 75 77 80 FragmentMessage header = 82 ((CDROutputObject)outputObject).getMessageHeader() 83 .createFragmentMessage(); 84 85 header.write((CDROutputObject)outputObject); 86 } 87 88 public void sendMessage () 90 { 91 queue.enqueue(((CDROutputObject)outputObject).getByteBufferWithInfo()); 93 94 Iterator bufs = iterator(); 95 96 Connection conn = 97 ((OutputObject)outputObject).getMessageMediator(). 98 getConnection(); 99 100 conn.writeLock(); 107 108 try { 109 110 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 if (debug) 126 { 127 int bbAddress = System.identityHashCode(bbwi.byteBuffer); 129 StringBuffer sb = new StringBuffer (80); 130 sb.append("sendMessage() - releasing ByteBuffer id ("); 131 sb.append(bbAddress).append(") to ByteBufferPool."); 132 String 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 154 public void close() 155 { 156 160 Iterator 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 int bbAddress = System.identityHashCode(bbwi.byteBuffer); 173 StringBuffer sb = new StringBuffer (80); 174 sb.append("close() - releasing ByteBuffer id ("); 175 sb.append(bbAddress).append(") to ByteBufferPool."); 176 String 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 msg) 187 { 188 ORBUtility.dprint("BufferManagerWriteCollect", msg); 189 } 190 191 private Iterator iterator () 192 { 193 return new BufferManagerWriteCollectIterator(); 194 } 195 196 private class BufferManagerWriteCollectIterator implements Iterator 197 { 198 public boolean hasNext () 199 { 200 return queue.size() != 0; 201 } 202 203 public Object next () 204 { 205 return queue.dequeue(); 206 } 207 208 public void remove () 209 { 210 throw new UnsupportedOperationException (); 211 } 212 } 213 } 214 | Popular Tags |