Project

General

Profile

Statistics
| Branch: | Revision:

root / src / sim / cpacketqueue.cc @ 3e29b8a0

History | View | Annotate | Download (2.58 KB)

1
//=========================================================================
2
//  CPACKETQUEUE.CC - part of
3
//
4
//                  OMNeT++/OMNEST
5
//           Discrete System Simulation in C++
6
//
7
//  Author: Andras Varga
8
//
9
//=========================================================================
10

    
11
/*--------------------------------------------------------------*
12
  Copyright (C) 1992-2008 Andras Varga
13
  Copyright (C) 2006-2008 OpenSim Ltd.
14

15
  This file is distributed WITHOUT ANY WARRANTY. See the file
16
  `license' for details on this and other legal matters.
17
*--------------------------------------------------------------*/
18

    
19
#include <stdio.h>
20
#include <string.h>
21
#include <sstream>
22
#include "globals.h"
23
#include "cpacketqueue.h"
24
#include "cexception.h"
25

    
26
#ifdef WITH_PARSIM
27
#include "ccommbuffer.h"
28
#endif
29

    
30
USING_NAMESPACE
31

    
32
using std::ostream;
33

    
34

    
35
Register_Class(cPacketQueue);
36

    
37

    
38
cPacketQueue::cPacketQueue(const char *name, CompareFunc cmp) : cQueue(name,cmp)
39
{
40
    bitlength = 0;
41
}
42

    
43
cPacketQueue::cPacketQueue(const cPacketQueue& queue)
44
{
45
    setName(queue.getName());
46
    operator=(queue);
47
}
48

    
49
cPacketQueue& cPacketQueue::operator=(const cPacketQueue& queue)
50
{
51
    cQueue::operator=(queue);
52
    bitlength = queue.bitlength;
53
    return *this;
54
}
55

    
56
std::string cPacketQueue::info() const
57
{
58
    if (empty())
59
        return std::string("empty");
60
    std::stringstream out;
61
    out << "len=" << getLength() << ", " << getBitLength() << " bits (" << getByteLength() << " bytes)";
62
    return out.str();
63
}
64

    
65
void cPacketQueue::parsimPack(cCommBuffer *buffer)
66
{
67
#ifndef WITH_PARSIM
68
    throw cRuntimeError(this,eNOPARSIM);
69
#else
70
    cQueue::parsimPack(buffer);
71
    buffer->pack(bitlength);
72
#endif
73
}
74

    
75
void cPacketQueue::parsimUnpack(cCommBuffer *buffer)
76
{
77
#ifndef WITH_PARSIM
78
    throw cRuntimeError(this,eNOPARSIM);
79
#else
80
    cQueue::parsimUnpack(buffer);
81
    buffer->unpack(bitlength);
82
#endif
83
}
84

    
85
void cPacketQueue::addLen(cPacket *pkt)
86
{
87
    if (pkt)
88
        bitlength += pkt->getBitLength();
89
}
90

    
91
void cPacketQueue::insert(cPacket *pkt)
92
{
93
    addLen(pkt);
94
    cQueue::insert(pkt);
95
}
96

    
97
void cPacketQueue::insertBefore(cPacket *where, cPacket *pkt)
98
{
99
    addLen(pkt);
100
    cQueue::insertBefore(where, pkt);
101
}
102

    
103
void cPacketQueue::insertAfter(cPacket *where, cPacket *pkt)
104
{
105
    addLen(pkt);
106
    cQueue::insertAfter(where, pkt);
107
}
108

    
109
cPacket *cPacketQueue::remove(cPacket *pkt)
110
{
111
    cPacket *pkt1 = (cPacket *)cQueue::remove(pkt);
112
    if (pkt1)
113
        bitlength -= pkt1->getBitLength();
114
    return pkt1;
115
}
116

    
117
cPacket *cPacketQueue::pop()
118
{
119
    cPacket *pkt = (cPacket *)cQueue::pop();
120
    if (pkt)
121
        bitlength -= pkt->getBitLength();
122
    return pkt;
123
}
124