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

zmq_proxy and optional parameter #208

Open
albestro opened this issue Jan 9, 2021 · 5 comments
Open

zmq_proxy and optional parameter #208

albestro opened this issue Jan 9, 2021 · 5 comments

Comments

@albestro
Copy link

albestro commented Jan 9, 2021

I'm not an expert both in Julia and zeromq and I was playing with this wrapper. My example uses the zmq_proxy, which is missing in the wrapper currently, and I managed to make it work.

I would have open a PR with a minimal proposal, but I faced an issue that I'd like to know from you Julia and ZMQ.jl experts about how to address it.

The wrapper function would be something similar to

function proxy(frontend::Socket, backend::Socket, context::Socket)
  ccall((:zmq_proxy, libzmq),
    Cint, (Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}),
    frontend, backend, context)
end

I have a few concerns about this function per se, but the main problem keeping me away from opening a PR is the fact that context may be NULL or a Socket, and I was not able to figure out a way in the ZMQ.jl design to create a "null" Socket (e.g. I may evaluate to add an additional Socket constructor that sets data = C_NULL, but then the problem is the Socket.pollfd, which in turn I don't know anything about).

@stevengj
Copy link
Contributor

stevengj commented Jan 10, 2021

If you want to be able to pass C_NULL for context, you could just declare a separate two-argument method:

function proxy(frontend::Socket, backend::Socket)
  ccall((:zmq_proxy, libzmq),
    Cint, (Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}),
    frontend, backend, C_NULL)
end

@albestro
Copy link
Author

If you want to be able to pass C_NULL for context, you could just declare a separate two-argument method:

function proxy(frontend::Socket, backend::Socket)
  ccall((:zmq_proxy, libzmq),
    Cint, (Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}),
    frontend, backend, C_NULL)
end

Aha! Thanks for the reply! I discarded that option because it does not follow DRY principle, but maybe it is the right way in Julia for a simple thing like this.

I will eventually open a PR with this simple proposal.
Thanks again!

@JamesWrigley
Copy link
Member

So we now have an automatically-wrapped lib.zmq_proxy:

ZMQ.jl/src/bindings.jl

Lines 358 to 360 in e66c836

function zmq_proxy(frontend_, backend_, capture_)
ccall((:zmq_proxy, libzmq), Cint, (Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}), frontend_, backend_, capture_)
end

Which you should be able to call like e.g. lib.zmq_proxy(s1, s2, s3) or lib.zmq_proxy(s1, s2, C_NULL). Do you still need a high-level wrapper? If so I'd probably PR something like:

function proxy(frontend::Socket, backend::Socket, capture=Union{Nothing, Socket})
    capture_arg = isnothing(capture) ? C_NULL : capture
    lib.zmq_proxy(frontend, backend, capture_arg)
end

@stevengj
Copy link
Contributor

stevengj commented Jul 19, 2024

Because zmq_proxy never returns, it's a bit tricky to call in a useful way. See the hoops that IJulia jumps through to call it in a separate thread: https://github.com/JuliaLang/IJulia.jl/blob/60568fabbe8abd54ae26b2acf2c426c830ed801b/src/heartbeat.jl#L13-L32

Might be nice to have a wrapper that helps with some of this in ZMQ.jl.

@JamesWrigley
Copy link
Member

Hum, interesting. What do you think about a pure Julia implementation of proxy()? It's a little unfortunate to duplicate the functionality but it might be a bit safer.

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

No branches or pull requests

3 participants