123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120(*********************************************************************************)(* Ojs-base *)(* *)(* Copyright (C) 2014-2021 INRIA. All rights reserved. *)(* *)(* This program is free software; you can redistribute it and/or modify *)(* it under the terms of the GNU General Public License as *)(* published by the Free Software Foundation, version 3 of the License. *)(* *)(* This program is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)(* GNU Library General Public License for more details. *)(* *)(* You should have received a copy of the GNU General Public *)(* License along with this program; if not, write to the Free Software *)(* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA *)(* 02111-1307 USA *)(* *)(* As a special exception, you have permission to link this program *)(* with the OCaml compiler and distribute executables, as long as you *)(* follow the requirements of the GNU GPL in regard to all of the *)(* software in the executable aside from the OCaml compiler. *)(* *)(* Contact: Maxence.Guesdon@inria.fr *)(* *)(*********************************************************************************)(** Handling lists on server side. *)openOjs_server.Serverlet(>>=)=Lwt.(>>=)moduleMake(P:Ojs_list.Types.P)=structclass['a]elist(broadcall:'aP.server_msg->('aP.client_msg->unitLwt.t)->unitLwt.t)(broadcast:'aP.server_msg->unitLwt.t)~idinit=object(self)valmutablelist=(init:'alist)methodlist=listmethodset_listl=list<-l;broadcast(P.SListl)methodid=(id:string)methodhandle_addreplyx=list<-x::list;replyP.SOk>>=fun_->broadcast(P.SAddx)methodhandle_deletereplyx=list<-List.filter((<>)x)list;replyP.SOk>>=fun_->broadcast(P.SAddx)methodhandle_getreply=reply(P.SListlist)methodhandle_message(send_msg:'aP.server_msg->unitLwt.t)(msg:'aP.client_msg)=self#handle_callsend_msgmsgmethodhandle_call(reply_msg:'aP.server_msg->unitLwt.t)(msg:'aP.client_msg)=matchmsgwith|P.Get->self#handle_getreply_msg|P.Addx->self#handle_addreply_msgx|P.Deletex->self#handle_deletereply_msgx|_->failwith"List: Unhandled message"endclass['a]elistsbroadcallbroadcastspawn(* (broadcall : (string * 'a P.server_msg) P.msg ->
((string * 'a P.client_msg) P.msg -> unit Lwt.t) -> unit Lwt.t)
(broadcast : (string * 'a P.server_msg) P.msg -> unit Lwt.t)
(spawn : ('a P.server_msg -> ('a P.client_msg -> unit Lwt.t) -> unit Lwt.t) ->
('a P.server_msg -> unit Lwt.t) ->
id: string -> 'a list -> 'a elist
)*)=object(self)valmutablelists=(SMap.empty:'aelistSMap.t)methodlistid=trySMap.findidlistswithNot_found->failwith(Printf.sprintf"No list with id %S"id)methodadd_list~id(init:'alist)=letbroadcallmsgcb=letcbmsg=matchP.unpack_client_msgmsgwith|Some(_,msg)->cbmsg|None->Lwt.return_unitinbroadcall(P.pack_server_msgidmsg)cbinletbroadcastmsg=broadcast(P.pack_server_msgidmsg)inletelist=spawnbroadcallbroadcast~idinitinlists<-SMap.addidelistlists;elistmethodhandle_message(send_msg:P.app_server_msg->unitLwt.t)(msg:P.app_client_msg)=matchP.unpack_client_msgmsgwith|Some(id,msg)->letsend_msgmsg=send_msg(P.pack_server_msgidmsg)in(self#listid)#handle_messagesend_msgmsg|None->Lwt.return_unitmethodhandle_call(return:P.app_server_msg->unitLwt.t)(msg:P.app_client_msg)=matchP.unpack_client_msgmsgwith|Some(id,msg)->letreply_msgmsg=return(P.pack_server_msgidmsg)in(self#listid)#handle_callreply_msgmsg|None->Lwt.return_unitendend