Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
106 lines (105 sloc) 2.82 KB
module Heartbeat = struct
type t =
{ session_id: string;
time: Time.t;
status_message: string;
}
end
module Logon = struct
type t =
{ session_id: string;
time: Time.t;
user: string;
credentials: string;
}
end;;
#part 1
module Log_entry = struct
type t =
{ session_id: string;
time: Time.t;
important: bool;
message: string;
}
end
;;
#part 2
type client_message = | Logon of Logon.t
| Heartbeat of Heartbeat.t
| Log_entry of Log_entry.t
;;
#part 3
let messages_for_user user messages =
let (user_messages,_) =
List.fold messages ~init:([],String.Set.empty)
~f:(fun ((messages,user_sessions) as acc) message ->
match message with
| Logon m ->
if m.Logon.user = user then
(message::messages, Set.add user_sessions m.Logon.session_id)
else acc
| Heartbeat _ | Log_entry _ ->
let session_id = match message with
| Logon m -> m.Logon.session_id
| Heartbeat m -> m.Heartbeat.session_id
| Log_entry m -> m.Log_entry.session_id
in
if Set.mem user_sessions session_id then
(message::messages,user_sessions)
else acc
)
in
List.rev user_messages
;;
#part 4
module Log_entry = struct
type t = { important: bool;
message: string;
}
end
module Heartbeat = struct
type t = { status_message: string; }
end
module Logon = struct
type t = { user: string;
credentials: string;
}
end ;;
#part 5
type details =
| Logon of Logon.t
| Heartbeat of Heartbeat.t
| Log_entry of Log_entry.t
;;
#part 6
module Common = struct
type t = { session_id: string;
time: Time.t;
}
end ;;
#part 7
let messages_for_user user messages =
let (user_messages,_) =
List.fold messages ~init:([],String.Set.empty)
~f:(fun ((messages,user_sessions) as acc) ((common,details) as message) ->
let session_id = common.Common.session_id in
match details with
| Logon m ->
if m.Logon.user = user then
(message::messages, Set.add user_sessions session_id)
else acc
| Heartbeat _ | Log_entry _ ->
if Set.mem user_sessions session_id then
(message::messages,user_sessions)
else acc
)
in
List.rev user_messages
;;
#part 8
let handle_message server_state (common,details) =
match details with
| Log_entry m -> handle_log_entry server_state (common,m)
| Logon m -> handle_logon server_state (common,m)
| Heartbeat m -> handle_heartbeat server_state (common,m)
;;