%description: Tests cGate::connectTo() and disconnect(). %file: test.ned simple Sender { gates: input in; output out; } simple Receiver { gates: input in; output out; } network Test { submodules: theSender : Sender; theReceiver : Receiver; connections: theSender.out --> theReceiver.in; theSender.in <-- theReceiver.out; } %file: test.cc #include using namespace omnetpp; namespace @TESTNAME@ { class Sender : public cSimpleModule { public: Sender() : cSimpleModule(32768) { } cChannel *createChannel(double delay); virtual void activity() override; }; Define_Module(Sender); #define PRINT(g) EV << #g << ":" << ((g) ? (g)->getFullName() : "nullptr") << endl cChannel *Sender::createChannel(double delay) { cDatarateChannel *ch = cDatarateChannel::create("chan"); ch->setDelay(delay); return ch; } void Sender::activity() { cGate *out = gate("out"); cGate *rcvin = gate("out")->getNextGate(); cChannel *ch; // start clean out->disconnect(); // connect, then check if OK ch = createChannel(0.5); out->connectTo(rcvin, ch); PRINT(out->getChannel()); // even send message to verify cMessage *msg = new cMessage("hello1"); send(msg, "out"); wait(1); // disconnect, then check if OK out->disconnect(); PRINT(out->getChannel()); // set channel, then reconnect ch = createChannel(0.25); out->connectTo(rcvin, ch); PRINT(out->getChannel()); // send to verify cMessage *msg2 = new cMessage("hello2"); send(msg2, "out"); wait(1); // change channel ch = createChannel(0.125); out->reconnectWith(ch); // send to verify cMessage *msg3 = new cMessage("hello3"); send(msg3, "out"); wait(1); // remove channel out->reconnectWith(nullptr); PRINT(out->getChannel()); EV << "done\n"; } class Receiver : public cSimpleModule { public: Receiver() : cSimpleModule(32768) { } virtual void activity() override; }; Define_Module(Receiver); void Receiver::activity() { while (true) { cMessage *msg = receive(); EV << msg->getName() << " arrived at " << simTime() << endl; delete msg; } } }; //namespace %contains-regex: stdout out->getChannel\(\):chan .* hello1 arrived at 0.5 .* out->getChannel\(\):nullptr .* out->getChannel\(\):chan .* hello2 arrived at 1.25 .* hello3 arrived at 2.125 .* out->getChannel\(\):nullptr done