UniSet  2.6.0
UNetExchange.h
00001 /*
00002  * Copyright (c) 2015 Pavel Vainerman.
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU Lesser General Public License as
00006  * published by the Free Software Foundation, version 2.1.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00011  * Lesser General Lesser Public License for more details.
00012  *
00013  * You should have received a copy of the GNU Lesser General Public License
00014  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00015  */
00016 // -----------------------------------------------------------------------------
00017 #ifndef UNetExchange_H_
00018 #define UNetExchange_H_
00019 // -----------------------------------------------------------------------------
00020 #include <ostream>
00021 #include <string>
00022 #include <queue>
00023 #include <deque>
00024 #include "UniSetObject.h"
00025 #include "Trigger.h"
00026 #include "Mutex.h"
00027 #include "SMInterface.h"
00028 #include "SharedMemory.h"
00029 #include "ThreadCreator.h"
00030 #include "UNetReceiver.h"
00031 #include "UNetSender.h"
00032 #include "LogServer.h"
00033 #include "DebugStream.h"
00034 #include "UNetLogSugar.h"
00035 #include "LogAgregator.h"
00036 #include "VMonitor.h"
00037 // -----------------------------------------------------------------------------
00038 #ifndef vmonit
00039 #define vmonit( var ) vmon.add( #var, var )
00040 #endif
00041 // --------------------------------------------------------------------------
00042 namespace uniset
00043 {
00044 // -----------------------------------------------------------------------------
00124 // -----------------------------------------------------------------------------
00125 class UNetExchange:
00126     public UniSetObject
00127 {
00128     public:
00129         UNetExchange( uniset::ObjectId objId, uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr, const std::string& prefix = "unet" );
00130         virtual ~UNetExchange();
00131 
00133         static std::shared_ptr<UNetExchange> init_unetexchange( int argc, const char* const argv[],
00134                 uniset::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = 0, const std::string& prefix = "unet" );
00135 
00137         static void help_print( int argc, const char* argv[] ) noexcept;
00138 
00139         bool checkExistUNetHost( const std::string& host, int port ) noexcept;
00140 
00141         inline std::shared_ptr<LogAgregator> getLogAggregator() noexcept
00142         {
00143             return loga;
00144         }
00145         inline std::shared_ptr<DebugStream> log() noexcept
00146         {
00147             return unetlog;
00148         }
00149 
00150         virtual uniset::SimpleInfo* getInfo( const char* userparam = 0 ) override;
00151 
00152     protected:
00153 
00154         xmlNode* cnode;
00155         std::string s_field;
00156         std::string s_fvalue;
00157 
00158         std::shared_ptr<SMInterface> shm;
00159         void step() noexcept;
00160 
00161         void sysCommand( const uniset::SystemMessage* msg ) override;
00162         void sensorInfo( const uniset::SensorMessage* sm ) override;
00163         void timerInfo( const uniset::TimerMessage* tm ) override;
00164         void askSensors( UniversalIO::UIOCommand cmd );
00165         void waitSMReady();
00166         void receiverEvent( const std::shared_ptr<UNetReceiver>& r, UNetReceiver::Event ev ) noexcept;
00167 
00168         virtual bool activateObject() override;
00169         virtual bool deactivateObject() override;
00170 
00171         // действия при завершении работы
00172         virtual void sigterm( int signo );
00173         void termSenders();
00174         void termReceivers();
00175 
00176         void initIterators() noexcept;
00177         void startReceivers();
00178 
00179         enum Timer
00180         {
00181             tmStep
00182         };
00183 
00184     private:
00185         UNetExchange();
00186         timeout_t initPause = { 0 };
00187         uniset::uniset_rwmutex mutex_start;
00188 
00189         PassiveTimer ptHeartBeat;
00190         uniset::ObjectId sidHeartBeat = { uniset::DefaultObjectId };
00191         timeout_t maxHeartBeat = { 10 };
00192         IOController::IOStateList::iterator itHeartBeat;
00193         uniset::ObjectId test_id = { uniset::DefaultObjectId };
00194 
00195         timeout_t steptime = { 1000 };    
00197         std::atomic_bool activated = { false };
00198         timeout_t activateTimeout = { 20000 }; // msec
00199 
00200         struct ReceiverInfo
00201         {
00202             ReceiverInfo() noexcept: r1(nullptr), r2(nullptr),
00203                 sidRespond(uniset::DefaultObjectId),
00204                 respondInvert(false),
00205                 sidLostPackets(uniset::DefaultObjectId),
00206                 sidChannelNum(uniset::DefaultObjectId)
00207             {}
00208 
00209             ReceiverInfo( const std::shared_ptr<UNetReceiver>& _r1, const std::shared_ptr<UNetReceiver>& _r2 ) noexcept:
00210                 r1(_r1), r2(_r2),
00211                 sidRespond(uniset::DefaultObjectId),
00212                 respondInvert(false),
00213                 sidLostPackets(uniset::DefaultObjectId),
00214                 sidChannelNum(uniset::DefaultObjectId)
00215             {}
00216 
00217             std::shared_ptr<UNetReceiver> r1;    
00218             std::shared_ptr<UNetReceiver> r2;    
00220             void step(const std::shared_ptr<SMInterface>& shm, const std::string& myname, std::shared_ptr<DebugStream>& log ) noexcept;
00221 
00222             inline void setRespondID( uniset::ObjectId id, bool invert = false ) noexcept
00223             {
00224                 sidRespond = id;
00225                 respondInvert = invert;
00226             }
00227             inline void setLostPacketsID( uniset::ObjectId id ) noexcept
00228             {
00229                 sidLostPackets = id;
00230             }
00231             inline void setChannelNumID( uniset::ObjectId id ) noexcept
00232             {
00233                 sidChannelNum = id;
00234             }
00235 
00236             inline void initIterators( const std::shared_ptr<SMInterface>& shm ) noexcept
00237             {
00238                 shm->initIterator(itLostPackets);
00239                 shm->initIterator(itRespond);
00240                 shm->initIterator(itChannelNum);
00241             }
00242 
00243             // Сводная информация по двум каналам
00244             // сумма потерянных пакетов и наличие связи
00245             // хотя бы по одному каналу, номер рабочего канала
00246             // ( реализацию см. ReceiverInfo::step() )
00247             uniset::ObjectId sidRespond;
00248             IOController::IOStateList::iterator itRespond;
00249             bool respondInvert = { false };
00250             uniset::ObjectId sidLostPackets;
00251             IOController::IOStateList::iterator itLostPackets;
00252             uniset::ObjectId sidChannelNum;
00253             IOController::IOStateList::iterator itChannelNum;
00254         };
00255 
00256         typedef std::deque<ReceiverInfo> ReceiverList;
00257         ReceiverList recvlist;
00258 
00259         bool no_sender = { false };  
00260         std::shared_ptr<UNetSender> sender;
00261         std::shared_ptr<UNetSender> sender2;
00262 
00263         std::shared_ptr<LogAgregator> loga;
00264         std::shared_ptr<DebugStream> unetlog;
00265         std::shared_ptr<LogServer> logserv;
00266         std::string logserv_host = {""};
00267         int logserv_port = {0};
00268 
00269         VMonitor vmon;
00270 };
00271 // --------------------------------------------------------------------------
00272 } // end of namespace uniset
00273 // -----------------------------------------------------------------------------
00274 #endif // UNetExchange_H_
00275 // -----------------------------------------------------------------------------