package cc.twittertools.spider;

import java.util.concurrent.TimeUnit;

import org.joda.time.DateTime;

import com.j256.simplejmx.common.JmxAttributeField;
import com.j256.simplejmx.common.JmxResource;

/**
 * Configurable class that gets called between every
 * call to the HTTP client, in order to limit the total amount of
 * bandwidth used.
 */
@JmxResource(description = "Limits HTTP Requests per unit time", domainName = "cc.twittertools.spider", beanName = "Throttle")
public class Throttle
{
  @JmxAttributeField(description = "Inter-request time in milliseconds outside of business hours", isWritable = true)
  private       long eveningInterRequestWaitMs = TimeUnit.SECONDS.toMillis(5);
  
  @JmxAttributeField(description = "Inter-request time in milliseconds outside of business hours", isWritable = true)
  private       long dayTimeInterRequestWaitMs = TimeUnit.SECONDS.toMillis(10);

  
  /**
   * Pause for some time to stop saturating the network. The 
   * duration depends on whether we're currently active 
   * during working hours or not.
   * @throws InterruptedException
   */
  public void pause() throws InterruptedException
  { DateTime now = new DateTime();
    if (now.getDayOfWeek() < 6 && now.getHourOfDay() >= 8 && now.getHourOfDay() < 19)
      Thread.sleep(dayTimeInterRequestWaitMs);
    else
      Thread.sleep(eveningInterRequestWaitMs);
  }


  public long getEveningInterRequestWaitMs() {
    return eveningInterRequestWaitMs;
  }


  public void setEveningInterRequestWaitMs(long eveningInterRequestWaitMs) {
    this.eveningInterRequestWaitMs = eveningInterRequestWaitMs;
  }


  public long getDayTimeInterRequestWaitMs() {
    return dayTimeInterRequestWaitMs;
  }


  public void setDayTimeInterRequestWaitMs(long dayTimeInterRequestWaitMs) {
    this.dayTimeInterRequestWaitMs = dayTimeInterRequestWaitMs;
  }
  
  
}

@@ -63,16 +63,63 @@
 imeUnit;
+%0Aimport java.util.concurrent.atomic.AtomicLong;
 %0A%0Aimport
@@ -189,20 +189,21 @@
 ttribute
-Fiel
+Metho
 d;%0Aimpor
@@ -545,126 +545,8 @@
 e%0A%7B%0A
-  @JmxAttributeField(description = %22Inter-request time in milliseconds outside of business hours%22, isWritable = true)%0A
   pr
@@ -553,25 +553,31 @@
 ivate       
-l
+AtomicL
 ong eveningI
@@ -588,32 +588,48 @@
 RequestWaitMs = 
+new AtomicLong (
 TimeUnit.SECONDS
@@ -642,133 +642,16 @@
 lis(
-5
+3)
 );%0A  %0A
-  @JmxAttributeField(description = %22Inter-request time in milliseconds outside of business hours%22, isWritable = true)%0A
   pr
@@ -662,17 +662,23 @@
 e       
-l
+AtomicL
 ong dayT
@@ -701,16 +701,32 @@
 aitMs = 
+new AtomicLong (
 TimeUnit
@@ -747,10 +747,10 @@
 lis(
-10
+6)
 );%0A%0A
@@ -1162,24 +1162,30 @@
 equestWaitMs
+.get()
 );%0A    else%0A
@@ -1228,16 +1228,22 @@
 stWaitMs
+.get()
 );%0A  %7D%0A%0A
@@ -1235,32 +1235,132 @@
 s.get());%0A  %7D%0A%0A%0A
+  @JmxAttributeMethod(description = %22Inter-request time in milliseconds outside of business hours%22)%0A
   public long ge
@@ -1422,32 +1422,38 @@
 equestWaitMs
+.get()
 ;%0A  %7D%0A%0A%0A
   public voi
@@ -1432,32 +1432,132 @@
 Ms.get();%0A  %7D%0A%0A%0A
+  @JmxAttributeMethod(description = %22Inter-request time in milliseconds outside of business hours%22)%0A
   public void se
@@ -1651,19 +1651,22 @@
 stWaitMs
- = 
+.set (
 eveningI
@@ -1678,32 +1678,33 @@
 equestWaitMs
+)
 ;%0A  %7D%0A%0A%0A
   public lon
@@ -1683,32 +1683,132 @@
 tWaitMs);%0A  %7D%0A%0A%0A
+  @JmxAttributeMethod(description = %22Inter-request time in milliseconds outside of business hours%22)%0A
   public long ge
@@ -1874,24 +1874,30 @@
 stWaitMs
+.get()
 ;%0A  %7D%0A%0A%0A
   public
@@ -1888,16 +1888,116 @@
 ;%0A  %7D%0A%0A%0A
+  @JmxAttributeMethod(description = %22Inter-request time in milliseconds outside of business hours%22)%0A
   public
@@ -2099,19 +2099,22 @@
 stWaitMs
- = 
+.set (
 dayTimeI
@@ -2130,16 +2130,17 @@
 stWaitMs
+)
 ;%0A  %7D%0A  

Make throttle modifications thread-safe