@@ -20,8 +20,8 @@ let get_addr_port cfg name =
2020let 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
9395let check_for_new_connections () =
@@ -107,7 +109,7 @@ let deserialize_msg s =
107109
108110let 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
123126let get_current_state () =
124127 let cfg = get_cfg " get_current_sate" in
0 commit comments