00001 #ifdef TEST 00002 00003 #include "headers.h" 00004 00011 00012 PacketWriterTest::PacketWriterTest() { 00013 m_packetQueue = new ThreadMessageQueue<TpPacket>(); 00014 m_dropDataProbability = 0; 00015 m_dropAckProbability = 0; 00016 00017 pthread_attr_t attr; 00018 pthread_attr_init(&attr); 00019 00020 // start a thread that takes a packet off a queue and gives it to the SAR 00021 pthread_create(&m_dataPusherThreadId, &attr, PacketWriterTest::dataPusher, this); 00022 } // ctor 00023 00024 00025 PacketWriterTest::~PacketWriterTest() { 00026 // cancel the thread 00027 pthread_cancel(m_dataPusherThreadId); 00028 // add a packet to the queue to wake up the thread 00029 TpPacket p(0,0,0); 00030 m_packetQueue->add(&p); 00031 // wait for the thread to finish 00032 void* statusp; 00033 pthread_join(m_dataPusherThreadId, &statusp); 00034 } // dtor 00035 00036 00040 void* 00041 PacketWriterTest::dataPusher(void* arg) { 00042 PacketWriterTest* pwt = (PacketWriterTest*)arg; 00043 pwt->dataPusherImpl(); 00044 return NULL; 00045 } // fn dataPusher 00046 00047 00051 void 00052 PacketWriterTest::dataPusherImpl() { 00053 while (true) { 00054 pthread_testcancel(); 00055 TpPacket* p = m_packetQueue->getNext(); 00056 m_sar->receivePacket(p); 00057 } 00058 } // fn dataPusherImpl 00059 00060 00061 void 00062 PacketWriterTest::setDropDataProbability(int p) { 00063 m_dropDataProbability = p; 00064 } // fn setDropDataProbability 00065 00066 00067 void 00068 PacketWriterTest::setDropAckProbability(int p) { 00069 m_dropAckProbability = p; 00070 } // fn setDropAckProbability 00071 00072 00073 int 00074 PacketWriterTest::sendDataPacket(int serviceNumber, TpPacket* packet) { 00075 if (packet->isAck()) { 00076 if (m_dropAckProbability > m_randomNumberGenerator.IRandom(0, 99)) { 00077 // dont do anything with the packet, just drop it. 00078 return 0; 00079 } 00080 } 00081 else { 00082 if (m_dropDataProbability > m_randomNumberGenerator.IRandom(0, 99)) { 00083 return 0; 00084 } 00085 } 00086 00087 TpPacket* copy = new TpPacket(*packet); 00088 m_packetQueue->add(copy); 00089 return 0; 00090 } // fn sendDataPacket 00091 00092 00093 void 00094 PacketWriterTest::setSar(SAR* sar) { 00095 m_sar = sar; 00096 } // fn setSar 00097 00098 00099 #endif 00100