namespace zavai {
public interface Resource : Object {
- /**
- * Shut down this resource.
- *
- * Normally one does nothing here, but it is important to give resources a
- * chance to do cleanup when the program quits.
- *
- * This can be used for tasks like closing the tags on a GPX track,
- * releasing a FSO resource, restoring mixer settings and so on.
- */
- public abstract void shutdown();
+ /**
+ * Shut down this resource.
+ *
+ * Normally one does nothing here, but it is important to give resources a
+ * chance to do cleanup when the program quits.
+ *
+ * This can be used for tasks like closing the tags on a GPX track,
+ * releasing a FSO resource, restoring mixer settings and so on.
+ */
+ public abstract void shutdown();
}
public abstract class Service : Object, Resource {
- public string name { get; construct; }
+ public string name { get; construct; }
- bool _started;
- public bool started {
- get { return _started; }
- set { _started = value; }
- default = false;
- }
+ bool _started;
+ public bool started {
+ get { return _started; }
+ set { _started = value; }
+ default = false;
+ }
- public signal void toggled(bool new_state);
+ public signal void toggled(bool new_state);
- protected HashMap<string, int> requests;
+ protected HashMap<string, int> requests;
- construct {
- requests = new HashMap<string, int>(str_hash, str_equal);
- }
+ construct {
+ requests = new HashMap<string, int>(str_hash, str_equal);
+ }
- public void shutdown()
- {
- stop();
- }
+ public void shutdown()
+ {
+ stop();
+ }
- /// Activate the service
- protected virtual void start()
- {
- if (!started)
- {
- zavai.log.info("Service " + name + " started\n");
- started = true;
- toggled(started);
- }
- }
+ /// Activate the service
+ protected virtual void start()
+ {
+ if (!started)
+ {
+ zavai.log.info("Service " + name + " started\n");
+ started = true;
+ toggled(started);
+ }
+ }
- /// Deactivate the service
- protected virtual void stop()
- {
- if (started)
- {
- zavai.log.info("Service " + name + " stopped\n");
- started = false;
- toggled(started);
- }
- }
+ /// Deactivate the service
+ protected virtual void stop()
+ {
+ if (started)
+ {
+ zavai.log.info("Service " + name + " stopped\n");
+ started = false;
+ toggled(started);
+ }
+ }
- /**
- Request a resource using the given ID.
- *
- * If it is the first time the resource is requested, start it and
- * return true. Else, take note of the request and return false.
- *
- * If a resource is requested multiple times with the same ID, it will
- * need to be released multiple times with that ID.
- */
- public bool request(string id)
- {
- bool res = (requests.size == 0);
- if (id in requests)
- requests[id] = requests[id] + 1;
- else
- requests.set(id, 1);
- if (res) start();
- return res;
- }
+ /**
+ Request a resource using the given ID.
+ *
+ * If it is the first time the resource is requested, start it and
+ * return true. Else, take note of the request and return false.
+ *
+ * If a resource is requested multiple times with the same ID, it will
+ * need to be released multiple times with that ID.
+ */
+ public bool request(string id)
+ {
+ bool res = (requests.size == 0);
+ if (id in requests)
+ requests[id] = requests[id] + 1;
+ else
+ requests.set(id, 1);
+ if (res) start();
+ return res;
+ }
- /**
- * Release a resource using the given ID.
- *
- * If after the call nothing is requesting the resource, stop it and
- * return true. Else, take note of the release and return false.
- *
- * If a resource is requested multiple times with the same ID, it will
- * need to be released multiple times with that ID.
- */
- public bool release(string id)
- {
- if (id in requests)
- {
- if (requests[id] > 1)
- requests[id] = requests[id] - 1;
- else
- requests.remove(id);
- } else {
- return false;
- }
- if (requests.size > 0)
- return false;
- stop();
- return true;
- }
+ /**
+ * Release a resource using the given ID.
+ *
+ * If after the call nothing is requesting the resource, stop it and
+ * return true. Else, take note of the release and return false.
+ *
+ * If a resource is requested multiple times with the same ID, it will
+ * need to be released multiple times with that ID.
+ */
+ public bool release(string id)
+ {
+ if (id in requests)
+ {
+ if (requests[id] > 1)
+ requests[id] = requests[id] - 1;
+ else
+ requests.remove(id);
+ } else {
+ return false;
+ }
+ if (requests.size > 0)
+ return false;
+ stop();
+ return true;
+ }
}
}