stegemr commented on a change in pull request #363: URL: https://github.com/apache/celix/pull/363#discussion_r723901731
########## File path: libs/pushstreams/docs/pushstreams.adoc ########## @@ -0,0 +1,140 @@ += PushStreams Review comment: Added ########## File path: libs/pushstreams/CMakeLists.txt ########## @@ -0,0 +1,70 @@ +# 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. +if (NOT COMMAND celix_subproject) + #If COMMAND celix_subproject is not defined, this CMakeLists will + #act as a top level project. Making the Celix::CelixPushStreams useable + #stand-alone + + cmake_minimum_required (VERSION 3.11) + project(celix_PushStreams + VERSION 1.0.0 + LANGUAGES CXX + ) + + include(GNUInstallDirs) + + set(CMAKE_CXX_FLAGS "-std=c++17 ${CMAKE_CXX_FLAGS}") + set(CMAKE_CXX_FLAGS_DEBUG "-g -DDEBUG ${CMAKE_CXX_FLAGS_DEBUG}") + + set(PUSHSTREAMS_STANDALONE ON) +else () + set(PUSHSTREAMS_DEFAULT_ON ${CELIX_CXX}) + celix_subproject(PUSHSTREAMS "Option to build the PushStreams library" ${PUSHSTREAMS_DEFAULT_ON}) +endif () + +if (PUSHSTREAMS OR PUSHSTREAMS_STANDALONE) + find_package(Threads) + + add_library(PushStreams INTERFACE) + target_include_directories(PushStreams INTERFACE + $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/api> + $<INSTALL_INTERFACE:include/celix/pushstreams> + ) + target_link_libraries(PushStreams INTERFACE Threads::Threads) + target_link_libraries(PushStreams INTERFACE Celix::Promises) + add_library(Celix::PushStreams ALIAS PushStreams) + + add_executable(PushStreamExamples src/PushStreamExamples.cc) + target_compile_options(PushStreamExamples PRIVATE -std=c++17) + target_link_libraries(PushStreamExamples PRIVATE Celix::PushStreams) + + if (ENABLE_TESTING AND NOT PUSHSTREAMS_STANDALONE) + add_subdirectory(gtest) + endif() + + install(TARGETS PushStreams EXPORT celix DESTINATION ${CMAKE_INSTALL_LIBDIR}) + install(DIRECTORY api/ DESTINATION include/celix/pushstreams) + + if (PUSHSTREAMS_STANDALONE) Review comment: Removed support for standalone, as dependent on Celix::Promises ########## File path: libs/pushstreams/api/celix/PushEvent.h ########## @@ -0,0 +1,142 @@ +/** + *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 "celix/IllegalStateException.h" + +namespace celix { + template <typename T> + class PushEvent { + public: + virtual ~PushEvent() = default; + + enum class EventType { + DATA, + ERROR, + CLOSE + }; + + explicit PushEvent(EventType _type); Review comment: Done ########## 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 ########## File path: libs/pushstreams/api/celix/IPushEventConsumer.h ########## @@ -0,0 +1,35 @@ +/** + *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 "celix/PushEvent.h" + +namespace celix { + template <typename T> + class IPushEventConsumer { + public: + static constexpr int const& ABORT = -1; Review comment: Are backpressure values: Spec: less than 0 means abort, 0 means continue, more than 0 means delay ms ########## 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. Spec: less than 0 means abort, 0 means continue, more than 0 means delay ms ########## File path: libs/pushstreams/api/celix/impl/BufferedPushStream.h ########## @@ -0,0 +1,85 @@ +/** + *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 "celix/IPushEventSource.h" + +namespace celix { + + template<typename T> + class BufferedPushStream: public UnbufferedPushStream<T> { + public: + BufferedPushStream(PromiseFactory& _promiseFactory); + + protected: + long handleEvent(const PushEvent<T>& event) override; + + private: + void startWorker(); + std::unique_ptr<PushEvent<T>> popQueue(); + + std::queue<std::unique_ptr<PushEvent<T>>> queue{}; + std::mutex mutex{}; + int nrWorkers{0}; + }; +} + +/********************************************************************************* + Implementation +*********************************************************************************/ + +template<typename T> +celix::BufferedPushStream<T>::BufferedPushStream(PromiseFactory& _promiseFactory) : celix::UnbufferedPushStream<T>(_promiseFactory) { +} + +template<typename T> +long celix::BufferedPushStream<T>::handleEvent(const PushEvent<T>& event) { + std::unique_lock lk(mutex); + queue.push(std::move(event.clone())); + if (nrWorkers == 0) startWorker(); Review comment: {} done popQueue is invoked in other thread. deferred. -- 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: dev-unsubscr...@celix.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org