diff --git a/main/Main.java b/main/Main.java index eaa8d41..b2d9243 100644 --- a/main/Main.java +++ b/main/Main.java @@ -1,43 +1,60 @@ package main; -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); - // Build Graph for graphstream - MyGraph gr=new MyGraph("Routage Oportuniste", g); - gr.display(); + 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 + + 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(); + // Update Graph - while(true){ + 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(); - System.out.println("Update !"); - gr.update(); + 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()); + } } + } diff --git a/resources/Graph.png b/resources/Graph.png new file mode 100644 index 0000000..a4adf57 Binary files /dev/null and b/resources/Graph.png differ diff --git a/structure/Grid.java b/structure/Grid.java index e5db1ef..5f6a1e0 100644 --- a/structure/Grid.java +++ b/structure/Grid.java @@ -1,25 +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 routers=new ArrayList<>(); - private ArrayList> links=new ArrayList<>(); - - + public ArrayList> links=new ArrayList<>(); private int bestLink; private Protocol protocol; private int counterCUSTOM=5; - private Random rand = new Random(); - private final int maxWeight=100; - - + private Random rand = new Random(); // Init rand + int debitTotal=0,nbmesure=0; // To compute debit moyen @@ -33,11 +34,11 @@ public class Grid { this.routers.add(new Router()); } - + this.buildEdgeWithRandomWeigth(); - this.buildPath(); - + //Build fixed link + //this.buildPath(); this.protocol=protocol; @@ -45,10 +46,13 @@ public class Grid { case AODV: this.bestLink=this.getBestLinkIndex(); break; - case DSDV: case CUSTOM: + this.bestLink=this.getBestLinkIndex(); + break; + case DSDV: + // Change radio conditions 100 times HashMap currentBestLink=new HashMap<>(); - for(int i=0;i<100000;i++){ + for(int i=0;i<100;i++){ int current=this.getBestLinkIndex(); if(currentBestLink.containsKey(current)){ currentBestLink.put(current, currentBestLink.get(current)+1); @@ -58,8 +62,8 @@ public class Grid { } this.buildEdgeWithRandomWeigth(); } + // Get Best Link Set entryTMP = currentBestLink.keySet(); - int max=currentBestLink.get(entryTMP.iterator().next()); int maxId=0; entryTMP = currentBestLink.keySet(); @@ -71,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; @@ -85,34 +85,45 @@ public class Grid { } - + /** + * Build the 3x3 links with random weight + */ public void buildEdgeWithRandomWeigth(){ // First line - this.buildLinkWithRandomWeight(routers.get(0), routers.get(1)); - this.buildLinkWithRandomWeight(routers.get(1), routers.get(2)); + this.buildLinkWithRandomWeight(routers.get(0), routers.get(1), 100); + this.buildLinkWithRandomWeight(routers.get(1), routers.get(2),100); // Second line - this.buildLinkWithRandomWeight(routers.get(3), routers.get(4)); - this.buildLinkWithRandomWeight(routers.get(4), routers.get(5)); + this.buildLinkWithRandomWeight(routers.get(3), routers.get(4),100); + this.buildLinkWithRandomWeight(routers.get(4), routers.get(5),50); // Third line - this.buildLinkWithRandomWeight(routers.get(6), routers.get(7)); - this.buildLinkWithRandomWeight(routers.get(7), routers.get(8)); + this.buildLinkWithRandomWeight(routers.get(6), routers.get(7),100); + this.buildLinkWithRandomWeight(routers.get(7), routers.get(8),60); // First column - this.buildLinkWithRandomWeight(routers.get(0), routers.get(3)); - this.buildLinkWithRandomWeight(routers.get(3), routers.get(6)); + this.buildLinkWithRandomWeight(routers.get(0), routers.get(3),80); + this.buildLinkWithRandomWeight(routers.get(3), routers.get(6),100); // Second column - this.buildLinkWithRandomWeight(routers.get(1), routers.get(4)); - this.buildLinkWithRandomWeight(routers.get(4), routers.get(7)); + this.buildLinkWithRandomWeight(routers.get(1), routers.get(4),100); + this.buildLinkWithRandomWeight(routers.get(4), routers.get(7),10); // Third column - this.buildLinkWithRandomWeight(routers.get(2), routers.get(5)); - this.buildLinkWithRandomWeight(routers.get(5), routers.get(8)); + this.buildLinkWithRandomWeight(routers.get(2), routers.get(5),100); + this.buildLinkWithRandomWeight(routers.get(5), routers.get(8),100); + + this.buildPath(); + + //System.out.println(this.links.get(this.getBestLinkByProtocol())); + this.debitTotal+=this.getMaxBottleneck(this.links.get(this.getBestLinkByProtocol())); + this.nbmesure++; } + /** + * Build all paths (with chained router id) + */ private void buildPath(){ // Link1 @@ -172,12 +183,21 @@ public class Grid { } - private void buildLinkWithRandomWeight(Router router1, Router router2){ - router1.buildLink(router2, rand.nextInt(this.maxWeight)); + /** + * 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; @@ -194,7 +214,12 @@ public class Grid { } - private int getMaxBottleneck(ArrayList link){ + /** + * Get the bottleneck of the link + * @param link + * @return + */ + public int getMaxBottleneck(ArrayList link){ int max=this.getWeigthOfLink(link.get(0), link.get(1)); for(int j=1;j link=this.links.get(i); @@ -219,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(ArrayListlink, Router src, Router dest){ for(int j=0;j getGrid() { return routers; } - + /** + * Setter for grid + * @param grid + */ public void setGrid(ArrayList grid) { this.routers = grid; } - + /** + * Getter for links + * @return + */ public ArrayList> getLinks() { return links; } - + /** + * Setter for links + * @param links + */ public void setLinks(ArrayList> links) { this.links = links; } @@ -281,11 +331,12 @@ public class Grid { * @return the bestLinkByProtocol */ public int getBestLinkByProtocol() { + if(this.protocol==Protocol.CUSTOM){ this.counterCUSTOM--; if(this.counterCUSTOM==0){ this.bestLink=this.getBestLinkIndex(); - this.counterCUSTOM=5; + this.counterCUSTOM=2; } } @@ -293,7 +344,13 @@ public class Grid { } - + /** + * Getter for debitMoy + * @return + */ + public int getDebitMoy(){ + return this.debitTotal/this.nbmesure; + } diff --git a/structure/Graph.java b/structure/MyGraph.java similarity index 78% rename from structure/Graph.java rename to structure/MyGraph.java index 1c2dc0f..a5c6f8f 100644 --- a/structure/Graph.java +++ b/structure/MyGraph.java @@ -2,27 +2,32 @@ 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 System.setProperty("org.graphstream.ui.renderer", "org.graphstream.ui.j2dviewer.J2DGraphRenderer"); + // Set graph CSS this.addAttribute("ui.stylesheet", "url('resources/style.css')"); @@ -60,20 +65,13 @@ public class MyGraph extends SingleGraph{ } - + /** + * Build edges + */ public void buildEdges(){ - /*Iterator edges=this.getEdgeIterator(); - while(edges.hasNext()){ - Edge edge=edges.next(); - this.removeEdge(edge); - }*/ - for(Router r : this.grid.getGrid()){ - String current=r.name; - - HashMap relier=r.getLinks(); Set k=relier.keySet(); Iterator i=k.iterator(); @@ -86,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 bestLink=this.grid.getLinks().get(this.grid.getBestLinkByProtocol()); for(int i=0;i edges=this.getEdgeIterator(); @@ -143,19 +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(); } diff --git a/structure/Router.java b/structure/Router.java index fce5977..3a8d8f9 100644 --- a/structure/Router.java +++ b/structure/Router.java @@ -22,7 +22,6 @@ public class Router { public void buildLink(Router router, int weight){ this.links.remove(router); router.removeLink(this); - this.links.put(router, weight); router.addLink(this, weight);