-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamTable.cpp
90 lines (77 loc) · 1.96 KB
/
CamTable.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* File: CamTable.cpp
* Author: xpalam00
*
* Created on 11. únor 2012, 21:15
*/
#include <map>
#include "CamTable.hpp"
CamTable::CamTable()
{
}
CamTable::~CamTable()
{
}
/**
* Pokusí se vyhledat cílovou MAC v CAM tabulce. Přestárlé záznamy smaže, čímž
* se vloží do tabulky záznam znovu.
* @param mac
* @return
*/
Iface *CamTable::find(MacAddress *mac)
{
map<string, CamTableRecord*>::iterator it = this->records.find(mac->getAddress());
// Hledaný cíl nebyl vůbec nalezen
if( it == this->records.end() ) return NULL;
// Nalezený záznam je přestárlý, smaže se a začne se hledat znovu
if( it->second->getAge() > CamTable::MAX_AGE )
{
this->records.erase(it);
return this->find(mac);
}
// Nalezený záznam je v pořádku a vrátí se nalezený Iface
return it->second->iface;
}
void CamTable::add(Iface *iface, MacAddress *mac)
{
CamTableRecord *ctr;
map<string, CamTableRecord*>::iterator it = this->records.find(mac->getAddress());
//cout << "Add to " << iface->getName() << " (" << iface->macAddress << ") new MAC " << mac << "?" << endl;
if(it == this->records.end()) // záznam není v tabulce
{
//cout << "- Create new CTR: ";
ctr = new CamTableRecord(iface, mac);
it = this->records.insert(it, make_pair(mac->getAddress(), ctr));
//cout << ctr << endl;
}
else
{
//cout << "- Already known, reset age: ";
ctr = it->second;
ctr->resetAge();
//cout << ctr << endl;
}
}
/**
* Vypíše CAM tabulku. Vypisuje pouze záznamy, které nepřekročily maximální dobu.
* @param stream
* @param cam
* @return
*/
ostream &operator<<(ostream &stream, const CamTable *cam)
{
stream << "MAC address\tPort\tAge" << endl;
map<string, CamTableRecord*>::const_iterator it = cam->records.begin();
for(it = cam->records.begin(); it != cam->records.end(); it++)
{
if(it->second->getAge() > CamTable::MAX_AGE)
{
//cam->records. erase(it);
}
else
{
stream << it->second << endl;
}
}
return stream;
}