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 
  26 package com.sun.corba.se.impl.encoding;
  27 
  28 import com.sun.corba.se.spi.logging.CORBALogDomains;
  29 
  30 import com.sun.corba.se.spi.orb.ORB;
  31 
  32 import com.sun.corba.se.impl.encoding.ByteBufferWithInfo;
  33 
  34 import com.sun.corba.se.impl.logging.ORBUtilSystemException;
  35 
  36 /**
  37  * Defines the contract between the BufferManager and
  38  * CDR stream on the writing side.  The CDR stream
  39  * calls back to the BufferManagerWrite when it needs
  40  * more room in the output buffer to continue.  The
  41  * BufferManager can then grow the output buffer or
  42  * use some kind of fragmentation technique.
  43  */
  44 public abstract class BufferManagerWrite
  45 {
  46     protected ORB orb ;
  47     protected ORBUtilSystemException wrapper ;
  48 
  49     BufferManagerWrite( ORB orb )
  50     {
  51         this.orb = orb ;
  52         this.wrapper = ORBUtilSystemException.get( orb,
  53             CORBALogDomains.RPC_ENCODING ) ;
  54     }
  55 
  56     /**
  57      * Has the stream sent out any fragments so far?
  58      */
  59     public abstract boolean sentFragment();
  60 
  61     /**
  62      * Has the entire message been sent?  (Has
  63      * sendMessage been called?)
  64      */
  65     public boolean sentFullMessage() {
  66         return sentFullMessage;
  67     }
  68 
  69     /**
  70      * Returns the correct buffer size for this type of
  71      * buffer manager as set in the ORB.
  72      */
  73     public abstract int getBufferSize();
  74 
  75     /*
  76      * Called from CDROutputStream.grow.
  77      *
  78      * bbwi.buf contains a byte array which needs to grow by bbwi.needed bytes.
  79      *
  80      * This can be handled in several ways:
  81      *
  82      * 1. Resize the bbwi.buf like the current implementation of
  83      *    CDROutputStream.grow.
  84      *
  85      * 2. Collect the buffer for a later send:
  86      *    this.bufQ.put(bbwi);
  87      *    return new ByteBufferWithInfo(bbwi.length);
  88      *
  89      * 3. Send buffer as fragment:
  90      *    Backpatch fragment size field in bbwi.buf.
  91      *    Set more fragments bit in bbwi.buf.
  92      *    this.connection.send(bbwi);
  93      *    return reinitialized bbwi.buf with fragment header
  94      *
  95      * All cases should adjust the returned bbwi.* appropriately.
  96      *
  97      * Should set the bbwi.fragmented flag to true only in cases 2 and 3.
  98      */
  99 
 100     public abstract void overflow (ByteBufferWithInfo bbwi);
 101 
 102     /**
 103      * Called after Stub._invoke (i.e., before complete message has been sent).
 104      *
 105      * IIOPOutputStream.writeTo called from IIOPOutputStream.invoke
 106      *
 107      * Case: overflow was never called (bbwi.buf contains complete message).
 108      *       Backpatch size field.
 109      *       If growing or collecting:
 110      *          this.bufQ.put(bbwi).
 111      *          this.bufQ.iterate // However, see comment in getBufferQ
 112      *             this.connection.send(fragment)
 113      *       If streaming:
 114      *          this.connection.send(bbwi).
 115      *
 116      * Case: overflow was called N times (bbwi.buf contains last buffer).
 117      *       If growing or collecting:
 118      *          this.bufQ.put(bbwi).
 119      *          backpatch size field in first buffer.
 120      *          this.bufQ.iterate // However, see comment in getBufferQ
 121      *             this.connection.send(fragment)
 122      *       If streaming:
 123      *          backpatch fragment size field in bbwi.buf.
 124      *          Set no more fragments bit.
 125      *          this.connection.send(bbwi).
 126      */
 127 
 128     public abstract void sendMessage ();
 129 
 130     /**
 131      * A reference to the connection level stream will be required when
 132      * sending fragments.
 133      */
 134     public void setOutputObject(Object outputObject) {
 135         this.outputObject = outputObject;
 136     }
 137 
 138     /**
 139      * Close the BufferManagerWrite and do any outstanding cleanup.
 140      */
 141      abstract public void close();
 142 
 143 
 144     // XREVISIT - Currently a java.lang.Object during
 145     // the rip-int-generic transition.  Should eventually
 146     // become a GIOPOutputObject.
 147     protected Object outputObject;
 148 
 149     protected boolean sentFullMessage = false;
 150 }