Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. open Lwt.Infix
  2.  
  3. (* a list associating user nicknames to the output channels that write to their connections *)
  4. (* Once we fix the other functions this should change to []*)
  5. let sessions = ref [("",Lwt_io.null)]
  6. exception Quit
  7.  
  8. (* replace Lwt.return below with code that uses Lwt_list.iter_p to
  9. print sender: msg on each output channel (excluding the sender's)*)
  10. let rec send_all sender msg = Lwt.return ()
  11.  
  12. (* remove a session from the list of sessions: important so we don't try to
  13. write to a closed connection *)
  14. let remove_session nn =
  15. sessions := List.remove_assoc nn !sessions;
  16. send_all nn "<left chat>" >>= fun () ->
  17. Lwt.return ()
  18.  
  19. (* Modify to handle the "Quit" case separately, closing the channels before removing the session *)
  20. let handle_error e nn inp outp = remove_session nn
  21.  
  22. (* modify sessions to remove (nn,outp) association, add (new_nn,outp) association.
  23. also notify other participants of new nickname *)
  24. let change_nn nn outp new_nn = Lwt.return ()
  25.  
  26. (* + obtain initial nick(name),
  27. + add (nick,outp) to !sessions, and
  28. + announce join to other users *)
  29. let handle_login nr (inp,outp) =
  30. Lwt_io.printl "Enter initial nick:" >>= fun () -> Lwt.return ();;
  31.  
  32. (* modify handle_input below to detect /q, /n, and /l commands *)
  33. let handle_input nr outp l = send_all !nr l
  34.  
  35. let chat_server _ (inp,outp) =
  36. let nick = ref "" in
  37. (* replace () below with call to handle_login *)
  38. let _ = handle_login nick (inp,outp) in
  39. let rec main_loop () =
  40. Lwt_io.read_line inp >>= handle_input nick outp >>= main_loop in
  41. Lwt.catch main_loop (fun e -> handle_error e !nick inp outp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement