Skip to content

Commit a046289

Browse files
authored
Merge pull request #7 from DistributedComponents/shim-safe-string
fix safe_string problems and compile OCaml programs with safe_string
2 parents 810c500 + 09217ea commit a046289

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ Makefile.coq: _CoqProject
1717
coq_makefile -f _CoqProject -o Makefile.coq
1818

1919
TPCMain.d.byte: default
20-
ocamlbuild -libs unix -I extraction/TPC -I shims shims/TPCMain.d.byte
20+
ocamlbuild -tag safe_string -libs unix -I extraction/TPC -I shims shims/TPCMain.d.byte
2121

2222
CalculatorMain.d.byte: default
23-
ocamlbuild -libs unix -I extraction/calculator -I shims shims/CalculatorMain.d.byte
23+
ocamlbuild -tag safe_string -libs unix -I extraction/calculator -I shims shims/CalculatorMain.d.byte
2424

2525
.PHONY: default clean install

shims/Shim.ml

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ let get_addr_port cfg name =
2020
let get_name_for_read_fd fd =
2121
Hashtbl.find read_fds fd
2222

23-
let send_chunk (fd : file_descr) (buf : string) : unit =
24-
let len = String.length buf in
23+
let send_chunk (fd : file_descr) (buf : bytes) : unit =
24+
let len = Bytes.length buf in
2525
(* Printf.printf "sending chunk of length %d" len; print_newline (); *)
2626
let n = Unix.send fd (Util.raw_bytes_of_int len) 0 4 [] in
2727
if n < 4 then
@@ -64,8 +64,9 @@ let get_write_fd name =
6464
let (ip, port) = get_addr_port cfg name in
6565
let entry = gethostbyname ip in
6666
let node_addr = ADDR_INET (Array.get entry.h_addr_list 0, port) in
67+
let chunk = Bytes.of_string (string_of_nat cfg.me) in
6768
connect write_fd node_addr;
68-
send_chunk write_fd (string_of_nat cfg.me);
69+
send_chunk write_fd chunk;
6970
Hashtbl.add write_fds name write_fd;
7071
write_fd
7172

@@ -84,10 +85,11 @@ let new_conn () =
8485
print_endline "new connection!";
8586
let (node_fd, node_addr) = accept listen_fd in
8687
let chunk = receive_chunk node_fd in
87-
let node_name = nat_of_string chunk in
88-
Hashtbl.add read_fds node_fd node_name;
89-
(* ignore (get_write_fd node_name); *)
90-
Printf.printf "done processing new connection from node %s" chunk;
88+
let node = Bytes.to_string chunk in
89+
let name = nat_of_string node in
90+
Hashtbl.add read_fds node_fd name;
91+
(* ignore (get_write_fd name); *)
92+
Printf.printf "done processing new connection from node %s" node;
9193
print_newline ()
9294

9395
let check_for_new_connections () =
@@ -107,7 +109,7 @@ let deserialize_msg s =
107109

108110
let recv_msg fd =
109111
let chunk = receive_chunk fd in
110-
let (l, tag, msg) = deserialize_msg chunk in
112+
let (l, tag, msg) = deserialize_msg (Bytes.to_string chunk) in
111113
let src = get_name_for_read_fd fd in
112114
Printf.printf "got msg in protocol %a with tag = %a, contents = %a from %s" print_nat l print_nat tag (print_list print_nat) msg (string_of_nat src);
113115
print_newline ();
@@ -118,7 +120,8 @@ let send_msg l dst tag msg =
118120
print_newline ();
119121
let fd = get_write_fd dst in
120122
let s = serialize_msg l tag msg in
121-
send_chunk fd s
123+
let chunk = Bytes.of_string s in
124+
send_chunk fd chunk
122125

123126
let get_current_state () =
124127
let cfg = get_cfg "get_current_sate" in

0 commit comments

Comments
 (0)