stegemr commented on a change in pull request #363: URL: https://github.com/apache/celix/pull/363#discussion_r723960995
########## File path: libs/pushstreams/api/celix/PushStream.h ########## @@ -0,0 +1,253 @@ +/** + *Licensed to the Apache Software Foundation (ASF) under one + *or more contributor license agreements. See the NOTICE file + *distributed with this work for additional information + *regarding copyright ownership. The ASF licenses this file + *to you under the Apache License, Version 2.0 (the + *"License"); you may not use this file except in compliance + *with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + *Unless required by applicable law or agreed to in writing, + *software distributed under the License is distributed on an + *"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + *specific language governing permissions and limitations + *under the License. + */ + +#pragma once + +#include <optional> +#include <iostream> +#include <queue> + +#include "celix/impl/PushEventConsumer.h" +#include "celix/IAutoCloseable.h" + +#include "celix/Promise.h" +#include "celix/PromiseFactory.h" +#include "celix/Deferred.h" + +namespace celix { + template<typename T> + class PushStream: public IAutoCloseable { + public: + using PredicateFunction = std::function<bool(const T&)>; + using CloseFunction = std::function<void(void)>; + using ErrorFunction = std::function<void(void)>; + using ForEachFunction = std::function<void(const T&)>; + + explicit PushStream(PromiseFactory& promiseFactory); + + Promise<void> forEach(ForEachFunction func); + + PushStream<T>& filter(PredicateFunction predicate); + + template<typename R> + PushStream<R>& map(std::function<R(const T&)>); + + std::vector<std::shared_ptr<PushStream<T>>> split(std::vector<PredicateFunction> predicates); + + PushStream<T>& onClose(CloseFunction closeFunction); + + PushStream<T>& onError(ErrorFunction errorFunction); + + void close() override; + + protected: + enum class State { + BUILDING, + STARTED, + CLOSED + }; + + virtual bool begin() = 0; + virtual void upstreamClose(const PushEvent<T>& event) = 0; + virtual long handleEvent(const PushEvent<T>& event); + + void close(const PushEvent<T>& event, bool sendDownStreamEvent); + bool internal_close(const PushEvent<T>& event, bool sendDownStreamEvent); + + bool compareAndSetState(State expectedValue, State newValue); + + State getAndSetState(State newValue); + + std::mutex mutex {}; + PromiseFactory& promiseFactory; + PushEventConsumer<T> nextEvent{}; + ErrorFunction onErrorCallback{}; + CloseFunction onCloseCallback{}; + State closed {State::BUILDING}; + private: + Deferred<void> streamEnd{promiseFactory.deferred<void>()}; + + template<typename, typename> friend class IntermediatePushStream; + template<typename> friend class UnbufferedPushStream; + template<typename> friend class PushStream; + template<typename> friend class StreamPushEventConsumer; + }; +} + +/********************************************************************************* + Implementation +*********************************************************************************/ + +#include "celix/impl/IntermediatePushStream.h" +#include "celix/impl/UnbufferedPushStream.h" +#include "celix/impl/BufferedPushStream.h" + +template<typename T> +celix::PushStream<T>::PushStream(PromiseFactory& _promiseFactory) : promiseFactory{_promiseFactory} { +} + +template<typename T> +long celix::PushStream<T>::handleEvent(const PushEvent<T>& event) { + if(closed != celix::PushStream<T>::State::CLOSED) { + return nextEvent.accept(event); + } + return IPushEventConsumer<T>::ABORT; +} + +template<typename T> +celix::Promise<void> celix::PushStream<T>::forEach(ForEachFunction func) { + nextEvent = PushEventConsumer<T>([func = std::move(func), this](const PushEvent<T>& event) -> long { + try { + switch(event.getType()) { + case celix::PushEvent<T>::EventType::DATA: + func(event.getData()); + return IPushEventConsumer<T>::CONTINUE; + case celix::PushEvent<T>::EventType::CLOSE: + streamEnd.resolve(); + break; + case celix::PushEvent<T>::EventType::ERROR: + streamEnd.fail(event.getFailure()); + break; + } + close(event, false); + return IPushEventConsumer<T>::ABORT; Review comment: In both cases communicate abort via backpressure. less than 0 means abort, 0 means continue, more than 0 means delay ms -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
