2008-12-21 Michael Barker <mike@middlesoft.co.uk>
authorMichael Barker <mike@middlesoft.co.uk>
Wed, 24 Dec 2008 09:55:54 +0000 (09:55 -0000)
committerMichael Barker <mike@middlesoft.co.uk>
Wed, 24 Dec 2008 09:55:54 +0000 (09:55 -0000)
* MessageQueueBase.cs:  Added optional base class that provide async
messaging support.
* CompletedEventArgs.cs:  Added for events on async methods.
* CompletedEventHandler.cs:  Added for events on async methods.
* IMessageQueue.cs:  Added method and event signatures for async messaging.
* MessagingProviderLocator.cs:  Added constant for InfiniteTimeout.
* QueueReference.cs:  Remove unecessary logging.

svn path=/trunk/mcs/; revision=122084

mcs/class/Mono.Messaging/Mono.Messaging/ChangeLog
mcs/class/Mono.Messaging/Mono.Messaging/CompletedEventArgs.cs [new file with mode: 0644]
mcs/class/Mono.Messaging/Mono.Messaging/CompletedEventHandler.cs [new file with mode: 0644]
mcs/class/Mono.Messaging/Mono.Messaging/IMessageQueue.cs
mcs/class/Mono.Messaging/Mono.Messaging/MessageQueueBase.cs [new file with mode: 0644]
mcs/class/Mono.Messaging/Mono.Messaging/MessagingProviderLocator.cs
mcs/class/Mono.Messaging/Mono.Messaging/QueueReference.cs

index 70948f474f90998852373deaba17345eb1e76508..222a74f0088f2ef1886340d06562263a1b9cac4a 100644 (file)
@@ -1,3 +1,13 @@
+2008-12-21  Michael Barker  <mike@middlesoft.co.uk>
+
+       * MessageQueueBase.cs:  Added optional base class that provide async
+       messaging support.
+       * CompletedEventArgs.cs:  Added for events on async methods.
+       * CompletedEventHandler.cs:  Added for events on async methods.
+       * IMessageQueue.cs:  Added method and event signatures for async messaging.
+       * MessagingProviderLocator.cs:  Added constant for InfiniteTimeout.
+       * QueueReference.cs:  Remove unecessary logging.
+
 2008-12-07  Michael Barker  <mike@middlesoft.co.uk>
 
        * MessageUnavailableException.cs:  Specific exception for messages not being
diff --git a/mcs/class/Mono.Messaging/Mono.Messaging/CompletedEventArgs.cs b/mcs/class/Mono.Messaging/Mono.Messaging/CompletedEventArgs.cs
new file mode 100644 (file)
index 0000000..bd8eba4
--- /dev/null
@@ -0,0 +1,50 @@
+//
+// Mono.Messaging
+//
+// Authors:
+//       Michael Barker (mike@middlesoft.co.uk)
+//
+// (C) 2008 Michael Barker
+//
+
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+
+namespace Mono.Messaging
+{
+       public class CompletedEventArgs : EventArgs \r
+       {\r
+               private IAsyncResult _result;\r
+\r
+               public CompletedEventArgs(IAsyncResult result)\r
+               {\r
+                       _result = result;\r
+               }\r
+\r
+               public IAsyncResult AsyncResult\r
+               {\r
+                       get { return _result; }\r
+                       set { _result = value; }\r
+               }\r
+       }\r
+}
diff --git a/mcs/class/Mono.Messaging/Mono.Messaging/CompletedEventHandler.cs b/mcs/class/Mono.Messaging/Mono.Messaging/CompletedEventHandler.cs
new file mode 100644 (file)
index 0000000..a6ea59d
--- /dev/null
@@ -0,0 +1,13 @@
+// CompletedEventHandler.cs created with MonoDevelop
+// User: mike at 8:16 p 21/12/2008
+//
+// To change standard headers go to Edit->Preferences->Coding->Standard Headers
+//
+
+using System;
+
+namespace Mono.Messaging
+{
+       [Serializable]
+       public delegate void CompletedEventHandler(object sender, CompletedEventArgs e);
+}
index 8146c503618217d9f5e47cbcbc893848d8eb3bdf..1781369e7c624f880473a623d044b4ee18d18db0 100644 (file)
@@ -34,7 +34,7 @@ using System.ComponentModel;
 namespace Mono.Messaging {
 
        public interface IMessageQueue {
-       
+               
                bool Authenticate {
                        get; set;
                }
@@ -158,14 +158,42 @@ namespace Mono.Messaging {
                IMessage ReceiveByCorrelationId (string correlationId, TimeSpan timeout);
                
                IMessage ReceiveByCorrelationId (string correlationId, IMessageQueueTransaction transaction);
-               
+
                IMessage ReceiveByCorrelationId (string correlationId, MessageQueueTransactionType transactionType);
                
                IMessage ReceiveByCorrelationId (string correlationId, TimeSpan timeout, IMessageQueueTransaction transaction);
                        
                IMessage ReceiveByCorrelationId (string correlationId, TimeSpan timeout, MessageQueueTransactionType transactionType);
                
+               IAsyncResult BeginPeek ();
+
+               IAsyncResult BeginPeek (TimeSpan timeout);
+
+               IAsyncResult BeginPeek (TimeSpan timeout, object stateObject);
+
+               IAsyncResult BeginPeek (TimeSpan timeout, object stateObject, AsyncCallback callback);
+               
+               IMessage EndPeek (IAsyncResult asyncResult);
+               
+               IAsyncResult BeginReceive ();
+
+               IAsyncResult BeginReceive (TimeSpan timeout);
+
+               IAsyncResult BeginReceive (TimeSpan timeout, object stateObject);
+
+               IAsyncResult BeginReceive (TimeSpan timeout, object stateObject, AsyncCallback callback);
+               
+               IMessage EndReceive (IAsyncResult asyncResult);
+               
                IMessageEnumerator GetMessageEnumerator ();
+
+               event CompletedEventHandler PeekCompleted;
+               
+               event CompletedEventHandler ReceiveCompleted;
+               
+               void SendReceiveCompleted (IAsyncResult result);
+               
+               void SendPeekCompleted (IAsyncResult result);
        }
 
 }
diff --git a/mcs/class/Mono.Messaging/Mono.Messaging/MessageQueueBase.cs b/mcs/class/Mono.Messaging/Mono.Messaging/MessageQueueBase.cs
new file mode 100644 (file)
index 0000000..e15d447
--- /dev/null
@@ -0,0 +1,263 @@
+//
+// Mono.Messaging
+//
+// Authors:
+//       Michael Barker (mike@middlesoft.co.uk)
+//
+// (C) 2008 Michael Barker
+//
+
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.Threading;
+using System.Collections;
+
+namespace Mono.Messaging
+{
+       
+       public abstract class MessageQueueBase
+       {
+               protected abstract IMessageQueue Queue {
+                       get;
+               }
+               
+               public event CompletedEventHandler PeekCompleted;
+               
+               public event CompletedEventHandler ReceiveCompleted;
+               
+               public IAsyncResult BeginPeek ()
+               {
+                       return new PeekAsyncResult (null, Queue, MessagingProviderLocator.InfiniteTimeout, 
+                                                   NullAsyncCallback);
+               }
+
+               public IAsyncResult BeginPeek (TimeSpan timeout)
+               {
+                       return new PeekAsyncResult (null, Queue, timeout, NullAsyncCallback);
+               }
+
+               public IAsyncResult BeginPeek (TimeSpan timeout, object stateObject)
+               {
+                       return new PeekAsyncResult (stateObject, Queue,
+                                                   timeout, NullAsyncCallback);
+               }
+
+               public IAsyncResult BeginPeek (TimeSpan timeout,
+                                                                         object stateObject,
+                                                                         AsyncCallback callback)
+               {
+                       return new PeekAsyncResult (stateObject, Queue, timeout, callback);
+               }
+               
+               public IMessage EndPeek (IAsyncResult asyncResult)
+               {
+                       PeekAsyncResult result = (PeekAsyncResult) asyncResult;
+                       return result.Message;                  
+               }
+               
+               public IAsyncResult BeginReceive ()
+               {
+                       return new ReceiveAsyncResult (null, Queue, MessagingProviderLocator.InfiniteTimeout, 
+                                                      NullAsyncCallback);
+               }
+
+               public IAsyncResult BeginReceive (TimeSpan timeout)
+               {
+                       return new ReceiveAsyncResult (null, Queue, timeout, NullAsyncCallback);
+               }
+
+               public IAsyncResult BeginReceive (TimeSpan timeout, object stateObject)
+               {
+                       return new ReceiveAsyncResult (stateObject, Queue, timeout, NullAsyncCallback);
+               }
+
+               public IAsyncResult BeginReceive (TimeSpan timeout,
+                                                 object stateObject,
+                                                 AsyncCallback callback)
+               {
+                       return new ReceiveAsyncResult (stateObject, Queue, timeout, callback);
+               }
+               
+               public IMessage EndReceive (IAsyncResult asyncResult)
+               {
+                       ReceiveAsyncResult result = (ReceiveAsyncResult) asyncResult;
+                       return result.Message;                  
+               }
+               
+               public void SendReceiveCompleted (IAsyncResult result)
+               {
+                       if (ReceiveCompleted == null)
+                               return;
+                       
+                       ReceiveCompleted (this, new CompletedEventArgs (result));
+               }
+               
+               public void SendPeekCompleted (IAsyncResult result)
+               {
+                       if (PeekCompleted == null)
+                               return;
+                       
+                       PeekCompleted (this, new CompletedEventArgs (result));
+               }
+               
+               internal void NullAsyncCallback (IAsyncResult result)
+               {
+               }
+               
+               internal class ThreadWaitHandle : WaitHandle {
+                       
+                       private readonly Thread t;
+                       
+                       public ThreadWaitHandle (Thread t)
+                       {
+                               this.t = t;
+                       }
+                       
+                       public override bool WaitOne ()
+                       {
+                               t.Join ();
+                               return true;
+                       }
+                       
+                       public override bool WaitOne (Int32 timeout, bool exitContext)
+                       {
+                               t.Join (timeout);
+                               return true;
+                       }
+                       
+                       public override bool WaitOne (TimeSpan timeout, bool exitContext)
+                       {
+                               t.Join (timeout);
+                               return true;
+                       }
+               }
+               
+               internal abstract class AsyncResultBase : IAsyncResult {
+                       
+                       private readonly object asyncState;
+                       protected readonly WaitHandle asyncWaitHandle;
+                       protected volatile bool isCompleted;
+                       protected readonly IMessageQueue q;
+                       private readonly Thread t;
+                       protected IMessage message;
+                       protected readonly TimeSpan timeout;
+                       protected readonly AsyncCallback callback;
+                       
+                       public AsyncResultBase (object asyncState,
+                                               IMessageQueue q,
+                                               TimeSpan timeout,
+                                               AsyncCallback callback)
+                       {
+                               this.asyncState = asyncState;
+                               this.asyncWaitHandle = new Mutex (false);
+                               this.q = q;
+                               this.timeout = timeout;
+                               this.callback = callback;
+                               this.t = new Thread(run);
+                               t.Start ();
+                               asyncWaitHandle = new ThreadWaitHandle(t);
+                       }
+                       
+                       public object AsyncState {
+                               get { return asyncState; }
+                       }
+                       
+                       public WaitHandle AsyncWaitHandle {
+                               get { return asyncWaitHandle; }
+                       }
+                       
+                       public bool CompletedSynchronously {
+                               get { return false; }
+                       }
+                       
+                       public bool IsCompleted {
+                               get { return isCompleted; }
+                       }
+                       
+                       internal IMessage Message {
+                               get { return message; }
+                       }
+                       
+                       protected abstract IMessage GetMessage ();
+                       
+                       protected abstract void SendCompletedEvent (IAsyncResult result);
+                       
+                       private void run ()
+                       {
+                               message = GetMessage ();                                        
+                               isCompleted = true;
+                               callback (this);
+                               SendCompletedEvent (this);
+                       }
+               }
+               
+               internal class ReceiveAsyncResult : AsyncResultBase {
+                       
+                       public ReceiveAsyncResult (object asyncState,
+                                                  IMessageQueue q,
+                                                  TimeSpan timeout,
+                                                  AsyncCallback callback)
+                               : base (asyncState, q, timeout, callback)
+                       {
+                       }
+                       
+                       protected override IMessage GetMessage ()
+                       {
+                               if (timeout == MessagingProviderLocator.InfiniteTimeout)
+                                       return q.Receive ();
+                               else
+                                       return q.Receive (timeout);
+                       }
+                       
+                       protected override void SendCompletedEvent (IAsyncResult result)
+                       {
+                               q.SendReceiveCompleted (result);
+                       }
+               }
+
+               internal class PeekAsyncResult : AsyncResultBase {
+                                               
+                       public PeekAsyncResult (object asyncState,
+                                               IMessageQueue q,
+                                               TimeSpan timeout,
+                                               AsyncCallback callback)
+                               : base (asyncState, q, timeout, callback)
+                       {
+                       }
+                       
+                       protected override void SendCompletedEvent (IAsyncResult result)
+                       {
+                               Console.WriteLine ("Send Peek Completed");
+                               q.SendPeekCompleted (result);
+                       }
+                       
+                       protected override IMessage GetMessage ()
+                       {
+                               if (timeout == MessagingProviderLocator.InfiniteTimeout)
+                                       return q.Peek ();
+                               else
+                                       return q.Peek (timeout);
+                       }
+               }
+       }
+}
index 2a647aa2a99e823ed574bfaa0e71473cabb57612..f267e7e19f678b0b821fe1609322b9ed8447cab3 100644 (file)
@@ -36,7 +36,8 @@ namespace Mono.Messaging
        {
                private static IMessagingProvider provider = null;
                private static readonly object syncObj = new object();
-               
+               public static readonly TimeSpan InfiniteTimeout = TimeSpan.MaxValue;
+       
                public static IMessagingProvider GetProvider ()
                {
                        //Assembly a = Assembly.Load("Mono.Messaging.RabbitMQ.dll");
index e9770b65c374f36ac54e3cc9160cf6654ef4ba9e..f85bc3a78205dd4445388fa34df0338584ae9493 100644 (file)
@@ -84,7 +84,6 @@ namespace Mono.Messaging
 
                public override bool Equals (object other)
                {
-                       Console.Write("Equals Called\n");
                        if (other == null)
                                return false;
                        else if (typeof (QueueReference) != other.GetType ())