1 package rero.client.functions; 2 3 import sleep.engine.*; 4 import sleep.runtime.*; 5 import sleep.interfaces.*; 6 import sleep.bridges.*; 7 8 import rero.client.*; 9 import rero.util.*; 10 import java.util.*; 11 12 public class TimerOperators extends Feature implements Loadable 13 { 14 public void init() 15 { 16 getCapabilities().getScriptCore().addBridge(this); 17 } 18 19 public boolean scriptLoaded(ScriptInstance script) 20 { 21 script.getScriptEnvironment().getEnvironment().put("&addTimer", new addTimer()); 22 script.getScriptEnvironment().getEnvironment().put("&stopTimer", new stopTimer()); 23 script.getScriptEnvironment().getEnvironment().put("&setTimerResolution", new setResolution()); 24 25 return true; 26 } 27 28 public boolean scriptUnloaded(ScriptInstance script) 29 { 30 return true; 31 } 32 33 private class addTimer implements Function 34 { 35 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 36 { 37 int repeats = -1; 38 39 SleepClosure func = BridgeUtilities.getFunction(locals, si); 40 int time = BridgeUtilities.getInt(locals); 41 42 if (!locals.isEmpty()) 43 { 44 repeats = BridgeUtilities.getInt(locals); 45 } 46 47 ScriptedTimer timer; 48 49 if (!locals.isEmpty()) 50 { 51 timer = new ScriptedTimer(func, si, BridgeUtilities.getScalar(locals)); 52 } 53 else 54 { 55 timer = new ScriptedTimer(func, si, null); 56 } 57 58 getCapabilities().getTimer().addTimer(timer, time, repeats); 59 60 return SleepUtils.getScalar(timer); 61 } 62 } 63 64 private class stopTimer implements Function 65 { 66 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 67 { 68 ScriptedTimer timer = (ScriptedTimer)BridgeUtilities.getObject(locals); 69 70 getCapabilities().getTimer().stopTimer(timer); 71 72 return SleepUtils.getEmptyScalar(); 73 } 74 } 75 76 private class setResolution implements Function 77 { 78 public Scalar evaluate(String f, ScriptInstance si, Stack locals) 79 { 80 getCapabilities().getTimer().setResolution((long)BridgeUtilities.getInt(locals)); 81 return SleepUtils.getEmptyScalar(); 82 } 83 } 84 85 private static class ScriptedTimer implements TimerListener 86 { 87 protected ScriptInstance si; 88 protected SleepClosure func; 89 protected Scalar args; 90 91 public ScriptedTimer(SleepClosure f, ScriptInstance script, Scalar a) 92 { 93 func = f; 94 si = script; 95 args = a; 96 } 97 98 public void timerExecute() 99 { 100 if (si == null || !si.isLoaded()) 101 { 102 args = null; 103 si = null; 104 func = null; 105 return; 106 } 107 108 Stack arg_stack = new Stack(); 109 if (args != null) 110 arg_stack.push(args); 111 112 func.callClosure("timer", si, arg_stack); 113 } 114 } 115 } 116 | Popular Tags |