Commit 80d8e5da authored by Fardale's avatar Fardale

master: safer send_computation function

parent ee10b8e4
......@@ -48,41 +48,46 @@ let get_stats pass sockaddr =
>|= fun () -> (0, 0))
with e ->
Logs_lwt.err (fun m ->
m "Error during the reception of the stats: %s" (Printexc.to_string e))
m "Error during the collect of the stats: %s" (Printexc.to_string e))
>|= fun () -> (0, 0)
let send_computation sockaddr pass computation =
Lwt_io.with_connection sockaddr (fun (ic, oc) ->
Lwt_io.write_line oc
(Serialization_j.string_of_query (pass, `COMPUTATION computation))
>>= fun () ->
Lwt_io.flush oc >>= fun () ->
try%lwt
Lwt_io.read_line ic >>= fun json ->
match
CCResult.guard (fun () -> Serialization_j.answer_of_string json)
with
| Ok answer -> (
match answer with
| `Ok ->
Logs_lwt.info (fun m ->
m "Computation %i,%i successfully sent to %s"
(fst computation.id) (snd computation.id)
(string_of_sockaddr sockaddr))
>|= fun () -> true
| `Error s ->
Logs_lwt.err (fun m ->
m "Error during the reception of the result %i,%i: %s"
(fst computation.id) (snd computation.id) s)
>|= fun () -> false )
| Error e ->
Logs_lwt.err (fun m ->
m "Error during the reception of the answer: %s"
(Printexc.to_string e))
>|= fun () -> false
with End_of_file ->
Logs_lwt.err (fun m -> m "Error during the read of the answer: EOF")
>|= fun () -> false)
try%lwt
Lwt_io.with_connection sockaddr (fun (ic, oc) ->
Lwt_io.write_line oc
(Serialization_j.string_of_query (pass, `COMPUTATION computation))
>>= fun () ->
Lwt_io.flush oc >>= fun () ->
try%lwt
Lwt_io.read_line ic >>= fun json ->
match
CCResult.guard (fun () -> Serialization_j.answer_of_string json)
with
| Ok answer -> (
match answer with
| `Ok ->
Logs_lwt.info (fun m ->
m "Computation %i,%i successfully sent to %s"
(fst computation.id) (snd computation.id)
(string_of_sockaddr sockaddr))
>|= fun () -> true
| `Error s ->
Logs_lwt.err (fun m ->
m "Error when sending copmutation %i,%i: %s"
(fst computation.id) (snd computation.id) s)
>|= fun () -> false )
| Error e ->
Logs_lwt.err (fun m ->
m "Error during the reception of the answer: %s"
(Printexc.to_string e))
>|= fun () -> false
with End_of_file ->
Logs_lwt.err (fun m -> m "Error during the read of the answer: EOF")
>|= fun () -> false)
with e ->
Logs_lwt.err (fun m ->
m "Error when sending a computation: %s" (Printexc.to_string e))
>|= fun () -> false
let mutex = ref true
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment