Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to easier to rewrite code to work with binary data? #11

Open
mryau opened this issue Nov 28, 2016 · 2 comments
Open

How to easier to rewrite code to work with binary data? #11

mryau opened this issue Nov 28, 2016 · 2 comments

Comments

@mryau
Copy link
Contributor

mryau commented Nov 28, 2016

Hello,
I want to work with structured messages packed to protocol buffers.
Is it right way to parametrize Router module by message type or you do not interested in such patches?

@hcarty
Copy link
Owner

hcarty commented Dec 1, 2016

Do you have an example of what you would want the interface to look like? I could see something along the lines of the following being simple but useful wrappers:

val make_send : ('a -> string list) -> _ Lwt_zmq.Socket.t -> ('a -> unit Lwt.t)
val make_recv : (string list -> 'a) -> _ Lwt_zmq.Socket.t -> (unit -> 'a Lwt.t)

val make_send_s : ('a -> string list Lwt.t) -> _ Lwt_zmq.Socket.t -> ('a -> unit Lwt.t)
val make_recv_s : (string list -> 'a Lwt.t) -> _ Lwt_zmq.Socket.t -> (unit -> 'a Lwt.t)

make_send to_message socket would return a function which sends values over socket, serialized by to_message.

make_recv of_message socket would return a function which received values over socket, deserialized by of_message.

The _s versions would take a cooperative function for the (de)serialization process.

@mryau
Copy link
Contributor Author

mryau commented Dec 5, 2016

Hello,

currently I need only properly typed messages on zmq bus so I prefer following modification of your code with functor:

module type IDType = sig
  type t
  val to_string : t -> string
  val of_string : string -> t
end

module FLSocket(DType : IDType) = struct

  type 'a t = {
    socket : 'a ZMQ.Socket.t;
    fd : Lwt_unix.file_descr;
  }

...
  let recv s =
    wrap (fun s -> let r = ZMQ.Socket.recv ~block:false s in DType.of_string r) s

  let send ?more s m =
    wrap (fun s -> ZMQ.Socket.send ?more ~block:false s (DType.to_string m)) s
...
end

type vlan = int list [@@deriving protobuf { protoc }]
type io =
        | Port of int [@key 1]
        | Agg of int [@key 2]
        | Vlan of vlan [@key 3]
        [@@deriving protobuf { protoc }]

type router = io list [@@deriving protobuf { protoc }]

module LSocket =
  FLSocket(struct
    type t = router
    let to_string = Protobuf.Encoder.encode_exn router_to_protobuf
    let of_string = Protobuf.Decoder.decode_exn router_from_protobuf
  end)

I don't know should I send such modification as pull request to your repository?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants