-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
80 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
let sendto addr msg = | ||
let open Lwt.Infix in | ||
let fd = Lwt_unix.socket ~cloexec:true PF_INET SOCK_DGRAM 0 in | ||
let write_ok = | ||
Lwt_unix.sendto fd (String.to_bytes msg) 0 (String.length msg) [] addr | ||
>|= fun code -> if code = -1 then Error "UDP: failed to send" else Ok () | ||
in | ||
write_ok >>= fun _ -> | ||
Lwt_unix.close fd >>= fun () -> write_ok | ||
|
||
module Warning = | ||
Once.Make () | ||
(** Make a [Once] module (a stateful module for conveniently managing side-effects that should be executed only once). *) | ||
|
||
let write_of_score addr score = | ||
let open Lwt.Infix in | ||
score >|= Oracle.string_of_score ~debug:false >>= fun str -> | ||
sendto addr str >>= function | ||
| Ok () -> | ||
Warning.reset (); | ||
Lwt.return () | ||
| Error reason -> | ||
Warning.once (fun () -> print_endline reason); | ||
Lwt.return () | ||
|
||
let addr_of_string addr_str = | ||
let handle_err ?msg () = | ||
match msg with | ||
| None -> | ||
failwith | ||
(Printf.sprintf | ||
"Failed parsing '%s' as a UDP address and port. Format should \ | ||
match e.g. '192.168.0.0:8081'" | ||
addr_str) | ||
| Some msg -> | ||
failwith | ||
(Printf.sprintf | ||
"Failed parsing '%s' as a UDP address and port. Format should \ | ||
match e.g. '192.168.0.0:8081': %s" | ||
addr_str msg) | ||
in | ||
let ip, port = | ||
match String.split_on_char ':' addr_str with | ||
| [ ip; port ] -> (ip, port) | ||
| _ -> handle_err () | ||
in | ||
let addr = | ||
try Unix.inet_addr_of_string ip with Failure msg -> handle_err ~msg () | ||
in | ||
let port_int = | ||
try int_of_string port with Failure msg -> handle_err ~msg () | ||
in | ||
Lwt_unix.ADDR_INET (addr, port_int) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
(** Convenience function to map an ['a lwt.t] to an [('a, string) result lwt.t]. Rejected promises are mapped to [Error]; fulfilled promises to [Ok]. *) | ||
let result_lwt_of_lwt promise = | ||
Lwt.try_bind | ||
(fun () -> promise) | ||
(fun inner -> inner |> Result.ok |> Lwt.return) | ||
(fun e -> e |> Printexc.to_string |> Result.error |> Lwt.return) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
open Getting | ||
open Utils | ||
open TestUtils | ||
|
||
let delays () = Delay.of_delays_s [ 1.0; 3.0; 2.0 ] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
open Getting | ||
open Utils | ||
open TestUtils | ||
|
||
let simulated_promises waits = | ||
let open Lwt.Infix in | ||
|
File renamed without changes.