2 // Mono.Messaging.RabbitMQ
5 // Michael Barker (mike@middlesoft.co.uk)
7 // (C) 2008 Michael Barker
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
34 using System.Net.Sockets;
35 using System.Threading;
39 namespace Mono.Messaging.RabbitMQ {
41 public class RabbitMQMessagingProvider : IMessagingProvider {
43 private int txCounter = 0;
44 private readonly uint localIp;
46 public RabbitMQMessagingProvider()
48 localIp = GetLocalIP ();
51 private static uint GetLocalIP ()
53 //IPHostEntry host = Dns.GetHostEntry (Dns.GetHostName ());
54 String strHostName = Dns.GetHostName ();
55 IPHostEntry ipEntry = Dns.GetHostByName (strHostName);
56 foreach (IPAddress ip in ipEntry.AddressList) {
57 if (AddressFamily.InterNetwork == ip.AddressFamily) {
58 byte[] addr = ip.GetAddressBytes ();
60 for (int i = 0; i < 4 && i < addr.Length; i++) {
61 localIP += (uint) (addr[i] << 8 * (3 - i));
69 public IMessage CreateMessage ()
71 return new MessageBase ();
74 public IMessageQueueTransaction CreateMessageQueueTransaction ()
76 Interlocked.Increment (ref txCounter);
77 string txId = localIp.ToString () + txCounter.ToString ();
78 return new RabbitMQMessageQueueTransaction (txId);
81 public void DeleteQueue (QueueReference qRef)
83 RabbitMQMessageQueue.Delete (qRef);
86 private readonly IDictionary queues = new Hashtable ();
87 private readonly ReaderWriterLock qLock = new ReaderWriterLock ();
88 private const int TIMEOUT = 15000;
90 public IMessageQueue[] GetPublicQueues ()
93 qLock.AcquireReaderLock (TIMEOUT);
95 ICollection qCollection = queues.Values;
96 qs = new IMessageQueue[qCollection.Count];
97 qCollection.CopyTo (qs, 0);
100 qLock.ReleaseReaderLock ();
104 public bool Exists (QueueReference qRef)
106 qLock.AcquireReaderLock (TIMEOUT);
108 return queues.Contains (qRef);
110 qLock.ReleaseReaderLock ();
114 public IMessageQueue CreateMessageQueue (QueueReference qRef,
117 qLock.AcquireWriterLock (TIMEOUT);
119 IMessageQueue mq = new RabbitMQMessageQueue (this, qRef,
124 qLock.ReleaseWriterLock ();
128 public IMessageQueue GetMessageQueue (QueueReference qRef)
130 qLock.AcquireReaderLock (TIMEOUT);
132 if (queues.Contains (qRef))
133 return (IMessageQueue) queues[qRef];
135 LockCookie lc = qLock.UpgradeToWriterLock (TIMEOUT);
137 IMessageQueue mq = new RabbitMQMessageQueue (this, qRef,
142 qLock.DowngradeFromWriterLock (ref lc);
146 qLock.ReleaseReaderLock ();