From e8d9244fddbc2198569b3b175af5a48c27c12d43 Mon Sep 17 00:00:00 2001 From: Maksim Tereshin Date: Sun, 3 Mar 2019 21:49:11 -0500 Subject: [PATCH] Fixed duplicated row in routing table when open a saved project. --- .../org/netsimulator/net/RoutingTable.java | 127 +++++++----------- .../org/netsimulator/net/RoutingTableRow.java | 38 ++---- .../netsimulator/term/RouteCLICommand.java | 2 +- 3 files changed, 63 insertions(+), 104 deletions(-) diff --git a/src/main/java/org/netsimulator/net/RoutingTable.java b/src/main/java/org/netsimulator/net/RoutingTable.java index 121cfc4..5b04a7a 100644 --- a/src/main/java/org/netsimulator/net/RoutingTable.java +++ b/src/main/java/org/netsimulator/net/RoutingTable.java @@ -27,14 +27,8 @@ public class RoutingTable { - private final List table = new ArrayList(); - - private static final Logger logger = Logger.getLogger( RoutingTable.class.getName() ); - - /** Creates a new instance of RoutingTable */ - public RoutingTable() { - } + private List table = Collections.unmodifiableList(new LinkedList<>()); public void addRoute( IP4Address target, @@ -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 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 l = new LinkedList<>(newTable); + l.sort(RoutingTableRow.COMPARATOR); + this.table = Collections.unmodifiableList(l); } } @@ -70,22 +64,20 @@ public int deleteRoute( int metric, Interface iface ) { int deleted = 0; - - synchronized( table ) { - for( Iterator 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 newTable = new LinkedList<>(table); + for( Iterator 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; } @@ -95,38 +87,33 @@ public int deleteRoute( IP4Address gateway, Interface iface ) { int deleted = 0; - - synchronized( table ) { - for( Iterator 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 newTable = new LinkedList<>(table); + for(Iterator 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; @@ -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 getRows() { - synchronized(table) { - return new LinkedList(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(); } diff --git a/src/main/java/org/netsimulator/net/RoutingTableRow.java b/src/main/java/org/netsimulator/net/RoutingTableRow.java index 53858c3..1b554dc 100644 --- a/src/main/java/org/netsimulator/net/RoutingTableRow.java +++ b/src/main/java/org/netsimulator/net/RoutingTableRow.java @@ -33,7 +33,7 @@ public class RoutingTableRow { - public static final Comparator COMPARATOR = new RowComparator(); + public static final Comparator COMPARATOR = new RowComparator(); private IP4Address target = null; private IP4Address netmask = null; @@ -138,9 +138,8 @@ private void setTarget(IP4Address target) throws NotAllowedAddressException this.target = target; } } - - + static void CheckAddressNetmaskCorrespondence( IP4Address address, IP4Address netmask) @@ -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(); } @@ -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; - */ } } diff --git a/src/main/java/org/netsimulator/term/RouteCLICommand.java b/src/main/java/org/netsimulator/term/RouteCLICommand.java index b322788..8b2aadb 100644 --- a/src/main/java/org/netsimulator/term/RouteCLICommand.java +++ b/src/main/java/org/netsimulator/term/RouteCLICommand.java @@ -293,7 +293,7 @@ public int go() { // searching throughout straight connected networks here RoutingTableRow rtr = router.getRoutingTable(). - routeThroghoutStraightConnectedNetworks(gw); + routeThroughoutStraightConnectedNetworks(gw); if(rtr==null) {