2012-03-07 18:23:41 +00:00
|
|
|
(* Copyright 2012 Tony Garnock-Jones <tonygarnockjones@gmail.com>. *)
|
|
|
|
|
2012-05-01 21:36:38 +00:00
|
|
|
(* This file is part of Hop. *)
|
2012-03-07 18:23:41 +00:00
|
|
|
|
2012-05-01 21:36:38 +00:00
|
|
|
(* Hop is free software: you can redistribute it and/or modify it *)
|
2012-03-07 18:23:41 +00:00
|
|
|
(* under the terms of the GNU General Public License as published by the *)
|
|
|
|
(* Free Software Foundation, either version 3 of the License, or (at your *)
|
|
|
|
(* option) any later version. *)
|
|
|
|
|
2012-05-01 21:36:38 +00:00
|
|
|
(* Hop is distributed in the hope that it will be useful, but *)
|
2012-03-07 18:23:41 +00:00
|
|
|
(* WITHOUT ANY WARRANTY; without even the implied warranty of *)
|
|
|
|
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *)
|
|
|
|
(* General Public License for more details. *)
|
|
|
|
|
|
|
|
(* You should have received a copy of the GNU General Public License *)
|
2012-05-01 21:36:38 +00:00
|
|
|
(* along with Hop. If not, see <http://www.gnu.org/licenses/>. *)
|
2012-03-07 18:23:41 +00:00
|
|
|
|
2012-01-08 17:41:04 +00:00
|
|
|
open Sexp
|
2012-03-04 17:12:20 +00:00
|
|
|
open Status
|
2012-01-08 17:41:04 +00:00
|
|
|
|
|
|
|
type t = {
|
2012-05-05 15:46:48 +00:00
|
|
|
name: Node.name;
|
2012-01-08 17:41:04 +00:00
|
|
|
subscriptions: Subscription.set_t;
|
2012-01-08 18:17:59 +00:00
|
|
|
ch: Message.t Squeue.t;
|
2012-05-05 13:47:20 +00:00
|
|
|
mutable backlog: Sexp.t Queue.t;
|
|
|
|
mutable waiters: Subscription.t Queue.t;
|
2012-01-08 17:41:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let classname = "queue"
|
|
|
|
|
2012-01-08 19:03:09 +00:00
|
|
|
let report info n =
|
2012-01-08 19:48:07 +00:00
|
|
|
Log.info (Printf.sprintf "do_burst %d capacity, %d backlog, %d waiters, %d ticks left\n%!"
|
|
|
|
(Squeue.approx_capacity info.ch)
|
2012-05-05 13:47:20 +00:00
|
|
|
(Queue.length info.backlog)
|
|
|
|
(Queue.length info.waiters)
|
2012-01-08 19:48:07 +00:00
|
|
|
n) []
|
2012-01-08 19:03:09 +00:00
|
|
|
|
|
|
|
let rec do_burst info n =
|
|
|
|
(* report info n; *)
|
2012-05-05 13:47:20 +00:00
|
|
|
if Queue.is_empty info.backlog then false
|
2012-01-08 17:41:04 +00:00
|
|
|
else
|
2012-05-05 13:47:20 +00:00
|
|
|
if Queue.is_empty info.waiters then false
|
2012-01-08 17:41:04 +00:00
|
|
|
else
|
|
|
|
if n = 0 then true (* maybe more work available, but should poll for outside events *)
|
|
|
|
else
|
2012-05-05 13:47:20 +00:00
|
|
|
let body = Queue.peek info.backlog in
|
|
|
|
let sub = Queue.pop info.waiters in
|
2012-03-06 22:05:57 +00:00
|
|
|
if Subscription.send_to_subscription info.name info.subscriptions sub body
|
2012-01-08 17:41:04 +00:00
|
|
|
then
|
2012-05-05 13:47:20 +00:00
|
|
|
(Queue.push sub info.waiters;
|
|
|
|
ignore (Queue.pop info.backlog);
|
2012-01-08 17:41:04 +00:00
|
|
|
do_burst info (n - 1))
|
|
|
|
else
|
|
|
|
do_burst info n
|
|
|
|
|
|
|
|
let rec process_and_wait info =
|
|
|
|
if not (do_burst info 1000)
|
2012-01-08 18:17:59 +00:00
|
|
|
then Squeue.pop info.ch
|
2012-01-08 17:41:04 +00:00
|
|
|
else
|
2012-01-08 18:17:59 +00:00
|
|
|
match Squeue.peek info.ch with
|
2012-01-08 17:41:04 +00:00
|
|
|
| Some m -> m
|
|
|
|
| None -> process_and_wait info
|
|
|
|
|
|
|
|
let shoveller info =
|
|
|
|
let rec loop () =
|
|
|
|
match process_and_wait info with
|
|
|
|
| Message.Post (name, body, token) ->
|
2012-05-05 13:47:20 +00:00
|
|
|
Queue.push body info.backlog;
|
2012-01-08 17:41:04 +00:00
|
|
|
loop ()
|
|
|
|
| Message.Subscribe (filter, Str sink, name, Str reply_sink, reply_name) ->
|
|
|
|
let sub =
|
2012-03-06 22:05:57 +00:00
|
|
|
Subscription.create
|
|
|
|
info.name info.subscriptions filter sink name reply_sink reply_name in
|
2012-05-05 13:47:20 +00:00
|
|
|
Queue.push sub info.waiters;
|
2012-01-08 17:41:04 +00:00
|
|
|
loop ()
|
|
|
|
| Message.Unsubscribe (Str token) ->
|
2012-03-06 22:05:57 +00:00
|
|
|
ignore (Subscription.delete info.name info.subscriptions token);
|
2012-01-08 17:41:04 +00:00
|
|
|
loop ()
|
|
|
|
| m ->
|
|
|
|
Util.message_not_understood "queue" m;
|
|
|
|
loop ()
|
|
|
|
in loop ()
|
|
|
|
|
|
|
|
let queue_factory arg =
|
|
|
|
match arg with
|
2012-05-05 15:46:48 +00:00
|
|
|
| (Arr [Str name_str]) ->
|
2012-01-08 17:41:04 +00:00
|
|
|
let info = {
|
2012-05-05 15:46:48 +00:00
|
|
|
name = Node.name_of_string name_str;
|
2012-01-08 17:41:04 +00:00
|
|
|
subscriptions = Subscription.new_set ();
|
2012-01-08 18:17:59 +00:00
|
|
|
ch = Squeue.create 1000;
|
2012-05-05 13:47:20 +00:00
|
|
|
backlog = Queue.create ();
|
|
|
|
waiters = Queue.create ()
|
2012-01-08 17:41:04 +00:00
|
|
|
} in
|
2012-05-05 15:46:48 +00:00
|
|
|
ignore (Util.create_thread name_str None shoveller info);
|
2012-01-08 18:17:59 +00:00
|
|
|
let queue_handler n sexp = Squeue.add (Message.message_of_sexp sexp) info.ch in
|
2012-05-05 15:46:48 +00:00
|
|
|
replace_ok (Node.make_idempotent_named classname info.name queue_handler) (Str name_str)
|
2012-01-08 17:41:04 +00:00
|
|
|
| _ ->
|
2012-03-04 17:12:20 +00:00
|
|
|
Problem (Str "bad-arg")
|
2012-01-08 17:41:04 +00:00
|
|
|
|
|
|
|
let init () =
|
|
|
|
Factory.register_class classname queue_factory
|