summaryrefslogtreecommitdiff
path: root/webapps/swat/source/class/swat/module/statistics/Fsm.js
blob: 3ecb42a49d17c7432613939cc85ec9e09efbed09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * Copyright:
 *   (C) 2006 by Derrell Lipman
 *       All rights reserved
 *
 * License:
 *   LGPL 2.1: http://creativecommons.org/licenses/LGPL/2.1/
 */

/**
 * Swat statistics class finite state machine
 */
qx.OO.defineClass("swat.module.statistics.Fsm", swat.main.AbstractModuleFsm,
function()
{
  swat.main.AbstractModuleFsm.call(this);
});


qx.Class._startTimer = function(fsm)
{
  // First, for good house keeping, ensure no timer exists
  swat.module.statistics.Fsm._stopTimer(fsm);

  // Create a timer instance to expire in a few seconds
  var timer = new qx.client.Timer(5000);
  timer.addEventListener("interval", fsm.eventListener, fsm);
  fsm.addObject("timer", timer);
  timer.start();
};


qx.Class._stopTimer = function(fsm)
{
  // ... then stop the timer.  Get the timer object.
  var timer = fsm.getObject("timer");
            
  // If it still exists...
  if (timer)
  {
    // ... then dispose of it.
    timer.dispose();
    fsm.removeObject("timer");
  }
};


qx.Proto.buildFsm = function(module)
{
  var fsm = module.fsm;
  var _this = this;
  var _module = module;

  /*
   * State: Idle
   *
   * Actions upon entry
   *   - if returning from RPC, display the result
   *   - start an interval timer to request statistics again in a while
   *
   * Transition on:
   *  "interval" on interval_timer
   */
  var state = new qx.util.fsm.State(
    "State_Idle",
    {
      "onentry" :
        function(fsm, event)
        {
          // Did we just return from an RPC request?
          if (fsm.getPreviousState() == "State_AwaitRpcResult")
          {
            // Yup.  Display the result.  We need to get the request object
            var rpcRequest = _this.popRpcRequest();

            // Display the result
            var gui = swat.module.statistics.Gui.getInstance();
            gui.displayData(module, rpcRequest.getUserData("result"));

            // Dispose of the request
            rpcRequest.request.dispose();
            rpcRequest.request = null;

            // Restart the timer.
            if (_module.visible)
            {
              swat.module.statistics.Fsm._startTimer(fsm);
            }
          }
        },

      "onexit" :
        function(fsm, event)
        {
          // If we're not coming right back into this state...
          if (fsm.getNextState() != "State_Idle")
          {
            // ... then stop the timer.
            swat.module.statistics.Fsm._stopTimer(fsm);
          }
        },

      "events" :
        {
          // If the timer expires, send a new statistics request
          "interval" :
          {
            "timer" :
              "Transition_Idle_to_AwaitRpcResult_via_request_statistics"
          },

          // When we get an appear event, start our timer
          "appear" :
          {
            "swat.main.canvas" :
              "Transition_Idle_to_Idle_via_appear"
          },

          // When we get a disappear event, stop our timer
          "disappear" :
          {
            "swat.main.canvas" :
              "Transition_Idle_to_Idle_via_disappear"
          }
        }
    });

  // Replace the initial Idle state with this one
  fsm.replaceState(state, true);
  
  /*
   * Transition: Idle to AwaitRpcResult
   *
   * Cause: "interval" on timer
   *
   * Action:
   *  Issue a Get Statistics request
   */
  var trans = new qx.util.fsm.Transition(
    "Transition_Idle_to_AwaitRpcResult_via_request_statistics",
    {
      "nextState" :
        "State_AwaitRpcResult",

      "ontransition" :
        function(fsm, event)
        {
          // Issue a Get Statistics call
          _this.callRpc(fsm,
                        "samba.management",
                        "get_statistics",
                        [ true, true ]);
        }
    });
  state.addTransition(trans);

  /*
   * Transition: Idle to Idle
   *
   * Cause: "appear" on canvas
   *
   * Action:
   *  Start our timer
   */
  var trans = new qx.util.fsm.Transition(
    "Transition_Idle_to_Idle_via_appear",
    {
      "nextState" :
        "State_Idle",

      "ontransition" :
        function(fsm, event)
        {
          _module.visible = true;
          swat.module.statistics.Fsm._startTimer(fsm);
        }
    });
  state.addTransition(trans);

  /*
   * Transition: Idle to Idle
   *
   * Cause: "disappear" on canvas
   *
   * Action:
   *  Stop our timer
   */
  var trans = new qx.util.fsm.Transition(
    "Transition_Idle_to_Idle_via_disappear",
    {
      "nextState" :
        "State_Idle",

      "ontransition" :
        function(fsm, event)
        {
          _module.visible = false;
          swat.module.statistics.Fsm._stopTimer(fsm);
        }
    });
  state.addTransition(trans);

  // Add the AwaitRpcResult state and all of its transitions
  this.addAwaitRpcResultState(module);
};


/**
 * Singleton Instance Getter
 */
qx.Class.getInstance = qx.util.Return.returnInstance;