annotate Messaging/Server/Listeners/RabbitQueueListener.cs @ 103:8cb4f36717e9

Fake it easy dll added
author stevenhollidge <stevenhollidge@hotmail.com>
date Sun, 06 May 2012 12:14:53 +0100
parents 7d9de5746f18
children
rev   line source
27
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
1 using System;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
2 using System.Diagnostics;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
3 using System.Text;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
4 using Common.Messages;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
5 using GalaSoft.MvvmLight.Messaging;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
6 using RabbitMQ.Client;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
7 using RabbitMQ.Client.Events;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
8 using RabbitMQ.Client.MessagePatterns;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
9
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
10 namespace Server.Listeners
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
11 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
12 class RabbitQueueListener : IListener
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
13 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
14 public bool IsListening { get; set; }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
15 private readonly int _port;
28
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
16 private Subscription _subscription;
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
17 private string _queueName;
27
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
18
28
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
19 public RabbitQueueListener(int port, string queueName)
27
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
20 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
21 _port = port;
28
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
22 _queueName = queueName;
27
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
23 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
24
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
25 public void Start()
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
26 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
27 try
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
28 {
28
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
29 if (IsListening) return;
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
30
27
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
31 var serverAddress = "localhost:" + _port;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
32
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
33 var connectionFactory = new ConnectionFactory { Address = serverAddress };
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
34
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
35 using (var connection = connectionFactory.CreateConnection())
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
36 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
37 using (var channel = connection.CreateModel())
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
38 {
31
7d9de5746f18 Working version
adminsh@apollo
parents: 28
diff changeset
39 Log("Opening listener");
28
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
40 string queueName = channel.QueueDeclare(_queueName, false, false, false, null);
27
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
41 channel.QueueBind(queueName, "amq.direct", queueName, null);
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
42
28
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
43 using (_subscription = new Subscription(channel, queueName))
27
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
44 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
45 IsListening = true;
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
46 while (IsListening)
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
47 {
28
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
48 foreach (BasicDeliverEventArgs eventArgs in _subscription)
27
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
49 {
28
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
50 //Log(Encoding.UTF8.GetString(eventArgs.Body));
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
51 Messenger.Default.Send(new RabbitClientMessage());
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
52 _subscription.Ack();
27
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
53 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
54 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
55 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
56 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
57 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
58 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
59 catch (Exception e)
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
60 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
61 Log(e.Message);
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
62 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
63 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
64
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
65 public void Stop()
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
66 {
31
7d9de5746f18 Working version
adminsh@apollo
parents: 28
diff changeset
67 if (!IsListening) return;
28
4c0dea4760c5 RabbitMq working
adminsh@apollo
parents: 27
diff changeset
68 _subscription.Close();
27
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
69 IsListening = false;
31
7d9de5746f18 Working version
adminsh@apollo
parents: 28
diff changeset
70 Log("Listener closed.");
27
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
71 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
72
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
73 private void Log(string message)
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
74 {
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
75 Debug.WriteLine(message);
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
76 Messenger.Default.Send(new RabbitLogMessage() { Body = message });
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
77 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
78 }
96fdf58e05b4 Server working with sockets and rabbitmq
adminsh@apollo
parents:
diff changeset
79 }