stream: add a job priority option to stream services
authorMartin Willi <martin@revosec.ch>
Fri, 28 Jun 2013 08:32:30 +0000 (10:32 +0200)
committerMartin Willi <martin@revosec.ch>
Thu, 18 Jul 2013 14:00:28 +0000 (16:00 +0200)
src/libstrongswan/networking/streams/stream_manager.c
src/libstrongswan/networking/streams/stream_manager.h
src/libstrongswan/networking/streams/stream_service.c
src/libstrongswan/networking/streams/stream_service.h

index fb35a83..c7e5fd1 100644 (file)
@@ -108,7 +108,7 @@ METHOD(stream_manager_t, connect_, stream_t*,
 
 METHOD(stream_manager_t, start_service, bool,
        private_stream_manager_t *this, char *uri, int backlog,
-       stream_service_cb_t cb, void *data)
+       stream_service_cb_t cb, void *data, job_priority_t prio)
 {
        running_entry_t *running;
        enumerator_t *enumerator;
@@ -140,7 +140,7 @@ METHOD(stream_manager_t, start_service, bool,
                .uri = strdup(uri),
                .service = service,
        );
-       service->on_accept(service, cb, data);
+       service->on_accept(service, cb, data, prio);
 
        this->lock->write_lock(this->lock);
        this->running->insert_last(this->running, running);
index ffb7ef3..8639893 100644 (file)
@@ -23,6 +23,7 @@
 
 typedef struct stream_manager_t stream_manager_t;
 
+#include <library.h>
 #include <networking/streams/stream_service.h>
 
 /**
@@ -45,10 +46,12 @@ struct stream_manager_t {
         * @param backlog       size of the backlog queue, as passed to listen()
         * @param cb            callback function invoked for each client connection
         * @param data          user data to pass to callback
+        * @param prio          job priority to invoke callback with
         * @return                      TRUE if service started, FALSE on failure
         */
        bool (*start_service)(stream_manager_t *this, char *uri, int backlog,
-                                                 stream_service_cb_t cb, void *data);
+                                                 stream_service_cb_t cb, void *data,
+                                                 job_priority_t prio);
 
        /**
         * Stop a service previously create with start_service().
index 8525039..5f29051 100644 (file)
@@ -49,6 +49,11 @@ struct private_stream_service_t {
         * Accept callback data
         */
        void *data;
+
+       /**
+        * Job priority to invoke callback with
+        */
+       job_priority_t prio;
 };
 
 /**
@@ -106,7 +111,7 @@ static bool watch(private_stream_service_t *this, int fd, watcher_event_t event)
        {
                lib->processor->queue_job(lib->processor,
                                (job_t*)callback_job_create_with_prio((void*)accept_async, data,
-                                                       (void*)destroy_async_data, NULL, JOB_PRIO_HIGH));
+                                                       (void*)destroy_async_data, NULL, this->prio));
        }
        else
        {
@@ -116,7 +121,8 @@ static bool watch(private_stream_service_t *this, int fd, watcher_event_t event)
 }
 
 METHOD(stream_service_t, on_accept, void,
-       private_stream_service_t *this, stream_service_cb_t cb, void *data)
+       private_stream_service_t *this, stream_service_cb_t cb, void *data,
+       job_priority_t prio)
 {
        if (this->cb)
        {
@@ -125,6 +131,10 @@ METHOD(stream_service_t, on_accept, void,
 
        this->cb = cb;
        this->data = data;
+       if (prio <= JOB_PRIO_MAX)
+       {
+               this->prio = prio;
+       }
 
        if (this->cb)
        {
@@ -136,7 +146,7 @@ METHOD(stream_service_t, on_accept, void,
 METHOD(stream_service_t, destroy, void,
        private_stream_service_t *this)
 {
-       on_accept(this, NULL, NULL);
+       on_accept(this, NULL, NULL, this->prio);
        close(this->fd);
        free(this);
 }
@@ -154,6 +164,7 @@ stream_service_t *stream_service_create_from_fd(int fd)
                        .destroy = _destroy,
                },
                .fd = fd,
+               .prio = JOB_PRIO_MEDIUM,
        );
 
        return &this->public;
index 91e5283..91a7a17 100644 (file)
@@ -23,6 +23,8 @@
 
 typedef struct stream_service_t stream_service_t;
 
+#include <library.h>
+#include <processing/jobs/job.h>
 #include <networking/streams/stream.h>
 
 /**
@@ -56,9 +58,10 @@ struct stream_service_t {
         *
         * @param cb            callback function to call for accepted client streams
         * @param data          data to pass to callback function
+        * @param prio          job priority to run callback with
         */
        void (*on_accept)(stream_service_t *this,
-                                         stream_service_cb_t cb, void *data);
+                                         stream_service_cb_t cb, void *data, job_priority_t prio);
 
        /**
         * Destroy a stream_service_t.