Refactored poll support. Add Chapter 2 examples from the official ZMQ guide.
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| MSPoller.mq4 |
|
||||
//| Copyright 2017, Li Ding |
|
||||
//| dingmaotu@126.com |
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "Copyright 2017, Li Ding"
|
||||
#property link "dingmaotu@126.com"
|
||||
#property version "1.00"
|
||||
#property strict
|
||||
#include <Zmq/Zmq.mqh>
|
||||
//+------------------------------------------------------------------+
|
||||
//| Reading from multiple sockets in MQL (adapted from C++ version) |
|
||||
//| This version uses zmq_poll() |
|
||||
//| |
|
||||
//| Olivier Chamoux <olivier.chamoux@fr.thalesgroup.com> |
|
||||
//+------------------------------------------------------------------+
|
||||
void OnStart()
|
||||
{
|
||||
//---
|
||||
Context context;
|
||||
|
||||
// Connect to task ventilator
|
||||
Socket receiver(context,ZMQ_PULL);
|
||||
receiver.connect("tcp://localhost:5557");
|
||||
|
||||
// Connect to weather server
|
||||
Socket subscriber(context,ZMQ_SUB);
|
||||
subscriber.connect("tcp://localhost:5556");
|
||||
subscriber.subscribe("10001 ");
|
||||
|
||||
// Initialize poll set
|
||||
PollItem items[2];
|
||||
receiver.fillPollItem(items[0],ZMQ_POLLIN);
|
||||
subscriber.fillPollItem(items[1],ZMQ_POLLIN);
|
||||
// Process messages from both sockets
|
||||
while(!IsStopped())
|
||||
{
|
||||
ZmqMsg message;
|
||||
//--- MQL Note: To handle Script exit properly, we set a timeout of 500 ms instead of infinite wait
|
||||
Socket::poll(items,500);
|
||||
|
||||
if(items[0].hasInput())
|
||||
{
|
||||
receiver.recv(message);
|
||||
// Process task
|
||||
}
|
||||
if(items[1].hasInput())
|
||||
{
|
||||
subscriber.recv(message);
|
||||
// Process weather update
|
||||
}
|
||||
}
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
@@ -0,0 +1,63 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| RRBroker.mq4 |
|
||||
//| Copyright 2017, Li Ding |
|
||||
//| dingmaotu@126.com |
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "Copyright 2017, Li Ding"
|
||||
#property link "dingmaotu@126.com"
|
||||
#property version "1.00"
|
||||
#property strict
|
||||
#include <Zmq/Zmq.mqh>
|
||||
//+------------------------------------------------------------------+
|
||||
//| Simple request-reply broker in MQL (adapted from C++ version) |
|
||||
//| |
|
||||
//| Olivier Chamoux <olivier.chamoux@fr.thalesgroup.com> |
|
||||
//+------------------------------------------------------------------+
|
||||
void OnStart()
|
||||
{
|
||||
// Prepare our context and sockets
|
||||
Context context;
|
||||
Socket frontend(context,ZMQ_ROUTER);
|
||||
Socket backend(context,ZMQ_DEALER);
|
||||
|
||||
frontend.bind("tcp://*:5559");
|
||||
backend.bind("tcp://*:5560");
|
||||
|
||||
// Initialize poll set
|
||||
PollItem items[2];
|
||||
frontend.fillPollItem(items[0],ZMQ_POLLIN);
|
||||
backend.fillPollItem(items[1],ZMQ_POLLIN);
|
||||
|
||||
// Switch messages between sockets
|
||||
while(!IsStopped())
|
||||
{
|
||||
ZmqMsg message;
|
||||
bool more=false; // Multipart detection
|
||||
|
||||
Socket::poll(items,500);
|
||||
|
||||
if(items[0].hasInput())
|
||||
{
|
||||
// Process all parts of the message
|
||||
do
|
||||
{
|
||||
frontend.recv(message);
|
||||
more=message.more();
|
||||
backend.send(message,false,more);
|
||||
}
|
||||
while(more);
|
||||
}
|
||||
if(items[1].hasInput())
|
||||
{
|
||||
// Process all parts of the message
|
||||
do
|
||||
{
|
||||
backend.recv(message);
|
||||
more=message.more();
|
||||
frontend.send(message,false,more);
|
||||
}
|
||||
while(more);
|
||||
}
|
||||
}
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
@@ -0,0 +1,36 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| RRClient.mq4 |
|
||||
//| Copyright 2017, Li Ding |
|
||||
//| dingmaotu@126.com |
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "Copyright 2017, Li Ding"
|
||||
#property link "dingmaotu@126.com"
|
||||
#property version "1.00"
|
||||
#property strict
|
||||
#include <Zmq/Zmq.mqh>
|
||||
//+------------------------------------------------------------------+
|
||||
//| Request-reply client in MQL (adapted from C++ version) |
|
||||
//| Connects REQ socket to tcp://localhost:5559 |
|
||||
//| Sends "Hello" to server, expects "World" back |
|
||||
//| |
|
||||
//| Olivier Chamoux <olivier.chamoux@fr.thalesgroup.com> |
|
||||
//+------------------------------------------------------------------+
|
||||
void OnStart()
|
||||
{
|
||||
Context context;
|
||||
|
||||
Socket requester(context,ZMQ_REQ);
|
||||
requester.connect("tcp://localhost:5559");
|
||||
|
||||
for(int request=0; request<10; request++)
|
||||
{
|
||||
ZmqMsg message("Hello");
|
||||
requester.send(message);
|
||||
|
||||
ZmqMsg reply;
|
||||
requester.recv(reply,true);
|
||||
|
||||
Print("Received reply ",reply.getData());
|
||||
}
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
@@ -0,0 +1,41 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| RRWorker.mq4 |
|
||||
//| Copyright 2017, Li Ding |
|
||||
//| dingmaotu@126.com |
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "Copyright 2017, Li Ding"
|
||||
#property link "dingmaotu@126.com"
|
||||
#property version "1.00"
|
||||
#property strict
|
||||
#include <Zmq/Zmq.mqh>
|
||||
//+------------------------------------------------------------------+
|
||||
//| Request-reply service in MQL (adapted from C++ version) |
|
||||
//| Connects REP socket to tcp://localhost:5560 |
|
||||
//| Expects "Hello" from client, replies with "World" |
|
||||
//| |
|
||||
//| Olivier Chamoux <olivier.chamoux@fr.thalesgroup.com> |
|
||||
//+------------------------------------------------------------------+
|
||||
void OnStart()
|
||||
{
|
||||
Context context;
|
||||
|
||||
Socket responder(context,ZMQ_REP);
|
||||
responder.connect("tcp://localhost:5560");
|
||||
|
||||
while(!IsStopped())
|
||||
{
|
||||
// Wait for next request from client
|
||||
ZmqMsg req;
|
||||
responder.recv(req);
|
||||
Print("Received request: ",req.getData());
|
||||
|
||||
// Do some 'work'
|
||||
Sleep(1000);
|
||||
|
||||
ZmqMsg reply("World");
|
||||
|
||||
// Send reply back to client
|
||||
responder.send(reply);
|
||||
}
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
@@ -0,0 +1,47 @@
|
||||
//+------------------------------------------------------------------+
|
||||
//| WUProxy.mq4 |
|
||||
//| Copyright 2017, Li Ding |
|
||||
//| dingmaotu@126.com |
|
||||
//+------------------------------------------------------------------+
|
||||
#property copyright "Copyright 2017, Li Ding"
|
||||
#property link "dingmaotu@126.com"
|
||||
#property version "1.00"
|
||||
#property strict
|
||||
#include <Zmq/Zmq.mqh>
|
||||
//+------------------------------------------------------------------+
|
||||
//| Weather proxy device MQL (adapted from C++) |
|
||||
//| |
|
||||
//| Olivier Chamoux <olivier.chamoux@fr.thalesgroup.com> |
|
||||
//+------------------------------------------------------------------+
|
||||
void OnStart()
|
||||
{
|
||||
//---
|
||||
Context context;
|
||||
|
||||
// This is where the weather server sits
|
||||
Socket frontend(context,ZMQ_XSUB);
|
||||
frontend.connect("tcp://192.168.55.210:5556");
|
||||
|
||||
// This is our public endpoint for subscribers
|
||||
Socket backend(context,ZMQ_XPUB);
|
||||
backend.bind("tcp://10.1.1.0:8100");
|
||||
|
||||
// Subscribe on everything
|
||||
frontend.subscribe("");
|
||||
|
||||
// Shunt messages out to our own subscribers
|
||||
while(!IsStopped())
|
||||
{
|
||||
// Process all parts of the message
|
||||
ZmqMsg message;
|
||||
bool more;
|
||||
do
|
||||
{
|
||||
frontend.recv(message);
|
||||
more=message.more();
|
||||
backend.send(message,false,more);
|
||||
}
|
||||
while(more);
|
||||
}
|
||||
}
|
||||
//+------------------------------------------------------------------+
|
||||
Reference in New Issue
Block a user