Clean code and add comment
This commit is contained in:
parent
f4e0747e59
commit
ac90cc9581
3 changed files with 141 additions and 141 deletions
121
main/Main.java
121
main/Main.java
|
@ -1,87 +1,60 @@
|
|||
package main;
|
||||
|
||||
import java.awt.Label;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
import org.graphstream.graph.Edge;
|
||||
import org.graphstream.graph.Graph;
|
||||
import org.graphstream.graph.implementations.SingleGraph;
|
||||
import org.graphstream.ui.layout.springbox.EdgeSpring;
|
||||
import org.graphstream.ui.swingViewer.basicRenderer.EdgeRenderer;
|
||||
import org.graphstream.ui.util.EdgePoints;
|
||||
|
||||
import structure.Grid;
|
||||
import structure.MyGraph;
|
||||
import structure.Router;
|
||||
import structure.*;
|
||||
|
||||
/**
|
||||
* Main class
|
||||
* @author loic, adama, othmane, saad
|
||||
*
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
/**
|
||||
* Main
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
//Grid g=new Grid(Grid.Protocol.AODV);
|
||||
Grid g=new Grid(Grid.Protocol.AODV); // Graph for AODV
|
||||
Grid g2=new Grid(Grid.Protocol.DSDV); // Graph for DSDV
|
||||
Grid g3=new Grid(Grid.Protocol.CUSTOM); // Graph for custom
|
||||
|
||||
|
||||
// Build Graph for graphstream
|
||||
//MyGraph gr=new MyGraph("Routage Oportuniste", g);
|
||||
//gr.display();
|
||||
//gr.update();
|
||||
MyGraph gD=new MyGraph("AODV", g); // GUI for g
|
||||
MyGraph g2D=new MyGraph("DSDV", g2); // GUI for g2
|
||||
MyGraph g3D=new MyGraph("CUSTOM", g3); // GUI for g3
|
||||
|
||||
// Display all graph
|
||||
gD.display();
|
||||
g2D.display();
|
||||
g3D.display();
|
||||
|
||||
for(int j=0;j<1;j++){
|
||||
Grid g=new Grid(Grid.Protocol.AODV);
|
||||
Grid g2=new Grid(Grid.Protocol.DSDV);
|
||||
Grid g3=new Grid(Grid.Protocol.CUSTOM);
|
||||
|
||||
MyGraph gD=new MyGraph("AODV", g);
|
||||
MyGraph g2D=new MyGraph("DSDV", g2);
|
||||
MyGraph g3D=new MyGraph("CUSTOM", g3);
|
||||
|
||||
gD.display();
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
g2D.display();
|
||||
try {
|
||||
Thread.sleep(2000);
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
g3D.display();
|
||||
try {
|
||||
Thread.sleep(3000);
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Update Graph
|
||||
for(int i=0;i<20;i++){
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
g.buildEdgeWithRandomWeigth();
|
||||
g2.buildEdgeWithRandomWeigth();
|
||||
g3.buildEdgeWithRandomWeigth();
|
||||
|
||||
|
||||
gD.update();
|
||||
g2D.update();
|
||||
g3D.update();
|
||||
//System.out.println("Update !");
|
||||
//gr.update();
|
||||
System.out.println("AODV :"+g.getDebitMoy() + " DSDV :"+g2.getDebitMoy()+" CUSTOM :"+g3.getDebitMoy());
|
||||
|
||||
}
|
||||
|
||||
// Update Graph
|
||||
for(int i=0;i<20;i++){
|
||||
|
||||
// Sleep
|
||||
try {
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Change radio conditions
|
||||
g.buildEdgeWithRandomWeigth();
|
||||
g2.buildEdgeWithRandomWeigth();
|
||||
g3.buildEdgeWithRandomWeigth();
|
||||
|
||||
// Update graph on GUI
|
||||
gD.update();
|
||||
g2D.update();
|
||||
g3D.update();
|
||||
|
||||
// Display current debMoy for each graph
|
||||
System.out.println("AODV :"+g.getDebitMoy() + " DSDV :"+g2.getDebitMoy()+" CUSTOM :"+g3.getDebitMoy());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,26 +1,26 @@
|
|||
package structure;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
/**
|
||||
* Grid structure
|
||||
* @author loic, adama
|
||||
*
|
||||
*/
|
||||
public class Grid {
|
||||
|
||||
// Define protocole name
|
||||
public enum Protocol {
|
||||
AODV, DSDV, CUSTOM
|
||||
}
|
||||
|
||||
private ArrayList<Router> routers=new ArrayList<>();
|
||||
public ArrayList<ArrayList<Integer>> links=new ArrayList<>();
|
||||
|
||||
private int[] pMoy={50,59,92,50,4,8,6,13,7,1,51,6};
|
||||
|
||||
private int bestLink;
|
||||
private Protocol protocol;
|
||||
private int counterCUSTOM=5;
|
||||
private Random rand = new Random();
|
||||
private final int maxWeight=100;
|
||||
|
||||
int debitTotal=0,nbmesure=0;
|
||||
private Random rand = new Random(); // Init rand
|
||||
int debitTotal=0,nbmesure=0; // To compute debit moyen
|
||||
|
||||
|
||||
|
||||
|
@ -43,13 +43,14 @@ public class Grid {
|
|||
this.protocol=protocol;
|
||||
|
||||
switch(protocol){
|
||||
case DSDV:
|
||||
case AODV:
|
||||
this.bestLink=this.getBestLinkIndex();
|
||||
break;
|
||||
case CUSTOM:
|
||||
this.bestLink=this.getBestLinkIndex();
|
||||
break;
|
||||
case AODV:
|
||||
case DSDV:
|
||||
// Change radio conditions 100 times
|
||||
HashMap<Integer,Integer> currentBestLink=new HashMap<>();
|
||||
for(int i=0;i<100;i++){
|
||||
int current=this.getBestLinkIndex();
|
||||
|
@ -61,8 +62,8 @@ public class Grid {
|
|||
}
|
||||
this.buildEdgeWithRandomWeigth();
|
||||
}
|
||||
// Get Best Link
|
||||
Set<Integer> entryTMP = currentBestLink.keySet();
|
||||
|
||||
int max=currentBestLink.get(entryTMP.iterator().next());
|
||||
int maxId=0;
|
||||
entryTMP = currentBestLink.keySet();
|
||||
|
@ -74,12 +75,8 @@ public class Grid {
|
|||
max=entry;
|
||||
maxId=entryId;
|
||||
}
|
||||
|
||||
//System.out.println("Id : "+ entryId + " max "+ entry);
|
||||
|
||||
}
|
||||
this.bestLink=maxId;
|
||||
// System.out.println("Retenu :"+maxId);
|
||||
break;
|
||||
|
||||
|
||||
|
@ -88,12 +85,10 @@ public class Grid {
|
|||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Build the 3x3 links with random weight
|
||||
*/
|
||||
public void buildEdgeWithRandomWeigth(){
|
||||
|
||||
|
||||
|
||||
|
||||
// First line
|
||||
this.buildLinkWithRandomWeight(routers.get(0), routers.get(1), 100);
|
||||
this.buildLinkWithRandomWeight(routers.get(1), routers.get(2),100);
|
||||
|
@ -126,6 +121,9 @@ public class Grid {
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Build all paths (with chained router id)
|
||||
*/
|
||||
private void buildPath(){
|
||||
|
||||
// Link1
|
||||
|
@ -185,12 +183,21 @@ public class Grid {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Build link with a random weight
|
||||
* @param router1 router 1 to link to router 2
|
||||
* @param router2 router 2 to link to router 1
|
||||
* @param pMoy max weight
|
||||
*/
|
||||
private void buildLinkWithRandomWeight(Router router1, Router router2, int pMoy){
|
||||
router1.buildLink(router2, rand.nextInt(pMoy));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Get the best link by bottleneck
|
||||
* @return
|
||||
*/
|
||||
public int getBestLinkIndex(){
|
||||
int currentBestLink=0;
|
||||
int currentBestLinkBottleneck=0;
|
||||
|
@ -207,6 +214,11 @@ public class Grid {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the bottleneck of the link
|
||||
* @param link
|
||||
* @return
|
||||
*/
|
||||
public int getMaxBottleneck(ArrayList<Integer> link){
|
||||
int max=this.getWeigthOfLink(link.get(0), link.get(1));
|
||||
for(int j=1;j<link.size()-1;j++){
|
||||
|
@ -219,11 +231,19 @@ public class Grid {
|
|||
return max;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the weight of a link
|
||||
* @param router1
|
||||
* @param router2
|
||||
* @return
|
||||
*/
|
||||
private int getWeigthOfLink(int router1,int router2){
|
||||
return this.routers.get(router1).getWeight(this.routers.get(router2));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Print infos
|
||||
*/
|
||||
public void printLinkWeight(){
|
||||
for(int i=0;i<this.links.size();i++){
|
||||
ArrayList<Integer> link=this.links.get(i);
|
||||
|
@ -232,13 +252,18 @@ public class Grid {
|
|||
System.out.print(this.getWeigthOfLink(link.get(j), link.get(j+1)) + " ");
|
||||
}
|
||||
System.out.println(" Goulot :"+this.getMaxBottleneck(link));
|
||||
//System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Check if a link is part of an edge
|
||||
* @param link
|
||||
* @param src
|
||||
* @param dest
|
||||
* @return
|
||||
*/
|
||||
public boolean isEdgeOfLink(ArrayList<Integer>link, Router src, Router dest){
|
||||
for(int j=0;j<link.size()-1;j++){
|
||||
Router current=this.routers.get(link.get(j));
|
||||
|
@ -268,22 +293,34 @@ public class Grid {
|
|||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Getter for grid
|
||||
* @return
|
||||
*/
|
||||
public ArrayList<Router> getGrid() {
|
||||
return routers;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Setter for grid
|
||||
* @param grid
|
||||
*/
|
||||
public void setGrid(ArrayList<Router> grid) {
|
||||
this.routers = grid;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Getter for links
|
||||
* @return
|
||||
*/
|
||||
public ArrayList<ArrayList<Integer>> getLinks() {
|
||||
return links;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Setter for links
|
||||
* @param links
|
||||
*/
|
||||
public void setLinks(ArrayList<ArrayList<Integer>> links) {
|
||||
this.links = links;
|
||||
}
|
||||
|
@ -307,7 +344,10 @@ public class Grid {
|
|||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Getter for debitMoy
|
||||
* @return
|
||||
*/
|
||||
public int getDebitMoy(){
|
||||
return this.debitTotal/this.nbmesure;
|
||||
}
|
||||
|
|
|
@ -2,23 +2,27 @@ package structure;
|
|||
|
||||
import java.util.*;
|
||||
|
||||
import org.graphstream.graph.Edge;
|
||||
import org.graphstream.graph.Graph;
|
||||
import org.graphstream.graph.Node;
|
||||
import org.graphstream.graph.*;
|
||||
import org.graphstream.graph.implementations.SingleGraph;
|
||||
import org.graphstream.ui.layout.springbox.EdgeSpring;
|
||||
import org.graphstream.ui.swingViewer.basicRenderer.EdgeRenderer;
|
||||
import org.graphstream.ui.util.EdgePoints;
|
||||
|
||||
|
||||
/**
|
||||
* Class for display graph with GraphStream
|
||||
* @author loic, adama
|
||||
*
|
||||
*/
|
||||
public class MyGraph extends SingleGraph{
|
||||
|
||||
|
||||
// Grid associate to the graph
|
||||
private Grid grid;
|
||||
|
||||
private int miss=0;
|
||||
private int success=0;
|
||||
|
||||
|
||||
/**
|
||||
* Build a graph with a grid
|
||||
*
|
||||
* @param title
|
||||
* @param grid
|
||||
*/
|
||||
public MyGraph(String title, Grid grid) {
|
||||
super(title);
|
||||
// Allow CSS on view
|
||||
|
@ -61,20 +65,13 @@ public class MyGraph extends SingleGraph{
|
|||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Build edges
|
||||
*/
|
||||
public void buildEdges(){
|
||||
|
||||
/*Iterator<Edge> edges=this.getEdgeIterator();
|
||||
while(edges.hasNext()){
|
||||
Edge edge=edges.next();
|
||||
this.removeEdge(edge);
|
||||
}*/
|
||||
|
||||
for(Router r : this.grid.getGrid()){
|
||||
|
||||
String current=r.name;
|
||||
|
||||
|
||||
HashMap<Router, Integer> relier=r.getLinks();
|
||||
Set<Router> k=relier.keySet();
|
||||
Iterator<Router> i=k.iterator();
|
||||
|
@ -87,14 +84,14 @@ public class MyGraph extends SingleGraph{
|
|||
toAdd.setAttribute("ui.label", relier.get(currentRouter));
|
||||
}
|
||||
catch(Exception e){
|
||||
// System.out.println("Bug de merde.");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update best link on screen
|
||||
*/
|
||||
public void showBestLink(){
|
||||
ArrayList<Integer> bestLink=this.grid.getLinks().get(this.grid.getBestLinkByProtocol());
|
||||
for(int i=0;i<bestLink.size();i++){
|
||||
|
@ -106,7 +103,6 @@ public class MyGraph extends SingleGraph{
|
|||
Edge edge=edges.next();
|
||||
|
||||
if(i<(bestLink.size()-1)){
|
||||
int destIndex=bestLink.get(i+1);
|
||||
String src=this.grid.getGrid().get(bestLink.get(i)).name;
|
||||
String dest=this.grid.getGrid().get(bestLink.get(i+1)).name;
|
||||
if((edge.getNode0().getId().equals(src) && edge.getNode1().getId().equals(dest))||(edge.getNode1().getId().equals(src) && edge.getNode0().getId().equals(dest))){
|
||||
|
@ -119,6 +115,9 @@ public class MyGraph extends SingleGraph{
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update all the graph on screen
|
||||
*/
|
||||
public void update(){
|
||||
// Reset color
|
||||
Iterator<Edge> edges=this.getEdgeIterator();
|
||||
|
@ -144,20 +143,8 @@ public class MyGraph extends SingleGraph{
|
|||
edge.setAttribute("ui.label", relier.get(currentRouter));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(this.grid.getBestLinkByProtocol()==this.grid.getBestLinkIndex()){
|
||||
this.success++;
|
||||
}
|
||||
else{
|
||||
this.miss++;
|
||||
}
|
||||
//System.out.println("Success = " + this.success + " Miss = " + this.miss + " try number :"+(this.success+this.miss)) ;
|
||||
|
||||
//Build bestLink
|
||||
this.showBestLink();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue