But in void LeachRouting::fromMacLayer(cPacket *pkt, int macAddress, double rssi, double lqi)
"pkt" has been cast in to LeachRoutingPacket *netPacket before push_back into bufferAggregate?
Ok. I see.
I read about object slicing. "Slicing means that the data added by a subclass are discarded when an object of the subclass is passed or returned by value or from a function expecting a base class object. As baseclass copy functions don't know anything about the derived only the base part of the derived is copied."
Do you mean, the buffer Aggregate [i] has no data element (e.g. from encapsulated packet) because suffered by object slicing?
How to recover this problem. Because I want to access the element of data of the bufferAggregate[i]. I other method, element of bufferAggregate[i] has been assigned with data.
class ApplicationPacket : public ::cPacket {};
void LeachRouting::fromMacLayer(cPacket *pkt, int macAddress, double rssi, double lqi)
{
LeachRoutingPacket *netPacket = dynamic_cast <LeachRoutingPacket*>(pkt);
....
....
if (isCH && dst.compare(SELF_NETWORK_ADDRESS) == 0) {
// to access the encapsulated packet
cPacket *pkt1;
pkt1 = decapsulatePacket(netPacket);
ApplicationPacket *appkt = dynamic_cast <ApplicationPacket*> (pkt1)
// and then... success to get the data from the appkt.
bufferAggregate.push_back(*netPacket);
}
}
and then
in other method I want to access bufferAggregate[i] to get the data, like this code:
It would appear that decapsulatePacket returned nullptr;
perhaps (I'm guessing here) indicating that it was unable to de-encapsulate this particular packet.
class ApplicationPacket : public ::cPacket {};
void LeachRouting::fromMacLayer(cPacket *pkt, int macAddress, double rssi, double lqi)
{
LeachRoutingPacket *netPacket = dynamic_cast <LeachRoutingPacket*>(pkt);
....
....
if (isCH && dst.compare(SELF_NETWORK_ADDRESS) == 0) {
// to access the encapsulated packet
cPacket *pkt1;
pkt1 = decapsulatePacket(netPacket);
ApplicationPacket *appkt = dynamic_cast <ApplicationPacket*> (pkt1)
// and then... success to get the data from the appkt.
bufferAggregate.push_back(*netPacket);
}
}
after bufferAggregate.push_back(*netPacket), the encapsulated packet is missing out?? whether this is called by "ne555" as a slicing object?
class ApplicationPacket : public ::cPacket {};
void LeachRouting::fromMacLayer(cPacket *pkt, int macAddress, double rssi, double lqi)
{
LeachRoutingPacket *netPacket = dynamic_cast <LeachRoutingPacket*>(pkt);
....
....
if (isCH && dst.compare(SELF_NETWORK_ADDRESS) == 0) {
// to access the encapsulated packet
cPacket *pkt1;
pkt1 = decapsulatePacket(netPacket);
ApplicationPacket *appkt = dynamic_cast <ApplicationPacket*> (pkt1)
// and then... success to get the data from the appkt.
bufferAggregate.push_back(*netPacket);
// just for trace
for (int i=0; i < bufferAggregate.size(); i++) {
LeachRoutingPacket *lpkt = (&bufferAggregate[i]);
printf("has encapsulated packet %d ", lpkt->hasEncapsulatedPacket());
}
}
}
result: 1 for index=0; 0 for index: 1, 2, ... (waow... why??). Missing out in the bufferAggregate??
I think, we have add additional code, to encapsulate pkt into netPacket, before push_back into bufferAggregate, like this code below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
class ApplicationPacket : public ::cPacket {};
void LeachRouting::fromMacLayer(cPacket *pkt, int macAddress, double rssi, double lqi)
{
LeachRoutingPacket *netPacket = dynamic_cast <LeachRoutingPacket*>(pkt);
....
....
if (isCH && dst.compare(SELF_NETWORK_ADDRESS) == 0) {
LeachRoutingPacket *netPacketData = new LeachRoutingPacket();
// to access the encapsulated packet
cPacket *pkt1;
pkt1 = decapsulatePacket(netPacket);
ApplicationPacket *appkt = dynamic_cast <ApplicationPacket*> (pkt1)
// and then... success to get the data from the appkt.
encapsulatePacket(netPacketData, pkt1); //
bufferAggregate.push_back(*netPacketData);
}
}
void LeachRouting::fromMacLayer(cPacket *pkt, int macAddress, double rssi, double lqi)
{
// omit the const in the following line if the API does not permit it
const LeachRoutingPacket *netPacket = dynamic_cast < const LeachRoutingPacket* >(pkt);
if(netPacket) printf( "1. after dynamic cast, netPacket has encapsulated packet %d ", netPacket->hasEncapsulatedPacket() ) ;
....
....
if ( netPacket && isCH && dst.compare(SELF_NETWORK_ADDRESS) == 0) {
// to access the encapsulated packet
cPacket *pkt1;
pkt1 = decapsulatePacket(netPacket);
ApplicationPacket *appkt = dynamic_cast <ApplicationPacket*> (pkt1)
// and then... success to get the data from the appkt.
printf( "2. after get data from the appkt, netPacket has encapsulated packet %d ", netPacket->hasEncapsulatedPacket() ) ;
bufferAggregate.push_back(*netPacket);
printf( "3. after push_back, copy in the vector has encapsulated packet %d ", bufferAggregate.back().hasEncapsulatedPacket() ) ;
}
}
this code, changing *netPacket (remove the encapsulated packet).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void LeachRouting::fromMacLayer(cPacket *pkt, int macAddress, double rssi, double lqi)
{
....
...
if ( netPacket && isCH && dst.compare(SELF_NETWORK_ADDRESS) == 0) {
// I HAVE REMOVED THIS CODE and SOLVED
// to access the encapsulated packet
cPacket *pkt1;
pkt1 = decapsulatePacket(netPacket);
ApplicationPacket *appkt = dynamic_cast <ApplicationPacket*> (pkt1)
// and then... success to get the data from the appkt.
}
}
Thank you so much JLBorges (10026), you make me, found my wrong :) :)