Skip to content

Instantly share code, notes, and snippets.

@AryanGodara
Created September 19, 2023 12:14
Show Gist options
  • Save AryanGodara/0b9767dc3c2838fb15d7a199a99ec7e4 to your computer and use it in GitHub Desktop.
Save AryanGodara/0b9767dc3c2838fb15d7a199a99ec7e4 to your computer and use it in GitHub Desktop.
Implementation of the topicFilter module
module TOPIC_FILTER = struct
(* module TOPICFILTER : TOPIC_FILTER = struct *)
module UnixSocketMap = Map.Make (String)
let socketMap = ref UnixSocketMap.empty
let addSocket topic socket =
print_endline "Adding socket to topic";
let sockets =
try UnixSocketMap.find topic !socketMap with Not_found -> []
in
socketMap :=
UnixSocketMap.add topic (List.append sockets [ socket ]) !socketMap
let removeSocket topic socket =
let sockets =
try UnixSocketMap.find topic !socketMap with Not_found -> []
in
let sockets' = List.filter (fun s -> s <> socket) sockets in
socketMap := UnixSocketMap.add topic sockets' !socketMap
let getSockets topic =
let sockets =
try UnixSocketMap.find topic !socketMap with Not_found -> []
in
List.rev sockets
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment