ZMQ REP Message Sink: Difference between revisions

From GNU Radio
Jump to navigation Jump to search
(Created page with "Category:Block Docs Category:Stub Docs This is the template for the "Page-per-block Docs". This first section should describe what the block...")
 
(add note about IP addresses)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Block Docs]]
[[Category:Block Docs]]
[[Category:Stub Docs]]
The ZMQ REP Message Sink block receives async messages and sends them to a ZMQ REP socket. This block will connect to a ZMQ REQ Message Source.<br>
This is the template for the [[:Category:Block_Docs|"Page-per-block Docs"]]. This first section should describe what the block does and how to use it, using however many paragraphs necessary.  Note that the title of the wiki page should match the block's name in GRC, i.e. the one defined in the block's .grc file.  Look at the [[FFT]] Block for a good example.
The zeromq.org website says:<br>
 
"The REQ-REP socket pair is in lockstep. The client issues zmq_send() and then zmq_recv(), in a loop (or once if that's all it needs). Doing any other sequence (e.g., sending two messages in a row) will result in a return code of -1 from the send or recv call." Likewise, the server "issues zmq_recv() and then zmq_send() in that order, as often as it needs to."<br>
As this is a basic template, it's also in the [[:Category:Stub_Docs|"Stub Docs category"]]. Please improve it.


== Parameters ==
== Parameters ==
(''R''): <span class="plainlinks">[https://wiki.gnuradio.org/index.php/GNURadioCompanion#Variable_Controls ''Run-time adjustable'']</span>
(''R''): <span class="plainlinks">[https://wiki.gnuradio.org/index.php/GNURadioCompanion#Variable_Controls ''Run-time adjustable'']</span>


; Param 1 (''R'')
; Address
: Description of parameter, provide any tips or recommended values. Note that the name of the parameter above should match the param's label that shows up in grc (e.g. Sample Rate).
: ZMQ socket address specifier. The format of the address is <code>tcp://*:port</code> where * should be 127.0.0.1 for localhost.<br>
: <b>Note:</b> If the Source and Sink blocks are on two different computers on the same LAN, then the IP and port number of the Sink block must be specified on each end of that connection. For example, if the Sink is on IP 192.168.2.14:5678 and the Source is on IP 192.168.2.5, both Source and Sink blocks must specify the Sink IP and port (192.168.2.14:5678).


; Param 2
; Timeout
: blah blah blah
: Socket timeout in milliseconds, default is 100ms.


== Example Flowgraph ==
== Example Flowgraph ==


Insert description of flowgraph here, then show a screenshot of the flowgraph and the output if there is an interesting GUI.  Currently we have no standard method of uploading the actual flowgraph to the wiki or git repo, unfortunately. The plan is to have an example flowgraph showing how the block might be used, for every block, and the flowgraphs will live in the git repo.
=== Inter-flowgraph ===
 
Request/Reply pairs can be used on one, or two separate, flowgraphs to exchange messages.<br>
 
[[File:REQ_REP_msg_demo_fg.png]]
 
=== External Python client (receive only) ===
 
An external Python program can receive messages from a ZMQ REP Message Sink block. An example flowgraph and Python code follow.<br>
 
[[File:Msg_test6_fg.png]]
 
The Python client code looks like this:<br>
 
<pre>
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
 
#  The REQest / REPly nomenclature of the GNU Radio message blocks is from
#  the perspective of the flowgraph. So, to send a 'request' to GNU Radio, the message
#  must be sent as a 'reply' from the Python client, Likewise, a 'reply' from GNU Radio
#  must be received as a 'request' to the Python client! Therefore, send on the reply socket
#  and receive on the request socket.
#
The zeromq.org website says:
#  "The REQ-REP socket pair is in lockstep. The client issues zmq_send() and then zmq_recv(),
#  in a loop (or once if that's all it needs). Doing any other sequence (e.g., sending two messages in a row)
#  will result in a return code of -1 from the send or recv call." Likewise, the server "issues zmq_recv()
#  and then zmq_send() in that order, as often as it needs to."
#
#  To conform to that requirement, a non-standard "kludge" is used (see below).
 
import zmq
import pmt
 
def main():
    zmq_context = zmq.Context()
    zmq_sock = zmq_context.socket(zmq.REQ)
    zmq_sock.connect("tcp://127.0.0.1:50247")
    while(True):
        zmq_sock.send_string("\x01\x00\x00\x00")    # this is the non-standard "kludge"
        msg = zmq_sock.recv()
        print (pmt.to_python(pmt.deserialize_str(msg)))
if __name__ == '__main__':
    main()
</pre>
 
=== GNU Radio as a server ===
 
If the GNU Radio flowgraph(s) is configured as a server, the REQ message is processed by the flowgraph and a message is sent back in a REP message as the response. See [[ZMQ_REQ_Message_Source#GNU_Radio_as_a_server]] for details.


== Source Files ==
== Source Files ==

Latest revision as of 15:25, 26 March 2020

The ZMQ REP Message Sink block receives async messages and sends them to a ZMQ REP socket. This block will connect to a ZMQ REQ Message Source.
The zeromq.org website says:
"The REQ-REP socket pair is in lockstep. The client issues zmq_send() and then zmq_recv(), in a loop (or once if that's all it needs). Doing any other sequence (e.g., sending two messages in a row) will result in a return code of -1 from the send or recv call." Likewise, the server "issues zmq_recv() and then zmq_send() in that order, as often as it needs to."

Parameters

(R): Run-time adjustable

Address
ZMQ socket address specifier. The format of the address is tcp://*:port where * should be 127.0.0.1 for localhost.
Note: If the Source and Sink blocks are on two different computers on the same LAN, then the IP and port number of the Sink block must be specified on each end of that connection. For example, if the Sink is on IP 192.168.2.14:5678 and the Source is on IP 192.168.2.5, both Source and Sink blocks must specify the Sink IP and port (192.168.2.14:5678).
Timeout
Socket timeout in milliseconds, default is 100ms.

Example Flowgraph

Inter-flowgraph

Request/Reply pairs can be used on one, or two separate, flowgraphs to exchange messages.

REQ REP msg demo fg.png

External Python client (receive only)

An external Python program can receive messages from a ZMQ REP Message Sink block. An example flowgraph and Python code follow.

Msg test6 fg.png

The Python client code looks like this:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

#   The REQest / REPly nomenclature of the GNU Radio message blocks is from
#   the perspective of the flowgraph. So, to send a 'request' to GNU Radio, the message
#   must be sent as a 'reply' from the Python client, Likewise, a 'reply' from GNU Radio
#   must be received as a 'request' to the Python client! Therefore, send on the reply socket
#   and receive on the request socket.
#
#   The zeromq.org website says:
#   "The REQ-REP socket pair is in lockstep. The client issues zmq_send() and then zmq_recv(),
#   in a loop (or once if that's all it needs). Doing any other sequence (e.g., sending two messages in a row)
#   will result in a return code of -1 from the send or recv call." Likewise, the server "issues zmq_recv() 
#   and then zmq_send() in that order, as often as it needs to."
#
#   To conform to that requirement, a non-standard "kludge" is used (see below).

import zmq
import pmt

def main():
    zmq_context = zmq.Context()
    zmq_sock = zmq_context.socket(zmq.REQ)
    zmq_sock.connect("tcp://127.0.0.1:50247")
    while(True):
         zmq_sock.send_string("\x01\x00\x00\x00")    # this is the non-standard "kludge"
         msg = zmq_sock.recv()
         print (pmt.to_python(pmt.deserialize_str(msg)))
if __name__ == '__main__':
    main()

GNU Radio as a server

If the GNU Radio flowgraph(s) is configured as a server, the REQ message is processed by the flowgraph and a message is sent back in a REP message as the response. See ZMQ_REQ_Message_Source#GNU_Radio_as_a_server for details.

Source Files

C++ files
TODO
Header files
TODO
Public header files
TODO
Block definition
TODO