UniSet  2.2.1
UNetExchange.h
00001 #ifndef UNetExchange_H_
00002 #define UNetExchange_H_
00003 // -----------------------------------------------------------------------------
00004 #include <ostream>
00005 #include <string>
00006 #include <queue>
00007 #include <deque>
00008 #include <cc++/socket.h>
00009 #include "UniSetObject.h"
00010 #include "Trigger.h"
00011 #include "Mutex.h"
00012 #include "SMInterface.h"
00013 #include "SharedMemory.h"
00014 #include "ThreadCreator.h"
00015 #include "UNetReceiver.h"
00016 #include "UNetSender.h"
00017 #include "LogServer.h"
00018 #include "DebugStream.h"
00019 #include "UNetLogSugar.h"
00020 #include "LogAgregator.h"
00021 #include "VMonitor.h"
00022 // -----------------------------------------------------------------------------
00023 #ifndef vmonit
00024 #define vmonit( var ) vmon.add( #var, var )
00025 #endif
00026 // -----------------------------------------------------------------------------
00100 // -----------------------------------------------------------------------------
00101 class UNetExchange:
00102     public UniSetObject
00103 {
00104     public:
00105         UNetExchange( UniSetTypes::ObjectId objId, UniSetTypes::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = nullptr, const std::string& prefix = "unet" );
00106         virtual ~UNetExchange();
00107 
00109         static std::shared_ptr<UNetExchange> init_unetexchange( int argc, const char* const argv[],
00110                 UniSetTypes::ObjectId shmID, const std::shared_ptr<SharedMemory>& ic = 0, const std::string& prefix = "unet" );
00111 
00113         static void help_print( int argc, const char* argv[] );
00114 
00115         bool checkExistUNetHost( const std::string& host, ost::tpport_t port );
00116 
00117         inline std::shared_ptr<LogAgregator> getLogAggregator()
00118         {
00119             return loga;
00120         }
00121         inline std::shared_ptr<DebugStream> log()
00122         {
00123             return unetlog;
00124         }
00125 
00126         virtual UniSetTypes::SimpleInfo* getInfo( CORBA::Long userparam = 0 ) override;
00127 
00128     protected:
00129 
00130         xmlNode* cnode;
00131         std::string s_field;
00132         std::string s_fvalue;
00133 
00134         std::shared_ptr<SMInterface> shm;
00135         void step();
00136 
00137         void sysCommand( const UniSetTypes::SystemMessage* msg ) override;
00138         void sensorInfo( const UniSetTypes::SensorMessage* sm ) override;
00139         void timerInfo( const UniSetTypes::TimerMessage* tm ) override;
00140         void askSensors( UniversalIO::UIOCommand cmd );
00141         void waitSMReady();
00142         void receiverEvent( const std::shared_ptr<UNetReceiver>& r, UNetReceiver::Event ev );
00143 
00144         virtual bool activateObject();
00145 
00146         // действия при завершении работы
00147         virtual void sigterm( int signo );
00148 
00149         void initIterators();
00150         void startReceivers();
00151 
00152         enum Timer
00153         {
00154             tmStep
00155         };
00156 
00157     private:
00158         UNetExchange();
00159         timeout_t initPause;
00160         UniSetTypes::uniset_rwmutex mutex_start;
00161 
00162         PassiveTimer ptHeartBeat;
00163         UniSetTypes::ObjectId sidHeartBeat = { UniSetTypes::DefaultObjectId };
00164         timeout_t maxHeartBeat = 10;
00165         IOController::IOStateList::iterator itHeartBeat;
00166         UniSetTypes::ObjectId test_id = { UniSetTypes::DefaultObjectId };
00167 
00168         timeout_t steptime = { 1000 };    
00170         std::atomic_bool activated = { false };
00171         timeout_t activateTimeout = { 20000 }; // msec
00172 
00173         struct ReceiverInfo
00174         {
00175             ReceiverInfo(): r1(nullptr), r2(nullptr),
00176                 sidRespond(UniSetTypes::DefaultObjectId),
00177                 respondInvert(false),
00178                 sidLostPackets(UniSetTypes::DefaultObjectId),
00179                 sidChannelNum(UniSetTypes::DefaultObjectId)
00180             {}
00181 
00182             ReceiverInfo( const std::shared_ptr<UNetReceiver>& _r1, const std::shared_ptr<UNetReceiver>& _r2 ):
00183                 r1(_r1), r2(_r2),
00184                 sidRespond(UniSetTypes::DefaultObjectId),
00185                 respondInvert(false),
00186                 sidLostPackets(UniSetTypes::DefaultObjectId),
00187                 sidChannelNum(UniSetTypes::DefaultObjectId)
00188             {}
00189 
00190             std::shared_ptr<UNetReceiver> r1;    
00191             std::shared_ptr<UNetReceiver> r2;    
00193             void step(const std::shared_ptr<SMInterface>& shm, const std::string& myname, std::shared_ptr<DebugStream>& log );
00194 
00195             inline void setRespondID( UniSetTypes::ObjectId id, bool invert = false )
00196             {
00197                 sidRespond = id;
00198                 respondInvert = invert;
00199             }
00200             inline void setLostPacketsID( UniSetTypes::ObjectId id )
00201             {
00202                 sidLostPackets = id;
00203             }
00204             inline void setChannelNumID( UniSetTypes::ObjectId id )
00205             {
00206                 sidChannelNum = id;
00207             }
00208 
00209             inline void initIterators( const std::shared_ptr<SMInterface>& shm )
00210             {
00211                 shm->initIterator(itLostPackets);
00212                 shm->initIterator(itRespond);
00213                 shm->initIterator(itChannelNum);
00214             }
00215 
00216             // Сводная информация по двум каналам
00217             // сумма потерянных пакетов и наличие связи
00218             // хотя бы по одному каналу, номер рабочего канала
00219             // ( реализацию см. ReceiverInfo::step() )
00220             UniSetTypes::ObjectId sidRespond;
00221             IOController::IOStateList::iterator itRespond;
00222             bool respondInvert;
00223             UniSetTypes::ObjectId sidLostPackets;
00224             IOController::IOStateList::iterator itLostPackets;
00225             UniSetTypes::ObjectId sidChannelNum;
00226             IOController::IOStateList::iterator itChannelNum;
00227         };
00228 
00229         typedef std::deque<ReceiverInfo> ReceiverList;
00230         ReceiverList recvlist;
00231 
00232         bool no_sender;  
00233         std::shared_ptr<UNetSender> sender;
00234         std::shared_ptr<UNetSender> sender2;
00235 
00236         std::shared_ptr<LogAgregator> loga;
00237         std::shared_ptr<DebugStream> unetlog;
00238         std::shared_ptr<LogServer> logserv;
00239         std::string logserv_host = {""};
00240         int logserv_port = {0};
00241 
00242         VMonitor vmon;
00243 };
00244 // -----------------------------------------------------------------------------
00245 #endif // UNetExchange_H_
00246 // -----------------------------------------------------------------------------