Last change
on this file since 174 was
174,
checked in by scheusso, 16 years ago
|
added boost:mutex to PacketBuffer in order to make it thread safe; adjuster PacketBufferTest
|
File size:
1.7 KB
|
Rev | Line | |
---|
[173] | 1 | // C++ PacketBuffer |
---|
| 2 | // |
---|
| 3 | // Author Oliver Scheuss |
---|
| 4 | #include <iostream> |
---|
| 5 | #include "network/PacketBuffer.h" |
---|
| 6 | |
---|
| 7 | namespace network{ |
---|
| 8 | |
---|
| 9 | PacketBuffer::PacketBuffer(){ |
---|
[174] | 10 | closed=false; |
---|
[173] | 11 | first=NULL; |
---|
| 12 | last=NULL; |
---|
| 13 | } |
---|
| 14 | |
---|
| 15 | bool PacketBuffer::push(PacketEnvelope pck){ |
---|
[174] | 16 | boost::mutex::scoped_lock lock(networkPacketBufferMutex); |
---|
| 17 | // also works if fifo is null (queue empty) |
---|
| 18 | // just to be sure last is really the last element |
---|
| 19 | while(last!=NULL && last->next!=NULL) |
---|
| 20 | last=last->next; |
---|
| 21 | // first element? |
---|
| 22 | if(first==NULL){ |
---|
| 23 | first=new QueueItem; |
---|
| 24 | last=first; |
---|
| 25 | last->next=NULL; |
---|
| 26 | // change this!!!!!!! |
---|
| 27 | last->packet = new PacketEnvelope; |
---|
| 28 | last->packet->data = pck.data; |
---|
| 29 | last->packet->length = pck.length; |
---|
| 30 | } else { |
---|
| 31 | //insert a new element at the bottom |
---|
| 32 | last->next = new QueueItem; |
---|
| 33 | last=last->next; |
---|
| 34 | // initialize last->next |
---|
| 35 | last->next=NULL; |
---|
| 36 | // save the packet to the new element |
---|
| 37 | // change this!!!!!!!! |
---|
| 38 | last->packet = new PacketEnvelope; |
---|
| 39 | last->packet->data = pck.data; |
---|
| 40 | last->packet->length = pck.length; |
---|
[173] | 41 | } |
---|
[174] | 42 | return true; |
---|
[173] | 43 | } |
---|
| 44 | |
---|
[174] | 45 | PacketEnvelope PacketBuffer::pop(){ |
---|
| 46 | boost::mutex::scoped_lock lock(networkPacketBufferMutex); |
---|
| 47 | if(first!=NULL){ |
---|
[173] | 48 | QueueItem *temp = first; |
---|
| 49 | // get packet |
---|
| 50 | PacketEnvelope *p = first->packet; |
---|
| 51 | // remove first element |
---|
| 52 | first = first->next; |
---|
| 53 | delete temp; |
---|
[174] | 54 | return *p; |
---|
| 55 | } else{ |
---|
| 56 | PacketEnvelope p = {0,0}; |
---|
[173] | 57 | return p; |
---|
[174] | 58 | } |
---|
[173] | 59 | } |
---|
| 60 | |
---|
| 61 | bool PacketBuffer::isEmpty(){ |
---|
| 62 | return (first==NULL); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | void PacketBuffer::print(){ |
---|
| 66 | QueueItem *temp=first; |
---|
| 67 | while(temp!=NULL){ |
---|
| 68 | std::cout << temp->packet->data << std::endl; |
---|
| 69 | temp=temp->next; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | } |
---|
| 73 | |
---|
[174] | 74 | bool PacketBuffer::isClosed(){ |
---|
| 75 | return closed; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | void PacketBuffer::setClosed(bool value){ |
---|
| 79 | closed=value; |
---|
| 80 | return; |
---|
| 81 | } |
---|
| 82 | |
---|
[173] | 83 | }// namespace network |
---|
Note: See
TracBrowser
for help on using the repository browser.