Project

General

Profile

Statistics
| Branch: | Revision:

root / src / envir / resultlistener.cc @ 7c7d95e5

History | View | Annotate | Download (4.71 KB)

1
//==========================================================================
2
//  RESULTLISTENER.CC - 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
#include "resultlistener.h"
19
#include "resultfilters.h"
20
#include "ccomponent.h"
21
#include "ctimestampedvalue.h"
22

    
23

    
24
void ResultListener::subscribedTo(ResultFilter *prev)
25
{
26
    ASSERT(getSubscribeCount() == 1);  // except for multi-signal ExpressionRecorders
27
}
28

    
29
void ResultListener::unsubscribedFrom(ResultFilter *prev)
30
{
31
    if (getSubscribeCount() == 0)
32
        delete this;
33
}
34

    
35
// original cIListener API that delegates to the simplified API:
36

    
37
#define THROW(source, signalID, datatype, e) \
38
    throw cRuntimeError("Error while processing statistic signal %s (id=%d) from (%s)%s, data type %s: %s", \
39
                        cComponent::getSignalName(signalID), (int)signalID, \
40
                        source->getClassName(), source->getFullPath().c_str(), datatype, e.what())
41

    
42
void ResultListener::receiveSignal(cComponent *source, simsignal_t signalID, long l)
43
{
44
    try
45
    {
46
        receiveSignal(NULL, simulation.getSimTime(), l);
47
    }
48
    catch (std::exception& e)
49
    {
50
        THROW(source, signalID, "long", e);
51
    }
52
}
53

    
54
void ResultListener::receiveSignal(cComponent *source, simsignal_t signalID, unsigned long l)
55
{
56
    try
57
    {
58
        receiveSignal(NULL, simulation.getSimTime(), l);
59
    }
60
    catch (std::exception& e)
61
    {
62
        THROW(source, signalID, "unsigned long", e);
63
    }
64
}
65

    
66
void ResultListener::receiveSignal(cComponent *source, simsignal_t signalID, double d)
67
{
68
    try
69
    {
70
        receiveSignal(NULL, simulation.getSimTime(), d);
71
    }
72
    catch (std::exception& e)
73
    {
74
        THROW(source, signalID, "double", e);
75
    }
76
}
77

    
78
void ResultListener::receiveSignal(cComponent *source, simsignal_t signalID, const SimTime& v)
79
{
80
    try
81
    {
82
        receiveSignal(NULL, simulation.getSimTime(), v);
83
    }
84
    catch (std::exception& e)
85
    {
86
        THROW(source, signalID, "simtime_t", e);
87
    }
88
}
89

    
90
void ResultListener::receiveSignal(cComponent *source, simsignal_t signalID, const char *s)
91
{
92
    try
93
    {
94
        receiveSignal(NULL, simulation.getSimTime(), s);
95
    }
96
    catch (std::exception& e)
97
    {
98
        THROW(source, signalID, "const char *", e);
99
    }
100
}
101

    
102
void ResultListener::receiveSignal(cComponent *source, simsignal_t signalID, cObject *obj)
103
{
104
    try
105
    {
106
        cITimestampedValue *v = dynamic_cast<cITimestampedValue *>(obj);
107
        if (!v)
108
            receiveSignal(NULL, simulation.getSimTime(), obj);
109
        else
110
        {
111
            // dispatch cITimestampedValue by data type
112
            switch (v->getValueType(signalID))
113
            {
114
                case cITimestampedValue::LONG: receiveSignal(NULL, v->getTimestamp(signalID), v->longValue(signalID)); break;
115
                case cITimestampedValue::ULONG: receiveSignal(NULL, v->getTimestamp(signalID), v->unsignedLongValue(signalID)); break;
116
                case cITimestampedValue::DOUBLE: receiveSignal(NULL, v->getTimestamp(signalID), v->doubleValue(signalID)); break;
117
                case cITimestampedValue::SIMTIME: receiveSignal(NULL, v->getTimestamp(signalID), v->simtimeValue(signalID)); break;
118
                case cITimestampedValue::STRING: receiveSignal(NULL, v->getTimestamp(signalID), v->stringValue(signalID)); break;
119
                case cITimestampedValue::OBJECT: receiveSignal(NULL, v->getTimestamp(signalID), v->objectValue(signalID)); break;
120
                default: throw opp_runtime_error("got cITimestampedValue with blank or invalid data type");
121
            }
122
        }
123
    }
124
    catch (std::exception& e)
125
    {
126
        THROW(source, signalID, (obj ? obj->getClassName() : "cObject* (NULL)"), e);
127
    }
128
}
129

    
130
#undef THROW
131

    
132
void ResultListener::subscribedTo(cComponent *component, simsignal_t signalID)
133
{
134
    subscribedTo(NULL);
135
}
136

    
137
void ResultListener::unsubscribedFrom(cComponent *component, simsignal_t signalID)
138
{
139
    unsubscribedFrom(NULL);
140
}
141

    
142
void ResultListener::finish(cComponent *component, simsignal_t signalID)
143
{
144
    finish(NULL);
145
}
146

    
147
//---
148

    
149
void SignalSource::subscribe(ResultListener *listener) const
150
{
151
    if (filter)
152
        filter->addDelegate(listener);
153
    else if (component && signalID!=SIMSIGNAL_NULL)
154
        component->subscribe(signalID, listener);
155
    else
156
        throw opp_runtime_error("subscribe() called on blank SignalSource");
157
}
158