From ac5cf14be05facc798e3f8cc655db95ba10f7c6d Mon Sep 17 00:00:00 2001 From: chenzo Date: Tue, 2 Oct 2012 09:23:40 +0000 Subject: [PATCH] Adds extra function to export harvesting time. --- src/org/ice4j/ice/Agent.java | 39 +++++++++++ .../ice4j/ice/harvest/CandidateHarvester.java | 64 +++++++++++++++++- .../ice/harvest/CandidateHarvesterSet.java | 9 +-- .../ice/harvest/HostCandidateHarvester.java | 67 +++++++++++++++++-- .../ice/harvest/StunCandidateHarvester.java | 2 +- src/org/ice4j/ice/harvest/UPNPHarvester.java | 2 +- 6 files changed, 168 insertions(+), 15 deletions(-) diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java index f509ba4..d68f073 100644 --- a/src/org/ice4j/ice/Agent.java +++ b/src/org/ice4j/ice/Agent.java @@ -2168,4 +2168,43 @@ public class Agent } return null; } + + + /** + * Returns the harvesting time (in ms) for the harvester given in parameter. + * + * @param harvesterName The class name if the harvester. + * + * @return The harvesting time (in ms) for the harvester given in parameter. + * -1 if this harvester does not exists, if the ICE agent is null, or if the + * agent is not currently harvesting with this harvester. + */ + public long getHarvestingTime(String harvesterName) + { + if(this.hostCandidateHarvester.getClass().getName().endsWith( + harvesterName)) + { + return this.hostCandidateHarvester.getHarvestingTime(); + } + + long harvestingTime = -1; + CandidateHarvester tmpHarvester; + Iterator itHarvesters = this.harvesters.iterator(); + while(itHarvesters.hasNext()) + { + tmpHarvester = itHarvesters.next(); + if(tmpHarvester.getClass().getName().endsWith(harvesterName)) + { + harvestingTime = tmpHarvester.getHarvestingTime(); + // Theremay be several harvester with the same class name. Thus, + // returns only an active one. + if(harvestingTime != -1) + { + return harvestingTime; + } + } + } + + return -1; + } } diff --git a/src/org/ice4j/ice/harvest/CandidateHarvester.java b/src/org/ice4j/ice/harvest/CandidateHarvester.java index 24ea5aa..9fc02d3 100644 --- a/src/org/ice4j/ice/harvest/CandidateHarvester.java +++ b/src/org/ice4j/ice/harvest/CandidateHarvester.java @@ -19,8 +19,19 @@ import org.ice4j.ice.*; * @author Emil Ivov * @author Lyubomir Marinov */ -public interface CandidateHarvester +public abstract class CandidateHarvester { + /** + * The last harvest start time for this harvester. -1 if this harvester is + * not currently harvesting. + */ + private long lastStartHarvestingTime = -1; + + /** + * The last ended harvesting time for this harvester. -1 if this harvester + * has never harvested yet. + */ + private long lastHarvestingTime = -1; /** * Gathers all candidate addresses of the type that this @@ -38,5 +49,54 @@ public interface CandidateHarvester * as they are discovered, they should also be returned in order to make * sure that the gathering will be considered successful. */ - public Collection harvest(Component component); + public abstract Collection harvest(Component component); + + /** + * Starts the harvesting timer. Called when the harvest begins. + */ + public void startHarvesting() + { + // Remember the start date of this harvester. + this.lastStartHarvestingTime = System.currentTimeMillis(); + // Reset the last harvesting time. + this.lastHarvestingTime = -1; + } + + /** + * Stops the harvesting timer. Called when the harvest ends. + */ + public void stopHarvesting() + { + // Remember the last harvesting time. + this.lastHarvestingTime = this.getHarvestingTime(); + // Stops the current timer. + this.lastStartHarvestingTime = -1; + } + + /** + * Returns the current harvesting time in ms. If this harvester is not + * currently harvesting, then returns the value of the last harvesting time. + * -1 if this harvester has nerver harvested. + * + * @return The current harvesting time in ms. If this harvester is not + * currently harvesting, then returns the value of the last harvesting time. + * -1 if this harvester has nerver harvested. + */ + public long getHarvestingTime() + { + if(this.lastStartHarvestingTime != -1) + { + long currentHarvestingTime + = System.currentTimeMillis() - lastStartHarvestingTime; + // Retest here, while the harvesting may be end while computing the + // harvsting time. + if(this.lastStartHarvestingTime != -1) + { + return currentHarvestingTime; + } + } + // If we are ont currently harvesting, then returns the value of the + // last harvesting time. + return this.lastHarvestingTime; + } } diff --git a/src/org/ice4j/ice/harvest/CandidateHarvesterSet.java b/src/org/ice4j/ice/harvest/CandidateHarvesterSet.java index bc81517..a6e0e60 100644 --- a/src/org/ice4j/ice/harvest/CandidateHarvesterSet.java +++ b/src/org/ice4j/ice/harvest/CandidateHarvesterSet.java @@ -416,17 +416,14 @@ public class CandidateHarvesterSet { if (isEnabled()) { - long startCandidateHarvestTime = System.currentTimeMillis(); - + harvester.startHarvesting(); Collection candidates = harvester.harvest(component); + harvester.stopHarvesting(); - long stopCandidateHarvestTime = System.currentTimeMillis(); - long candidateHarvestTime - = stopCandidateHarvestTime - startCandidateHarvestTime; logger.info( "End candidate harvest within " - + candidateHarvestTime + + harvester.getHarvestingTime() + " ms, for " + harvester.getClass().getName() + ", component: " + component.getComponentID()); diff --git a/src/org/ice4j/ice/harvest/HostCandidateHarvester.java b/src/org/ice4j/ice/harvest/HostCandidateHarvester.java index 261f529..5767cad 100644 --- a/src/org/ice4j/ice/harvest/HostCandidateHarvester.java +++ b/src/org/ice4j/ice/harvest/HostCandidateHarvester.java @@ -33,6 +33,18 @@ public class HostCandidateHarvester private static final Logger logger = Logger.getLogger(HostCandidateHarvester.class.getName()); + /** + * The last harvest start time for this harvester. -1 if this harvester is + * not currently harvesting. + */ + private long lastStartHarvestingTime = -1; + + /** + * The last ended harvesting time for this harvester. -1 if this harvester + * has never harvested yet. + */ + private long lastHarvestingTime = -1; + /** * Gathers all candidate addresses on the local machine, binds sockets on * them and creates {@link HostCandidate}s. The harvester would always @@ -64,7 +76,7 @@ public class HostCandidateHarvester throws IllegalArgumentException, IOException { - long startCandidateHarvestTime = System.currentTimeMillis(); + this.startHarvesting(); Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); @@ -172,12 +184,10 @@ public class HostCandidateHarvester + " maxPort=" + maxPort); } - long stopCandidateHarvestTime = System.currentTimeMillis(); - long candidateHarvestTime - = stopCandidateHarvestTime - startCandidateHarvestTime; + this.stopHarvesting(); logger.info( "End candidate harvest within " - + candidateHarvestTime + + this.getHarvestingTime() + " ms, for " + this.getClass().getName() + ", component: " + component.getComponentID()); @@ -403,4 +413,51 @@ public class HostCandidateHarvester + ") and maxPort (" + maxPort + ")"); } } + + /** + * Starts the harvesting timer. Called when the harvest begins. + */ + public void startHarvesting() + { + // Remember the start date of this harvester. + this.lastStartHarvestingTime = System.currentTimeMillis(); + } + + /** + * Stops the harvesting timer. Called when the harvest ends. + */ + public void stopHarvesting() + { + // Remember the last harvesting time. + this.lastHarvestingTime = this.getHarvestingTime(); + // Stops the current timer. + this.lastStartHarvestingTime = -1; + } + + /** + * Returns the current harvesting time in ms. If this harvester is not + * currently harvesting, then returns the value of the last harvesting time. + * -1 if this harvester has nerver harvested. + * + * @return The current harvesting time in ms. If this harvester is not + * currently harvesting, then returns the value of the last harvesting time. + * -1 if this harvester has nerver harvested. + */ + public long getHarvestingTime() + { + if(this.lastStartHarvestingTime != -1) + { + long currentHarvestingTime + = System.currentTimeMillis() - lastStartHarvestingTime; + // Retest here, while the harvesting may be end while computing the + // harvsting time. + if(this.lastStartHarvestingTime != -1) + { + return currentHarvestingTime; + } + } + // If we are ont currently harvesting, then returns the value of the + // last harvesting time. + return this.lastHarvestingTime; + } } diff --git a/src/org/ice4j/ice/harvest/StunCandidateHarvester.java b/src/org/ice4j/ice/harvest/StunCandidateHarvester.java index e5e6414..cb28dfc 100644 --- a/src/org/ice4j/ice/harvest/StunCandidateHarvester.java +++ b/src/org/ice4j/ice/harvest/StunCandidateHarvester.java @@ -26,7 +26,7 @@ import org.ice4j.stack.*; * @author Lyubomir Marinov */ public class StunCandidateHarvester - implements CandidateHarvester + extends CandidateHarvester { /** diff --git a/src/org/ice4j/ice/harvest/UPNPHarvester.java b/src/org/ice4j/ice/harvest/UPNPHarvester.java index b782656..41c05a3 100644 --- a/src/org/ice4j/ice/harvest/UPNPHarvester.java +++ b/src/org/ice4j/ice/harvest/UPNPHarvester.java @@ -24,7 +24,7 @@ import org.bitlet.weupnp.*; * @author Sebastien Vincent */ public class UPNPHarvester - implements CandidateHarvester + extends CandidateHarvester { /** * The logger.