ZMQ REQ Message Source: Difference between revisions
(rewording opening section) |
(expand Example Flowgraph section) |
||
Line 14: | Line 14: | ||
== Example Flowgraph == | == Example Flowgraph == | ||
=== 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]] | [[File:REQ_REP_msg_demo_fg.png]] | ||
=== External Python client (send only) === | |||
An external Python program can send messages to a ZMQ REQ Message Source block. An example flowgraph and Python code follow.<br> | |||
=== 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. An example flowgraph and Python code follow.<br> | |||
[[File:Server_demo_fg.png]] | |||
The Embedded Python block "Server demo" contains the following code:<br> | |||
<pre> | |||
from gnuradio import gr | |||
import pmt | |||
inputText = "" | |||
class my_sync_block (gr.sync_block): | |||
# accepts message string from input port | |||
# capitalizes the string | |||
# sends message to output port | |||
def __init__(self): | |||
gr.sync_block.__init__(self, | |||
name = "Server demo", | |||
in_sig = None, | |||
out_sig = None) | |||
self.message_port_register_in(pmt.intern('msg_in')) | |||
self.message_port_register_out(pmt.intern('msg_out')) | |||
self.set_msg_handler(pmt.intern('msg_in'), self.handle_msg) | |||
def handle_msg(self, msg): | |||
global inputText | |||
inputText = pmt.symbol_to_string (msg) | |||
# print (inputText) | |||
if (len (inputText) > 0): | |||
# capitalize the string | |||
outputText = inputText.upper() | |||
# print (outputText) | |||
# Send reply back to client | |||
self.message_port_pub(pmt.intern('msg_out'), pmt.intern(outputText)) | |||
def work(self, input_items, output_items): | |||
# with no data ports, there is nothing to do | |||
return (0) | |||
</pre> | |||
== Source Files == | == Source Files == |
Revision as of 01:34, 12 March 2020
The ZMQ REQ Message Source block receives messages from a ZMQ REQ socket and outputs async messages. This block will connect to a ZMQ REP Message Sink.
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.
- 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.
External Python client (send only)
An external Python program can send messages to a ZMQ REQ Message Source block. An example flowgraph and Python code follow.
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. An example flowgraph and Python code follow.
The Embedded Python block "Server demo" contains the following code:
from gnuradio import gr import pmt inputText = "" class my_sync_block (gr.sync_block): # accepts message string from input port # capitalizes the string # sends message to output port def __init__(self): gr.sync_block.__init__(self, name = "Server demo", in_sig = None, out_sig = None) self.message_port_register_in(pmt.intern('msg_in')) self.message_port_register_out(pmt.intern('msg_out')) self.set_msg_handler(pmt.intern('msg_in'), self.handle_msg) def handle_msg(self, msg): global inputText inputText = pmt.symbol_to_string (msg) # print (inputText) if (len (inputText) > 0): # capitalize the string outputText = inputText.upper() # print (outputText) # Send reply back to client self.message_port_pub(pmt.intern('msg_out'), pmt.intern(outputText)) def work(self, input_items, output_items): # with no data ports, there is nothing to do return (0)
Source Files
- C++ files
- TODO
- Header files
- TODO
- Public header files
- TODO
- Block definition
- TODO