Project

General

Profile

Statistics
| Branch: | Revision:

root / src / scave / channel.h @ a3be1d55

History | View | Annotate | Download (3.17 KB)

1 01873262 Georg Kunz
//=========================================================================
2
//  CHANNEL.H - part of
3
//                  OMNeT++/OMNEST
4
//           Discrete System Simulation in C++
5
//
6
//  Author: Andras Varga
7
//
8
//=========================================================================
9
10
/*--------------------------------------------------------------*
11
  Copyright (C) 1992-2008 Andras Varga
12
  Copyright (C) 2006-2008 OpenSim Ltd.
13

14
  This file is distributed WITHOUT ANY WARRANTY. See the file
15
  `license' for details on this and other legal matters.
16
*--------------------------------------------------------------*/
17
18
#ifndef _CHANNEL_H_
19
#define _CHANNEL_H_
20
21
#include "commonutil.h"
22
#include <deque>
23
#include "node.h"
24
25
NAMESPACE_BEGIN
26
27
28
/**
29
 * Does buffering between two processing nodes (Node).
30
 *
31
 * @see Node, Port, Datum
32
 */
33
class SCAVE_API Channel
34
{
35
    private:
36
        // note: a Channel should *never* hold a pointer back to its Ports
37
        // because ports may be copied after having been assigned to channels
38
        // (e.g. in VectorFileReader which uses std::vector). Node ptrs are OK.
39
        std::deque<Datum> buffer;  //XXX deque has very poor performance under VC++ (pagesize==1!), consider using std::vector here instead
40
        Node *producernode;
41
        Node *consumernode;
42
        bool producerfinished;
43
        bool consumerfinished;
44
    public:
45
        Channel();
46
        ~Channel() {}
47
48
        void setProducerNode(Node *node) {producernode = node;}
49
        Node *getProducerNode() const {return producernode;}
50
51
        void setConsumerNode(Node *node) {consumernode = node;}
52
        Node *getConsumerNode() const {return consumernode;}
53
54
        /**
55
         * Returns ptr to the first buffered data item (next one to be read), or NULL
56
         */
57
        const Datum *peek() const;
58
59
        /**
60
         * Writes an array.
61
         */
62
        void write(Datum *a, int n);
63
64
        /**
65
         * Reads into an array. Returns number of items actually stored.
66
         */
67
        int read(Datum *a, int max);
68
69
        /**
70
         * Returns true if producer has already called close() which means
71
         * there will not be any more data except those already in the buffer
72
         */
73
        bool isClosing()  {return producerfinished;}
74
75
        /**
76
         * Returns true if close() has been called and there is no buffered data
77
         */
78
        bool eof()  {return producerfinished && length()==0;}
79
80
        /**
81
         * Called by the producer to declare it will not write any more --
82
         * if also there is no more buffered data (length()==0), that means EOF.
83
         */
84
        void close()  {producerfinished=true;}
85
86
        /**
87
         * Called when consumer has finished. Causes channel to ignore
88
         * further writes (discard any data written).
89
         */
90
        void consumerClose() {buffer.clear();consumerfinished=true;}
91
92
        /**
93
         * Returns true when the consumer has closed the channel, that is,
94
         * it will not read any more data from the channel.
95
         */
96
        bool isConsumerClosed() {return consumerfinished;}
97
98
        /**
99
         * Number of currently buffered items.
100
         */
101
        int length() {return buffer.size();}
102
};
103
104
NAMESPACE_END
105
106
107
#endif
108