Project

General

Profile

Statistics
| Branch: | Revision:

root / src / scave / indexedvectorfile.h @ cbd2c699

History | View | Annotate | Download (6.95 KB)

1
//=========================================================================
2
//  INDEXEDVECTORFILE.H - part of
3
//                  OMNeT++/OMNEST
4
//           Discrete System Simulation in C++
5
//
6
//  Author: Tamas Borbely
7
//
8
//=========================================================================
9

    
10
/*--------------------------------------------------------------*
11
  Copyright (C) 2006-2008 OpenSim Ltd.
12

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

    
17
#ifndef _INDEXEDVECTORFILE_H_
18
#define _INDEXEDVECTORFILE_H_
19

    
20
#include <assert.h>
21
#include <float.h>
22
#include <vector>
23
#include <map>
24
#include <stdarg.h>
25
#include "filereader.h"
26
#include "scavedefs.h"
27
#include "indexfile.h"
28
#include "node.h"
29
#include "nodetype.h"
30
#include "resultfilemanager.h"
31

    
32
NAMESPACE_BEGIN
33

    
34
struct OutputVectorEntry {
35
    long serial;
36
    eventnumber_t eventNumber;
37
    simultime_t simtime;
38
    double value;
39

    
40
    OutputVectorEntry()
41
        : eventNumber(-1) {}
42
    OutputVectorEntry(long serial, eventnumber_t eventNumber, simultime_t simtime, double value)
43
        : serial(serial), eventNumber(eventNumber), simtime(simtime), value(value) {}
44
};
45

    
46
typedef std::vector<OutputVectorEntry> Entries;
47

    
48
/**
49
 * Vector file reader with random access.
50
 * Each instance reads one vector from a vector file.
51
 */
52
class SCAVE_API IndexedVectorFileReader
53
{
54
    std::string fname;  // file name of the vector file
55

    
56
    VectorFileIndex *index; // index of the vector file, loaded fully into the memory
57
    const VectorData *vector;     // index data of the read vector, points into index
58
    const Block *currentBlock;    // last loaded block, points into index
59
    Entries currentEntries; // entries of the loaded block
60

    
61
    public:
62
        explicit IndexedVectorFileReader(const char* filename, int vectorId);
63
        ~IndexedVectorFileReader();
64
    protected:
65
        /** reads a block from the vector file */
66
        void loadBlock(const Block &block);
67
    public:
68
        /**
69
         * Returns the number of entries in the vector.
70
         */
71
        int getNumberOfEntries() const { return vector->getCount(); };
72
        /**
73
         * Returns the entry with the specified serial,
74
         * or NULL if the serial is out of range.
75
         * The pointer will be valid until the next call to getEntryBySerial().
76
         */
77
        OutputVectorEntry *getEntryBySerial(long serial);
78

    
79
        /**
80
         * Returns the first entry whose simtime is >= than the given simtime (when after is true),
81
         * or the last entry whose simtime is <= than the given simtime (when after is false).
82
         * Returns NULL when no entry after/before.
83
         */
84
        OutputVectorEntry *getEntryBySimtime(simultime_t simtime, bool after);
85

    
86
        /**
87
         * Returns the first entry whose simtime is >= than the given simtime (when after is true),
88
         * or the last entry whose simtime is <= than the given simtime (when after is false).
89
         * Returns NULL when no entry after/before.
90
         */
91
        OutputVectorEntry *getEntryByEventnum(eventnumber_t eventNum, bool after);
92

    
93
        /**
94
         * Adds output vector entries in the [startTime,endTime] interval to
95
         * the specified vector. Returns the number of entries added.
96
         */
97
        long collectEntriesInSimtimeInterval(simultime_t startTime, simultime_t endTime, Entries &out);
98
        /**
99
         * Adds output vector entries in the [startTime,endTime] interval to
100
         * the specified vector. Returns the number of entries added.
101
         */
102
        long collectEntriesInEventnumInterval(eventnumber_t startEventNum, eventnumber_t endEventNum, Entries &out);
103
};
104

    
105
/**
106
 * Consumer node which writes an indexed output vector file.
107
 */
108
class SCAVE_API IndexedVectorFileWriterNode : public Node
109
{
110
    protected:
111
        class VectorInputPort : public Port {
112
            public:
113
            VectorData vector;
114
            char *buffer;     // buffer holding recorded data
115
            int bufferSize;  // size of the allocated buffer
116
            char *bufferPtr; // pointer to the current position in the buffer
117
            int bufferNumOfRecords; //
118
            bool finished;
119

    
120
            VectorInputPort(int id, std::string moduleName, std::string name, std::string columns, int bufferSize, Node *owner)
121
                : Port(owner), vector(id, moduleName, name, columns, bufferSize), finished(false)
122
                { this->allocateBuffer(bufferSize); vector.blocks.push_back(Block()); }
123
            ~VectorInputPort() { if (buffer) delete[] buffer; }
124

    
125
            void allocateBuffer(int size) { buffer=new char[size]; bufferSize=size; clearBuffer(); }
126
            void clearBuffer() { bufferPtr = buffer; bufferNumOfRecords=0; if (buffer) buffer[0] = '\0'; }
127
            bool hasBufferedData() const { return bufferPtr!=buffer; }
128
        };
129

    
130
        typedef std::vector<VectorInputPort*> PortVector;
131
        typedef std::map<std::string,std::string> StringMap;
132

    
133
    private:
134
        PortVector ports;
135
        std::string fileHeader;
136
        std::string fileName;
137
        std::string indexFileName;
138
        int blockSize;
139
        RunData run;
140
        FILE *f;
141
        IndexFileWriter *indexWriter;
142
        int prec;
143

    
144
    public:
145
        IndexedVectorFileWriterNode(const char *fileName, const char* indexFileName, int blockSize, const char *fileHeader=NULL);
146
        virtual ~IndexedVectorFileWriterNode();
147

    
148
        Port *addVector(int vectorId, const char *module, const char *name, const char *columns);
149
        Port *addVector(const VectorResult &vector);
150
        void setPrecision(int prec) {this->prec = prec;}
151
        void setHeader(const std::string &header) { fileHeader = header; }
152
        void setRun(const char *runName, const StringMap &attributes, const StringMap &parameters)
153
            { run.runName = runName; run.attributes = attributes; run.moduleParams = parameters; };
154
        std::string getFilename() const {return fileName;}
155

    
156
        virtual bool isReady() const;
157
        virtual void process();
158
        virtual bool isFinished() const;
159
    protected:
160
        void writeRecordsToBuffer(VectorInputPort *port);
161
        void writeBufferToFile(VectorInputPort *port);
162
        void writeIndex(VectorInputPort *port);
163
    private:
164
#ifndef SWIG
165
        // note: swig 1.3.33 cannot parse this (even though this class is ignored in scave.i)
166
        void bufferPrintf(VectorInputPort *port, const char *format...);
167
#endif
168
};
169

    
170
class SCAVE_API IndexedVectorFileWriterNodeType : public NodeType
171
{
172
    public:
173
        virtual const char *getName() const {return "indexedvectorfilewriter";}
174
        virtual const char *getCategory() const {return "multi-port sink";}
175
        virtual const char *getDescription() const;
176
        virtual bool isHidden() const {return true;}
177
        virtual void getAttributes(StringMap& attrs) const;
178
        virtual Node *create(DataflowManager *mgr, StringMap& attrs) const;
179
        virtual Port *getPort(Node *node, const char *portname) const;
180
};
181

    
182
NAMESPACE_END
183

    
184
#endif
185

    
186