00001 /* 00002 * Copyright (c) 2017 Roboception GmbH 00003 * All rights reserved 00004 * 00005 * Author: Christian Emmerich 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions are met: 00009 * 00010 * 1. Redistributions of source code must retain the above copyright notice, 00011 * this list of conditions and the following disclaimer. 00012 * 00013 * 2. Redistributions in binary form must reproduce the above copyright notice, 00014 * this list of conditions and the following disclaimer in the documentation 00015 * and/or other materials provided with the distribution. 00016 * 00017 * 3. Neither the name of the copyright holder nor the names of its contributors 00018 * may be used to endorse or promote products derived from this software without 00019 * specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00022 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00023 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00024 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00025 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00026 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00027 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00028 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00029 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00030 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00031 * POSSIBILITY OF SUCH DAMAGE. 00032 */ 00033 00034 #ifndef RC_VISARD_DRIVER_THREADEDSTREAMER_H 00035 #define RC_VISARD_DRIVER_THREADEDSTREAMER_H 00036 00037 #include <memory> 00038 #include <thread> 00039 #include <atomic> 00040 00041 #include <rc_dynamics_api/remote_interface.h> 00042 #include <ros/ros.h> 00043 00044 00045 namespace rc 00046 { 00047 00061 class ThreadedStream 00062 { 00063 public: 00064 typedef std::shared_ptr<ThreadedStream> Ptr; 00065 00066 class Manager: public std::enable_shared_from_this<Manager> 00067 { 00068 public: 00069 typedef std::shared_ptr<Manager> Ptr; 00070 00071 static Ptr create(); 00072 00073 void add(ThreadedStream::Ptr stream); 00074 const std::list<ThreadedStream::Ptr>& get(); 00075 00076 void start_all(); 00077 void stop_all(); 00078 void join_all(); 00079 00080 bool all_succeeded() const; 00081 inline const std::atomic_bool& any_failed() const { return _any_failed; } 00082 00083 protected: 00084 Manager(); 00085 00086 std::atomic_bool _any_failed; 00087 std::list<ThreadedStream::Ptr> _streams; 00088 00089 friend ThreadedStream; 00090 }; 00091 00092 void start(); 00093 void stop(); 00094 void join(); 00095 00096 inline const std::string &name() const { return _stream; } 00097 inline const std::atomic_bool &requested() const { return _requested; } 00098 inline const std::atomic_bool &succeeded() const { return _success; } 00099 00100 protected: 00101 ThreadedStream(rc::dynamics::RemoteInterface::Ptr rcdIface, 00102 const std::string &stream, ros::NodeHandle &nh); 00103 00107 virtual bool startReceivingAndPublishingAsRos() = 0; 00108 00109 virtual void work(); 00110 00111 std::atomic_bool _stop, _requested, _success; 00112 00113 std::thread _thread; 00114 Manager::Ptr _manager; 00115 00116 rc::dynamics::RemoteInterface::Ptr _rcdyn; 00117 std::string _stream; 00118 ros::NodeHandle _nh; 00119 }; 00120 00121 } 00122 00123 00124 #endif //RC_VISARD_DRIVER_THREADEDSTREAMER_H