Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(704)

Side by Side Diff: mojo/public/java/src/org/chromium/mojo/system/DataPipe.java

Issue 228723002: Java API for mojo system. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Follow review Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.mojo.system;
6
7 import java.nio.ByteBuffer;
8
9 /**
10 * Interface for data pipes. A data pipe is a unidirectional communication chann el for unframed
11 * data. Data is unframed, but must come as (multiples of) discrete elements, of the size given at
12 * creation time.
13 */
14 public interface DataPipe {
15
16 /**
17 * Flags for the data pipe creation operation.
18 */
19 public static class CreateFlags extends Flags<CreateFlags> {
20 private static final int FLAG_NONE = 0;
21 private static final int FLAG_MAY_DISCARD = 1 << 0;
bulach 2014/04/17 13:15:42 nit: ditto for these flags..
22
23 /**
24 * Dedicated constructor.
25 *
26 * @param flags initial value of the flags.
27 */
28 protected CreateFlags(int flags) {
29 super(flags);
30 }
31
32 /**
33 * Change the may-discard bit of this flag. This indicates that the data pipe may discard
34 * data for whatever reason; best-effort delivery. In particular, if the capacity is
35 * reached, old data may be discard to make room for new data.
36 *
37 * @param mayDiscard the new value of the may-discard bit.
38 * @return this.
39 */
40 public CreateFlags mayDiscard(boolean mayDiscard) {
41 return setFlag(FLAG_MAY_DISCARD, mayDiscard);
42 }
43
44 /**
45 * @return flags with no bit set.
46 */
47 public static CreateFlags none() {
48 return new CreateFlags(FLAG_NONE);
49 }
50
51 }
52
53 /**
54 * Used to specify creation parameters for a data pipe to |Core.createDataPi pe()|.
55 */
56 public static class CreateOptions {
57
58 /**
59 * Used to specify different modes of operation, see |DataPipe.CreateFla gs|.
60 */
61 private CreateFlags mFlags = CreateFlags.none();
62 /**
63 * The size of an element, in bytes. All transactions and buffers will c onsist of an
64 * integral number of elements. Must be nonzero.
65 */
66 private int mElementNumBytes;
67 /**
68 * The capacity of the data pipe, in number of bytes; must be a multiple of
69 * |element_num_bytes|. The data pipe will always be able to queue AT LE AST this much data.
70 * Set to zero to opt for a system-dependent automatically-calculated ca pacity (which will
71 * always be at least one element).
72 */
73 private int mCapacityNumBytes;
74
75 /**
76 * @return the flags
77 */
78 public CreateFlags getFlags() {
79 return mFlags;
80 }
81
82 /**
83 * @return the elementNumBytes
84 */
85 public int getElementNumBytes() {
86 return mElementNumBytes;
87 }
88
89 /**
90 * @param elementNumBytes the elementNumBytes to set
91 */
92 public void setElementNumBytes(int elementNumBytes) {
93 mElementNumBytes = elementNumBytes;
94 }
95
96 /**
97 * @return the capacityNumBytes
98 */
99 public int getCapacityNumBytes() {
100 return mCapacityNumBytes;
101 }
102
103 /**
104 * @param capacityNumBytes the capacityNumBytes to set
105 */
106 public void setCapacityNumBytes(int capacityNumBytes) {
107 mCapacityNumBytes = capacityNumBytes;
108 }
109
110 }
111
112 /**
113 * Flags for the write operations on MessagePipeHandle .
114 */
115 public static class WriteFlags extends Flags<WriteFlags> {
116 private static final int FLAG_NONE = 0;
117 private static final int FLAG_ALL_OR_NONE = 1 << 0;
118
119 /**
120 * Dedicated constructor.
121 *
122 * @param flags initial value of the flags.
123 */
124 private WriteFlags(int flags) {
125 super(flags);
126 }
127
128 /**
129 * Change the all-or-none bit of those flags. If set, write either all t he elements
130 * requested or none of them.
131 *
132 * @param allOrNone the new value of all-or-none bit.
133 * @return this.
134 */
135 public WriteFlags allOrNone(boolean allOrNone) {
136 return setFlag(FLAG_ALL_OR_NONE, allOrNone);
137 }
138
139 /**
140 * @return a flag with no bit set.
141 */
142 public static WriteFlags none() {
143 return new WriteFlags(FLAG_NONE);
144 }
145 }
146
147 /**
148 * Flags for the read operations on MessagePipeHandle.
149 */
150 public static class ReadFlags extends Flags<ReadFlags> {
151 private static final int FLAG_NONE = 0;
152 private static final int FLAG_ALL_OR_NONE = 1 << 0;
153 private static final int FLAG_QUERY = 1 << 2;
154
155 /**
156 * Dedicated constructor.
157 *
158 * @param flags initial value of the flag.
159 */
160 private ReadFlags(int flags) {
161 super(flags);
162 }
163
164 /**
165 * Change the all-or-none bit of this flag. If set, read (or discard) ei ther the requested
166 * number of elements or none.
167 *
168 * @param allOrNone the new value of the all-or-none bit.
169 * @return this.
170 */
171 public ReadFlags allOrNone(boolean allOrNone) {
172 return setFlag(FLAG_ALL_OR_NONE, allOrNone);
173 }
174
175 /**
176 * Change the query bit of this flag. If set query the number of element s available to read.
177 * Mutually exclusive with |dicard| and |allOrNone| is ignored if this f lag is set.
178 *
179 * @param query the new value of the query bit.
180 * @return this.
181 */
182 public ReadFlags query(boolean query) {
183 return setFlag(FLAG_QUERY, query);
184 }
185
186 /**
187 * @return a flag with no bit set.
188 */
189 public static ReadFlags none() {
190 return new ReadFlags(FLAG_NONE);
191 }
192
193 }
194
195 /**
196 * Handle for the producer part of a data pipe.
197 */
198 public static interface ProducerHandle extends Handle {
bulach 2014/04/17 13:15:42 these Producer|ConsumerHandle interfaces look fair
qsr 2014/04/17 13:50:24 They are the two sides of a channel. I find it nic
199
200 /**
201 * Writes the given data to the data pipe producer. |elements| points to data; the buffer
202 * must be a direct ByteBuffer and the limit should be a multiple of the data pipe's element
203 * size. If |allOrNone| is set in |flags|, either all the data will be w ritten or none is.
204 * <p>
205 * On success, returns the amount of data that was actually written.
206 * <p>
207 * Note: If the data pipe has the "may discard" option flag (specified o n creation), this
208 * will discard as much data as required to write the given data, starti ng with the earliest
209 * written data that has not been consumed. However, even with "may disc ard", if the buffer
210 * limit is greater than the data pipe's capacity (and |allOrNone| is no t set), this will
211 * write the maximum amount possible (namely, the data pipe's capacity) and return that
212 * amount. It will *not* discard data from |elements|.
213 *
214 * @return number of written bytes.
215 */
216 public int writeData(ByteBuffer elements, WriteFlags flags);
217
218 /**
219 * Begins a two-phase write to the data pipe producer . On success, retu rns a |ByteBuffer|
220 * to which the caller can write. If flags has |allOrNone| set, then the buffer capacity
221 * will be at least as large as |numBytes|, which must also be a multipl e of the element
222 * size (if |allOrNone| is not set, |numBytes| is ignored and the caller must check the
223 * capacity of the buffer).
224 * <p>
225 * During a two-phase write, this handle is *not* writable. E.g., if ano ther thread tries to
226 * write to it, it will throw a |MojoException| with code |MojoResult.BU SY|; that thread can
227 * then wait for this handle to become writable again.
228 * <p>
229 * Once the caller has finished writing data to the buffer, it should ca ll |endWriteData()|
230 * to specify the amount written and to complete the two-phase write.
231 * <p>
232 * Note: If the data pipe has the "may discard" option flag (specified o n creation) and
233 * |flags| has |allOrNone| set, this may discard some data.
234 *
235 * @return The buffer to write to.
236 */
237 public ByteBuffer beginWriteData(int numBytes, WriteFlags flags);
238
239 /**
240 * Ends a two-phase write to the data pipe producer that was begun by a call to
241 * |beginWriteData()| on the same handle. |numBytesWritten| should indic ate the amount of
242 * data actually written; it must be less than or equal to the capacity of the buffer
243 * returned by |beginWriteData()| and must be a multiple of the element size. The buffer
244 * returned from |beginWriteData()| must have been filled with exactly | numBytesWritten|
245 * bytes of data.
246 * <p>
247 * On failure, the two-phase write (if any) is ended (so the handle may become writable
248 * again, if there's space available) but no data written to the buffer is "put into" the
249 * data pipe.
250 */
251 public void endWriteData(int numBytesWritten);
252 }
253
254 /**
255 * Handle for the consumer part of a data pipe.
256 */
257 public static interface ConsumerHandle extends Handle {
258
259 /**
260 * Discards data on the data pie consumer. This method discards up to |n umBytes| (which
261 * again be a multiple of the element size) bytes of data, returning the amount actually
262 * discarded. if |flags| has |allOrNone|, it will either discard exactly |numBytes| bytes of
263 * data or none. In this case, |query| must not be set.
264 */
265 public int discardData(int numBytes, ReadFlags flags);
266
267 /**
268 * Reads data from the data pipe consumer. May also be used to query the amount of data
269 * available. If |flags| has not |query| set, this tries to read up to | elements| capacity
270 * (which must be a multiple of the data pipe's element size) bytes of d ata to |elements|
271 * and returns the amount actually read. |elements| must be a direct Byt eBuffer. If flags
272 * has |allOrNone| set, it will either read exactly |elements| capacity bytes of data or
273 * none.
274 * <p>
275 * If flags has |query| set, it queries the amount of data available, re turning the number
276 * of bytes available. In this case |allOrNone| is ignored, as are |elem ents|.
277 */
278 public int readData(ByteBuffer elements, ReadFlags flags);
279
280 /**
281 * Begins a two-phase read from the data pipe consumer. On success, retu rns a |ByteBuffer|
282 * from which the caller can read up to its limit bytes of data. If flag s has |allOrNone|
283 * set, then the limit will be at least as large as |numBytes|, which mu st also be a
284 * multiple of the element size (if |allOrNone| is not set, |numBytes| i s ignored). |flags|
285 * must not have |query| set.
286 * <p>
287 * During a two-phase read, this handle is *not* readable. E.g., if anot her thread tries to
288 * read from it, it will throw a |MojoException| with code |MojoResult.B USY|; that thread
289 * can then wait for this handle to become readable again.
290 * <p>
291 * Once the caller has finished reading data from the buffer, it should call |endReadData()|
292 * to specify the amount read and to complete the two-phase read.
293 */
294 public ByteBuffer beginReadData(int numBytes, ReadFlags flags);
295
296 /**
297 * Ends a two-phase read from the data pipe consumer that was begun by a call to
298 * |beginReadData()| on the same handle. |numBytesRead| should indicate the amount of data
299 * actually read; it must be less than or equal to the limit of the buff er returned by
300 * |beginReadData()| and must be a multiple of the element size.
301 * <p>
302 * On failure, the two-phase read (if any) is ended (so the handle may b ecome readable
303 * again) but no data is "removed" from the data pipe.
304 */
305 public void endReadData(int numBytesRead);
306 }
307
308 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698