Skip to content

Commit

Permalink
Fixed duplicated row in routing table when open a saved project.
Browse files Browse the repository at this point in the history
  • Loading branch information
checkpoint20 committed Mar 4, 2019
1 parent 6261ebb commit e8d9244
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 104 deletions.
127 changes: 52 additions & 75 deletions src/main/java/org/netsimulator/net/RoutingTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,8 @@

public class RoutingTable {

private final List<RoutingTableRow> table = new ArrayList<RoutingTableRow>();


private static final Logger logger = Logger.getLogger( RoutingTable.class.getName() );

/** Creates a new instance of RoutingTable */
public RoutingTable() {
}
private List<RoutingTableRow> table = Collections.unmodifiableList(new LinkedList<>());

public void addRoute(
IP4Address target,
Expand All @@ -52,14 +46,14 @@ public void addRoute(
addRoute( row );
}

public void addRoute( RoutingTableRow row ) {
synchronized(table) {
if( table.add( row ) ) {
logger.log( Level.FINEST, "{0}: the row:\n{1}\n was added to the routing table", new Object[]{hashCode() + "", row});
Collections.sort(table, RoutingTableRow.COMPARATOR);
} else {
logger.log( Level.FINEST, "{0}: the row:\n{1}\n wasn't added to the routing table", new Object[]{hashCode() + "", row});
}
// Visible for testing
void addRoute( RoutingTableRow row ) {
Set<RoutingTableRow> newTable = new HashSet<>(table);
if(newTable.add(row)) {
logger.log( Level.FINEST, "{0}: the row:\n{1}\n was added to the routing table", new Object[]{hashCode() + "", row});
List<RoutingTableRow> l = new LinkedList<>(newTable);
l.sort(RoutingTableRow.COMPARATOR);
this.table = Collections.unmodifiableList(l);
}
}

Expand All @@ -70,22 +64,20 @@ public int deleteRoute(
int metric,
Interface iface ) {
int deleted = 0;

synchronized( table ) {
for( Iterator<RoutingTableRow> i = table.iterator(); i.hasNext();) {
RoutingTableRow row = i.next();

if( row.getTarget().equals( target ) &&
row.getNetmask().equals( netmask ) &&
row.getInterface().getName().equals( iface.getName() ) &&
row.getMetric() == metric &&
( ( row.getGateway() != null && row.getGateway().equals( gateway ) ) ||
( row.getGateway() == null && gateway == null ) ) ) {
i.remove();
deleted++;
}
List<RoutingTableRow> newTable = new LinkedList<>(table);
for( Iterator<RoutingTableRow> i = newTable.iterator(); i.hasNext();) {
RoutingTableRow row = i.next();
if( row.getTarget().equals( target ) &&
row.getNetmask().equals( netmask ) &&
row.getInterface().getName().equals( iface.getName() ) &&
row.getMetric() == metric &&
( ( row.getGateway() != null && row.getGateway().equals( gateway ) ) ||
( row.getGateway() == null && gateway == null ) ) ) {
i.remove();
deleted++;
}
}
this.table = Collections.unmodifiableList(newTable);
return deleted;
}

Expand All @@ -95,38 +87,33 @@ public int deleteRoute(
IP4Address gateway,
Interface iface ) {
int deleted = 0;

synchronized( table ) {
for( Iterator<RoutingTableRow> i = table.iterator(); i.hasNext();) {
RoutingTableRow row = i.next();

if( row.getTarget().equals( target ) &&
row.getNetmask().equals( netmask ) &&
row.getInterface().getName().equals( iface.getName() ) &&
( ( row.getGateway() != null && row.getGateway().equals( gateway ) ) ||
( row.getGateway() == null && gateway == null ) ) ) {
i.remove();
deleted++;
}
List<RoutingTableRow> newTable = new LinkedList<>(table);
for(Iterator<RoutingTableRow> i = newTable.iterator(); i.hasNext();) {
RoutingTableRow row = i.next();
if(row.getTarget().equals(target) &&
row.getNetmask().equals( netmask ) &&
row.getInterface().getName().equals( iface.getName() ) &&
( ( row.getGateway() != null && row.getGateway().equals( gateway ) ) ||
( row.getGateway() == null && gateway == null ) ) ) {
i.remove();
deleted++;
}
}
this.table = Collections.unmodifiableList(newTable);
return deleted;
}

public RoutingTableRow route( IP4Address destination ) {
RoutingTableRow route(IP4Address destination) {
RoutingTableRow res = null;
for (RoutingTableRow r : table) {
logger.log( Level.FINE, "Processing row: {0}", r);

synchronized( table ) {
for (RoutingTableRow r : table) {
logger.log( Level.FINE, "Processing row: {0}", r);

if( r.match( destination ) ) {
logger.log( Level.FINE, "the destination {0} matchs the row", destination);
res = r;
break;
} else {
logger.log( Level.FINE, "the destination {0} doesn''t match the row", destination);
}
if( r.match( destination ) ) {
logger.log( Level.FINE, "the destination {0} matchs the row", destination);
res = r;
break;
} else {
logger.log( Level.FINE, "the destination {0} doesn''t match the row", destination);
}
}
return res;
Expand All @@ -139,40 +126,30 @@ public RoutingTableRow route( IP4Address destination ) {
* @return found route.
* @see RouteCLICommand
*/
public RoutingTableRow routeThroghoutStraightConnectedNetworks( IP4Address destination ) {
public RoutingTableRow routeThroughoutStraightConnectedNetworks(IP4Address destination ) {
RoutingTableRow res = null;
for (RoutingTableRow r : table) {
if( r.getGateway() != null ) {
continue;
}

synchronized( table ) {
for (RoutingTableRow r : table) {
if( r.getGateway() != null ) {
continue;
}

if( r.match( destination ) ) {
res = r;
break;
}
if( r.match( destination ) ) {
res = r;
break;
}
}
return res;
}

/**
* @return Snapshot of routing table.
*/
public List<RoutingTableRow> getRows() {
synchronized(table) {
return new LinkedList<RoutingTableRow>(table);
}
return table;
}

@Override
public String toString() {
StringBuilder curTable = new StringBuilder("Routing table:\n");
synchronized(table) {
for (RoutingTableRow routingTableRow : table) {
curTable.append(routingTableRow.toString()).append("\n");
}
for (RoutingTableRow routingTableRow : table) {
curTable.append(routingTableRow.toString()).append("\n");
}
return curTable.toString();
}
Expand Down
38 changes: 10 additions & 28 deletions src/main/java/org/netsimulator/net/RoutingTableRow.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

public class RoutingTableRow
{
public static final Comparator COMPARATOR = new RowComparator();
public static final Comparator<RoutingTableRow> COMPARATOR = new RowComparator();

private IP4Address target = null;
private IP4Address netmask = null;
Expand Down Expand Up @@ -138,9 +138,8 @@ private void setTarget(IP4Address target) throws NotAllowedAddressException
this.target = target;
}
}



static void CheckAddressNetmaskCorrespondence(
IP4Address address,
IP4Address netmask)
Expand Down Expand Up @@ -202,10 +201,14 @@ private void setInterface(Interface iface)
this.iface = iface;
}
}


public boolean match(IP4Address address)
{
/**
* Checks if an address exists in the subnet specified
* by this routing table row.
* @param address to check against the subnet.
* @return true if the address is in the subnet.
*/
public boolean match(IP4Address address) {
return (address.toIntValue() & netmask.toIntValue()) == target.toIntValue();
}

Expand Down Expand Up @@ -286,29 +289,8 @@ else if( countNetmaskLength(r1.getNetmask()) >
return res;
}

private int countNetmaskLength(IP4Address netmask)
{
private int countNetmaskLength(IP4Address netmask) {
return Integer.bitCount(netmask.toIntValue());

/*
int count = 0;
int addr = netmask.toIntValue();
for(count=0; count!=32; count++)
{
// System.out.println("buf digit : "+Integer.toBinaryString(addr));
if( (addr | 0) == 0 )
{
break;
}else
{
addr <<= 1;
}
}
return count;
*/
}

}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/netsimulator/term/RouteCLICommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ public int go()
{
// searching throughout straight connected networks here
RoutingTableRow rtr = router.getRoutingTable().
routeThroghoutStraightConnectedNetworks(gw);
routeThroughoutStraightConnectedNetworks(gw);

if(rtr==null)
{
Expand Down

0 comments on commit e8d9244

Please sign in to comment.