2012-03-07 18:23:41 +00:00
|
|
|
(* Copyright 2012 Tony Garnock-Jones <tonygarnockjones@gmail.com>. *)
|
|
|
|
|
|
|
|
(* This file is part of Ocamlmsg. *)
|
|
|
|
|
|
|
|
(* Ocamlmsg is free software: you can redistribute it and/or modify it *)
|
|
|
|
(* 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. *)
|
|
|
|
|
|
|
|
(* Ocamlmsg is distributed in the hope that it will be useful, but *)
|
|
|
|
(* 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 *)
|
|
|
|
(* along with Ocamlmsg. If not, see <http://www.gnu.org/licenses/>. *)
|
|
|
|
|
2012-03-03 23:04:28 +00:00
|
|
|
open Unix
|
|
|
|
open Printf
|
|
|
|
open Thread
|
|
|
|
open Sexp
|
|
|
|
|
2012-03-06 22:30:39 +00:00
|
|
|
let connection_mtx = Mutex.create ()
|
2012-03-03 23:04:28 +00:00
|
|
|
let connection_count = ref 0
|
|
|
|
|
|
|
|
let endpoint_name n =
|
|
|
|
match n with
|
|
|
|
| ADDR_INET (host, port) -> sprintf "%s:%d" (string_of_inet_addr host) port
|
|
|
|
| _ -> "??unknown??"
|
|
|
|
|
|
|
|
let flush_output mtx flush_control cout =
|
|
|
|
let rec loop () =
|
|
|
|
match Event.poll (Event.receive flush_control) with
|
|
|
|
| Some () -> ()
|
|
|
|
| None ->
|
|
|
|
let ok = Util.with_mutex0 mtx (fun () -> try flush cout; true with _ -> false) in
|
|
|
|
if ok then (Thread.delay 0.1; loop ()) else ()
|
|
|
|
in loop ()
|
|
|
|
|
2012-03-04 17:12:20 +00:00
|
|
|
let connection_main class_name peername cin cout issue_banner boot_fn node_fn mainloop =
|
2012-03-03 23:04:28 +00:00
|
|
|
Log.info ("Accepted "^class_name) [Str (endpoint_name peername)];
|
|
|
|
if issue_banner cin cout
|
|
|
|
then
|
|
|
|
let mtx = Mutex.create () in
|
|
|
|
let flush_control = Event.new_channel () in
|
|
|
|
ignore (Util.create_thread (endpoint_name peername ^ " flush") None
|
|
|
|
(flush_output mtx flush_control) cout);
|
2012-03-04 17:12:20 +00:00
|
|
|
let shared_state = boot_fn (peername, mtx, cin, cout) in
|
|
|
|
let n = Node.make class_name (node_fn shared_state) in
|
2012-03-03 23:04:28 +00:00
|
|
|
(try
|
2012-03-04 17:12:20 +00:00
|
|
|
mainloop shared_state n
|
2012-03-03 23:04:28 +00:00
|
|
|
with
|
|
|
|
| End_of_file ->
|
|
|
|
Log.info ("Disconnecting "^class_name^" normally") [Str (endpoint_name peername)]
|
|
|
|
| Sys_error message ->
|
|
|
|
Log.warn ("Disconnected "^class_name^" by Sys_error")
|
|
|
|
[Str (endpoint_name peername); Str message]
|
|
|
|
| exn ->
|
|
|
|
Log.error ("Uncaught exception in "^class_name) [Str (Printexc.to_string exn)]
|
|
|
|
);
|
|
|
|
Node.unbind_all n;
|
|
|
|
Event.sync (Event.send flush_control ())
|
|
|
|
else
|
|
|
|
Log.error ("Disconnected "^class_name^" by failed initial handshake") []
|
|
|
|
|
2012-03-04 17:12:20 +00:00
|
|
|
let start_connection' class_name issue_banner boot_fn node_fn mainloop (s, peername) =
|
2012-03-03 23:04:28 +00:00
|
|
|
let cin = in_channel_of_descr s in
|
|
|
|
let cout = out_channel_of_descr s in
|
2012-03-06 22:30:39 +00:00
|
|
|
Util.with_mutex0 connection_mtx (fun () -> connection_count := !connection_count + 1);
|
2012-03-04 17:12:20 +00:00
|
|
|
connection_main class_name peername cin cout issue_banner boot_fn node_fn mainloop;
|
2012-03-06 22:30:39 +00:00
|
|
|
Util.with_mutex0 connection_mtx (fun () -> connection_count := !connection_count - 1);
|
2012-03-03 23:04:28 +00:00
|
|
|
(try flush cout with _ -> ());
|
|
|
|
close s
|
|
|
|
|
2012-03-04 17:12:20 +00:00
|
|
|
let start_connection class_name issue_banner boot_fn node_fn mainloop (s, peername) =
|
2012-03-03 23:04:28 +00:00
|
|
|
Util.create_thread
|
|
|
|
(endpoint_name peername ^ " input")
|
|
|
|
None
|
2012-03-04 17:12:20 +00:00
|
|
|
(start_connection' class_name issue_banner boot_fn node_fn mainloop)
|
2012-03-03 23:04:28 +00:00
|
|
|
(s, peername)
|