Skip to content
This repository
tag: v729
Fetching contributors…

Cannot retrieve contributors at this time

file 113 lines (80 sloc) 3.626 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA 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 Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
(** Communications with clients. *)

type json = JsonTypes.json

(** Client module should be define type of client identifier and
message and a serialization function for ping register. *)
module type CLIENT = sig

  (** Type of identifier of a client. *)
  type key

  (** Type of message can be sent to clients. *)
  type msg

  (** Serialiaze a message to json which can be interpreted by a client. *)
  val serialize : msg -> json Cps.t

  (** {6 For debug}*)
  (** Used by [PING_DEBUG]. *)
  val key_to_string : key -> string

end

module type SCHEDULER = sig
  type async_key
  val sleep : int -> (unit -> unit) -> async_key
  val abort : async_key -> unit
end

(** Make a ping register. A ping register is a module which allows to
communicates with cient.*)
module Make (S : SCHEDULER) (C : CLIENT) : sig

  (** Kind of ping register events. *)
  type event =
    | Connect (** Launched when a client connects to ping register. *)
    | Disconnect (** Launched when a client is disconnected to ping register. *)
    | Inactive (** Launched when a client is inactive for a while *)

  (** Type of key binded to an event handler (see section Events). *)
  type event_key

  (** {6 Sending messages} *)

  (** [send message id] Send a message to client identified by
[id]. *)
  val send : C.msg -> C.key -> unit

  (** Broadcast a message to all connected clients. *)
  val broadcast : C.msg -> unit

  (** {6 Interactions with client}*)

  (** [ping id winfo nb] Ping the register form client [id], [winfo]
must allows to reply to client. [nb] is the ping number. *)
  val ping : C.key -> HttpServerTypes.web_info -> int -> unit

  (** [pang id winfo nb] Like as [ping id winfo nb] but you can reply
to this specific [pang] with [return id nb response]. *)
  val pang : C.key -> HttpServerTypes.web_info -> int -> bool -> unit

  (** Returns a [pang]. *)
  val return : C.key -> int -> string -> unit

  (** {6 Events} *)

  (** Register a callback that will be executed when a corresponding
event will be launched. The first parameters indicates on
which client the callback will be registered, if it's None
then the callback will be executed for all client. *)
  val register_event : C.key option -> event -> (C.key -> unit) -> event_key

  (** Remove callback event registered associated with [event_key]. *)
  val remove_event : event_key -> unit

  (** Set delay before raising an Inactive even. *)
  val set_inactive_delay : C.key option -> Time.t option -> unit

  (** {6 Utils} *)

  (** Check if the client is already connected to the ping
register. *)
  val mem : C.key -> bool

  (** Delete a client entry. *)
  val delete : C.key -> unit

  (** Create a client entry (Is needed ...). *)
  val create : C.key -> unit

  (** Return the number of connections. *)
  val size : unit -> int

  val update_activity : ?nb:int -> ?is_ping:bool -> ?is_active:bool -> ?winfo:HttpServerTypes.web_info -> C.key -> bool

end
Something went wrong with that request. Please try again.