annotate Messaging/Server/Listeners/ZeroMqListener.cs @ 60:fc62c971a117

upload to blog
author Steven Hollidge <stevenhollidge@hotmail.com>
date Sat, 21 Apr 2012 21:19:39 +0100
parents 7d9de5746f18
children
rev   line source
31
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
1 using System;
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
2 using System.Text;
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
3 using Common.Messages;
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
4 using GalaSoft.MvvmLight.Messaging;
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
5
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
6 namespace Server.Listeners
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
7 {
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
8 public class ZeroMqListener : IListener
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
9 {
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
10 public bool IsListening { get; set; }
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
11 private readonly string _zeroMqAddress;
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
12 public ZeroMqListener(string zeroMqAddress)
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
13 {
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
14 _zeroMqAddress = zeroMqAddress;
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
15 }
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
16
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
17 public void Start()
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
18 {
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
19 if (IsListening) return;
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
20
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
21 Messenger.Default.Send(new ZeroMqLogMessage() { Body = "Opening listener" });
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
22 IsListening = true;
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
23
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
24 try
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
25 {
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
26 using (var ctx = new ZMQ.Context(1))
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
27 {
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
28
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
29 var socket = ctx.Socket(ZMQ.REP);
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
30 socket.Bind(_zeroMqAddress);
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
31
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
32 while (IsListening)
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
33 {
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
34 byte[] message;
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
35 socket.Recv(out message);
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
36 Messenger.Default.Send(new ZeroMqClientMessage());
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
37
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
38 socket.Send(Encoding.ASCII.GetBytes(""));
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
39 }
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
40 }
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
41 }
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
42 catch (Exception e)
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
43 {
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
44 Messenger.Default.Send(new ZeroMqLogMessage() { Body = e.Message });
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
45 }
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
46 }
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
47
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
48 public void Stop()
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
49 {
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
50 if (!IsListening) return;
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
51 IsListening = false;
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
52 Messenger.Default.Send(new ZeroMqLogMessage() { Body = "Listener closed." });
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
53 }
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
54 }
7d9de5746f18 Working version
adminsh@apollo
parents:
diff changeset
55 }