view Messaging/Server/Listeners/ZeroMqListener.cs @ 37:3783cb9b371f

Working version
author adminsh@apollo
date Thu, 29 Mar 2012 15:00:44 +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." });
        }
    }
}