lryan | 56e307f | 2014-12-05 13:25:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014, Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following disclaimer |
| 12 | * in the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * * Neither the name of Google Inc. nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived from |
| 17 | * this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
nathanmittler | 29cbef1 | 2014-10-27 11:33:19 -0700 | [diff] [blame] | 32 | package com.google.net.stubby.transport; |
nathanmittler | ef31a5f | 2014-06-03 07:48:25 -0700 | [diff] [blame] | 33 | |
| 34 | import java.io.InputStream; |
| 35 | |
nathanmittler | d784765 | 2014-06-06 10:14:22 -0700 | [diff] [blame] | 36 | import javax.annotation.Nullable; |
| 37 | |
nathanmittler | ef31a5f | 2014-06-03 07:48:25 -0700 | [diff] [blame] | 38 | /** |
| 39 | * A single stream of communication between two end-points within a transport. |
zhangkun | c7d33bf | 2014-08-04 16:25:02 -0700 | [diff] [blame] | 40 | * |
| 41 | * <p>An implementation doesn't need to be thread-safe. |
nathanmittler | ef31a5f | 2014-06-03 07:48:25 -0700 | [diff] [blame] | 42 | */ |
ejona | 4fd4845 | 2014-06-11 16:32:20 -0700 | [diff] [blame] | 43 | public interface Stream { |
nathanmittler | ef31a5f | 2014-06-03 07:48:25 -0700 | [diff] [blame] | 44 | /** |
nmittler | de3a131 | 2015-01-16 11:54:24 -0800 | [diff] [blame^] | 45 | * Requests up to the given number of messages from the call to be delivered to |
| 46 | * {@link StreamListener#messageRead(java.io.InputStream, int)}. No additional messages will be |
| 47 | * delivered. |
| 48 | * |
| 49 | * @param numMessages the requested number of messages to be delivered to the listener. |
| 50 | */ |
| 51 | void request(int numMessages); |
| 52 | |
| 53 | /** |
nathanmittler | d784765 | 2014-06-06 10:14:22 -0700 | [diff] [blame] | 54 | * Writes a message payload to the remote end-point. The bytes from the stream are immediate read |
| 55 | * by the Transport. This method will always return immediately and will not wait for the write to |
| 56 | * complete. |
nathanmittler | ef31a5f | 2014-06-03 07:48:25 -0700 | [diff] [blame] | 57 | * |
nathanmittler | d784765 | 2014-06-06 10:14:22 -0700 | [diff] [blame] | 58 | * <p>When the write is "accepted" by the transport, the given callback (if provided) will be |
| 59 | * called. The definition of what it means to be "accepted" is up to the transport implementation, |
| 60 | * but this is a general indication that the transport is capable of handling more out-bound data |
| 61 | * on the stream. If the stream/connection is closed for any reason before the write could be |
nathanmittler | 0304b3d | 2014-10-24 13:39:13 -0700 | [diff] [blame] | 62 | * accepted, the callback will never be invoked. |
nathanmittler | ef31a5f | 2014-06-03 07:48:25 -0700 | [diff] [blame] | 63 | * |
| 64 | * @param message stream containing the serialized message to be sent |
| 65 | * @param length the length of the {@link InputStream}. |
nathanmittler | d784765 | 2014-06-06 10:14:22 -0700 | [diff] [blame] | 66 | * @param accepted an optional callback for when the transport has accepted the write. |
nathanmittler | ef31a5f | 2014-06-03 07:48:25 -0700 | [diff] [blame] | 67 | */ |
ejona | 4fd4845 | 2014-06-11 16:32:20 -0700 | [diff] [blame] | 68 | void writeMessage(InputStream message, int length, @Nullable Runnable accepted); |
nathanmittler | ef31a5f | 2014-06-03 07:48:25 -0700 | [diff] [blame] | 69 | |
| 70 | /** |
nathanmittler | d784765 | 2014-06-06 10:14:22 -0700 | [diff] [blame] | 71 | * Flushes any internally buffered messages to the remote end-point. |
nathanmittler | ef31a5f | 2014-06-03 07:48:25 -0700 | [diff] [blame] | 72 | */ |
ejona | 4fd4845 | 2014-06-11 16:32:20 -0700 | [diff] [blame] | 73 | void flush(); |
nathanmittler | ef31a5f | 2014-06-03 07:48:25 -0700 | [diff] [blame] | 74 | } |