Hello Tommaso,
as a non-working example you can take my first message, and the corrected code is this:
struct HostEntry {
Ipv4Address ip;
Timer timerLifeHostEntry;
HostEntry(Ipv4Address ipAddr):
ip(ipAddr),
timerLifeHostEntry(Timer::CANCEL_ON_DESTROY)
{
}
};
std::list<HostEntry> m_hostsTable;
bool VirtualLayerPlusNetDevice::PutHost(Ipv4Address ip) {
for (std::list<HostEntry>::iterator i = m_hostsTable.begin(); i != m_hostsTable.end(); ++i) {
if (i->ip.IsEqual(ip)) {
NS_LOG_INFO(ip << " ya estaba en la HostTable, reiniciamos su timerLife...");
i->timerLifeHostEntry.Cancel();
i->timerLifeHostEntry.Schedule();
return false;
}
}
m_hostsTable.push_back(HostEntry(ip));
NS_LOG_INFO(ip << " added to the HostTable, there are " << m_hostsTable.size());
std::list<HostEntry>::iterator it = m_hostsTable.end();
it--;
NS_LOG_INFO("Configuring Timer of the Host " << it->ip);
it->timerLifeHostEntry.SetFunction(&VirtualLayerPlusNetDevice::TimerLifeHostEntryExpire, this);
it->timerLifeHostEntry.SetArguments(ip);
it->timerLifeHostEntry.SetDelay(TIME_LIFE_HOST_ENTRY);
it->timerLifeHostEntry.Schedule();
return true;
}
bool VirtualLayerPlusNetDevice::QuitHost(Ipv4Address ip) {
for (std::list<HostEntry>::iterator i = m_hostsTable.begin(); i != m_hostsTable.end(); ++i) {
if (i->ip.IsEqual(ip)) {
i->timerLifeHostEntry.Cancel();
i = m_hostsTable.erase(i);
NS_LOG_INFO(ip << " deleted from the HostTable, there are " << m_hostsTable.size() << "\n");
return true;
}
}
return false;
}
void VirtualLayerPlusNetDevice::TimerLifeHostEntryExpire(Ipv4Address ip) {
NS_LOG_INFO("Timer LifeHostEntry expired in the node " << m_node->GetId() << ", deleting host " << ip << " from the HostTable...");
QuitHost(ip);
}
I can send you the files with the complete code if you consider it necessary, but the percentage of code relevant to this issue is minimum. As you can see the principal change was adding a constructor to my struct in order to avoid invalid objects in the list when I add an element in a function (due to the fact that the elements declared in the function will be destructed when it ends). And now I only access to the objects of the list through iterator objects instead of access directly to the object through the back() function.
Regards
Víctor