implement a new 'service' interface that's similar to the transport interface
authorTero Marttila <terom@fixme.fi>
Thu, 07 May 2009 02:12:55 +0300
changeset 175 a816950a6548
parent 174 c56680e9e021
child 176 6750d50ee8cd
implement a new 'service' interface that's similar to the transport interface
src/service.c
src/service.h
src/service_internal.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/service.c	Thu May 07 02:12:55 2009 +0300
@@ -0,0 +1,25 @@
+#include "service_internal.h"
+
+const struct object_type service_type_type = {
+    .parent = NULL,
+};
+
+void service_init (service_t *service, const struct service_type *type, const struct service_info *info)
+{
+    // init object
+    object_init(&service->base_obj, &type->base_type);
+
+    // store user info
+    service->info = *info;
+}
+
+void* service_check (service_t *service, const struct service_type *type)
+{
+    return object_cast(&service->base_obj, &type->base_type);
+}
+
+void service_error (service_t *service, const error_t *err)
+{
+    // just call the user callback
+    service->info.cb_tbl->on_error(service, err, service->info.cb_arg);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/service.h	Thu May 07 02:12:55 2009 +0300
@@ -0,0 +1,50 @@
+#ifndef SERVICE_H
+#define SERVICE_H
+
+/**
+ * @file
+ *
+ * Defines a simple interface for creating services, which listen for connections and create transport_t's.
+ */
+#include "transport.h"
+
+/**
+ * Opaque state struct.
+ */
+typedef struct service service_t;
+
+/**
+ * User callbacks for services.
+ */
+struct service_callbacks {
+    /**
+     * The service broke.
+     *
+     * This is only called for errors which occur when called directly from the event loop, and never for errors that
+     * occur inside of calls to service_*.
+     */
+    void (*on_error) (service_t *service, const error_t *err, void *arg);
+};
+
+/**
+ * User info required to build a service
+ */
+struct service_info {
+    /** Callback table */
+    const struct service_callbacks *cb_tbl;
+
+    /** Callback context arg */
+    void *cb_arg;
+
+    /** Settings for the service's client transports */
+    struct transport_info trans_info;
+};
+
+/**
+ * Destroy a service to stop accepting any connections and release all resources.
+ *
+ * Any connected client transports should stay intact (?)
+ */
+void service_destroy (service_t *service);
+
+#endif /* SERVICE_H */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/service_internal.h	Thu May 07 02:12:55 2009 +0300
@@ -0,0 +1,60 @@
+#ifndef SERVICE_INTERNAL_H
+#define SERVICE_INTERNAL_H
+
+/**
+ * @file
+ *
+ * Internal interface for implementations of service_t
+ */
+#include "service.h"
+#include "transport.h"
+#include "object.h"
+
+/**
+ * The object_type of service_type
+ */
+extern const struct object_type service_type_type;
+
+/**
+ * Type definition with method table
+ */
+struct service_type {
+    struct object_type base_type;
+    
+    /** Method table */
+    struct service_methods {
+        /**
+         * Release internal state, but not the service_t itself
+         */
+        void (*deinit) (service_t *service);
+    } methods;
+};
+
+/**
+ * Base service_t state
+ */
+struct service {
+    struct object base_obj;
+
+    /** User info */
+    struct service_info info;
+};
+
+/**
+ * Initialize a service by binding it to a specific type, with the given user info for this service, and for spawned transports.
+ */
+void service_init (service_t *service, const struct service_type *type, const struct service_info *info);
+
+/**
+ * Used to up-cast a generic service_t pointer to an implementation of the given service_type (or subtype).
+ *
+ * It is a bug to call this with a service of a different type.
+ */
+void* service_check (service_t *service, const struct service_type *type);
+
+/**
+ * The service failed, call the user callback
+ */
+void service_error (service_t *service, const error_t *err);
+
+#endif /* SERVICE_INTERNAL_H */