1 3 package org.jgroups.protocols; 4 5 import org.jgroups.Event; 6 import org.jgroups.stack.Protocol; 7 import org.jgroups.util.Util; 8 9 import java.util.Properties ; 10 11 12 18 19 public class DELAY extends Protocol { 20 int in_delay=0, out_delay=0; 21 22 25 public String getName() { 26 return "DELAY"; 27 } 28 29 30 public boolean setProperties(Properties props) { 31 String str; 32 33 super.setProperties(props); 34 35 str=props.getProperty("in_delay"); 36 if(str != null) { 37 in_delay=Integer.parseInt(str); 38 props.remove("in_delay"); 39 } 40 41 str=props.getProperty("out_delay"); 42 if(str != null) { 43 out_delay=Integer.parseInt(str); 44 props.remove("out_delay"); 45 } 46 47 if(props.size() > 0) { 48 System.err.println("DELAY.setProperties(): these properties are not recognized:"); 49 props.list(System.out); 50 return false; 51 } 52 return true; 53 } 54 55 56 public void up(Event evt) { 57 int delay=in_delay > 0 ? computeDelay(in_delay) : 0; 58 59 60 switch(evt.getType()) { 61 case Event.MSG: 63 if(log.isInfoEnabled()) log.info("delaying incoming message for " + delay + " milliseconds"); 64 Util.sleep(delay); 65 break; 66 } 67 68 passUp(evt); } 70 71 72 public void down(Event evt) { 73 int delay=out_delay > 0 ? computeDelay(out_delay) : 0; 74 75 switch(evt.getType()) { 76 77 case Event.MSG: 79 if(log.isInfoEnabled()) log.info("delaying outgoing message for " + delay + " milliseconds"); 80 Util.sleep(delay); 81 break; 82 } 83 84 passDown(evt); } 86 87 88 91 int computeDelay(int n) { 92 return (int)((Math.random() * 1000000) % n); 93 } 94 95 96 } 97 | Popular Tags |