stream: add a job priority option to stream services

This commit is contained in:
Martin Willi
2013-07-18 16:00:28 +02:00
parent 441bb9e7b7
commit db0e160ba2
4 changed files with 24 additions and 7 deletions
@@ -108,7 +108,7 @@ METHOD(stream_manager_t, connect_, stream_t*,
METHOD(stream_manager_t, start_service, bool, METHOD(stream_manager_t, start_service, bool,
private_stream_manager_t *this, char *uri, int backlog, 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; running_entry_t *running;
enumerator_t *enumerator; enumerator_t *enumerator;
@@ -140,7 +140,7 @@ METHOD(stream_manager_t, start_service, bool,
.uri = strdup(uri), .uri = strdup(uri),
.service = service, .service = service,
); );
service->on_accept(service, cb, data); service->on_accept(service, cb, data, prio);
this->lock->write_lock(this->lock); this->lock->write_lock(this->lock);
this->running->insert_last(this->running, running); this->running->insert_last(this->running, running);
@@ -23,6 +23,7 @@
typedef struct stream_manager_t stream_manager_t; typedef struct stream_manager_t stream_manager_t;
#include <library.h>
#include <networking/streams/stream_service.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 backlog size of the backlog queue, as passed to listen()
* @param cb callback function invoked for each client connection * @param cb callback function invoked for each client connection
* @param data user data to pass to callback * @param data user data to pass to callback
* @param prio job priority to invoke callback with
* @return TRUE if service started, FALSE on failure * @return TRUE if service started, FALSE on failure
*/ */
bool (*start_service)(stream_manager_t *this, char *uri, int backlog, 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(). * Stop a service previously create with start_service().
@@ -49,6 +49,11 @@ struct private_stream_service_t {
* Accept callback data * Accept callback data
*/ */
void *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, lib->processor->queue_job(lib->processor,
(job_t*)callback_job_create_with_prio((void*)accept_async, data, (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 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, 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) if (this->cb)
{ {
@@ -125,6 +131,10 @@ METHOD(stream_service_t, on_accept, void,
this->cb = cb; this->cb = cb;
this->data = data; this->data = data;
if (prio <= JOB_PRIO_MAX)
{
this->prio = prio;
}
if (this->cb) if (this->cb)
{ {
@@ -136,7 +146,7 @@ METHOD(stream_service_t, on_accept, void,
METHOD(stream_service_t, destroy, void, METHOD(stream_service_t, destroy, void,
private_stream_service_t *this) private_stream_service_t *this)
{ {
on_accept(this, NULL, NULL); on_accept(this, NULL, NULL, this->prio);
close(this->fd); close(this->fd);
free(this); free(this);
} }
@@ -154,6 +164,7 @@ stream_service_t *stream_service_create_from_fd(int fd)
.destroy = _destroy, .destroy = _destroy,
}, },
.fd = fd, .fd = fd,
.prio = JOB_PRIO_MEDIUM,
); );
return &this->public; return &this->public;
@@ -23,6 +23,8 @@
typedef struct stream_service_t stream_service_t; typedef struct stream_service_t stream_service_t;
#include <library.h>
#include <processing/jobs/job.h>
#include <networking/streams/stream.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 cb callback function to call for accepted client streams
* @param data data to pass to callback function * @param data data to pass to callback function
* @param prio job priority to run callback with
*/ */
void (*on_accept)(stream_service_t *this, 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. * Destroy a stream_service_t.