123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- error_reporting(E_ALL|E_STRICT);
- abstract class Background_Application
- {
-
- public $backgroundTrace = FALSE ;
-
- public $finiteStateMachine;
-
- public $backgroundPath = NULL;
-
- protected $_backgroundId;
-
- public $backgroundGoals = NULL;
-
- public function backgroundDo()
- {
- $ret = TRUE;
- while(Gtk::events_pending())
- Gtk::main_iteration();
- if (!is_array($this->backgroundGoals))
- throw new Exception('Background_Application needs an array of goals to be set before background work can run.');
- $msg = "background work: ";
- $event = $this->backgroundPath;
- if (!array_key_exists($event, $this->backgroundGoals))
- {
- $msg = "Nothing to do for now. Stop idling";
- $ret = FALSE;
- }
- elseif ($this->finiteStateMachine->get_state() != $this->backgroundGoals[$event])
- {
- $msg .= "state " . $this->finiteStateMachine->get_state() . "($event)";
- $result = $this->finiteStateMachine->apply_event($event);
- $msg .= "[$result->fsm_return] => $result->fsm_state";
- if (!empty($result->fsm_action))
- $msg .= " / $result->fsm_action";
- }
- else
- {
- $msg .= "End of scheduled work";
- $this->backgroundStop();
- $ret = FALSE;
- }
- if ($this->backgroundTrace)
- echo $msg . PHP_EOL;
- return $ret;
- }
-
- function backgroundStart($path = NULL)
- {
- if (!is_array($this->backgroundGoals))
- throw new Exception('Background_Application needs an array of goals to be set before background work can be started.');
- $this->backgroundPath = $path;
- $this->_backgroundId = Gtk::idle_add(array($this, 'backgroundDo'));
- }
-
- function backgroundStop()
- {
- Gtk::idle_remove($this->_backgroundId);
- $this->_backgroundId = NULL;
- }
- }
|