blob: 85c32cde8101e4ef60f718532bb324cacf7c4e22 [file] [log] [blame]
lryan56e307f2014-12-05 13:25:08 -08001/*
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
nathanmittler29cbef12014-10-27 11:33:19 -070032package com.google.net.stubby.transport;
nathanmittleref31a5f2014-06-03 07:48:25 -070033
34import java.io.InputStream;
35
nathanmittlerd7847652014-06-06 10:14:22 -070036import javax.annotation.Nullable;
37
nathanmittleref31a5f2014-06-03 07:48:25 -070038/**
39 * A single stream of communication between two end-points within a transport.
zhangkunc7d33bf2014-08-04 16:25:02 -070040 *
41 * <p>An implementation doesn't need to be thread-safe.
nathanmittleref31a5f2014-06-03 07:48:25 -070042 */
ejona4fd48452014-06-11 16:32:20 -070043public interface Stream {
nathanmittleref31a5f2014-06-03 07:48:25 -070044 /**
nmittlerde3a1312015-01-16 11:54:24 -080045 * 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 /**
nathanmittlerd7847652014-06-06 10:14:22 -070054 * 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.
nathanmittleref31a5f2014-06-03 07:48:25 -070057 *
nathanmittlerd7847652014-06-06 10:14:22 -070058 * <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
nathanmittler0304b3d2014-10-24 13:39:13 -070062 * accepted, the callback will never be invoked.
nathanmittleref31a5f2014-06-03 07:48:25 -070063 *
64 * @param message stream containing the serialized message to be sent
65 * @param length the length of the {@link InputStream}.
nathanmittlerd7847652014-06-06 10:14:22 -070066 * @param accepted an optional callback for when the transport has accepted the write.
nathanmittleref31a5f2014-06-03 07:48:25 -070067 */
ejona4fd48452014-06-11 16:32:20 -070068 void writeMessage(InputStream message, int length, @Nullable Runnable accepted);
nathanmittleref31a5f2014-06-03 07:48:25 -070069
70 /**
nathanmittlerd7847652014-06-06 10:14:22 -070071 * Flushes any internally buffered messages to the remote end-point.
nathanmittleref31a5f2014-06-03 07:48:25 -070072 */
ejona4fd48452014-06-11 16:32:20 -070073 void flush();
nathanmittleref31a5f2014-06-03 07:48:25 -070074}