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-05-05 22:18:23 +00:00
|
|
|
open Lwt
|
2012-01-08 17:41:04 +00:00
|
|
|
open Unix
|
|
|
|
open Printf
|
|
|
|
open Sexp
|
|
|
|
|
|
|
|
let send_error ch message details =
|
|
|
|
let m = Message.error (Str message, details) in
|
2012-05-05 22:18:23 +00:00
|
|
|
ignore (Log.warn "Sending error" [m]);
|
2012-01-08 18:19:58 +00:00
|
|
|
ch m
|
2012-01-08 17:41:04 +00:00
|
|
|
|
|
|
|
let send_sexp_syntax_error ch explanation =
|
|
|
|
send_error ch explanation (Str "http://people.csail.mit.edu/rivest/Sexp.txt")
|
|
|
|
|
|
|
|
let dispatch_message n ch m =
|
|
|
|
match m with
|
2012-05-05 22:18:23 +00:00
|
|
|
| Message.Post (Str name, body, token) ->
|
2012-05-05 15:46:48 +00:00
|
|
|
Node.send_ignore' name body
|
2012-05-05 22:18:23 +00:00
|
|
|
| Message.Subscribe (Str filter, sink, name, Str reply_sink, Str reply_name) ->
|
|
|
|
(match_lwt Node.bind (Node.name_of_string filter, n) with
|
|
|
|
| true ->
|
|
|
|
Node.post_ignore'
|
|
|
|
reply_sink
|
|
|
|
(Str reply_name)
|
|
|
|
(Message.subscribe_ok (Str filter))
|
|
|
|
(Str "")
|
|
|
|
| false ->
|
|
|
|
Log.warn "Bind failed" [Str filter])
|
|
|
|
| Message.Unsubscribe (Str token) ->
|
|
|
|
(match_lwt Node.unbind (Node.name_of_string token) with
|
|
|
|
| true -> return ()
|
|
|
|
| false -> Log.warn "Unbind failed" [Str token])
|
|
|
|
| _ ->
|
2012-01-08 17:41:04 +00:00
|
|
|
send_error ch "Message not understood" (Message.sexp_of_message m)
|
|
|
|
|
2012-03-03 23:04:28 +00:00
|
|
|
let issue_banner cin cout =
|
2012-05-07 10:31:59 +00:00
|
|
|
lwt () = output_sexp cout (Arr [Str "hop"; Str ""]) in
|
|
|
|
lwt () = Lwt_io.flush cout in
|
|
|
|
lwt () = output_sexp cout (Message.subscribe (Str (Node.local_container_name()),
|
|
|
|
Str "", Str "",
|
|
|
|
Str "", Str "")) in
|
|
|
|
lwt () = Lwt_io.flush cout in
|
2012-05-05 22:18:23 +00:00
|
|
|
return true
|
2012-03-03 23:04:28 +00:00
|
|
|
|
2012-05-05 22:18:23 +00:00
|
|
|
let relay_boot (peername, cin, cout) = return (peername, Lwt_mutex.create (), cin, cout)
|
2012-03-04 17:12:20 +00:00
|
|
|
|
|
|
|
let relay_handler (_, mtx, _, cout) _ m =
|
2012-05-05 23:37:29 +00:00
|
|
|
Lwt_mutex.with_lock mtx (fun () -> output_sexp cout m)
|
2012-03-03 23:04:28 +00:00
|
|
|
|
2012-03-04 17:12:20 +00:00
|
|
|
let relay_mainloop (peername, mtx, cin, cout) n =
|
2012-05-05 22:18:23 +00:00
|
|
|
let write_sexp sexp = Lwt_mutex.with_lock mtx (fun () -> output_sexp cout sexp) in
|
|
|
|
(try_lwt
|
|
|
|
while_lwt true do
|
|
|
|
lwt message_sexp = Sexp.input_sexp cin in
|
|
|
|
dispatch_message n write_sexp (Message.message_of_sexp message_sexp)
|
|
|
|
done
|
|
|
|
with
|
|
|
|
| Sexp.Syntax_error explanation ->
|
|
|
|
lwt () = send_sexp_syntax_error write_sexp explanation in
|
2012-03-03 23:04:28 +00:00
|
|
|
Log.info "Disconnected relay for syntax error"
|
|
|
|
[Str (Connections.endpoint_name peername); Str explanation])
|
2012-01-08 17:41:04 +00:00
|
|
|
|
2012-03-03 23:04:28 +00:00
|
|
|
let start (s, peername) =
|
2012-03-04 17:12:20 +00:00
|
|
|
Connections.start_connection "relay" issue_banner
|
|
|
|
relay_boot relay_handler relay_mainloop (s, peername)
|
2012-05-01 19:37:39 +00:00
|
|
|
|
|
|
|
let init () =
|
2012-05-06 01:11:54 +00:00
|
|
|
Util.create_daemon_thread "Hop listener" None (Net.start_net "Hop" 5671) start
|