1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
3 * This file is open source software, licensed to you under the terms
4 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
5 * distributed with this work for additional information regarding copyright
6 * ownership. You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
20 * Copyright (C) 2014 Cloudius Systems, Ltd.
23 * Ceph - scalable distributed file system
25 * Copyright (C) 2015 XSky <haomai@xsky.com>
27 * Author: Haomai Wang <haomaiwang@gmail.com>
29 * This is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU Lesser General Public
31 * License version 2.1, as published by the Free Software
32 * Foundation. See file COPYING.
36 #ifndef CEPH_MSG_STREAM_H_
37 #define CEPH_MSG_STREAM_H_
42 // A stream<> is the producer side. It may call produce() as long
43 // as the returned from the previous invocation is ready.
44 // To signify no more data is available, call close().
46 // A subscription<> is the consumer side. It is created by a call
47 // to stream::listen(). Calling subscription::start(),
48 // which registers the data processing callback, starts processing
49 // events. It may register for end-of-stream notifications by
50 // return the when_done() future, which also delivers error
51 // events (as exceptions).
53 // The consumer can pause generation of new data by returning
54 // positive integer; when it becomes ready, the producer
55 // will resume processing.
57 template <typename... T>
60 template <typename... T>
62 subscription<T...>* _sub = nullptr;
66 using next_fn = std::function<int (T...)>;
68 stream(const stream&) = delete;
69 stream(stream&&) = delete;
72 _sub->_stream = nullptr;
76 void operator=(const stream&) = delete;
77 void operator=(stream&&) = delete;
79 // Returns a subscription that reads value from this
81 subscription<T...> listen() {
82 return subscription<T...>(this);
85 // Returns a subscription that reads value from this
86 // stream, and also sets up the listen function.
87 subscription<T...> listen(next_fn next) {
88 auto sub = subscription<T...>(this);
89 sub.start(std::move(next));
93 // Becomes ready when the listener is ready to accept
94 // values. Call only once, when beginning to produce
100 // Produce a value. Call only after started(), and after
101 // a previous produce() is ready.
102 int produce(T... data) {
103 return _sub->_next(std::move(data)...);
106 // End the stream. Call only after started(), and after
107 // a previous produce() is ready. No functions may be called
113 // Signal an error. Call only after started(), and after
114 // a previous produce() is ready. No functions may be called
116 void set_exception(int error) {
121 friend class subscription<T...>;
124 template <typename... T>
127 using next_fn = typename stream<T...>::next_fn;
129 stream<T...>* _stream;
132 explicit subscription(stream<T...>* s): _stream(s) {
133 assert(!_stream->_sub);
134 _stream->_sub = this;
138 subscription(subscription&& x)
139 : _stream(x._stream), _next(std::move(x._next)) {
142 _stream->_sub = this;
147 _stream->_sub = nullptr;
151 /// \brief Start receiving events from the stream.
153 /// \param next Callback to call for each event
154 void start(std::function<int (T...)> next) {
155 _next = std::move(next);
156 _stream->ready = true;
159 // Becomes ready when the stream is empty, or when an error
160 // happens (in that case, an exception is held).
162 return _stream->done;
165 friend class stream<T...>;
168 #endif /* CEPH_MSG_STREAM_H_ */