Ant.java

Index Score
ants.p2p
ANtsP2P

View: Reasons, Metrics, Source Code

These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.

MetricDescription
COMPARISONSNumber of comparison operators
CYCLOMATICCyclomatic complexity
OPERATORSNumber of operators
PROGRAM_LENGTHHalstead program length
JAVA0034JAVA0034 Missing braces in if statement
OPERANDSNumber of operands
ELOCEffective lines of code
BLOCKSNumber of blocks
SIZESize of the file in bytes
LOOPSNumber of loops
LOCLines of code
LOGICAL_LINESNumber of statements
JAVA0049JAVA0049 Nested block at depth N (maximum: M)
LINESNumber of lines in the source file
UNIQUE_OPERANDSNumber of unique operands
EXITSProcedure exits
PROGRAM_VOCABHalstead program vocabulary
JAVA0166JAVA0166 Generic exception caught
JAVA0076JAVA0076 Use of magic number
JAVA0081JAVA0081 Boolean literal in comparison
JAVA0117JAVA0117 Missing javadoc: method 'method'
RETURNSNumber of return points from functions
INTERFACE_COMPLEXITYInterface complexity
JAVA0031JAVA0031 Case statement not properly closed
PARAMSNumber of formal parameter declarations
FUNCTIONSNumber of function declarations
JAVA0007JAVA0007 Should not declare public field
JAVA0116JAVA0116 Missing javadoc: field 'field'
LINE_COMMENTNumber of line comments
JAVA0083JAVA0083 Unnecessary instanceof test
JAVA0144JAVA0144 Line exceeds maximum M characters
EXEC_COMMENTSComments in executable code
JAVA0087JAVA0087 Use of Thread.sleep()
JAVA0079JAVA0079 Use of instance to reference static member
NEST_DEPTHMaximum nesting depth
JAVA0160JAVA0160 Method does not throw specified exception
JAVA0284JAVA0284 Explicit garbage collection
JAVA0177JAVA0177 Variable declaration missing initializer
JAVA0270JAVA0270 Use Java 5.0 enhanced for loop construct to iterate over all elements in an array
JAVA0283JAVA0283 Control variable not updated in loop body
JAVA0272JAVA0272 Thread.run() called
JAVA0287JAVA0287 Unnecessary null check
UNIQUE_OPERATORSNumber of unique operators
JAVA0035JAVA0035 Missing braces in for statement
JAVA0285JAVA0285 Dereference of potentially null variable
JAVA0040JAVA0040 Switch statement contains N cases (maximum: M)
JAVA0075JAVA0075 Method parameter hides field
JAVA0143JAVA0143 Synchronized method
PROGRAM_VOLUMEHalstead program volume
JAVA0259JAVA0259 Return of collection/array field
JAVA0136JAVA0136 N methods defined in class (maximum: M)
JAVA0138JAVA0138 N parameters defined for method (maximum: M)
JAVA0126JAVA0126 Method declares unchecked exception in throws
JAVA0273JAVA0273 Non-final derivative of Thread calls start() in constructor
JAVA0110JAVA0110 Incorrect javadoc: no @return tag
JAVA0264JAVA0264 Integer math in long context - check for overflow
JAVA0173JAVA0173 Unused method parameter
JAVA0029JAVA0029 Private method not used
JAVA0067JAVA0067 Array descriptor on identifier name
JAVA0100JAVA0100 Class contains N non-final fields (maximum: M)
JAVA0108JAVA0108 Incorrect javadoc: no @param tag for 'parameter'
DECL_COMMENTSComments in declarations
COMMENTSComment lines
DOC_COMMENTNumber of javadoc comment lines
JAVA0130JAVA0130 Non-static method does not use instance fields
JAVA0003JAVA0003 Minimize use of on-demand (.*) imports
JAVA0145JAVA0145 Tab character used in source file
//****************************************************************** //****************************************************************** //********** ANts Peer To Peer Sources ************* // // ANts P2P realizes a third generation P2P net. It protects your // privacy while you are connected and makes you not trackable, hiding // your identity (ip) and crypting everything you are sending/receiving // from others. // Copyright (C) 2004 Roberto Rossi // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. package ants.p2p; import java.util.*; import java.net.*; import java.beans.*; import java.security.*; import ants.p2p.security.sockets.*; import ants.p2p.messages.*; import ants.p2p.messages.security.*; import ants.p2p.utils.indexer.*; import ants.p2p.utils.encoding.*; import ants.p2p.query.*; import ants.p2p.exceptions.*; import ants.p2p.http.*; import org.apache.log4j.*; import ants.p2p.utils.net.*; public class Ant extends Thread { private static final String protocolVersion = "1.0.0"; private static final String applicationName = "ANtsP2P"; private static final String applicationVersion = "beta1.6.0"; private static final String netName = "ANtsNet"; private static Logger _logger = Logger.getLogger(Ant.class.getName()); int serverPort = 443; public static String ConnectionType = "56K"; public static int myMessageSizeMax = 10000; public static int inTransitMessageSizeMax = 10000; public static long routeInactiveTimeout = 10*60*1000; public static int maxNeighbours = 15; java.util.List neighbours = new ArrayList();//Collections.synchronizedList(new ArrayList()); java.util.Hashtable netModifications = new java.util.Hashtable(); public java.util.List myMessages = new ArrayList()/*Collections.synchronizedList(new ArrayList())*/; public java.util.List failedMessages = new ArrayList()/*Collections.synchronizedList(new ArrayList())*/; public Hashtable routingTable = new Hashtable(); public java.util.List inTransitMessages = new ArrayList()/*Collections.synchronizedList(new ArrayList())*/; protected SecureServer ss; public static long messageTimeout = 180000; public static int maxRetransmissions = 3; public static int maxRetransmissionsForceDirection = 2; public static int maxFailedMessageToTrace = 100; public static int beingRoutedMessages = 0; public static int maxMessagesToRouteToghether = 50; public static int netModificationsTimeout = 60*1000*5; public static int probeCheckInterval = 60*1000*6; public static double underRateConnections = 2.0/3.0; boolean terminate = false; protected boolean acceptTCPDirectConnections = false; String localInetAddress = null; public PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this); String id = null; public int localNattedPort = 0; public String localNattedInetAddress = null; public static boolean proxied = false; boolean upnp; public Ant(String id, int maxNeighbours, int serverPort, boolean acceptDC, String localInetAddress, boolean UPnP) { this.id = id; this.upnp = UPnP; this.acceptTCPDirectConnections = acceptDC; this.localInetAddress = localInetAddress; this.serverPort = serverPort; if (maxNeighbours >= 1) { this.setMaxNeighbours(maxNeighbours); } this.setPriority(6); this.start(); } public static void setProxied(InetAddress proxyAddress, int proxyPort){ System.setProperty("http.proxySet","true"); System.setProperty("http.proxyPort",proxyPort+""); System.setProperty("http.proxyHost",proxyAddress.getHostAddress()); System.setProperty("https.proxyHost", proxyAddress.getHostAddress()); System.setProperty("https.proxyPort", proxyPort+""); proxied = true; } public static void setDirectConnection(){ System.setProperty("http.proxySet","false"); proxied = false; } public boolean acceptTCPDirectConnections(){ return this.acceptTCPDirectConnections; } public String getLocalInetAddress(){ if(this.acceptTCPDirectConnections){ try{ if (!this.isInternetPublicAddress(InetAddress.getByName(this.localInetAddress))){ return ""; }else{ return this.localInetAddress + ":" + (this.serverPort); } }catch(Exception e){ return ""; } } else return ""; } public void setLocalInetAddress(String localInetAddress){ if(localInetAddress.startsWith("/")) localInetAddress = localInetAddress.substring(1); this.localInetAddress = localInetAddress; } public String getLanAddress(){ try{ InetAddress[] addresses = InetAddress.getAllByName(InetAddress.getLocalHost().getHostName()); for (int x = 0; x < addresses.length; x++) { if (!isInternetPublicAddress(addresses[x]) && !addresses[x].isLoopbackAddress()) { String address = addresses[x].getHostAddress(); if(address.startsWith("/")) address = address.substring(1); return address; } } for (int x = 0; x < addresses.length; x++) { if (!addresses[x].isLoopbackAddress()) { String address = addresses[x].getHostAddress(); if(address.startsWith("/")) address = address.substring(1); return address; } } }catch(Exception e){ _logger.error("Invalid InetAddress: ",e); } return null; } public static boolean isInternetPublicAddress(InetAddress address) { try { byte head = address.getAddress()[0]; byte body = address.getAddress()[1]; byte tail = address.getAddress()[2]; if (head >= 89 && head <= 127) return false; if (head >= 173 && head <= 187) return false; if (head >= 224 && head <= 239) return false; if (head >= 240 && head <= 255) return false; switch (head) { case (byte) 0: break; case (byte) 1: break; case (byte) 2: break; case (byte) 5: break; case (byte) 10: break; case (byte) 23: break; case (byte) 27: break; case (byte) 31: break; case (byte) 36: break; case (byte) 37: break; case (byte) 39: break; case (byte) 41: break; case (byte) 42: break; case (byte) 46: break; case (byte) 49: break; case (byte) 50: break; case (byte) 58: break; case (byte) 59: break; case (byte) 60: break; case (byte) 71: break; case (byte) 72: break; case (byte) 73: break; case (byte) 74: break; case (byte) 75: break; case (byte) 76: break; case (byte) 77: break; case (byte) 78: break; case (byte) 79: break; case (byte) 128: if (body == (byte) 0) break; case (byte) 169: if (body == (byte) 254) break; case (byte) 172: if (body >= (byte) 16 && body <= (byte) 31) break; case (byte) 189: break; case (byte) 190: break; case (byte) 191: if (body == (byte) 255) break; case (byte) 192: if (body == (byte) 0 || (body == (byte) 68 && tail == (byte) 185) || body == (byte) 168) break; case (byte) 197: break; case (byte) 198: if (body >= (byte) 18 && body <= (byte) 19) break; case (byte) 223: break; default: return true; } return false; } catch (Exception e) { return false; } } public static String getVersion() { return applicationVersion; } public static String getProtocolVersion() { return protocolVersion; } public static String getApplicationName() { return applicationName; } public static String getNetName(){ return netName; } public String getIdent() { return this.id; } public String getShortId(){ return this.id.substring(0,10); } public int getServerPort() { return this.serverPort; } public java.util.List getFailedMessages() { return this.failedMessages; } public void setMaxNeighbours(int mn) { maxNeighbours = mn; } public int getMaxNeighbours() { return maxNeighbours; } public void removeNeighbour(NeighbourAnt n) { synchronized (neighbours) { try { n.terminate(); if(this.neighbours.remove(n)){ this.generateNetModificationAlert(n); } } catch (Exception e) { _logger.error(this.getShortId() + "",e); } } } public void generateNetModificationAlert(NeighbourAnt disconnectedNeighbour){ try{ ArrayList unreachableNeighbours = new ArrayList(); Enumeration routingElements = this.routingTable.keys(); while(routingElements.hasMoreElements()){ String nodeID = (String)routingElements.nextElement(); if(nodeID.equals(this.getIdent())) continue; RoutingTableElement rte = (RoutingTableElement)this.routingTable.get(nodeID); if(rte != null && rte.removeRoute(disconnectedNeighbour.getIdent()) && rte.getRoutes().size() == 0){ unreachableNeighbours.add(nodeID); this.routingTable.remove(nodeID); if(rte != null && rte.getIP() != null){ String unreachableIP = rte.getIP(); unreachableNeighbours.addAll(this.removeRoutes(unreachableIP, disconnectedNeighbour.getIdent())); } this.propertyChangeSupport.firePropertyChange("routeLost", disconnectedNeighbour.getIdent(), nodeID); _logger.info("[1]UNREACHABLE: "+nodeID.substring(0,10)); } } if(unreachableNeighbours.size() != 0){ NetModificationAlert alert = new NetModificationAlert( unreachableNeighbours); this.netModifications.put(alert.getAck_Id(), new Long(System.currentTimeMillis())); for (int x = 0; x < this.neighbours.size(); x++) { ( (NeighbourAnt)this.neighbours.get(x)).route(alert); } } }catch(Exception e){ _logger.error("Error in processing netModification upon peer disconnection",e); } } public void generateNetModificationAlert(Message m, String neighbourId){ try{ if(m.getDest().equals(this.getIdent())) return; RoutingTableElement rte = (RoutingTableElement)this.routingTable.get(m.getDest()); if(rte != null && rte.removeRoute(neighbourId) && rte.getRoutes().size() == 0){ _logger.info("[2]UNREACHABLE: "+m.getDest().substring(0,10)); this.routingTable.remove(neighbourId); ArrayList unreachableNeighbours = new ArrayList(); unreachableNeighbours.add(m.getDest()); if(rte != null && rte.getIP() != null){ String unreachableIP = rte.getIP(); unreachableNeighbours.addAll(this.removeRoutes(unreachableIP, neighbourId)); } this.propertyChangeSupport.firePropertyChange("routeLost", neighbourId, m.getDest()); if (unreachableNeighbours.size() != 0) { NetModificationAlert alert = new NetModificationAlert( unreachableNeighbours); this.netModifications.put(alert.getAck_Id(), new Long(System.currentTimeMillis())); for (int x = 0; x < this.neighbours.size(); x++) { ( (NeighbourAnt)this.neighbours.get(x)).route(alert); } } } } catch (Exception e) { _logger.error("Error in generating netModification Alert upon message deletion", e); } } public void generateNetModificationAlert(ArrayList unreachableNeighbours, String requirer){ try{ if (unreachableNeighbours.size() != 0){ for(int x = unreachableNeighbours.size() - 1; x >= 0; x--){ if(unreachableNeighbours.get(x).equals(this.getIdent())) { unreachableNeighbours.remove(x); continue; } RoutingTableElement rte = (RoutingTableElement)this.routingTable.remove(unreachableNeighbours.get(x)); if(rte != null && rte.getIP() != null){ String unreachableIP = rte.getIP(); unreachableNeighbours.addAll(this.removeRoutes(unreachableIP, requirer)); } this.propertyChangeSupport.firePropertyChange("routeLost", null, unreachableNeighbours.get(x)); _logger.info("[3]UNREACHABLE: "+(unreachableNeighbours.get(x).toString().length() > 10 ? unreachableNeighbours.get(x).toString().subSequence(0,10) : unreachableNeighbours.get(x).toString())); } NetModificationAlert alert = new NetModificationAlert(unreachableNeighbours); this.netModifications.put(alert.getAck_Id(), new Long(System.currentTimeMillis())); if(this.acceptTCPDirectConnections){ try{ Neighbour n = new DirectNeighbour(requirer, this); if (n != null) { n.send(alert); } }catch(Exception e){} } for (int x = 0; x < this.neighbours.size(); x++) { ( (NeighbourAnt)this.neighbours.get(x)).route(alert); } } } catch (Exception e) { _logger.error("Error in generating netModification Alert upon message deletion", e); } } private ArrayList removeRoutes(String ip, String sourceId){ Enumeration routingElements = this.routingTable.keys(); ArrayList unreachableNeighbours = new ArrayList(); while(routingElements.hasMoreElements()){ String nodeID = (String)routingElements.nextElement(); if(nodeID.equals(this.getIdent())) continue; RoutingTableElement rte = (RoutingTableElement)this.routingTable.get(nodeID); if(rte != null && rte.removeRoute(ip) && rte.getRoutes().size() == 0){ unreachableNeighbours.add(nodeID); this.routingTable.remove(nodeID); String unreachableIP = rte.getIP(); unreachableNeighbours.addAll(this.removeRoutes(unreachableIP, sourceId)); this.propertyChangeSupport.firePropertyChange("routeLost", sourceId, nodeID); } } return unreachableNeighbours; } protected synchronized void processNetModificationAlert(NetModificationAlert alert, Neighbour source){ try{ if (this.netModifications.get(alert.getAck_Id()) != null) return; this.netModifications.put(alert.getAck_Id(), new Long(System.currentTimeMillis())); ArrayList unreachableNeighbours = new ArrayList(); Enumeration routingElements = this.routingTable.keys(); while(routingElements.hasMoreElements()){ String nodeID = (String)routingElements.nextElement(); if(nodeID.equals(this.getIdent())) continue; RoutingTableElement rte = (RoutingTableElement)this.routingTable.get(nodeID); if(alert.getUnreachablePeers().contains(nodeID) && rte != null && rte.removeRoute(source.getIdent()) && rte.getRoutes().size() == 0){ unreachableNeighbours.add(nodeID); _logger.info("[4]UNREACHABLE: "+nodeID.substring(0,10)); this.routingTable.remove(nodeID); if(rte != null && rte.getIP() != null){ String unreachableIP = rte.getIP(); unreachableNeighbours.addAll(this.removeRoutes(unreachableIP, source.getIdent())); } this.propertyChangeSupport.firePropertyChange("routeLost", source.getIdent(), nodeID); } } if (unreachableNeighbours.size() != 0){ alert.setUnreachablePeers(unreachableNeighbours); for (int x = 0; x < this.neighbours.size(); x++) { if (! ( (NeighbourAnt)this.neighbours.get(x)).equals(source)) ( (NeighbourAnt)this.neighbours.get(x)).route(alert); } } } catch (Exception e) { _logger.error("Error in processing netModification Alert", e); } } public NeighbourAnt getNeighbour(String id) { for (int x = 0; x < this.neighbours.size(); x++) { if ( ( (NeighbourAnt)this.neighbours.get(x)).getIdent().equals(id) || ( (NeighbourAnt)this.neighbours.get(x)).getRemoteId().equals(id)) { return (NeighbourAnt)this.neighbours.get(x); } } return null; } public static int getRateThresold(){ int rateThresold = Integer.MAX_VALUE; if(Ant.ConnectionType.equals("56K")) rateThresold = Integer.MAX_VALUE; else if(Ant.ConnectionType.equals("ISDN")) rateThresold = Integer.MAX_VALUE; else if(Ant.ConnectionType.equals("DSL")) rateThresold = 3000; else if(Ant.ConnectionType.equals("CABLE")) rateThresold = 1500; else if(Ant.ConnectionType.equals("LAN T3")) rateThresold = 750; else if(Ant.ConnectionType.equals("LAN T2")) rateThresold = 375; else if(Ant.ConnectionType.equals("LAN T1")) rateThresold = 186; else if(Ant.ConnectionType.equals("LAN or Fiber Net")) rateThresold = 186; return rateThresold; } public int getUnderRatedNeighbours() { int rateThresold = this.getRateThresold(); int underRatedNeighbours = 0; for (int x = 0; x < this.getNeighbours().size(); x++) { if ( ( (NeighbourAnt)this.getNeighbours().get(x)).getTimeElapsed() >= rateThresold) underRatedNeighbours++; } return underRatedNeighbours; } public boolean equals(Object o) { if (o instanceof Ant) return ( (Ant) o).getIdent().equals(this.getIdent()); else return o == this; } public int getNeighboursNumber() { return this.neighbours.size(); } public void addNeighbour(NeighbourAnt n) throws Exception { synchronized (neighbours) { if (this.neighbours.size() < this.maxNeighbours && !this.neighbours.contains(n) && !this.isDisconnected()) { this.neighbours.add(n); } else { if (this.neighbours.size() >= this.maxNeighbours) throw new Exception(this.getShortId() + ": Max neighbourg number reached"); else if (this.neighbours.contains(n)) throw new Exception(this.getShortId() + ": Neighbourg already connected"); else throw new Exception(this.getShortId() + ": Strange thing in neighbour add process."); } } _logger.debug(this.getIdent() + ": Added Neighbour by request IP(local): "+n.getIdent()+" IP(remote): "+n.getRemoteId()); this.propertyChangeSupport.firePropertyChange("newNeighbour", null, n); } public void addP2PNeighbour(String remoteAddress, int port, boolean isRequirer, InetAddress localhost) throws Exception { if(this.isDisconnected()) return; InetAddress remote = InetAddress.getByName(remoteAddress); if ( (remote.isLoopbackAddress() || remote.isAnyLocalAddress() || remote.isLinkLocalAddress() || remote.isSiteLocalAddress()) && port == this.getServerPort()){ return; } if (localhost.getHostAddress().equals(remote.getHostAddress()) && port == this.getServerPort()){ return; } for (int x = 0; x < this.neighbours.size(); x++) { NeighbourAnt na = ( (NeighbourAnt)this.neighbours.get(x)); if (na.getIdent().equals(remoteAddress + " " + port) || na.getRemoteId().equals(remoteAddress + " " + port) || ((na.s.getInetAddress().isLoopbackAddress() || na.s.getInetAddress().isAnyLocalAddress() || na.s.getInetAddress().isLinkLocalAddress() || na.s.getInetAddress().isSiteLocalAddress()) && (na.remoteServerPort == port || na.port == port))){ return; } } SecureClientSocket scs = new SecureP2PClientSocket(remoteAddress, port, this.getServerPort(), this.proxied); if (scs.isNewVersionDetected()) { this.getPropertyChangeSupport().firePropertyChange( "newANtsVersionDetected", null, scs.getNewerVersion()); } if (!scs.isClosed()) { _logger.info(scs.getInetAddress().getHostAddress() +": Local time elapsed: "+scs.getTimeElapsed()+"[Thresold: "+this.getRateThresold()+"]"); /*if(this.getUnderRatedNeighbours() >= Math.floor(Ant.maxNeighbours*underRateConnections) && scs.getTimeElapsed() >= this.getRateThresold()){ throw new Exception(scs.getInetAddress().getHostAddress() + ": Rejected neighbour cause it doesn't satisfy bandwith request: ["+this.getUnderRatedNeighbours()+"/"+Math.floor(Ant.maxNeighbours*underRateConnections)+"]"); }*/ NeighbourAnt n = new NeighbourAnt(this, remoteAddress, port, scs.getLocalServerPort(), scs.getCipherEnc(), scs.getCipherDec(), scs.getSocket(), isRequirer, scs.getTimeElapsed()); synchronized (neighbours) { if (this.neighbours.size() < this.maxNeighbours && !this.neighbours.contains(n)) { this.neighbours.add(n); n.start(); } else { n.terminate(); n.setFailure(); n.start(); if (this.neighbours.size() >= this.maxNeighbours){ throw new Exception(this.getShortId() + ": Max neighbourg number reached"); } else if (this.neighbours.contains(n)){ throw new Exception(this.getShortId() + ": Neighbourg already connected"); } throw new Exception(this.getShortId() + ": Strange thing in neighbour add process."); } } _logger.debug(this.getIdent() + ": Added Neighbourg from address IP(local): "+n.getIdent()+" IP(remote): "+n.getRemoteId()); this.propertyChangeSupport.firePropertyChange("newNeighbour", null, n); } else { _logger.info(this.getShortId() + ": Connection rejected..."); } } public java.util.List getNeighbours() { return this.neighbours; } public MessageWrapper getMessage(String id) { for (int x = 0; x < this.myMessages.size(); x++) { if ( ( (MessageWrapper)this.myMessages.get(x)).getMessage().getAck_Id(). equals(id)) { return (MessageWrapper)this.myMessages.get(x); } } return null; } public void traceHints(Message m, String requirer){ if(!m.getSource().equals(this.getIdent())){ if(m instanceof QueryMessage && m.getType() == 0 && ((QueryMessage)m).getProcessed()){ QueryMessage qm = (QueryMessage)m; for(int x = 0; qm.getTuples() != null && x < qm.getTuples().size(); x++){ if(qm.getTuples().get(x) instanceof QueryRemoteFileTuple){ QueryRemoteFileTuple qrft = (QueryRemoteFileTuple) qm.getTuples().get(x); if(this.getIdent().equals(qrft.getOwnerID())) continue; if (this.routingTable.get(qrft.getOwnerID()) != null) { RoutingTableElement rte = (RoutingTableElement)this.routingTable.get(qrft.getOwnerID()); if (!this.acceptTCPDirectConnections || m.getSourceAddress().equals("")) rte.addHintRoute(requirer, qrft.getLastTimeSeen().longValue()); else rte.setIP(qrft.getOwnerIP()); } else { RoutingTableElement rte = new RoutingTableElement(); if (!this.acceptTCPDirectConnections || m.getSourceAddress().equals("")) rte.addHintRoute(requirer, qrft.getLastTimeSeen().longValue()); else rte.setIP(qrft.getOwnerIP()); this.routingTable.put(qrft.getOwnerID(), rte); } } else if(qm.getTuples().get(x) instanceof HttpServerInfo){ HttpServerInfo si = (HttpServerInfo) qm.getTuples().get(x); if(this.getIdent().equals(si.getOwnerId())) continue; if (this.routingTable.get(si.getOwnerId()) != null) { RoutingTableElement rte = (RoutingTableElement)this.routingTable.get(si.getOwnerId()); if (!this.acceptTCPDirectConnections || si.getOwnerIp().equals("")) rte.addHintRoute(requirer, si.getSeenOn().longValue()); else rte.setIP(si.getOwnerIp()); } else { RoutingTableElement rte = new RoutingTableElement(); if (!this.acceptTCPDirectConnections || si.getOwnerIp().equals("")) rte.addHintRoute(requirer, si.getSeenOn().longValue()); else rte.setIP(si.getOwnerIp()); this.routingTable.put(si.getOwnerId(), rte); } } } } } } public void traceDeliveredMessage(MessageWrapper wm) { if(!wm.getMessage().getSource().equals(this.getIdent()) && !wm.getMessage().getSource().equals("")){ if(this.routingTable.get(wm.getMessage().getSource()) != null){ RoutingTableElement rte = (RoutingTableElement) this.routingTable.get(wm.getMessage().getSource()); if (!this.acceptTCPDirectConnections || wm.getMessage().getSourceAddress().equals("")) rte.addTracedRoute(wm.getRequirer()); else rte.setIP(wm.getMessage().getSourceAddress()); }else{ RoutingTableElement rte = new RoutingTableElement(); if (!this.acceptTCPDirectConnections || wm.getMessage().getSourceAddress().equals("")) rte.addTracedRoute(wm.getRequirer()); else rte.setIP(wm.getMessage().getSourceAddress()); this.routingTable.put(wm.getMessage().getSource(), rte); } } if(!wm.getMessage().getDest().equals(this.getIdent()) && !wm.getMessage().getDest().equals("")){ if(this.routingTable.get(wm.getMessage().getDest()) != null){ RoutingTableElement rte = (RoutingTableElement) this.routingTable.get(wm.getMessage().getDest()); rte.addTracedRoute(wm.getRoutedTo()); }else{ RoutingTableElement rte = new RoutingTableElement(); rte.addTracedRoute(wm.getRoutedTo()); this.routingTable.put(wm.getMessage().getDest(), rte); } if(wm.getMessage() instanceof QueryMessage && wm.getMessage().getType() == 2){ QueryMessage qm = (QueryMessage)wm.getMessage(); for(int x = 0; qm.getTuples() != null && x < qm.getTuples().size(); x++){ if(qm.getTuples().get(x) instanceof QueryRemoteFileTuple){ QueryRemoteFileTuple qrft = (QueryRemoteFileTuple) qm.getTuples().get(x); if(this.getIdent().equals(qrft.getOwnerID())) continue; if (this.routingTable.get(qrft.getOwnerID()) != null) { RoutingTableElement rte = (RoutingTableElement)this.routingTable.get(qrft.getOwnerID()); if (!this.acceptTCPDirectConnections || qrft.getOwnerIP().equals("")) rte.addHintRoute(wm.getRoutedTo(), qrft.getLastTimeSeen().longValue()); else rte.setIP(qrft.getOwnerIP()); } else { RoutingTableElement rte = new RoutingTableElement(); if (!this.acceptTCPDirectConnections || qrft.getOwnerIP().equals("")) rte.addHintRoute(wm.getRoutedTo(), qrft.getLastTimeSeen().longValue()); else rte.setIP(qrft.getOwnerIP()); this.routingTable.put(qrft.getOwnerID(), rte); } } else if(qm.getTuples().get(x) instanceof QuerySupernodeTuple){ QuerySupernodeTuple qst = (QuerySupernodeTuple) qm.getTuples().get(x); if(this.getIdent().equals(qst.getOwnerID())) continue; if (this.routingTable.get(qst.getOwnerID()) != null) { RoutingTableElement rte = (RoutingTableElement)this.routingTable.get(qst.getOwnerID()); if (!this.acceptTCPDirectConnections || qst.getOwnerIP().equals("")) rte.addHintRoute(wm.getRoutedTo(), qst.getSeenOn().longValue()); else rte.setIP(qst.getOwnerIP()); } else { RoutingTableElement rte = new RoutingTableElement(); if (!this.acceptTCPDirectConnections || qst.getOwnerIP().equals("")) rte.addHintRoute(wm.getRoutedTo(), qst.getSeenOn().longValue()); else rte.setIP(qst.getOwnerIP()); this.routingTable.put(qst.getOwnerID(), rte); } } else if(qm.getTuples().get(x) instanceof HttpServerInfo){ HttpServerInfo si = (HttpServerInfo) qm.getTuples().get(x); if(this.getIdent().equals(si.getOwnerId())) continue; if (this.routingTable.get(si.getOwnerId()) != null) { RoutingTableElement rte = (RoutingTableElement)this.routingTable.get(si.getOwnerId()); if (!this.acceptTCPDirectConnections || si.getOwnerIp().equals("")) rte.addHintRoute(wm.getRoutedTo(), si.getSeenOn().longValue()); else rte.setIP(si.getOwnerIp()); } else { RoutingTableElement rte = new RoutingTableElement(); if (!this.acceptTCPDirectConnections || si.getOwnerIp().equals("")) rte.addHintRoute(wm.getRoutedTo(), si.getSeenOn().longValue()); else rte.setIP(si.getOwnerIp()); this.routingTable.put(si.getOwnerId(), rte); } } } } } } public void traceMessage(Message m, String requirer, String routedTo) { MessageWrapper wm = new MessageWrapper(m, requirer); wm.setStoreFormat(); wm.setRoutedTo(routedTo); if (this.routingTable.get(m.getSource()) != null) { RoutingTableElement rte = (RoutingTableElement)this.routingTable.get(m.getSource()); if(!this.acceptTCPDirectConnections || m.getSourceAddress().equals("")) rte.addTracedRoute(requirer); else rte.setIP(m.getSourceAddress()); } else { RoutingTableElement rte = new RoutingTableElement(); if(!this.acceptTCPDirectConnections || m.getSourceAddress().equals("")) rte.addTracedRoute(requirer); else rte.setIP(m.getSourceAddress()); this.routingTable.put(m.getSource(), rte); } this.traceHints(m, requirer); int index = this.inTransitMessages.indexOf(m); if (index >= 0) { this.inTransitMessages.remove(index); } if (this.inTransitMessages.size() < this.inTransitMessageSizeMax && !this.inTransitMessages.contains(m)) { this.inTransitMessages.add(wm); } else if (this.inTransitMessages.size() >= this.inTransitMessageSizeMax && !this.inTransitMessages.contains(m)) { this.inTransitMessages.add(wm); this.inTransitMessages.remove(0); } } public void traceMyMessage(MessageWrapper wm, String routedTo) { //synchronized (wm) { wm.setRoutedTo(routedTo); //} //synchronized (this.myMessages) { int index = this.myMessages.indexOf(wm); if(index < 0){ this.myMessages.add(wm); }else{ this.myMessages.remove(wm); this.myMessages.add(wm); //this.myMessages.trimToSize(); } //} } public static void decBeingRoutedMessages() { Ant.beingRoutedMessages--; //_logger.info("[Routed] There are "+Ant.beingRoutedMessages+" messages being routed"); } public static boolean verifyAndIncBeingRoutedMessages() { if (Ant.beingRoutedMessages < Ant.maxMessagesToRouteToghether) { Ant.beingRoutedMessages++; //_logger.info("[Routing] There are "+Ant.beingRoutedMessages+" messages being routed"); return true; } else { return false; } } public Router activateNewRouterProcess(Message m, String requirer) throws Exception { if (Ant.verifyAndIncBeingRoutedMessages()) { String source = m.getSource(); if(source.length() > 10) source = source.substring(0, 10); String dest = m.getDest(); if(dest.length() > 10) dest = dest.substring(0, 10); java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream(); java.io.ObjectOutputStream oss = new java.io.ObjectOutputStream(bos); oss.writeObject(m); oss.close(); bos.close(); _logger.info("Routing["+m.getType()+"]: "+m.getClass().toString()+" from " + source + " to " + dest + " size " + (bos.toByteArray().length / 1024.0) + "KB"); Router r = new Router(this, m, requirer); /*Prova Priorit‡*/ if (m instanceof ControlMessage || m instanceof FileTransferEndControlMessage || m instanceof FileTransferErrorControlMessage || m instanceof FileSizePullErrorControlMessage || m instanceof FileInfosPullErrorControlMessage || m instanceof HttpTransferEndControlMessage || m instanceof HttpInterruptTransferMessage || m instanceof SecureConnectionErrorControlMessage || m instanceof NetModificationAlert) { r.setPriority(10); } else if (m instanceof SecurityRequestMessage || m instanceof SecurityResponseMessage || m instanceof FilePullMessage || m instanceof FilePushMessage || m instanceof FileInfosPullMessage || m instanceof FileInfosPushMessage || m instanceof HttpRequestMessage) { r.setPriority(10); } else if (m instanceof FileSizePullMessage || m instanceof FileSizePushMessage) { r.setPriority(8); } else if (m instanceof PrivateChatMessage) { r.setPriority(7); } else if (m instanceof QueryMessage) { QueryMessage qm = (QueryMessage) m; if (qm.getQuery() instanceof QueryStringItem || qm.getQuery() instanceof QueryHashItem){ r.setPriority(10); //was 6 } else if (qm.getQuery() instanceof QueryFileListItem || qm.getQuery() instanceof QuerySupernodeItem || qm.getQuery() instanceof QueryInetAddressItem || qm.getQuery() instanceof QueryRandomItem) { r.setPriority(6); } else r.setPriority(10); } else if (m instanceof HttpResponsePartMessage) { r.setPriority(6);//was 4 } else if (m instanceof FilePartMessage) { r.setPriority(6);// was 3 } else if (m instanceof Message) { r.setPriority(6); } if (Router.PARALLEL) r.start(); else r.run(); return r; }else{ _logger.error("Too many messages being routed!"); return null; } } public SenderThread[] route(MessageWrapper wm, Message m, String requirer, Router r, boolean external, boolean retransmission)throws Exception { if(external){ return this.routeExternalMessage(m, requirer, r); }else if(!external && retransmission){ return this.retransmitMessage(wm); }else{ return this.routeMyMessage(wm); } } private SenderThread[] routeExternalMessage(Message m, String requirer, Router r) throws Exception { try { if (!m.getSource().equals("") && m.getSource().equals(this.getIdent())) { try { MessageSigner.getInstance().sign(m); } catch (Exception e) { _logger.error("Failed signing broadcast message", e); } } if (Router.delay > 0) Thread.sleep( (long) Math.floor(Router.delay * Math.random())); if (this.neighbours.size() == 0) { ArrayList unreachableNeighbours = new ArrayList(); unreachableNeighbours.add(m.getDest()); this.generateNetModificationAlert(unreachableNeighbours, requirer); throw new Exception(this.getShortId() + ": Destination node " + m.getDest().substring(0,10) + " unreachable for message from node " + (m.getSource().length() > 0 ? m.getSource().substring(0,10) : m.getSource()) + " with id " + m.getAck_Id() + " type = " + m.getType() + "\nReason: No Route Found " + "Node " + this.getShortId() + " has no neighbours"); } if (this.myMessages.contains(m)) { if(m.getType() != 2){ int index = this.myMessages.indexOf(m); MessageWrapper wm = (MessageWrapper)this.myMessages.get(index); /*if (wm.getRetrasmissions() < m.getRetrasmissions() && m.getRetrasmissions() < Ant.maxRetransmissionsForceDirection) { wm.notToBeConsidered = new ArrayList();//Collections.synchronizedList(new ArrayList()) } else*/ if (!wm.notToBeConsidered.contains(wm.getRoutedTo())) { wm.notToBeConsidered.add(wm.getRoutedTo()); this.generateNetModificationAlert(wm.getMessage(), wm.getRoutedTo()); } Neighbour selectedNeighbour = this.checkForRoute(wm, wm.notToBeConsidered); if(selectedNeighbour != null){ SenderThread[] st = new SenderThread[1]; st[0] = selectedNeighbour.route(m); return st; } if(wm.notToBeConsidered.size() == 0){ ArrayList unreachableNeighbours = new ArrayList(); unreachableNeighbours.add(wm.getMessage().getDest()); this.generateNetModificationAlert(unreachableNeighbours, requirer); } throw new Exception(this.getShortId() + ": Destination node " + m.getDest().substring(0,10) + " unreachable for message from node " + (m.getSource().length() > 0 ? m.getSource().substring(0,10) : m.getSource()) + " with id " + m.getAck_Id() + " type = " + m.getType() + "\nReason: No Route Found " + "Node " + this.getShortId() + " excluded all its " + wm.notToBeConsidered.size() + " neghbours"); } else { // m.getType() == 2 _logger.debug(this.getShortId() + ": Broadcast Message returned no more node avaiable to send. Killing..."); return null; } } else if (!m.getDest().equals(this.getIdent())) { if (!this.inTransitMessages.contains(m)) { if (m.getType() == 1) { for (int k = 0; k < this.inTransitMessages.size(); k++) { MessageWrapper inTransit = (MessageWrapper)this.inTransitMessages.get(k); if ( (inTransit.getMessage().getType() == 0 || inTransit.getMessage().getType() == 2) && inTransit.getMessage().getAck_Id().equals(m.getAck_Id())) { Neighbour n = null; if(inTransit.getMessage().getSourceAddress().equals("")){ try{ n = new DirectNeighbour(inTransit.getRequirer(), this); }catch(Exception e){ n = this.getNeighbour(inTransit.getRequirer()); } } else{ try{ n = new DirectNeighbour(inTransit.getMessage().getSourceAddress(), this); }catch(Exception e){ n = this.getNeighbour(inTransit.getRequirer()); } } if (n != null) { inTransit.getMessage().invalidate(); if(inTransit.getMessage().getType() == 2){ Message broadTracer; if (inTransit.getMessage() instanceof QueryMessage) broadTracer = new QueryMessage( (QueryMessage) m); else broadTracer = new Message(m); broadTracer.fillMessageProperties(inTransit.getMessage().getSource(), inTransit.getMessage().getSourceAddress() , m.getSource(), 2, m.getAck_Id()); MessageWrapper broadTracerWrapper = new MessageWrapper(broadTracer, inTransit.getRequirer()); broadTracerWrapper.setRoutedTo(requirer); this.traceDeliveredMessage(broadTracerWrapper); }else{ this.inTransitMessages.remove(k); this.traceDeliveredMessage(inTransit); } _logger.debug(this.getShortId() + ": C Forwarding message with id = " + m.getAck_Id() + " type = " + m.getType() + " to id = " + n.getIdent()); SenderThread[] st = new SenderThread[1]; st[0] = n.route(m); return st; }else{ throw new NullNeighbourException(this.getShortId() + ": Ack forwarding error [" +inTransit.getRequirer()+"] Dest "+ m.getDest() + " source " + m.getSource() + " with id " + m.getAck_Id() + " type = " + m.getType() + "\nReason: No Route Found Node " + this.getShortId()); } } } throw new NullNeighbourException(this.getShortId() + ": Ack message error: dest " + m.getDest() + " source " + m.getSource() + " with id " + m.getAck_Id() + " type = " + m.getType() + "\nReason: No Route Found Node " + this.getShortId()); } else if (m.getType() == 2) { this.traceMessage(m, requirer, ""); double mustDie = ((QueryMessage)m).getNextRandomDoubleAndRollback() * 100; if(!(mustDie < r.queryDieProbability)) this.processMessage(m, r); ArrayList threadsList = new ArrayList(); int floodedMessages = 0; List neighboursShuffled = (ArrayList)((ArrayList)this.getNeighbours()).clone(); Collections.shuffle(neighboursShuffled); for(int x = 0; x < neighboursShuffled.size() && !(mustDie < r.queryDieProbability); x++){ double mustRoute = Math.random() * 100; if(mustRoute < (floodedMessages > 0 ? 100 * Math.pow(r.routeProbability / 100.0, (floodedMessages + 1.0)): r.routeProbability)){ floodedMessages++; QueryMessage copy = new QueryMessage((QueryMessage)m); double modifySeed = Math.random() * 100; if(modifySeed < r.querySeedModificationProbability) copy.getNextRandomDouble(); NeighbourAnt n = (NeighbourAnt)neighboursShuffled.get(x); if (n != null && !n.getIdent().equals(requirer)) { try{ SenderThread broadSender = n.route(copy); threadsList.add(broadSender); _logger.debug(this.getShortId() + ": Flooding message with id = " + m.getAck_Id() + " type = " + m.getType() + " to id = " + n.getIdent()); }catch(Exception e){ _logger.error("Cannot flood to neighbour",e); } } } } SenderThread[] st = new SenderThread[threadsList.size()]; for(int x = 0; x < threadsList.size(); x++) st[x] = (SenderThread)threadsList.get(x); return st; }else{ Neighbour selectedNeighbour = this.checkForRoute(m, requirer, new ArrayList(), false); if(selectedNeighbour != null){ SenderThread[] st = new SenderThread[1]; st[0] = selectedNeighbour.route(m); return st; } ArrayList unreachableNeighbours = new ArrayList(); unreachableNeighbours.add(m.getDest()); this.generateNetModificationAlert(unreachableNeighbours, requirer); throw new NullNeighbourException(this.getShortId() + ": Destination node " + m.getDest().substring(0,10) + " unreachable for message from node " + (m.getSource().length() > 0 ? m.getSource().substring(0,10) : m.getSource()) + " with id " + m.getAck_Id() + " type = " + m.getType() + "\nReason: No Route Found with no excluded neghbours"); } } else { int index = this.inTransitMessages.indexOf(m); MessageWrapper wm = (MessageWrapper)this.inTransitMessages.get(index); if(m.getType() != 2){ /*if (wm.getRetrasmissions() < m.getRetrasmissions() && m.getRetrasmissions() < Ant.maxRetransmissionsForceDirection) { wm.notToBeConsidered = new ArrayList();//Collections.synchronizedList(new ArrayList()) } else */if (!wm.notToBeConsidered.contains(wm.getRoutedTo())) { wm.notToBeConsidered.add(wm.getRoutedTo()); this.generateNetModificationAlert(wm.getMessage(), wm.getRoutedTo()); } _logger.debug(this.getShortId() + ": Message with id = " + m.getAck_Id() + " type = " + m.getType() + " returned. Not considering node " + wm.getRoutedTo()); Neighbour selectedNeighbour = this.checkForRoute(m, requirer, wm.notToBeConsidered, true); if(selectedNeighbour != null){ SenderThread[] st = new SenderThread[1]; st[0] = selectedNeighbour.route(m); return st; } if(wm.notToBeConsidered.size() == 0){ ArrayList unreachableNeighbours = new ArrayList(); unreachableNeighbours.add(m.getDest()); this.generateNetModificationAlert(unreachableNeighbours, requirer); } throw new NullNeighbourException(this.getShortId() + ": Destination node " + m.getDest().substring(0,10) + " unreachable for message from node " + (m.getSource().length() > 0 ? m.getSource().substring(0,10) : m.getSource()) + " with id " + m.getAck_Id() + " type = " + m.getType() + "\nReason: No Route Found Node " + this.getShortId() + " excluded all its " + wm.notToBeConsidered.size() + " neghbours " + m); }else{ // m.getType() == 2 this.traceMessage(m, requirer, ""); _logger.debug(this.getShortId() + ": D Flooded message with id = " + m.getAck_Id() + " type = " + m.getType() + " returned. Killing!"); return null; } } } else if (m.getDest().equals(this.getIdent())) { if (m.getType() == 0 && !m.getSource().equals("")) { _logger.debug(this.getShortId() + ": Received Message sending Ack for message with id = " + m.getAck_Id() + " type = " + m.getType()); MessageWrapper toBeStored = new MessageWrapper(m, requirer); this.traceHints(m, requirer); this.traceDeliveredMessage(toBeStored); Message ackMessage = new Message(); ackMessage.fillMessageProperties(this.getIdent(), "", m.getSource(), 1, m.getAck_Id() ); MessageWrapper wm = new MessageWrapper(ackMessage, requirer); if (!wm.getMessage().getSource().equals("") && wm.getMessage().getSource().equals(this.getIdent())) { try { MessageSigner.getInstance().sign(wm.getMessage()); } catch (Exception e) { _logger.error("Failed signing message", e); } } SenderThread[] st = null; Neighbour n = null; if(m.getSourceAddress().equals("")){ try{ n = new DirectNeighbour(requirer, this); }catch(Exception e){ n = this.getNeighbour(requirer); } } else{ try{ n = new DirectNeighbour(m.getSourceAddress(), this); }catch(Exception e){ n = this.getNeighbour(requirer); } } if (n != null) { st = new SenderThread[1]; st[0] = n.route(new Message(wm)); _logger.debug(this.getShortId() + ": D1 Forwarding message with id = " + wm.getMessage().getAck_Id() + " type = " + wm.getMessage().getType() + " to id = " + requirer); } else { ArrayList unreachableNeighbours = new ArrayList(); unreachableNeighbours.add(wm.getMessage().getDest()); this.generateNetModificationAlert(unreachableNeighbours, this.getIdent()); throw new NullNeighbourException(this.getShortId() + ": Destination node " + wm.getMessage().getDest().substring(0,10) + " unreachable for message from node " + (wm.getMessage().getSource().length() > 0 ? wm.getMessage().getSource().substring(0,10) : wm.getMessage().getSource()) + " with id " + wm.getMessage().getAck_Id() + " type = " + wm.getMessage().getType() + "\nReason: No Route Found Node " + this.getShortId() + " excluded all its " + wm.notToBeConsidered.size() + " neghbours"); } this.processMessage(m, r); return st; } else if ( (m.getType() == 0 || m.getType() == 1) && m.getSource().equals("")) { _logger.debug(this.getShortId() + ": Received Message with masked source id = " + m.getAck_Id() + " type = " + m.getType()); this.processMessage(m, r); return null; } else if (m.getType() == 1) { if (this.getMessage(m.getAck_Id()) != null) { MessageWrapper movingMessage = null; movingMessage = (MessageWrapper)this.getMessage(m.getAck_Id()); if (movingMessage != null) { if(movingMessage.getMessage().getType() == 2){ Message broadTracer; if(m instanceof QueryMessage) broadTracer = new QueryMessage((QueryMessage)m); else broadTracer = new Message(m); broadTracer.fillMessageProperties(m.getDest(), movingMessage.getMessage().getSourceAddress(), m.getSource(), 2, m.getAck_Id()); broadTracer.invalidate(); MessageWrapper broadTracerWrapper = new MessageWrapper(broadTracer, this.getIdent()); broadTracerWrapper.setRoutedTo(((Router)Thread.currentThread()).getRequirer()); this.traceDeliveredMessage(broadTracerWrapper); }else{ movingMessage.getMessage().invalidate(); this.myMessages.remove(movingMessage); this.traceDeliveredMessage(movingMessage); } } _logger.debug(this.getShortId() + ": Message succesfully routed id = " + m.getAck_Id() + " type = " + m.getType()); this.processMessage(m, r); return null; } else { _logger.debug(this.getShortId() + ": (Retransmission) Message succesfully routed id = " + m.getAck_Id() + " type = " + m.getType()); this.processMessage(m, r); return null; } } else { throw new Exception(this.getShortId() + ": There is something strange(2) for message with id = " + m.getAck_Id() + " type = " + m.getType()); } } else { throw new Exception(this.getShortId() + ": There is something strange(3) for message with id = " + m.getAck_Id() + " type = " + m.getType()); } } catch (Exception e) { _logger.error(this.getShortId() + " routing error["+requirer+"]: ", e); return null; } } private SenderThread[] routeMyMessage(MessageWrapper wm) throws Exception { if (!wm.getMessage().getSource().equals("") && wm.getMessage().getSource().equals(this.getIdent())) { try { MessageSigner.getInstance().sign(wm.getMessage()); } catch (Exception e) { _logger.error("Failed signing broadcast message", e); } } if(wm.getMessage().getType() != 2){ Neighbour n = this.checkForRoute(wm, wm.notToBeConsidered); if (n != null) { SenderThread[] st = new SenderThread[1]; st[0] = n.route(wm.getMessage()); return st; } else { if(wm.notToBeConsidered.size() == 0){ ArrayList unreachableNeighbours = new ArrayList(); unreachableNeighbours.add(wm.getMessage().getDest()); this.generateNetModificationAlert(unreachableNeighbours, this.getIdent()); } throw new Exception(this.getShortId() + ": Destination unreachable for message with id = " + wm.getMessage().getAck_Id().substring(0,10) + " type = " + wm.getMessage().getType()); } }else{ ArrayList threadsList = new ArrayList(); int floodedMessages = 0; int routeProbability = 25 + (int)(System.currentTimeMillis() % 50); int queryDieProbability = 5 + (int)(System.currentTimeMillis() % 10); int querySeedModificationProbability = 50 + (int)(System.currentTimeMillis() % 25); double mustDie = ((QueryMessage)wm.getMessage()).getNextRandomDoubleAndRollback() * 100; List neighboursShuffled = (ArrayList)((ArrayList)this.getNeighbours()).clone(); Collections.shuffle(neighboursShuffled); for (int x = 0; x < neighboursShuffled.size() && !(mustDie < queryDieProbability); x++) { NeighbourAnt n = (NeighbourAnt)neighboursShuffled.get(x); double mustRoute = Math.random() * 100; if(mustRoute < (floodedMessages > 0 ? 100 * Math.pow(routeProbability / 100.0, (floodedMessages + 1.0)): routeProbability)){ try{ QueryMessage copy = new QueryMessage((QueryMessage)wm.getMessage()); double modifySeed = Math.random() * 100; if(modifySeed < querySeedModificationProbability) copy.getNextRandomDouble(); SenderThread broadSender = n.route(copy); threadsList.add(broadSender); _logger.debug(this.getShortId() + ": Flooding message with id = " + wm.getMessage().getAck_Id() + " type = " + wm.getMessage().getType() + " to id = " + n.getIdent()); } catch (Exception e) { _logger.error("Cannot flood to neighbour", e); } } } this.traceMyMessage(wm, ""); SenderThread[] st = new SenderThread[threadsList.size()]; for (int x = 0; x < threadsList.size(); x++) st[x] = (SenderThread) threadsList.get(x); return st; } } private int changeThreadPriority(Message m){ int oldPriority = Thread.currentThread().getPriority(); if (m instanceof ControlMessage || m instanceof FileTransferEndControlMessage || m instanceof FileTransferErrorControlMessage || m instanceof FileSizePullErrorControlMessage || m instanceof FileInfosPullErrorControlMessage || m instanceof HttpTransferEndControlMessage || m instanceof HttpInterruptTransferMessage || m instanceof SecureConnectionErrorControlMessage || m instanceof NetModificationAlert) { Thread.currentThread().setPriority(10); } else if (m instanceof SecurityRequestMessage || m instanceof SecurityResponseMessage || m instanceof FilePullMessage || m instanceof FilePushMessage || m instanceof FileInfosPullMessage || m instanceof FileInfosPushMessage || m instanceof HttpRequestMessage) { Thread.currentThread().setPriority(10); } else if (m instanceof FileSizePullMessage || m instanceof FileSizePushMessage) { Thread.currentThread().setPriority(8); } else if (m instanceof PrivateChatMessage) { Thread.currentThread().setPriority(7); } else if (m instanceof QueryMessage) { QueryMessage qm = (QueryMessage) m; if (qm.getQuery() instanceof QueryStringItem || qm.getQuery() instanceof QueryHashItem){ Thread.currentThread().setPriority(10); //was 7 } else if (qm.getQuery() instanceof QueryFileListItem || qm.getQuery() instanceof QuerySupernodeItem || qm.getQuery() instanceof QueryInetAddressItem || qm.getQuery() instanceof QueryRandomItem) { Thread.currentThread().setPriority(6); // was 2 } else Thread.currentThread().setPriority(10); } else if (m instanceof HttpResponsePartMessage) { Thread.currentThread().setPriority(6); // was 4 } else if (m instanceof FilePartMessage) { Thread.currentThread().setPriority(6); // was 3 } else if (m instanceof Message) { Thread.currentThread().setPriority(6); } return oldPriority; } public MessageWrapper sendMessage(Message message, String dest, boolean disableAutoRetransmit, boolean maskSource) throws InterruptedException { int oldPriority = this.changeThreadPriority(message); String messageID; try{ Thread.sleep(2); messageID = this.getIdent() + "@" + (new DigestManager()).getDigest(System. currentTimeMillis() + ""); Thread.sleep(2); }catch(Exception e){messageID = Math.floor(Math.random()*Math.pow(10,32))+"";} if (!maskSource) message.fillMessageProperties(this.getIdent(), this.getLocalInetAddress(), dest, 0, messageID); else message.fillMessageProperties("", "", dest, 0, messageID); _logger.debug("There are " + this.failedMessages.size() + " messages failed!"); _logger.debug("There are " + this.myMessages.size() + " messages pending!"); _logger.debug(this.getShortId() + ": Sending Message from id = " + this.getShortId() + " to id = " + dest + " message id = " + messageID); MessageWrapper wm = new MessageWrapper(message, this.getIdent()); if (disableAutoRetransmit) { wm.disableAutoRetransmit(); } try { SenderThread[] st = this.route(wm,null,null,null,false,false); if(st != null){ final SenderThread[] stFinal = st; Thread waiter = new Thread(){ public void run() { for(int x = 0; x < stFinal.length; x++){ try{ stFinal[x].join(Router.routeTimeout); }catch(Exception e){ _logger.error("Cannot join send process",e); } if(stFinal[x].isAlive()){ stFinal[x].interruptSending(); } } } }; waiter.start(); } Thread.currentThread().setPriority(oldPriority); return wm; } catch (Exception e) { _logger.error(this.getShortId() + "",e); Thread.currentThread().setPriority(oldPriority); return null; } } public MessageWrapper sendBroadcastMessage(Message message) { int oldPriority = this.changeThreadPriority(message); String messageID; try{ Thread.sleep(2); messageID = this.getIdent() + "@" + (new DigestManager()).getDigest(System. currentTimeMillis() + ""); Thread.sleep(2); }catch(Exception e){messageID = Math.floor(Math.random()*Math.pow(10,32))+"";} SecureRandom sr = new SecureRandom(); byte[] randomDest = new byte[10]; sr.nextBytes(randomDest); String dest = Base16.toHexString(randomDest); message.fillMessageProperties(this.getIdent(), this.getLocalInetAddress(), dest, 2, messageID); MessageWrapper wm = new MessageWrapper(message, this.getIdent()); wm.disableAutoRetransmit(); try { SenderThread st[] = this.route(wm,null,null,null,false,false); if(st != null){ final SenderThread[] stFinal = st; Thread waiter = new Thread(){ public void run() { for(int x = 0; x < stFinal.length; x++){ try{ stFinal[x].join(Router.routeTimeout); }catch(Exception e){ _logger.error("Cannot join send process",e); } if(stFinal[x].isAlive()){ stFinal[x].interruptSending(); } } } }; waiter.start(); } Thread.currentThread().setPriority(oldPriority); return wm; } catch (Exception e) { _logger.error(this.getShortId() + "",e); Thread.currentThread().setPriority(oldPriority); return wm; } } private SenderThread[] retransmitMessage(MessageWrapper wm) { try { Thread.sleep(1000); } catch (Exception e) { _logger.error(this.getShortId() + "",e); } try { if (!wm.getMessage().getSource().equals("") && wm.getMessage().getSource().equals(this.getIdent())) { try { MessageSigner.getInstance().sign(wm.getMessage()); } catch (Exception e) { _logger.error("Failed signing broadcast message", e); } } if (wm.getRetrasmissions() >= Ant.maxRetransmissions) throw new Exception("Retransmissions exceeeded"); _logger.debug(this.getShortId() + ": Retransmitting Message from id = " + this.getShortId() + " to id = " + wm.getMessage().getDest() + " message id = " + wm.getMessage().getAck_Id()); wm.getMessage().resetDelivered(); /*if (wm.getRetrasmissions() < Ant.maxRetransmissionsForceDirection) { wm.notToBeConsidered = new ArrayList();//Collections.synchronizedList(new ArrayList()) } else*/ if (!wm.notToBeConsidered.contains(wm.getRoutedTo())) { wm.notToBeConsidered.add(wm.getRoutedTo()); this.generateNetModificationAlert(wm.getMessage(), wm.getRoutedTo());// } Neighbour n = this.checkForRoute(wm, wm.notToBeConsidered); if (n != null) { SenderThread[] st = new SenderThread[1]; st[0] = n.route(wm.getMessage()); return st; }else{ if(wm.notToBeConsidered.size() == 0){ ArrayList unreachableNeighbours = new ArrayList(); unreachableNeighbours.add(wm.getMessage().getDest()); this.generateNetModificationAlert(unreachableNeighbours, this.getIdent()); } throw new Exception(this.getShortId() + ": Destination (retransmission) unreachable for message with id = " + wm.getMessage().getAck_Id().substring(0,10) + " type = " + wm.getMessage().getType()); } } catch (Exception e) { _logger.error(this.getShortId() + "",e); return null; } } public Neighbour checkForRoute(MessageWrapper wm, List notToBeConsidered) throws Exception{ Neighbour selectedNeighbour = null; String neighbourId = null; RoutingTableElement rte = (RoutingTableElement)this.routingTable.get(wm.getMessage().getDest()); if(rte != null && rte.getIP() != null){ try{ selectedNeighbour = new DirectNeighbour(rte.getIP(), this); neighbourId = selectedNeighbour.getIdent(); } catch (Exception e) { rte.firewalled = true; } } if(selectedNeighbour == null){ for (int x = 0; rte != null && x < rte.getRoutes().size(); x++) { String routingTo = (String) rte.getRoutes().get(x); Neighbour n; try{ n = new DirectNeighbour(routingTo, this); }catch(Exception e){ n = this.getNeighbour(routingTo); } if (n == null || notToBeConsidered.contains(routingTo)) { continue; } else if (selectedNeighbour == null || selectedNeighbour.getQueuedMessages() > n.getQueuedMessages()) { selectedNeighbour = n; neighbourId = routingTo; } } } if (selectedNeighbour != null && neighbourId != null) { rte.setLastTimeUsed(); if(wm.getMessage().getType() != 1) this.traceMyMessage(wm, neighbourId); _logger.debug(this.getShortId() + ": Routing message with id = " + wm.getMessage().getAck_Id() + " type = " + wm.getMessage().getType() + " to id = " + neighbourId); } return selectedNeighbour; } public boolean existRouteTo(String dest){ RoutingTableElement rte = (RoutingTableElement)this.routingTable.get(dest); if(rte != null && (rte.getIP() != null || rte.getRoutes().size() > 0)) return true; else return false; } public Neighbour checkForRoute(Message m, String requirer, List notToBeConsidered, boolean alreadySeen) throws Exception{ Neighbour selectedNeighbour = null; String neighbourId = null; RoutingTableElement rte = (RoutingTableElement)this.routingTable.get(m.getDest()); if(rte != null && rte.getIP() != null){ try{ selectedNeighbour = new DirectNeighbour(rte.getIP(), this); neighbourId = selectedNeighbour.getIdent(); }catch(Exception e){ rte.firewalled = true; } } if(selectedNeighbour == null){ for (int x = 0; rte != null && x < rte.getRoutes().size(); x++) { String routingTo = (String) rte.getRoutes().get(x); Neighbour n; try{ n = new DirectNeighbour(routingTo, this); }catch(Exception e){ n = this.getNeighbour(routingTo); } if (n == null || notToBeConsidered.contains(routingTo) || routingTo.equals(requirer)) { continue; } else if (selectedNeighbour == null || selectedNeighbour.getQueuedMessages() > n.getQueuedMessages()) { selectedNeighbour = n; neighbourId = routingTo; } } } if (selectedNeighbour != null && neighbourId != null) { rte.setLastTimeUsed(); if(alreadySeen){ int index = this.inTransitMessages.indexOf(m); MessageWrapper wm = (MessageWrapper)this.inTransitMessages.get(index); wm.setRoutedTo(neighbourId); }else{ this.traceMessage(m, requirer, neighbourId); } _logger.debug(this.getShortId() + ": Routing message with id = " + m.getAck_Id() + " type = " + m.getType() + " to id = " + neighbourId); } return selectedNeighbour; } public void uPnPMapping(){ try{ UPnPManager upnpManager = UPnPManager.instance(); int result = 0; if (upnpManager.isNATPresent()) result = upnpManager.mapPort(this.serverPort); if (result > 0) { this.localNattedInetAddress = this.localInetAddress; this.localInetAddress = upnpManager.getNATAddress().getHostAddress(); this.localNattedPort = this.serverPort; this.serverPort = result; } }catch (Exception ex) { _logger.error("", ex); } } public void run() { try { ss = new SecureServer(this, this.serverPort); //Does also UPnP port mapping if (ss.getServerSocket() == null) return; if(this.upnp) UPnPManager.instance().startDevice(); //Update LAN address ants.p2p.query.ServerInfo si = new ants.p2p.query.ServerInfo("", getLanAddress(), new Integer(getServerPort()), ""); _logger.info("Updating UPnP LAN address: "+si); ants.p2p.utils.net.UPnPManager.instance().setCurrentLanAddress(si); while (!terminate) { try { System.gc(); _logger.info("Running ANts main loop..."); for (int z = 0; z < 3; z++) { int sleepTime = 1000; for(int indx = 0; indx < 60; indx++){ this.sleep(sleepTime); //this.propertyChangeSupport.firePropertyChange("updatePacketCompressionStats", null, null); } try { for (int x = this.neighbours.size() - 1; x >= 0; x--) { for (int y = x - 1; y >= 0; y--) { if (this.neighbours.get(x).equals(this.neighbours.get(y))) { this.removeNeighbour( (NeighbourAnt)this.neighbours.get(x)); _logger.info("Removing duplicated neighbour..."); break; } } } boolean[] activeUploadNeighbour = new boolean[this.neighbours.size()]; boolean[] activeDownloadNeighbour = new boolean[this.neighbours.size()]; for (int x = 0; x < this.neighbours.size(); x++) { NeighbourAnt toBeRemoved = (NeighbourAnt)this.neighbours.get(x); _logger.info(toBeRemoved + " Alive: " + toBeRemoved.isAlive() + " Connected:"+toBeRemoved.isConnected()); if (! toBeRemoved.isAlive() || ! toBeRemoved.isConnected()) { this.removeNeighbour(toBeRemoved); propertyChangeSupport.firePropertyChange("removedNeighbour", null, toBeRemoved); _logger.info("Removing stuck neighbour..."); }else if(toBeRemoved.getLastProbedAt() < System.currentTimeMillis() - probeCheckInterval) { toBeRemoved.terminate(); propertyChangeSupport.firePropertyChange("removedNeighbour", null, toBeRemoved); _logger.info("Removing stuck neighbour..."); }else{ NeighbourAnt current = (NeighbourAnt)this.neighbours.get(x); if (System.currentTimeMillis() - current.getLastActiveDownloadTime() > Ant.messageTimeout * Ant.maxRetransmissions) { activeDownloadNeighbour[x] = false; }else{ activeDownloadNeighbour[x] = true; } if(System.currentTimeMillis() - current.getLastActiveUploadTime() > Ant.messageTimeout * Ant.maxRetransmissions){ activeUploadNeighbour[x] = false; }else{ activeUploadNeighbour[x] = true; } //this.checkNeighboursActivity(activeDownloadNeighbour, activeUploadNeighbour); } } } catch (Exception e) { _logger.error(this.getShortId() + ": Error in removing stuck neighbour", e); } } //synchronized(this){ while (this.failedMessages.size() > Ant.maxFailedMessageToTrace) { this.failedMessages.remove(0); } //} Enumeration netMods = this.netModifications.keys(); while (netMods.hasMoreElements()) { String mod = (String) netMods.nextElement(); if (System.currentTimeMillis() - ( (Long)this.netModifications.get(mod)).longValue() > netModificationsTimeout) { this.netModifications.remove(mod); } } Enumeration routingTableElementKeys = this.routingTable.keys(); ArrayList un = new ArrayList(); while (routingTableElementKeys.hasMoreElements()) { String routingTableElementKey = (String) routingTableElementKeys.nextElement(); if (System.currentTimeMillis() - ( (RoutingTableElement)this.routingTable.get(routingTableElementKey)).getLastTimeUsed() > routeInactiveTimeout) { this.routingTable.remove(routingTableElementKey); un.add(routingTableElementKey); } } this.generateNetModificationAlert(un, this.getIdent()); //synchronized (this.myMessages) { if (this.myMessages.size() != 0) { _logger.debug(this.getShortId() + ": Processing " + this.myMessages.size() + " messages for eventual retransmission..."); _logger.debug("There are " + this.failedMessages.size() + " messages failed!"); ArrayList toBeRemoved = new ArrayList(); for (int x = 0; x < this.myMessages.size(); x++) { MessageWrapper wm = (MessageWrapper)this.myMessages.get(x); if (wm.getLifetime() > Ant.messageTimeout) { if (wm.getRetrasmissions() + 1 < Ant.maxRetransmissions) { wm.retrasmitted(this); SenderThread[] st = this.route(wm,null,null,null,false,true); if (st != null) { final SenderThread[] stFinal = st; Thread waiter = new Thread() { public void run() { for (int x = 0; x < stFinal.length; x++) { try { stFinal[x].join(Router.routeTimeout); } catch (Exception e) { _logger.error("Cannot join send process", e); } if (stFinal[x].isAlive()) { stFinal[x].interruptSending(); } } } }; waiter.start(); } _logger.debug("Retransmited Message: " + wm.getMessage().getAck_Id() + wm.getMessage().getType()); } else { if (wm.getMessage().getType() == 2) { _logger.info(this.getShortId() + ": Broadcast message timed out " + wm.getMessage().getDest()); } else if (wm.getMessage().getType() == 0 && wm.getMessage().getSource().equals("")) { _logger.info(this.getShortId() + ": Masked message timed out " + wm.getMessage().getDest()); } else { _logger.info(this.getShortId() + ": Destination unreachable " + wm.getMessage().getDest().substring(0,10)); //synchronized (this.failedMessages) { this.failedMessages.add(wm); //} } toBeRemoved.add(wm); } } } synchronized(this){ for (int x = toBeRemoved.size() - 1; x >= 0; x--) { MessageWrapper wm = (MessageWrapper) toBeRemoved.get(x); if (this.myMessages.remove(wm)) { _logger.info("Removed Message from myMessages: " + wm.getMessage().getAck_Id().substring(0,10) + wm.getMessage().getType()); } } } //this.myMessages.trimToSize(); } //} }catch (Exception cycleException) { _logger.error("ANt(ID_" + this.getShortId() + ") main cycle error!", cycleException); } } _logger.error("ANts MAIN ENGINE TERMINATED********************************************************"); } catch (Exception e) { this.terminate = true; _logger.info(this.getShortId() + " - Failure in setting up Ant server port", e); _logger.error("ANts MAIN ENGINE TERMINATED********************************************************"); } } private void checkNeighboursActivity(boolean[] activeDownload, boolean[] activeUpload){ int activeNumber = 0; int activeUp = 0; int activeDown = 0; final int maxActiveOneWay = (int)Math.floor(Ant.maxNeighbours * 2.0 / 3); for(int x = 0; x < activeDownload.length; x++){ if (activeDownload[x] == true) { activeDown++; } if (activeUpload[x] == true) { activeUp++; } if (activeUpload[x] == true || activeDownload[x] == true) { activeNumber++; } } if((this.neighbours.size() - activeNumber) > Ant.maxNeighbours * 2.0 / 3){ int toBeRemoved = -1; long currentPing = 0; for (int x = 0; x < this.neighbours.size(); x++) { NeighbourAnt current = (NeighbourAnt)this.neighbours.get(x); if(activeDownload[x] == false && activeUpload[x] == false && current.getTimeElapsed() > currentPing){ currentPing = current.getTimeElapsed(); toBeRemoved = x; } } if(toBeRemoved >= 0){ _logger.info("Neighbour "+this.neighbours.get(toBeRemoved)+" is not active up/down, removing..."); this.removeNeighbour( (NeighbourAnt)this.neighbours.get(toBeRemoved)); } } else if (activeNumber > Ant.maxNeighbours * 2.0 / 3) { if (activeUp == this.neighbours.size() && (activeUp - activeDown) > maxActiveOneWay) { int toBeRemoved = -1; long currentPing = 0; for (int x = 0; x < this.neighbours.size(); x++) { NeighbourAnt current = (NeighbourAnt)this.neighbours.get(x); if (activeDownload[x] == false && activeUpload[x] == true && current.getTimeElapsed() > currentPing) { currentPing = current.getTimeElapsed(); toBeRemoved = x; } } if (toBeRemoved >= 0) { _logger.info("Neighbour " + this.neighbours.get(toBeRemoved) + " is not active down, removing..."); this.removeNeighbour( (NeighbourAnt)this.neighbours.get(toBeRemoved)); } } else if (activeDown == this.neighbours.size() && (activeDown - activeUp) > maxActiveOneWay) { int toBeRemoved = -1; long currentPing = 0; for (int x = 0; x < this.neighbours.size(); x++) { NeighbourAnt current = (NeighbourAnt)this.neighbours.get(x); if (activeDownload[x] == true && activeUpload[x] == false && current.getTimeElapsed() > currentPing) { currentPing = current.getTimeElapsed(); toBeRemoved = x; } } if (toBeRemoved >= 0) { _logger.info("Neighbour " + this.neighbours.get(toBeRemoved) + " is not active up, removing..."); this.removeNeighbour( (NeighbourAnt)this.neighbours.get(toBeRemoved)); } } } } protected void processMessage(Message m, Router r) throws Exception { _logger.debug(this.getShortId() + ": Received Message = " + m.getAck_Id() + " type = " + m.getType() + " from a peer running version: " + m.getVersion()); } public PropertyChangeSupport getPropertyChangeSupport() { return this.propertyChangeSupport; } public void disconnect() { try { for (int x = 0; x < this.neighbours.size(); x++) { NeighbourAnt na = ( (NeighbourAnt)this.neighbours.get(x)); na.terminate(); } if (ss.getServerSocket() != null) { ss.getServerSocket().close(); } UPnPManager.instance().stopDevice(); if(UPnPManager.instance().mappingsExist()){ UPnPManager.instance().clearMappingsOnShutdown(); this.localInetAddress = this.localNattedInetAddress; this.serverPort = this.localNattedPort; } this.terminate = true; } catch (Exception e) { _logger.error(this.getShortId() + "",e); } } public boolean isDisconnected() { return this.terminate; } }

The table below shows all metrics for Ant.java.

MetricValueDescription
BLOCKS384.00Number of blocks
BLOCK_COMMENT15.00Number of block comment lines
COMMENTS52.00Comment lines
COMMENT_DENSITY 0.03Comment density
COMPARISONS532.00Number of comparison operators
CYCLOMATIC524.00Cyclomatic complexity
DECL_COMMENTS 5.00Comments in declarations
DOC_COMMENT 0.00Number of javadoc comment lines
ELOC1530.00Effective lines of code
EXEC_COMMENTS21.00Comments in executable code
EXITS184.00Procedure exits
FUNCTIONS60.00Number of function declarations
HALSTEAD_DIFFICULTY169.77Halstead difficulty
HALSTEAD_EFFORT 0.00Halstead effort
INTERFACE_COMPLEXITY182.00Interface complexity
JAVA0001 1.00JAVA0001 Package name does not contain only lower case letters
JAVA0002 1.00JAVA0002 Package name does not begin with a top level domain name or country code
JAVA0003 1.00JAVA0003 Minimize use of on-demand (.*) imports
JAVA0004 0.00JAVA0004 Unnecessary import from java.lang
JAVA0005 1.00JAVA0005 Imports not in specified order
JAVA0006 0.00JAVA0006 Empty finally block
JAVA000722.00JAVA0007 Should not declare public field
JAVA0008 1.00JAVA0008 Empty catch block
JAVA0009 0.00JAVA0009 Protected member in final class
JAVA0010 0.00JAVA0010 Non-instantiable class does not contain a non-private static member
JAVA0011 0.00JAVA0011 Abstract class does not contain an abstract method
JAVA0012 0.00JAVA0012 Non-constructor method with same name as declaring class
JAVA0013 0.00JAVA0013 Non-blank final field is not static
JAVA0014 0.00JAVA0014 Class with only static members has non-private constructor
JAVA0015 0.00JAVA0015 Package class contains public nested type
JAVA0016 0.00JAVA0016 Abstract class contains public constructor
JAVA0017 0.00JAVA0017 Class name does not have required form
JAVA0018 0.00JAVA0018 Method name does not have required form
JAVA0019 0.00JAVA0019 Interface name does not have required form
JAVA0020 0.00JAVA0020 Field name does not have required form
JAVA0021 0.00JAVA0021 Interface method name does not have required form
JAVA0022 0.00JAVA0022 Static final field name does not have required form
JAVA0023 0.00JAVA0023 Empty finalize method
JAVA0024 0.00JAVA0024 Empty class
JAVA0025 0.00JAVA0025 Method override is empty
JAVA0026 0.00JAVA0026 Finalize method with parameters
JAVA0029 1.00JAVA0029 Private method not used
JAVA0030 0.00JAVA0030 Private field not used
JAVA0031 6.00JAVA0031 Case statement not properly closed
JAVA0032 0.00JAVA0032 Switch statement missing default
JAVA0033 0.00JAVA0033 default: not last case in switch statement
JAVA003491.00JAVA0034 Missing braces in if statement
JAVA0035 2.00JAVA0035 Missing braces in for statement
JAVA0036 0.00JAVA0036 Missing braces in while statement
JAVA0038 0.00JAVA0038 Non-case label in switch statement
JAVA0039 0.00JAVA0039 Break statement with label
JAVA0040 1.00JAVA0040 Switch statement contains N cases (maximum: M)
JAVA0041 0.00JAVA0041 Nested synchronized block
JAVA0042 0.00JAVA0042 Empty synchronized statement
JAVA0043 0.00JAVA0043 Inner class does not use outer class
JAVA0044 0.00JAVA0044 Serializable class with no instance variables
JAVA0045 0.00JAVA0045 Serializable class with only transient fields
JAVA0046 0.00JAVA0046 Name of class not derived from Exception ends with 'Exception'
JAVA0047 0.00JAVA0047 Serializable class derives from invalid base class
JAVA0048 0.00JAVA0048 Name of class derived from Exception does not end with 'Exception'
JAVA004940.00JAVA0049 Nested block at depth N (maximum: M)
JAVA0050 0.00JAVA0050 Class derives from java.lang.Error
JAVA0051 0.00JAVA0051 Class derives from java.lang.RuntimeException
JAVA0052 0.00JAVA0052 Class derives from java.lang.Throwable
JAVA0053 0.00JAVA0053 Unused label
JAVA0054 0.00JAVA0054 Inheritance depth N exceeds maximum M
JAVA0055 0.00JAVA0055 Class should be interface
JAVA0056 0.00JAVA0056 Unnecessary abstract modifier for interface or annotation
JAVA0057 0.00JAVA0057 Unnecessary default constructor
JAVA0058 0.00JAVA0058 Constructor calls super()
JAVA0059 0.00JAVA0059 Method override only calls super()
JAVA0061 0.00JAVA0061 Inaccessible member in anonymous class
JAVA0062 0.00JAVA0062 Public class missing public member or protected constructor
JAVA0063 0.00JAVA0063 Identifier name should not contain '$'
JAVA0064 1.00JAVA0064 N variations of identifier name (maximum: M)
JAVA0065 0.00JAVA0065 Unnecessary final modifier for method in final class
JAVA0066 0.00JAVA0066 Unnecessary modifier for interface nested type
JAVA0067 1.00JAVA0067 Array descriptor on identifier name
JAVA0068 0.00JAVA0068 Modifiers not declared in recommended order
JAVA0071 0.00JAVA0071 Strings compared with ==
JAVA0073 0.00JAVA0073 Integer division in floating-point context
JAVA0074 0.00JAVA0074 Use of Object.notify()
JAVA0075 2.00JAVA0075 Method parameter hides field
JAVA007689.00JAVA0076 Use of magic number
JAVA0077 0.00JAVA0077 Private field not used in declaring class
JAVA0078 0.00JAVA0078 Floating point values compared with ==
JAVA007911.00JAVA0079 Use of instance to reference static member
JAVA0080 0.00JAVA0080 Import declaration not used
JAVA008110.00JAVA0081 Boolean literal in comparison
JAVA0082 1.00JAVA0082 Unnecessary widening cast
JAVA0083 2.00JAVA0083 Unnecessary instanceof test
JAVA0084 0.00JAVA0084 Should use compound assignment operator
JAVA0085 0.00JAVA0085 Use of sun.* class
JAVA0087 7.00JAVA0087 Use of Thread.sleep()
JAVA0089 0.00JAVA0089 Use of restricted package
JAVA0092 0.00JAVA0092 Use of restricted type
JAVA0093 0.00JAVA0093 Redundant assignment
JAVA0094 0.00JAVA0094 Field hides a superclass field
JAVA0095 0.00JAVA0095 Uninitialized private field
JAVA0096 0.00JAVA0096 Field in nested class hides outer field
JAVA0098 0.00JAVA0098 Minimize use of implicit field initializers
JAVA0100 1.00JAVA0100 Class contains N non-final fields (maximum: M)
JAVA0101 0.00JAVA0101 Unnecessary modifier for field in interface
JAVA0102 0.00JAVA0102 Last statement in finalize() not super.finalize()
JAVA0103 0.00JAVA0103 Explicit call to finalize()
JAVA0104 0.00JAVA0104 finalize() only calls super.finalize()
JAVA0105 0.00JAVA0105 Duplicate import declaration
JAVA0106 0.00JAVA0106 Unnecessary import from current package
JAVA0108 0.00JAVA0108 Incorrect javadoc: no @param tag for 'parameter'
JAVA0109 0.00JAVA0109 Incorrect javadoc: no parameter 'parameter'
JAVA0110 0.00JAVA0110 Incorrect javadoc: no @return tag
JAVA0111 0.00JAVA0111 Incorrect javadoc: @return tag for void method
JAVA0112 0.00JAVA0112 Incorrect javadoc: no exception 'exception' in throws
JAVA0113 0.00JAVA0113 Incorrect javadoc: no @author tag
JAVA0114 0.00JAVA0114 Incorrect javadoc: no @version tag
JAVA0115 0.00JAVA0115 Incorrect javadoc: no @throws or @exception tag for 'exception'
JAVA011622.00JAVA0116 Missing javadoc: field 'field'
JAVA011749.00JAVA0117 Missing javadoc: method 'method'
JAVA0118 1.00JAVA0118 Missing javadoc: type 'type'
JAVA0119 0.00JAVA0119 Control variable changed within body of for loop
JAVA0123 0.00JAVA0123 Use all three components of for loop
JAVA0125 0.00JAVA0125 Continue statement with label
JAVA0126 0.00JAVA0126 Method declares unchecked exception in throws
JAVA0128 0.00JAVA0128 Public constructor in non-public class
JAVA0130 1.00JAVA0130 Non-static method does not use instance fields
JAVA0131 0.00JAVA0131 Compatible method does not override base
JAVA0132 0.00JAVA0132 Method overload with compatible signature
JAVA0133 0.00JAVA0133 Non-synchronized method overrides synchronized method
JAVA0135 1.00JAVA0135 Only one of Object.equals and Object.hashCode defined: missing 'method'
JAVA0136 1.00JAVA0136 N methods defined in class (maximum: M)
JAVA0137 0.00JAVA0137 Non-abstract class missing constructor
JAVA0138 2.00JAVA0138 N parameters defined for method (maximum: M)
JAVA0139 0.00JAVA0139 Definition of main other than public static void main(java.lang.String[])
JAVA0141 0.00JAVA0141 Unnecessary modifier for method in interface
JAVA0143 1.00JAVA0143 Synchronized method
JAVA0144 9.00JAVA0144 Line exceeds maximum M characters
JAVA0145 0.00JAVA0145 Tab character used in source file
JAVA0150 0.00JAVA0150 java.lang.Error (or subclass) thrown
JAVA0153 0.00JAVA0153 Inefficient conversion of integer to string
JAVA0159 0.00JAVA0159 Inefficient conversion of string to integer
JAVA0160 5.00JAVA0160 Method does not throw specified exception
JAVA0161 0.00JAVA0161 Conditional wait() not in loop
JAVA0163 0.00JAVA0163 Empty statement
JAVA0165 0.00JAVA0165 Conflicting return statement in finally block
JAVA016638.00JAVA0166 Generic exception caught
JAVA0167 0.00JAVA0167 ThreadDeath not rethrown
JAVA0169 0.00JAVA0169 Unnecessary catch block: exception 'exception'
JAVA0170 0.00JAVA0170 Caught exception not derived from java.lang.Exception
JAVA0171 0.00JAVA0171 Unused local variable
JAVA0173 1.00JAVA0173 Unused method parameter
JAVA0174 0.00JAVA0174 Assigned local variable never used
JAVA0175 0.00JAVA0175 Successive assignment to variable
JAVA0176 0.00JAVA0176 Local variable name does not have required form
JAVA0177 6.00JAVA0177 Variable declaration missing initializer
JAVA0179 0.00JAVA0179 Local variable hides visible field
JAVA0233 0.00JAVA0233 Definition of serialVersionUID other than 'private static final long serialVersionUID'
JAVA0234 0.00JAVA0234 Class is Serializable but does not define serialVersionUID
JAVA0235 0.00JAVA0235 Class defines serialVersionUID but does not implement Serializable
JAVA0236 0.00JAVA0236 Attempt to clone an object which does not implement Cloneable
JAVA0237 0.00JAVA0237 Class implements Cloneable but does not have public clone method
JAVA0238 0.00JAVA0238 Clone method does not call super.clone()
JAVA0239 0.00JAVA0239 Class declares 'readObject' or 'writeObject' but does not implement Serializable
JAVA0240 0.00JAVA0240 Serializable class which declares readObject or writeObject but not both
JAVA0241 0.00JAVA0241 'readObject' or 'writeObject' should be declared private in Serializable class
JAVA0242 0.00JAVA0242 Transient field in non-Serializable class
JAVA0243 0.00JAVA0243 'readResolve' or 'writeReplace' should be declared private or protected
JAVA0244 0.00JAVA0244 Field or method name in subclass differs only by case from inherited field or method
JAVA0245 0.00JAVA0245 JUnit TestCase with non-trivial constructor
JAVA0246 0.00JAVA0246 JUnit assertXXX statement missing message parameter
JAVA0247 0.00JAVA0247 JUnit 'setUp()' and 'tearDown()' should call super method
JAVA0248 0.00JAVA0248 JUnit method 'setUp' or 'tearDown' with incorrect signature
JAVA0249 0.00JAVA0249 JUnit TestCase 'suite()' should be declared static
JAVA0250 0.00JAVA0250 JUnit TestCase declares testXXX method with incorrect signature
JAVA0251 0.00JAVA0251 Use '%n' for line breaks in printf/format for platform independence
JAVA0252 0.00JAVA0252 'enum' is a Java 1.5 reserved word
JAVA0253 0.00JAVA0253 Not all enum constants consumed in switch statement
JAVA0254 0.00JAVA0254 Use enhanced for loop construct instead of Iterator
JAVA0255 0.00JAVA0255 Result of method invocation not used
JAVA0256 0.00JAVA0256 Assignment of external collection/array to field
JAVA0257 0.00JAVA0257 Use of 'Constant Interface' anti-pattern
JAVA0258 0.00JAVA0258 Implement Iterable for foreach compatibility
JAVA0259 2.00JAVA0259 Return of collection/array field
JAVA0260 0.00JAVA0260 Use 'enum' instead of Enumerated Type pattern
JAVA0261 0.00JAVA0261 Use specialized Enum collection types
JAVA0262 0.00JAVA0262 Use of char in integer context
JAVA0263 0.00JAVA0263 Long literal ends with 'l' instead of 'L'
JAVA0264 1.00JAVA0264 Integer math in long context - check for overflow
JAVA0265 0.00JAVA0265 Use of Throwable.printStackTrace()
JAVA0266 0.00JAVA0266 Use of System.out
JAVA0267 0.00JAVA0267 Use of System.err
JAVA0269 0.00JAVA0269 Contents of StringBuffer never used
JAVA0270 5.00JAVA0270 Use Java 5.0 enhanced for loop construct to iterate over all elements in an array
JAVA0271 0.00JAVA0271 Minimize use of on-demand (.*) static imports
JAVA0272 1.00JAVA0272 Thread.run() called
JAVA0273 1.00JAVA0273 Non-final derivative of Thread calls start() in constructor
JAVA0274 0.00JAVA0274 Serializable class has a synchronized readObject()
JAVA0275 0.00JAVA0275 Serializable class has a synchronized writeObject() and no other synchronized methods
JAVA0276 0.00JAVA0276 Unnecessary use of String constructor
JAVA0277 0.00JAVA0277 Iterator.next() implementation does not throw NoSuchElementException
JAVA0278 0.00JAVA0278 Unnecessary use of Boolean constructor
JAVA0279 0.00JAVA0279 Serialization method readObject or readObjectNoData calls an overridable method
JAVA0280 0.00JAVA0280 IllegalMonitorStateException caught
JAVA0281 0.00JAVA0281 Iterator.next() not called in loop
JAVA0282 0.00JAVA0282 Call to Iterator.next() in loop which does not test Iterator.hasNext()
JAVA0283 4.00JAVA0283 Control variable not updated in loop body
JAVA0284 1.00JAVA0284 Explicit garbage collection
JAVA0285 2.00JAVA0285 Dereference of potentially null variable
JAVA0286 0.00JAVA0286 Dereference of null variable
JAVA0287 4.00JAVA0287 Unnecessary null check
JAVA0288 0.00JAVA0288 Inconsistent null check
LINES2032.00Number of lines in the source file
LINE_COMMENT37.00Number of line comments
LOC1876.00Lines of code
LOGICAL_LINES847.00Number of statements
LOOPS42.00Number of loops
NEST_DEPTH12.00Maximum nesting depth
OPERANDS4674.00Number of operands
OPERATORS10439.00Number of operators
PARAMS67.00Number of formal parameter declarations
PROGRAM_LENGTH15113.00Halstead program length
PROGRAM_VOCAB945.00Halstead program vocabulary
PROGRAM_VOLUME 0.00Halstead program volume
RETURNS115.00Number of return points from functions
SIZE84658.00Size of the file in bytes
UNIQUE_OPERANDS881.00Number of unique operands
UNIQUE_OPERATORS64.00Number of unique operators
WHITESPACE104.00Number of whitespace lines