-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroom.ml
More file actions
69 lines (54 loc) · 1.64 KB
/
Copy pathroom.ml
File metadata and controls
69 lines (54 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
open Hashtbl
(* Weakly Linked List Module *)
module type DataObject = sig
type t
end
module MakeWeaklyLinkedList (Data : DataObject)= struct
type node = {
data: Data.t;
mutable next: node option
}
type cursor = node ref
end
(* End Weakly Linked List Module *)
module ChatHistory = MakeWeaklyLinkedList (String);;
type username = string
type t = {
name: string;
mutable last: ChatHistory.node;
mutable logs: (username, ChatHistory.cursor) Hashtbl.t
}
let _announce room message =
let announcement = {
ChatHistory.data = room.name^": "^message;
ChatHistory.next = None } in
room.last.ChatHistory.next <- Some announcement;
room.last <- announcement
let get_backlog room username =
let rec read_log node acc =
match node.ChatHistory.next with
| None -> List.rev acc
| Some next ->
read_log next (next.ChatHistory.data::acc) in
let backlog = read_log !(find room.logs username) [] in
replace room.logs username (ref room.last);
backlog
let add_user room username =
_announce room (username^" has joined the room.");
add room.logs username (ref room.last)
let remove_user room username =
if Hashtbl.mem room.logs username then
(remove room.logs username;
_announce room (username^" has left the room."))
let user_message room username message =
if Hashtbl.mem room.logs username then
_announce room (username^": "^message)
let create (username: username) room_name =
let room = {
name = room_name;
last = {
ChatHistory.data = ("User "^username^" started "^room_name);
ChatHistory.next = None };
logs = Hashtbl.create 16 } in
add_user room username;
room