Alex Bikfalvi
SimStream Documentation
StreamPullReceiver.cpp
00001 #include "Headers.h" 00002 #include "StreamPullReceiver.h" 00003 #include "PacketStreamFrame.h" 00004 00005 CStreamPullReceiver::CStreamPullReceiver( 00006 CSimHandler* sim, 00007 CConnectionSender* connection, 00008 __time interval, 00009 __uint32 id, 00010 TReceiverList* list 00011 ) 00012 { 00013 // Input 00014 this->connection = connection; 00015 this->interval = interval; 00016 00017 // Timer 00018 this->timer = new CTimer<CStreamPullReceiver>(sim, this, &CStreamPullReceiver::Timer); 00019 00020 // List 00021 this->id = id; 00022 this->list = list; 00023 if(this->list) 00024 { 00025 assert(this->list->find(this->id) == this->list->end()); 00026 this->list->insert(pair<__uint32, CStreamPullReceiver*>(this->id, this)); 00027 } 00028 00029 // Add this as tag to the connection 00030 this->connection->Tag(this); 00031 } 00032 00033 CStreamPullReceiver::~CStreamPullReceiver() 00034 { 00035 // Timer 00036 delete this->timer; 00037 00038 // If a list is specified, remove the receiver from the list 00039 if(this->list) 00040 { 00041 assert(this->list->find(this->id) != this->list->end()); 00042 this->list->erase(this->id); 00043 } 00044 00045 // Remove this as tag from the connection 00046 assert(this->connection->Tag() == this); 00047 this->connection->Tag(NULL); 00048 } 00049 00050 void CStreamPullReceiver::Send(CStreamFrame frame) 00051 { 00052 // If the sending timer is not set 00053 if(!this->timer->IsSet()) 00054 { 00055 // Send the frame through the connection 00056 00057 // Create a stream packet 00058 CPacketStreamFrame* packet = new CPacketStreamFrame(frame); 00059 00060 this->connection->Send(packet); 00061 00062 // Set the timer to block the transmission 00063 this->timer->SetAfter(this->interval); 00064 } 00065 else 00066 { 00067 // Add the packet to the buffer 00068 this->buffer.push(frame); 00069 } 00070 } 00071 00072 void CStreamPullReceiver::Timer(CTimerInfo* info) 00073 { 00074 // If the buffer is not empty 00075 if(!this->buffer.empty()) 00076 { 00077 // Pop the next frame from the buffer 00078 CStreamFrame frame = this->buffer.front(); 00079 this->buffer.pop(); 00080 00081 // Create a stream packet 00082 CPacketStreamFrame* packet = new CPacketStreamFrame(frame); 00083 00084 this->connection->Send(packet); 00085 00086 // Set the timer to block the transmission 00087 this->timer->SetAfter(this->interval); 00088 } 00089 } 00090 00091 00092 void CStreamPullReceiver::List(TReceiverList* list) 00093 { 00094 // If there is an old list, remove the sender from the list 00095 if(this->list) 00096 { 00097 assert(this->list->find(this->id) != this->list->end()); 00098 this->list->erase(this->id); 00099 } 00100 00101 // Change the list 00102 this->list = list; 00103 00104 // If a list is specified, add the sender to the list 00105 if(this->list) 00106 { 00107 assert(this->list->find(this->id) == this->list->end()); 00108 this->list->insert(pair<__uint32, CStreamPullReceiver*>(this->id, this)); 00109 } 00110 }
Last updated: February 8, 2011