view Messaging/Server/Listeners/ZeroMqListener.cs @ 44:382813d76dde Test

Removal of pdf project
author Steven Hollidge <stevenhollidge@hotmail.com>
date Wed, 04 Apr 2012 19:21:31 +0100
parents 7d9de5746f18
children
line wrap: on
line source

using System;
using System.Text;
using Common.Messages;
using GalaSoft.MvvmLight.Messaging;

namespace Server.Listeners
{
    public class ZeroMqListener : IListener
    {
        public bool IsListening { get; set; }
        private readonly string _zeroMqAddress;
        public ZeroMqListener(string zeroMqAddress)
        {
            _zeroMqAddress = zeroMqAddress;
        }

        public void Start()
        {
            if (IsListening) return;

            Messenger.Default.Send(new ZeroMqLogMessage() { Body = "Opening listener" });
            IsListening = true;
            
            try
            {
                using (var ctx = new ZMQ.Context(1))
                {
                    
                    var socket = ctx.Socket(ZMQ.REP);
                    socket.Bind(_zeroMqAddress);

                    while (IsListening)
                    {
                        byte[] message;
                        socket.Recv(out message);
                        Messenger.Default.Send(new ZeroMqClientMessage());

                        socket.Send(Encoding.ASCII.GetBytes(""));
                    }
                }
            }
            catch (Exception e)
            {
                Messenger.Default.Send(new ZeroMqLogMessage() { Body = e.Message });
            }
        }

        public void Stop()
        {
            if (!IsListening) return;
            IsListening = false;
            Messenger.Default.Send(new ZeroMqLogMessage() { Body = "Listener closed." });
        }
    }
}