/**
 * 
 */
package se.sics.mspsim.net;

import java.util.Vector;
import se.sics.mspsim.util.Utils;

/**
 * @author joakim
 *
 */
public class NeighborTable {
  // currently supports max 64 neighbors...
  Neighbor[] neighbors = new Neighbor[64];
  int neighborCount = 0;
  
  Neighbor defrouter;
  
  public synchronized Neighbor addNeighbor(byte[] ipAddress, byte[] linkAddress) {
    Neighbor nb = new Neighbor();
    nb.ipAddress = ipAddress;
    nb.linkAddress = linkAddress;
    nb.state = Neighbor.INCOMPLETE;
    neighbors[neighborCount++] = nb;
    return nb;
  }
  
  public Neighbor getDefrouter() {
    return defrouter;
  }
  
  public void setDefrouter(Neighbor neighbor) {
    defrouter = neighbor;
  }
  
  public synchronized boolean removeNeighbor(Neighbor nb) {
    for (int i = 0; i < neighborCount; i++) {
      if (nb == neighbors[i]) {
        // move last element forward to this position...
        neighbors[i] = neighbors[neighborCount - 1];
        neighborCount--;
        return true;
      }
    }
    return false;
  }
  
  public Neighbor getNeighbor(byte[] ipAddress) {
    int neighborCount0 = neighborCount;
    Neighbor[] neis = neighbors;
    for (int i = 0; i < neighborCount0; i++) {
      if (Utils.equals(ipAddress, neis[i].ipAddress)) {
        return neis[i];
      }
    }
    return null;
  }
}

@@ -40,36 +40,8 @@
 et;%0D
-%0A%0D%0Aimport java.util.Vector;%0D
 %0Aimp
@@ -384,16 +384,75 @@
 bor nb =
+ getNeighbor(ipAddress);%0D%0A    if (nb == null) %7B%0D%0A      nb =
  new Nei
@@ -457,24 +457,26 @@
 eighbor();%0D%0A
+  
     nb.ipAdd
@@ -498,16 +498,18 @@
 s;%0D%0A    
+  
 nb.linkA
@@ -535,16 +535,18 @@
 s;%0D%0A    
+ 
+ 
 nb.state
@@ -578,40 +578,342 @@
     
-neighbors%5BneighborCount++%5D = nb;
+  if (neighborCount %3C neighbors.length) %7B%0D%0A        neighbors%5BneighborCount++%5D = nb;%0D%0A      %7D else %7B%0D%0A        // TODO select suitable neighbor to replace%0D%0A        neighbors%5B0%5D = nb;%0D%0A      %7D%0D%0A    %7D else %7B%0D%0A      /* Neighbor already in neighbor table */%0D%0A      nb.linkAddress = linkAddress;%0D%0A      nb.state = Neighbor.INCOMPLETE;%0D%0A    %7D
 %0D%0A  

handle update of existing neighbors