I'm trying to have one WPF application to communicate with an other application when something change on the UI. For example : a slider changes and I send the new value or a textbox input change and I send the new input value.
The second application is listening and receive those changes to update different properties.
To achieve this I'm using NetMQ following the Actor pattern example from the documentation : https://netmq.readthedocs.io/en/latest/actor/
Right now I have these Client/Server class :
The Server :
public class NetMQServer
{
public class ShimHandler : IShimHandler
{
private PairSocket shim;
private NetMQPoller poller;
private PublisherSocket publisher;
private string Address;
public ShimHandler(string address)
{
Address = address;
}
public void Initialise(object state)
{
}
public void Run(PairSocket shim)
{
using (publisher = new PublisherSocket())
{
publisher.Bind(Address);
publisher.Options.SendHighWatermark = 1000;
this.shim = shim;
shim.ReceiveReady += OnShimReady;
shim.SignalOK();
poller = new NetMQPoller { shim, publisher };
poller.Run();
}
}
private void OnShimReady(object sender, NetMQSocketEventArgs e)
{
string command = e.Socket.ReceiveFrameString();
if(command == NetMQActor.EndShimMessage)
{
poller.Stop();
return;
}
else
{
byte[] byteMessage = e.Socket.ReceiveFrameBytes();
publisher.SendMoreFrame(command).SendFrame(byteMessage);
}
}
private void UpdateString(string stringmessage, string propertyToUpdate)
{
propertyToUpdate = stringmessage;
}
}
public NetMQServer(string ip, int port)
{
IP = ip;
Port = port;
Serializer = new CerasSerializer();
}
public CerasSerializer Serializer { get; set; }
private NetMQActor actor;
private string _name;
public string Name
{
get { return _name; }
set { _name = value;}
}
private int _port;
public int Port
{
get { return _port; }
set
{
_port = value;
ReStart();
}
}
private string _ip;
public string IP
{
get { return _ip; }
set
{
_ip = value;
ReStart();
}
}
public string Address
{
get { return String.Format("tcp://{0}:{1}", IP, Port); }
}
public void Start()
{
if (actor != null)
return;
actor = NetMQActor.Create(new ShimHandler(Address));
}
public void Stop()
{
if (actor != null)
{
actor.Dispose();
actor = null;
}
}
public void ReStart()
{
if (actor == null)
return;
Stop();
Start();
}
public void SendObject(string topic, object commandParameter)
{
if (actor == null)
return;
byte[] Serialized = Serializer.Serialize(commandParameter);
var message = new NetMQMessage();
message.Append(topic);
message.Append(Serialized);
actor.SendMultipartMessage(message);
}
}
Here when the property of a control change I call SendStringMessage(string stringToSend)
function and it is sending with the NetMQ publisher (the string is only for testing, I could send whatever object as byte too).
Anyway, here is the client running on the second app :
public class NetMQClient
{
public class ShimHandler : IShimHandler
{
private PairSocket shim;
private NetMQPoller poller;
private SubscriberSocket subscriber;
private ByteMessage ByteMessage;
private string Address;
private string Topic;
private string MessageType;
public ShimHandler(ByteMessage byteMessage, string address, string topic)
{
this.ByteMessage = byteMessage;
this.Address = address;
this.Topic = topic;
}
public void Initialise(object state)
{
}
public void Run(PairSocket shim)
{
using (subscriber = new SubscriberSocket())
{
subscriber.Connect(Address);
subscriber.Subscribe(Topic);
subscriber.Options.ReceiveHighWatermark = 1000;
subscriber.ReceiveReady += OnSubscriberReady;
this.shim = shim;
shim.ReceiveReady += OnShimReady;
shim.SignalOK();
poller = new NetMQPoller { shim, subscriber };
poller.Run();
}
}
private void OnSubscriberReady(object sender, NetMQSocketEventArgs e)
{
string topic = e.Socket.ReceiveFrameString();
if (topic == Topic)
{
this.ByteMessage.Message = e.Socket.ReceiveFrameBytes();
}
}
private void OnShimReady(object sender, NetMQSocketEventArgs e)
{
string command = e.Socket.ReceiveFrameString();
if (command == NetMQActor.EndShimMessage)
{
poller.Stop();
}
}
}
public ByteMessage ByteMessage { get; set; }
private NetMQActor actor;
private string _command;
public string Command
{
get { return _command; }
set { _command = value;}
}
private string _topic;
public string Topic
{
get { return _topic; }
set
{
_topic = value;
ReStart();
}
}
private int _port;
public int Port
{
get { return _port; }
set
{
_port = value;
ReStart();
}
}
private string _ip;
public string IP
{
get { return _ip; }
set
{
_ip = value;
ReStart();
}
}
public string Address
{
get { return String.Format("tcp://{0}:{1}", IP, Port); }
}
public NetMQClient(string ip, int port, string topic)
{
ByteMessage = new ByteMessage();
IP = ip;
Port = port;
Topic = topic;
}
public void Start()
{
if (actor != null)
return;
actor = NetMQActor.Create(new ShimHandler(ByteMessage, Address, Topic));
}
public void Stop()
{
if (actor != null)
{
actor.Dispose();
actor = null;
}
}
public void ReStart()
{
if (actor == null)
return;
Stop();
Start();
}
}
Now the ByteMessage class just look as simple as this :
public class ByteMessage : INotifyPropertyChanged
{
public ByteMessage()
{
}
private byte[] _message;
public byte[] Message
{
get { return _message; }
set
{
_message = value;
OnPropertyChanged("Message");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
One the second App, Classes will have a NetMQClient as property and register to the OnPropertyChange
event of the NetMQClient's ByteMessage
.
Now when ByteMessage.Message
is updated from the ShimHandler
I can Deserialize the data and do something with it.
So far this is working but I'm really not sure if I'm doing it correctly... and if this is ThreadSafe
Shouldn't it be better if each class on the Listening app would have their own NetMQClient listening to a specific topic ?
Really confuse here how to use all that, even after reading all examples provided around NetMQ.
Thanks