1 /*
   2  * Copyright (c) 2000, 2003, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package com.sun.corba.se.impl.encoding;
  26 
  27 import java.nio.ByteBuffer;
  28 
  29 import com.sun.corba.se.impl.orbutil.ORBConstants;
  30 import com.sun.corba.se.impl.protocol.giopmsgheaders.Message;
  31 import com.sun.corba.se.impl.protocol.giopmsgheaders.MessageBase;
  32 import com.sun.corba.se.impl.protocol.giopmsgheaders.FragmentMessage;
  33 import com.sun.corba.se.impl.protocol.giopmsgheaders.ReplyMessage;
  34 import com.sun.corba.se.impl.encoding.BufferManagerWrite;
  35 import com.sun.corba.se.impl.encoding.ByteBufferWithInfo;
  36 import com.sun.corba.se.impl.encoding.CDROutputObject;
  37 import com.sun.corba.se.spi.orb.ORB;
  38 import com.sun.corba.se.pept.transport.Connection;
  39 import com.sun.corba.se.pept.encoding.OutputObject;
  40 import org.omg.CORBA.SystemException;
  41 
  42 /**
  43  * Streaming buffer manager.
  44  */
  45 public class BufferManagerWriteStream extends BufferManagerWrite
  46 {
  47     private int fragmentCount = 0;
  48 
  49     BufferManagerWriteStream( ORB orb )
  50     {
  51         super(orb) ;
  52     }
  53 
  54     public boolean sentFragment() {
  55         return fragmentCount > 0;
  56     }
  57 
  58     /**
  59      * Returns the correct buffer size for this type of
  60      * buffer manager as set in the ORB.
  61      */
  62     public int getBufferSize() {
  63         return orb.getORBData().getGIOPFragmentSize();
  64     }
  65 
  66     public void overflow (ByteBufferWithInfo bbwi)
  67     {
  68         // Set the fragment's moreFragments field to true
  69         MessageBase.setFlag(bbwi.byteBuffer, Message.MORE_FRAGMENTS_BIT);
  70 
  71         try {
  72            sendFragment(false);
  73         } catch(SystemException se){
  74                 orb.getPIHandler().invokeClientPIEndingPoint(
  75                         ReplyMessage.SYSTEM_EXCEPTION, se);
  76                 throw se;
  77         }
  78 
  79         // Reuse the old buffer
  80 
  81         // REVISIT - need to account for case when needed > available
  82         // even after fragmenting.  This is the large array case, so
  83         // the caller should retry when it runs out of space.
  84         bbwi.position(0);
  85         bbwi.buflen = bbwi.byteBuffer.limit();
  86         bbwi.fragmented = true;
  87 
  88         // Now we must marshal in the fragment header/GIOP header
  89 
  90         // REVISIT - we can optimize this by not creating the fragment message
  91         // each time.
  92 
  93         FragmentMessage header = ((CDROutputObject)outputObject).getMessageHeader().createFragmentMessage();
  94 
  95         header.write(((CDROutputObject)outputObject));
  96     }
  97 
  98     private void sendFragment(boolean isLastFragment)
  99     {
 100         Connection conn = ((OutputObject)outputObject).getMessageMediator().getConnection();
 101 
 102         // REVISIT: need an ORB
 103         //System.out.println("sendFragment: last?: " + isLastFragment);
 104         conn.writeLock();
 105 
 106         try {
 107             // Send the fragment
 108             conn.sendWithoutLock(((OutputObject)outputObject));
 109 
 110             fragmentCount++;
 111 
 112         } finally {
 113 
 114             conn.writeUnlock();
 115         }
 116 
 117     }
 118 
 119     // Sends the last fragment
 120     public void sendMessage ()
 121     {
 122         sendFragment(true);
 123 
 124         sentFullMessage = true;
 125     }
 126 
 127     /**
 128      * Close the BufferManagerWrite and do any outstanding cleanup.
 129      *
 130      * No work to do for a BufferManagerWriteStream
 131      */
 132     public void close(){};
 133 
 134 }