include/UNetReceiver.h
00001 #ifndef UNetReceiver_H_
00002 #define UNetReceiver_H_
00003
00004 #include <ostream>
00005 #include <string>
00006 #include <queue>
00007 #include <cc++/socket.h>
00008 #include <sigc++/sigc++.h>
00009 #include "UniSetObject_LT.h"
00010 #include "Trigger.h"
00011 #include "Mutex.h"
00012 #include "SMInterface.h"
00013 #include "SharedMemory.h"
00014 #include "ThreadCreator.h"
00015 #include "UDPPacket.h"
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 class UNetReceiver
00051 {
00052 public:
00053 UNetReceiver( const std::string host, const ost::tpport_t port, SMInterface* smi );
00054 ~UNetReceiver();
00055
00056 void start();
00057 void stop();
00058
00059 void receive();
00060 void update();
00061
00062 inline std::string getName(){ return myname; }
00063
00064
00065 void setLockUpdate( bool st );
00066
00067 void resetTimeout();
00068
00069 inline bool isRecvOK(){ return !ptRecvTimeout.checkTime(); }
00070 inline unsigned long getLostPacketsNum(){ return lostPackets; }
00071
00072 void setReceiveTimeout( timeout_t msec );
00073 void setReceivePause( timeout_t msec );
00074 void setUpdatePause( timeout_t msec );
00075 void setLostTimeout( timeout_t msec );
00076 void setPrepareTime( timeout_t msec );
00077 void setMaxDifferens( unsigned long set );
00078
00079 void setRespondID( UniSetTypes::ObjectId id, bool invert=false );
00080 void setLostPacketsID( UniSetTypes::ObjectId id );
00081
00082 void setMaxProcessingCount( int set );
00083
00084 inline ost::IPV4Address getAddress(){ return addr; }
00085 inline ost::tpport_t getPort(){ return port; }
00086
00088 enum Event
00089 {
00090 evOK,
00091 evTimeout
00092 };
00093
00094 typedef sigc::slot<void,UNetReceiver*,Event> EventSlot;
00095 void connectEvent( EventSlot sl );
00096
00097 protected:
00098
00099 SMInterface* shm;
00100
00101 bool recv();
00102 void step();
00103 void real_update();
00104
00105 void initIterators();
00106
00107 private:
00108 UNetReceiver();
00109
00110 int recvpause;
00111 int updatepause;
00113 ost::UDPReceive* udp;
00114 ost::IPV4Address addr;
00115 ost::tpport_t port;
00116 std::string myname;
00117
00118 UniSetTypes::uniset_mutex pollMutex;
00119 PassiveTimer ptRecvTimeout;
00120 PassiveTimer ptPrepare;
00121 timeout_t recvTimeout;
00122 timeout_t prepareTime;
00123 timeout_t lostTimeout;
00124 PassiveTimer ptLostTimeout;
00125 unsigned long lostPackets;
00127 UniSetTypes::ObjectId sidRespond;
00128 IOController::DIOStateList::iterator ditRespond;
00129 bool respondInvert;
00130 UniSetTypes::ObjectId sidLostPackets;
00131 IOController::AIOStateList::iterator aitLostPackets;
00132
00133 bool activated;
00134
00135 ThreadCreator<UNetReceiver>* r_thr;
00136 ThreadCreator<UNetReceiver>* u_thr;
00137
00138
00139 struct PacketCompare:
00140 public std::binary_function<UniSetUDP::UDPMessage, UniSetUDP::UDPMessage, bool>
00141 {
00142 inline bool operator()(const UniSetUDP::UDPMessage& lhs,
00143 const UniSetUDP::UDPMessage& rhs) const
00144 { return lhs.num > rhs.num; }
00145 };
00146 typedef std::priority_queue<UniSetUDP::UDPMessage,std::vector<UniSetUDP::UDPMessage>,PacketCompare> PacketQueue;
00147 PacketQueue qpack;
00148 UniSetUDP::UDPMessage pack;
00149 UniSetUDP::UDPPacket r_buf;
00150 UniSetTypes::uniset_mutex packMutex;
00151 unsigned long pnum;
00156 unsigned long maxDifferens;
00157
00158 PacketQueue qtmp;
00159 bool waitClean;
00160 unsigned long rnum;
00162 int maxProcessingCount;
00164 bool lockUpdate;
00165 UniSetTypes::uniset_mutex lockMutex;
00166
00167 EventSlot slEvent;
00168 Trigger trTimeout;
00169 UniSetTypes::uniset_mutex tmMutex;
00170
00171 struct ItemInfo
00172 {
00173 long id;
00174 IOController::AIOStateList::iterator ait;
00175 IOController::DIOStateList::iterator dit;
00176 UniversalIO::IOTypes iotype;
00177
00178 ItemInfo():
00179 id(UniSetTypes::DefaultObjectId),
00180 iotype(UniversalIO::UnknownIOType){}
00181 };
00182
00183 typedef std::vector<ItemInfo> ItemVec;
00184 ItemVec d_icache;
00185 ItemVec a_icache;
00187 bool d_cache_init_ok;
00188 bool a_cache_init_ok;
00189
00190 void initDCache( UniSetUDP::UDPMessage& pack, bool force=false );
00191 void initACache( UniSetUDP::UDPMessage& pack, bool force=false );
00192 };
00193
00194 #endif // UNetReceiver_H_
00195