1 23 package org.objectweb.clif.isac.plugins.timers; 24 25 26 import java.util.Hashtable ; 27 import org.objectweb.clif.scenario.util.isac.exception.IsacRuntimeException; 28 import org.objectweb.clif.scenario.util.isac.plugin.TimerAction; 29 import org.objectweb.clif.scenario.util.isac.plugin.TestAction; 30 import org.objectweb.clif.scenario.util.isac.plugin.SampleAction; 31 import org.objectweb.clif.scenario.util.isac.util.SessionObjectAction; 32 import org.objectweb.clif.storage.api.ActionEvent; 33 34 35 41 public class ConstantTimer implements SampleAction, TestAction, TimerAction, SessionObjectAction 42 { 43 static public final String DURATION_ARG = "duration_arg"; 45 46 static public final int RUNNING_TEST = 0; 48 static public final int PASSED_TEST = 1; 49 50 static public final int RESET_SAMPLE = 0; 52 static public final int SET_SAMPLE = 1; 53 54 protected long defaultDuration; 56 protected long duration; 58 protected long startTime; 60 61 62 66 public ConstantTimer(Hashtable params) 67 { 68 startTime = -1; 69 String value = (String )params.get(DURATION_ARG); 70 if (value != null && value.length() > 0) 71 { 72 duration = defaultDuration = Integer.parseInt(value); 73 } 74 else 75 { 76 duration = defaultDuration = 0; 77 } 78 } 79 80 81 87 private ConstantTimer(ConstantTimer toClone) 88 { 89 startTime = -1; 90 defaultDuration = toClone.defaultDuration; 91 duration = toClone.defaultDuration; 92 } 93 94 95 99 100 public ActionEvent doSample(int number, Hashtable params, ActionEvent dummy) 101 { 102 switch (number) 103 { 104 case RESET_SAMPLE: 105 startTime = System.currentTimeMillis(); 106 break; 107 case SET_SAMPLE: 108 startTime = System.currentTimeMillis(); 109 duration = Integer.parseInt((String )params.get(DURATION_ARG)); 110 break; 111 } 112 return null; 113 } 114 115 116 120 121 public boolean doTest(int number, Hashtable params) 122 throws IsacRuntimeException 123 { 124 switch (number) 125 { 126 case RUNNING_TEST: 127 return duration > System.currentTimeMillis() - startTime; 128 case PASSED_TEST: 129 return duration <= System.currentTimeMillis() - startTime; 130 } 131 throw new Error ("Fatal error in ISAC's ConstantTimer plug-in: unknown test identifier " + number); 132 } 133 134 135 139 140 public long doTimer(int number, Hashtable params) 141 throws IsacRuntimeException 142 { 143 String durationStr = (String )params.get(DURATION_ARG); 144 if (durationStr != null && durationStr.length() > 0) 145 { 146 try 147 { 148 return Integer.parseInt(durationStr); 149 } 150 catch (Exception ex) 151 { 152 throw new IsacRuntimeException("Can't get timer duration", ex); 153 } 154 } 155 else 156 { 157 return duration; 158 } 159 } 160 161 162 166 167 public Object createNewSessionObject() 168 { 169 return new ConstantTimer(this); 170 } 171 172 173 public void close() 174 { 175 } 176 177 178 public void reset() 179 { 180 startTime = System.currentTimeMillis(); 181 duration = defaultDuration; 182 } 183 } 184 | Popular Tags |