Switch to unified view

a b/src/policies/DIF/RA/PDUFG/BiDomainGenerator/BiDomainGenerator.cc
1
//
2
// This program is free software: you can redistribute it and/or modify
3
// it under the terms of the GNU Lesser General Public License as published by
4
// the Free Software Foundation, either version 3 of the License, or
5
// (at your option) any later version.
6
// 
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
// GNU Lesser General Public License for more details.
11
// 
12
// You should have received a copy of the GNU Lesser General Public License
13
// along with this program.  If not, see http://www.gnu.org/licenses/.
14
// 
15
16
#include <BiDomainGenerator/BiDomainGenerator.h>
17
#include "APN.h"
18
#include <Utils.h>
19
20
21
22
namespace BiDomainGenerator {
23
24
Register_Class(BiDomainGenerator);
25
26
using namespace std;
27
28
// A new flow has been inserted/or removed
29
void BiDomainGenerator::insertedFlow(const Address &addr, const unsigned short &qos, RMTPort * port){
30
    std::string dst = addr.getIpcAddress().getName();
31
    neighbours[dst].insert(port);
32
    if(neighbours[dst].size() == 1){
33
        pAddr parsedA = parseAddr(dst);
34
        if(parsedA.domain != ""){
35
            rt->addFlow(addr, "", parsedA.domain, 1);
36
            rt->addFlow(addr, parsedA.domain, parsedA.addr,1);
37
        } else {
38
            rt->addFlow(addr, parsedA.domain, parsedA.addr,1);
39
        }
40
41
        routingUpdated();
42
    }
43
}
44
void BiDomainGenerator::removedFlow(const Address &addr, const unsigned short &qos, RMTPort * port){
45
    std::string dst = addr.getIpcAddress().getName();
46
    neighbours[dst].erase(port);
47
    if(neighbours[dst].size() <= 0){
48
        pAddr parsedA = parseAddr(dst);
49
50
        if(parsedA.domain != ""){
51
            rt->removeFlow(addr, "", parsedA.domain);
52
            rt->removeFlow(addr, parsedA.domain, parsedA.addr);
53
        } else {
54
            rt->removeFlow(addr, parsedA.domain, parsedA.addr);
55
        }
56
57
        neighbours.erase(dst);
58
59
        routingUpdated();
60
    }
61
}
62
63
//Routing has processes a routing update
64
void BiDomainGenerator::routingUpdated(){
65
    DMRnms::dmUpdateM changes = rt->getChanges();
66
    for(DMRnms::dmUpdateMIt it = changes.begin(); it!= changes.end(); it++){
67
        for(DMRnms::s2AIt eIt = it->entries.begin(); eIt != it->entries.end(); eIt++){
68
            std::string dst = eIt->first;
69
            std::string nextHop = eIt->second.getIpcAddress().getName();
70
71
            EV << "Entry ::: \""<< it->domain << "\"/\""<< dst << "\" -> " << nextHop << " ("<< eIt->second<<")" <<endl;
72
            RMTPort * p = NULL;
73
74
            NTableIt n = neighbours.find(nextHop);
75
            if(nextHop != "" && n != neighbours.end()){
76
                p = *(n->second.begin());
77
            }
78
79
            if(p == NULL) {
80
                fwd->remove(it->domain, dst);
81
            } else {
82
                fwd->insert(it->domain, dst, p);
83
            }
84
        }
85
    }
86
}
87
88
// Called after initialize
89
void BiDomainGenerator::onPolicyInit(){
90
    //Set Forwarding policy
91
    fwd = check_and_cast<DomainTable::DomainTable *>
92
        (getModuleByPath("^.^.relayAndMux.pduForwardingPolicy"));
93
94
    rt = check_and_cast<DMRnms::Routing *>
95
        (getModuleByPath("^.^.routingPolicy"));
96
97
    string myAddr = getParentModule()->getParentModule()->par("ipcAddress").stringValue();
98
99
    vector<string> parsStr = split(myAddr, '.');
100
    if(parsStr.size() != 2) {
101
        error("BiDomainGenerator own address must be on the form A.B");
102
    }
103
    myPrefix = parsStr[0];
104
    mySufix = parsStr[1];
105
106
    string alg0 = par("alg0").stdstringValue();
107
    string alg1 = par("alg1").stdstringValue();
108
109
    fwd->addDomain(myPrefix);
110
    fwd->addDomain("");
111
112
    if(alg0 == "LS"){
113
        rt->addDomain("", myPrefix, DMRnms::LS);
114
    } else {
115
        rt->addDomain("", myPrefix, DMRnms::DV);
116
    }
117
118
    if(alg1 == "LS"){
119
        rt->addDomain(myPrefix, mySufix, DMRnms::LS);
120
    } else {
121
        rt->addDomain(myPrefix, mySufix, DMRnms::DV);
122
    }
123
124
125
    difA = check_and_cast<DA *>(getModuleByPath("^.^.^.difAllocator.da"));
126
}
127
128
pAddr BiDomainGenerator::parseAddr(const string &addr){
129
    vector<string> sep = split(addr, '.');
130
    if(isPrefix(myPrefix, addr)){
131
        if(sep.size()>=2) {
132
            return pAddr(myPrefix, sep[1]);
133
        } else {
134
            return pAddr(myPrefix, "");
135
        }
136
    } else {
137
        return pAddr("", sep[0]);
138
    }
139
}
140
141
}