Flutter Engine
The Flutter Engine
BlockingRequestSink.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, the Dart project authors.
3 *
4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14package org.dartlang.vm.service.internal;
15
16import com.google.common.collect.Lists;
17import com.google.gson.JsonObject;
18
19import java.util.LinkedList;
20
21/**
22 * A {@link RequestSink} that enqueues all requests and can be later converted into a "passthrough"
23 * or an "error" {@link RequestSink}.
24 */
25public class BlockingRequestSink implements RequestSink {
26 /**
27 * The base {@link RequestSink}
28 */
29 private final RequestSink base;
30
31 /**
32 * A queue of requests.
33 */
34 private final LinkedList<JsonObject> queue = Lists.newLinkedList();
35
37 this.base = base;
38 }
39
40 @Override
41 public void add(JsonObject request) {
42 synchronized (queue) {
43 queue.add(request);
44 }
45 }
46
47 @Override
48 public void close() {
49 base.close();
50 }
51
52 /**
53 * Responds with an error to all the currently queued requests and return a {@link RequestSink} to
54 * do the same for all the future requests.
55 *
56 * @param errorResponseSink the sink to send error responses to, not {@code null}
57 */
58 public RequestSink toErrorSink(ResponseSink errorResponseSink, String errorResponseCode,
59 String errorResponseMessage) {
60 ErrorRequestSink errorRequestSink = new ErrorRequestSink(errorResponseSink, errorResponseCode,
61 errorResponseMessage);
62 synchronized (queue) {
63 for (JsonObject request : queue) {
64 errorRequestSink.add(request);
65 }
66 }
67 return errorRequestSink;
68 }
69
70 /**
71 * Returns the passthrough {@link RequestSink}.
72 */
74 synchronized (queue) {
75 for (JsonObject request : queue) {
76 base.add(request);
77 }
78 }
79 return base;
80 }
81}
RequestSink toErrorSink(ResponseSink errorResponseSink, String errorResponseCode, String errorResponseMessage)
VkQueue queue
Definition: main.cc:55