How to concatenate messages in omnet++

131 views
Skip to first unread message

Aditi Gupta

unread,
Mar 14, 2020, 10:31:41 AM3/14/20
to omn...@googlegroups.com
I am trying to concatenate messages coming from client node to server node in aggregation node.
But not able to do that properly.
on building the project I am getting many errors in aggregation.cc file.

Below are my three files:

client.cc
#include <stdio.h>
#include <string.h>
#include <omnetpp.h>


using namespace omnetpp;

//Client Module
class Client : public cSimpleModule {
protected:
   
virtual void initialize() override;
   
virtual void handleMessage(cMessage *msg) override;
   
virtual cMessage *generateNewMessage();
private:
   
int seq=0;
    cMessage
*message;

};


Define_Module(Client);

void Client :: initialize()
{
   
//scheduling first sending
    scheduleAt
(simTime(),new cMessage);
}

void Client :: handleMessage(cMessage *msg){
   
//generate and send message
    message
= generateNewMessage();
    send
(message,"out");
   
//schedule next call
    scheduleAt
(simTime()+exponential(1.0),msg);
}

cMessage
*Client::generateNewMessage()
{
   
// Generate a message with a different name every time.
   
char msgname[20];
    sprintf
(msgname, "msg-%d", ++seq);
    cMessage
*msg = new cMessage(msgname);
   
return msg;
}

aggregation.cc
#include <stdio.h>
#include <string.h>
#include <omnetpp.h>


using namespace omnetpp;


class Aggregator : public cSimpleModule{
protected:
   
virtual void initialize() override;
   
virtual void handleMessage(cMessage *msg) override;
   
virtual cMessage *concateMsg(cMessage *msg);
private:
    cMessage
*con_message;
    simtime_t timeout
;//timeout;
    cMessage
*timeoutEvent;//holds pointer to the timeout self message

};
Define_Module(Aggregator);

void Aggregator :: initialize()
{
    timeout
=5.0;
    timeoutEvent
=new cMessage("timeout");
   
//initialize the timer
    scheduleAt
(simTime()+timeout,timeoutEvent);
}
void Aggregator :: handleMessage(cMessage *msg)
{
   
if(msg==timeoutEvent)
   
{
       
//send the concatenated message to the server
        send
(con_message,"out");
   
}
   
else{
        con_message
=concateMsg(msg);
   
}
}

cMessage
*Aggregator::concateMsg(cMessage *msg)
{
   
// concatenation of messages
    cMessage
*s1 = new cMessage(s1);
    s1
=msg;
    msg
=msg+s1;
   
return msg;
}

server.c

#include <stdio.h>
#include <string.h>
#include <omnetpp.h>


using namespace omnetpp;



class Server : public cSimpleModule{

   
virtual void handleMessage(cMessage *msg) override;
};
Define_Module(Server);

void Server :: handleMessage(cMessage *msg)
{
    EV
<<"Message arrived at destination";
    bubble
("Arrived at Destination");
   
delete msg;
}

I am getting error in aggregation.cc

20:05:59 **** Incremental Build of configuration release for project Aggregation ****
make MODE=release all
cd src && /usr/bin/make
make[1]: Entering directory '/c/Users/ADITI/Documents/omnetpp-5.6.1/samples/my_projects/Aggregation/src'
aggregator.cc
aggregator.cc:50:24: error: no matching constructor for initialization of 'omnetpp::cMessage'
    cMessage *s1 = new cMessage(s1);
                       ^        ~~
C:/Users/ADITI/Documents/omnetpp-5.6.1/include/omnetpp/cmessage.h:172:5: note: candidate constructor not viable: no known conversion from 'omnetpp::cMessage *' to 'const omnetpp::cMessage' for 1st argument; dereference the argument with *
    cMessage(const cMessage& msg);
    ^
C:/Users/ADITI/Documents/omnetpp-5.6.1/include/omnetpp/cmessage.h:177:14: note: candidate constructor not viable: no known conversion from 'omnetpp::cMessage *' to 'const char *' for 1st argument
    explicit cMessage(const char *name=nullptr, short kind=0);
             ^
aggregator.cc:52:12: error: invalid operands to binary expression ('omnetpp::cMessage *' and 'omnetpp::cMessage *')
    msg=msg+s1;
        ~~~^~~
2 errors generated.
make[1]: *** [Makefile:104: ../out/clang-release/src/aggregator.o] Error 1
make[1]: Leaving directory '/c/Users/ADITI/Documents/omnetpp-5.6.1/samples/my_projects/Aggregation/src'
make: *** [Makefile:2: all] Error 2
"make MODE=release all" terminated with exit code 2. Build might be incomplete.

20:06:00 Build Failed. 4 errors, 0 warnings. (took 1s.546ms)



 

Alfonso Ariza Quintana

unread,
Mar 14, 2020, 12:41:13 PM3/14/20
to omn...@googlegroups.com
You cannot concatenate two messages like two strings, you can encapsulate two packets and there are similar to concatenate, bit both must be of the class cPacket

De: omn...@googlegroups.com <omn...@googlegroups.com> en nombre de Aditi Gupta <gupta8...@gmail.com>
Enviado: sábado, 14 de marzo de 2020 15:31
Para: OMNeT++ Users <omn...@googlegroups.com>
Asunto: [Omnetpp-l] How to concatenate messages in omnet++
 


cMessage
*Aggregator::concateMsg(msg)

{
   
// concatenation of messages
    cMessage
*s1 = new cMessage(s1);
    s1
=msg;
    msg
=msg+s1;
   
return msg;
}

server.c

#include <stdio.h>
#include <string.h>
#include <omnetpp.h>


using namespace omnetpp;



class Server : public cSimpleModule{

   
virtual void handleMessage(cMessage *msg) override;
};
Define_Module(Server);

void Server :: handleMessage(cMessage *msg)
{
    EV
<<"Message arrived at destination";
    bubble
("Arrived at Destination");
   
delete msg;
}

I am getting error in aggregation.cc

ake MODE=release all
cd src && /usr/bin/make
make[1]: Entering directory '/c/Users/ADITI/Documents/omnetpp-5.6.1/samples/my_projects/Aggregation/src'
aggregator.cc
aggregator.cc:47:34: error: use of undeclared identifier 'msg'; did you mean 'osg'?
cMessage *Aggregator::concateMsg(msg)
                                 ^~~
                                 osg
C:/Users/ADITI/Documents/omnetpp-5.6.1/include/omnetpp/cosgcanvas.h:23:11: note: 'osg' declared here
namespace osg { class Node;  }
          ^
aggregator.cc:47:34: error: unexpected namespace name 'osg': expected expression
cMessage *Aggregator::concateMsg(msg)
                                 ^
aggregator.cc:47:23: error: redefinition of 'concateMsg' as different kind of symbol
cMessage *Aggregator::concateMsg(msg)
                      ^
aggregator.cc:19:23: note: previous definition is here
    virtual cMessage *concateMsg(cMessage *msg);
                      ^
aggregator.cc:47:38: error: expected ';' after top level declarator
cMessage *Aggregator::concateMsg(msg)
                                     ^
                                     ;
4 errors generated.

make[1]: *** [Makefile:104: ../out/clang-release/src/aggregator.o] Error 1
make[1]: Leaving directory '/c/Users/ADITI/Documents/omnetpp-5.6.1/samples/my_projects/Aggregation/src'
make: *** [Makefile:2: all] Error 2
"make MODE=release all" terminated with exit code 2. Build might be incomplete.

19:36:52 Build Failed. 6 errors, 0 warnings. (took 3s.31ms)


 

--
You received this message because you are subscribed to the Google Groups "OMNeT++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to omnetpp+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/omnetpp/5067c7b2-6dc9-4b6c-8b9a-6c71f92fbedf%40googlegroups.com.
Message has been deleted

Aditi Gupta

unread,
Mar 16, 2020, 10:42:26 PM3/16/20
to OMNeT++ Users
With encapsulate() I can only encapsulate two packets.But I want to encapsulate multiple packets in a single packet.How can that be achieved please tell.
#include <stdio.h>
#include <string.h>
#include <omnetpp.h>


using namespace omnetpp;


class Aggregator : public cSimpleModule{
protected:
   
virtual void initialize() override;
   
virtual void handleMessage(cMessage *msg) override;

   
virtual void concateMsg(cPacket *msg);
private:
    cPacket
*con_message;
    simtime_t timeout
;//timeout;
    cPacket
*timeoutEvent;//holds pointer to the timeout self message
   
bool flag = true;

   
};
Define_Module(Aggregator);

void Aggregator :: initialize()
{
    timeout
=5.0;

    timeoutEvent
=new cPacket("timeout");

   
//initialize the timer
    scheduleAt
(simTime()+timeout,timeoutEvent);
}
void Aggregator :: handleMessage(cMessage *msg)
{
   
if(msg==timeoutEvent)
   
{
       
//send the concatenated message to the server
        send
(con_message,"out");

        flag
= true;
   
}
   
else{
        cPacket
*pkt = check_and_cast<cPacket *>(msg);
        concateMsg
(pkt);
   
}
}

void Aggregator::concateMsg(cPacket *msg)
{

   
// concatenation of messages
   
if (flag) {
        con_message
= msg->dup();
        flag
= false;
       
return;
   
}

    con_message
->encapsulate(msg);


}
To unsubscribe from this group and stop receiving emails from it, send an email to omn...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages