NetworkLayer没有子类;
NetworkLayer作为DTNHost的成员,维护 和 哪些Host存在Connection;
在World.update()时会被调用
1.NetworkLayer Init流程
在DTNHost.DTNHost()构造时候发生,
成员: Connection连接:private List connections; // connected hosts
通信范围 private double transmitRange; // transmission coverage
传输速度 private int transmitSpeed; // bandwidth of the transmission (Bps)
绑定来自SimScenario的ConnectionListeners this.cListeners = cListeners;
2.NetworkLayer.update()
DTNHost.update()时发生,
两端的NetworkLayer的成员 List connections删去响应的connection,
两端的Router 触发 changedConnection().
MessageRouter 和 ActiveRouter都不触发流程, EpidemicRouter和DirectDeliveryRouter也不响应,
只有ProphetRouter响应(但是只响应up事件)
public void update() {
for (int i=0; i<this.connections.size(); ) { //for each conn,
Connection con = this.connections.get(i);
DTNHost anotherNode = con.getOtherNode(this.host);
// all connections should be up at this stage
if (!isWithinRange(anotherNode)) { //若 对端 已经超出通信范围
con.setUpState(false); //Connection.isUp赋值为false
notifyConnectionListeners(CON_DOWN, anotherNode); //通知Report类 Host间Conn Down
// tear down bidirectional connection
if (!anotherNode.getConnections().remove(con)) { //对端的NetworkLayer的connections删除
throw new SimError("No connection " + con + " found in " + anotherNode);
}
this.host.changedConnection(con); //本端Router触发 changedConnection()
anotherNode.changedConnection(con);
connections.remove(i); //本端的NetworkLayer的connections删除
}
else {
i++;
}
}
}
3.connect()流程
由World.update() 中的 World.connectHosts()触发(存在conGrid机制,分成cell的connection优化计算方法)
if (simulateConnections) { //配置文件里 Scenario.simulateConnections = true
connectHosts(); // make connections
}
World.connectHosts() 相当于 对任何两个DTNHost尝试connect 即DTNHost.connect();
// the old way to do it (aka Algorithm no 1)
// try to connect every single node to all other nodes
for (int i=0, n = hosts.size();i < n; i++) {
for (int j=0;j < n; j++) {
if (isCancelled) {
return;
}
hosts.get(i).connect(hosts.get(j));
}
}
DTNHost.connect(anotherHost); (1)该DTNHost是Active; 且 该DTNHost !=anotherHost (2)DTNHost.NetworkLayer.connect(anotherHost)
NetworkLayer.connect(anotherHost) 流程:
(1)要求 对端DTNHost是Active, 两个DTNHost距离在通信范围内, 两个DTNHost间的connection不存在在 List NetworkLayer.connections
(2)得到最小ConSpeed, 建立Connection
(3)通知Listener Connection_UP事件
(4)两个DTNHost NetworkLayer 成员 connections, 添加新Connection
(5)Router类响应 Connection状态改变,比如ProphetRouter 改变delivery predictions
public void connect(DTNHost anotherHost) {
if (anotherHost.isActive() && isWithinRange(anotherHost) && !isConnected(anotherHost)) {
// new contact within range
// connection speed is the lower one of the two speeds
...
Connection con = new Connection(this.host, anotherHost, conSpeed);
this.connections.add(con);
notifyConnectionListeners(CON_UP, anotherHost);
// set up bidirectional connection
anotherHost.getConnections().add(con);
// inform routers about the connection
this.host.changedConnection(con);
anotherHost.changedConnection(con);
}
}