123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151# 1 "src/lib/eliom_bus.server.ml"(* Ocsigen
* http://www.ocsigen.org
* Copyright (C) 2010
* Raphaël Proust
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, with linking exception;
* either version 2.1 of the License, or (at your option) any later version.
*
* 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser 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.
*)moduleEcb=Eliom_comet_basetype('a,'b)t={stream:'bLwt_stream.t;scope:Eliom_comet.Channel.comet_scope;name:stringoption;channel:'bEliom_comet.Channel.toption;write:'a->unitLwt.t;service:'aEcb.bus_send_service;service_registered:boolEliom_state.volatile_tableoption;size:intoption;bus_mark:('a,'b)tEliom_common.wrapper(* must be the last field ! *)}[@@warning"-69"]letregister_senderscopeservicewrite=Eliom_registration.Action.register~scope~options:`NoReload~service(fun()x->Lwt_list.iter_swritex)letinternal_wrap(bus:('a,'b)t):('a,'b)Ecb.wrapped_bus*Eliom_common.unwrapper=letchannel=matchbus.channelwith|None->Eliom_comet.Channel.create~scope:bus.scope?name:bus.name?size:bus.size(Lwt_stream.clonebus.stream)|Somec->cin(matchbus.service_registeredwith|None->()|Sometable->(matchEliom_state.get_volatile_data~table()with|Eliom_state.Datatrue->()|_->let{service=Ecb.Bus_send_servicesrv;_}=businregister_senderbus.scope(srv:>(_,_list,_,_,_,Eliom_service.non_ext,_,_,_,_,_)Eliom_service.t)bus.write;Eliom_state.set_volatile_data~tabletrue));((Eliom_comet.Channel.get_wrappedchannel,bus.service),Eliom_common.make_unwrapperEliom_common.bus_unwrap_id)letbus_mark()=Eliom_common.make_wrapperinternal_wrapletderiving_to_list:'aDeriving_Json.t->'alistDeriving_Json.t=fun(typetyp)typ->let(typ_list:typlistDeriving_Json.t)=letmoduleM=Deriving_Json.Json_list(Deriving_Json.Defaults''(structtypea=typlett=typend))inM.tintyp_listletcreate_filtered?scope?name?size~filtertyp=(*The stream*)letstream,push=Lwt_stream.create()inletpushx=let%lwty=filterxinpush(Somey);Lwt.return_unitinletscope=matchscopewith|None|Some`Site->`Site|Some(`Client_processn)->`Client_processninletchannel=matchscopewith|`Site->Some(Eliom_comet.Channel.create~scope?name?size(Lwt_stream.clonestream))|`Client_process_->Noneinlettyp_list=deriving_to_listtypin(*The service*)letpost_params=(Eliom_parameter.ocaml"bus_write"typ_list:('a,'aa,'aaa)Eliom_parameter.params_type)inletdistant_write=Eliom_service.create?name~meth:(Eliom_service.Post(Eliom_parameter.unit,post_params))~path:Eliom_service.No_path()inletservice_registered=matchscopewith|`Site->register_senderscopedistant_writepush;None|`Client_process_asscope->Some(Eliom_state.create_volatile_table~scope())in(*The bus*)letbus={stream;channel;scope;name;write=push;service=Eliom_comet_base.Bus_send_servicedistant_write;service_registered;bus_mark=bus_mark();size}inbusletcreate?scope?name?sizetyp=create_filtered~filter:Lwt.return?scope?name?sizetypletstreambus=matchbus.scopewith`Site->bus.stream|`Client_process_->bus.streamletwritebusx=bus.writex