问题
I am trying to simulate 10 submodules. But omnet++ gives an error when the number of submodules are greater than five. here is the error:
(omnetpp::cMessage)sampleEvent: par(int): Has no parameter #4 -- in module (Sam1) Net.sampler1[4] (id=6), during network initialization
My .ned file
simple Sam1
{
parameters:
@display("i=block/routing");
gates:
inout gate[]; // declare two way connections
}
simple Svr1
{
parameters:
@display("i=block/process");
gates:
inout gate[]; // declare two way connections
}
network Net
{
@display("bgb=670.56,274.32");
types:
channel Ch extends ned.DelayChannel
{
delay = 100ns;
}
submodules:
sampler1[10]: Sam1 {
@display("p=334,127;is=l");
}
server: Svr1 {
parameters:
@display("i=,gold;p=109.21999,88.899994");
}
connections:
for i=0..9 {
server.gate++ <--> Ch <--> sampler1[i].gate++;
}
}
omnetpp.ini file
[General]
[Config Net]
network = Net
record-eventlog = true
sam1 class
#include <stdio.h>
#include <string.h>
#include <omnetpp.h>
using namespace omnetpp;
class Sam1 : public cSimpleModule
{
private:
simtime_t timeout; // timeout
cMessage *sampleEvent; // holds pointer to the self-message
int seq; // message sequ ence number
cMessage *message; // message that has to be re-sent on timeout
double vK;
double vM;
double threshold = 1.0;
int counter;
double xmean = 0.01;
double xsigma = 1;
double sensigma = 0.1;
double x;
double zOut = 0.0;
double previousSentTime = 0.0;
int samplerID;
double xSum;
long numSent;
long numReceived;
long totalEvents;
cLongHistogram SampleStats;
cOutVector SampleVector;
cLongHistogram interSampleStats;
cOutVector interSampleVector;
cLongHistogram sentSampleStats;
cOutVector sentSampleVector;
cOutVector constructedSignal;
public:
Sam1();
virtual ~Sam1();
protected:
virtual cMessage *generateNewMessage();
virtual void sampleAndSend(cMessage *msg);
virtual void initialize() override;
virtual void handleMessage(cMessage *msg) override;
// The finish() function is called by OMNeT++ at the end of the simulation:
virtual void finish();
};
Define_Module(Sam1);
Sam1::Sam1()
{
sampleEvent = message = nullptr;
}
Sam1::~Sam1()
{
cancelAndDelete(sampleEvent);
delete message;
}
void Sam1::initialize()
{
// Initialize variables.
vM = 0;
seq = 0;
timeout = 100.0; // in every 100 second
sampleEvent = new cMessage("sampleEvent");
xSum = 0.0;
counter = 1;
numSent = 0;
numReceived = 0;
totalEvents = 0;
WATCH(numSent);
WATCH(totalEvents);
samplerID = getIndex();
interSampleStats.setName("interSampleStats");
interSampleStats.setRangeAutoUpper(0, 10, 1.5);
interSampleVector.setName("interSample");
SampleStats.setName("SampleStats");
// interSampleStats.setRangeAutoUpper(0, 10, 1.5);
SampleVector.setName("Sample");
sentSampleStats.setName("sentSampleStats");
// interSampleStats.setRangeAutoUpper(0, 10, 1.5);
sentSampleVector.setName("sentSample");
constructedSignal.setName("constructedSignal");
// Generate and send initial message.
EV << "Sending initial message\n";
message = generateNewMessage();
sampleAndSend(message);
// scheduleAt(simTime()+timeout, sampleEvent);
}
void Sam1::handleMessage(cMessage *msg)
{
if (msg == sampleEvent) {
// EV << "sampling Again\n";
// Ready to send another one.
message = generateNewMessage();
sampleAndSend(message);
}
}
cMessage *Sam1::generateNewMessage()
{
// Generate a message
cMessage *msg = new cMessage("sampleEvent");
double t1 = simTime().dbl() * 1000;
msg->addPar("sampleValue");
x = normal(xmean, xsigma) + normal(xmean, sensigma);
xSum = x + xSum;
msg->par("sampleValue").setDoubleValue(xSum);
msg->addPar("counter");
msg->par("counter").setLongValue(counter);
msg->addPar("generatedTimestamp");
msg->par("generatedTimestamp").setDoubleValue(t1);
msg->addPar("samplerName");
msg->par(samplerID);
counter = counter +1;
totalEvents++;
return msg;
}
void Sam1::sampleAndSend(cMessage *msg)
{
double sampleValue = msg->par("sampleValue").doubleValue();
double genTimestamp = msg->par("generatedTimestamp").doubleValue();
cMessage *copy = (cMessage *)msg->dup();
vK = sampleValue-vM;
// EV << vK << " vK \n";
if(fabs(sampleValue-vM) > threshold){
vM = sampleValue;
zOut = sampleValue;
// In this example, we just pick a random gate to send it on.
// We draw a random number between 0 and the size of gate `out[]'.
int n = gateSize("gate");
int k = intuniform(0, n-1);
send(copy, "gate$o", k);
double t2 = simTime().dbl() * 1000; // sample time in milli seconds
double interSample = t2 - previousSentTime;
previousSentTime = genTimestamp;
numSent++;
interSampleVector.record(interSample);
interSampleStats.collect(interSample);
sentSampleVector.record(1);
sentSampleStats.collect(1);
}else{
zOut = vM;
sentSampleVector.record(0);
sentSampleStats.collect(0);
}
EV << "vM " << vM << endl;
// EV << "sampling Again. node " << simTime() << endl;
SampleVector.record(sampleValue);
SampleStats.collect(sampleValue);
constructedSignal.record(zOut);
scheduleAt(simTime()+timeout, sampleEvent);
}
void Sam1::finish(){
// This function is called by OMNeT++ at the end of the simulation.
EV << "Sent: " << numSent << endl;
EV << "Total Events: " << totalEvents << endl;
float rate = (float)numSent/totalEvents;
EV << "Traffic Reduction: " << rate << endl;
EV << "Threshold: " << threshold << endl;
EV << "Signal Mean: " << xmean << endl;
EV << "Signam sigma: " << xsigma << endl;
EV << "Sensor Sigma: " << sensigma << endl;
EV << "Inter Sample Time, min: " << interSampleStats.getMin() << endl;
EV << "Inter Sample Time, max: " << interSampleStats.getMax() << endl;
EV << "Inter Sample Time, mean: " << interSampleStats.getMean() << endl;
EV << "Inter Sample Time, stddev: " << interSampleStats.getStddev() << endl;
recordScalar("#sent", numSent);
recordScalar("#total", totalEvents);
interSampleStats.recordAs("inter sample");
SampleStats.recordAs("sample");
sentSampleStats.recordAs("sent sample");
}
/**
* Sends back an acknowledgement -- or not.
*/
class Svr1 : public cSimpleModule
{
protected:
virtual void handleMessage(cMessage *msg) override;
};
Define_Module(Svr1);
void Svr1::handleMessage(cMessage *msg)
{
double sampleValue = msg->par("sampleValue").doubleValue();
long counter = msg->par("counter").longValue();
long samplerID = msg->par("samplerName").longValue();
EV << sampleValue << " received, sending back an acknowledgment from sampler "<< samplerID << ". counter " << counter << "\n";
delete msg;
// In this example, we just pick a random gate to send it on.
// We draw a random number between 0 and the size of gate `out[]'.
int n = gateSize("gate");
int k = msg->getArrivalGate()->getIndex();
EV << "n: " << n << "k: " << k << endl;
// send(new cMessage("ack"), "gate$o", k);
}
来源:https://stackoverflow.com/questions/65261964/omnet-error-when-simulating-multiple-submodules