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 setMaxDifferens( unsigned long set );
00077
00078 void setRespondID( UniSetTypes::ObjectId id );
00079 void setLostPacketsID( UniSetTypes::ObjectId id );
00080
00081 void setMaxProcessingCount( int set );
00082
00083 inline ost::IPV4Address getAddress(){ return addr; }
00084 inline ost::tpport_t getPort(){ return port; }
00085
00087 enum Event
00088 {
00089 evOK,
00090 evTimeout
00091 };
00092
00093 typedef sigc::slot<void,UNetReceiver*,Event> EventSlot;
00094 void connectEvent( EventSlot sl );
00095
00096 protected:
00097
00098 SMInterface* shm;
00099
00100 bool recv();
00101 void step();
00102 void real_update();
00103
00104 void initIterators();
00105
00106 private:
00107 UNetReceiver();
00108
00109 int recvpause;
00110 int updatepause;
00112 ost::UDPReceive* udp;
00113 ost::IPV4Address addr;
00114 ost::tpport_t port;
00115 std::string myname;
00116
00117 UniSetTypes::uniset_mutex pollMutex;
00118 PassiveTimer ptRecvTimeout;
00119 timeout_t recvTimeout;
00120 timeout_t lostTimeout;
00121 PassiveTimer ptLostTimeout;
00122 unsigned long lostPackets;
00124 UniSetTypes::ObjectId sidRespond;
00125 IOController::DIOStateList::iterator ditRespond;
00126 UniSetTypes::ObjectId sidLostPackets;
00127 IOController::AIOStateList::iterator aitLostPackets;
00128
00129 bool activated;
00130
00131 ThreadCreator<UNetReceiver>* r_thr;
00132 ThreadCreator<UNetReceiver>* u_thr;
00133
00134
00135 struct PacketCompare:
00136 public std::binary_function<UniSetUDP::UDPMessage, UniSetUDP::UDPMessage, bool>
00137 {
00138 inline bool operator()(const UniSetUDP::UDPMessage& lhs,
00139 const UniSetUDP::UDPMessage& rhs) const
00140 { return lhs.num > rhs.num; }
00141 };
00142 typedef std::priority_queue<UniSetUDP::UDPMessage,std::vector<UniSetUDP::UDPMessage>,PacketCompare> PacketQueue;
00143 PacketQueue qpack;
00144 UniSetUDP::UDPMessage pack;
00145 UniSetUDP::UDPPacket r_buf;
00146 UniSetTypes::uniset_mutex packMutex;
00147 unsigned long pnum;
00152 unsigned long maxDifferens;
00153
00154 PacketQueue qtmp;
00155 bool waitClean;
00156 unsigned long rnum;
00158 int maxProcessingCount;
00160 bool lockUpdate;
00161 UniSetTypes::uniset_mutex lockMutex;
00162
00163 EventSlot slEvent;
00164 Trigger trTimeout;
00165 UniSetTypes::uniset_mutex tmMutex;
00166
00167 struct ItemInfo
00168 {
00169 long id;
00170 IOController::AIOStateList::iterator ait;
00171 IOController::DIOStateList::iterator dit;
00172 UniversalIO::IOTypes iotype;
00173
00174 ItemInfo():
00175 id(UniSetTypes::DefaultObjectId),
00176 iotype(UniversalIO::UnknownIOType){}
00177 };
00178
00179 typedef std::vector<ItemInfo> ItemVec;
00180 ItemVec d_icache;
00181 ItemVec a_icache;
00183 bool d_cache_init_ok;
00184 bool a_cache_init_ok;
00185
00186 void initDCache( UniSetUDP::UDPMessage& pack, bool force=false );
00187 void initACache( UniSetUDP::UDPMessage& pack, bool force=false );
00188 };
00189
00190 #endif // UNetReceiver_H_
00191