Advertisement
Guest User

Untitled

a guest
Mar 27th, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.40 KB | None | 0 0
  1.     class FuturePool
  2.     {
  3.         private class Item
  4.         {
  5.             public Type            request_type;  // response type
  6.             public Type            response_type; // request type
  7.             public System.Delegate operation;     // signature
  8.             public object          deferred;      // deferred
  9.             public object          request;       // request
  10.         }
  11.  
  12.         private Queue<Item> queue;
  13.         private int         concurrency;
  14.         private int         running;
  15.  
  16.         public FuturePool(int concurrency)
  17.         {
  18.             this.queue       = new System.Collections.Generic.Queue<Item>();
  19.             this.concurrency = concurrency;
  20.             this.running     = 0;
  21.         }
  22.  
  23.         public Future<TResponse> Queue<TRequest, TResponse>(Func<TRequest, Future<TResponse>> operation, TRequest request)
  24.         {
  25.             var deferred = new Deferred<TResponse>();
  26.  
  27.             //---------------------------
  28.             // queue up the work
  29.             //---------------------------
  30.             this.queue.Enqueue(new Item {
  31.                 request_type  = typeof(TRequest),
  32.                 response_type = typeof(TResponse),
  33.                 deferred      = deferred,
  34.                 request       = request,
  35.                 operation     = operation
  36.             });
  37.  
  38.             this.Process();
  39.             return deferred.Future;
  40.         }
  41.  
  42.         private void Process()
  43.         {
  44.             if ((this.queue.Count > 0) && (this.running < this.concurrency)) {
  45.  
  46.                 //-------------------------------
  47.                 // increment
  48.                 //-------------------------------
  49.                 this.running += 1;
  50.  
  51.                 //-------------------------------
  52.                 // dequeue and inspect
  53.                 //-------------------------------
  54.                 var item             = this.queue.Dequeue();
  55.                 var future           = item.operation.DynamicInvoke(new object[] { item.request });
  56.                 var future_type      = future.GetType();
  57.                 var deffered         = item.deferred;
  58.                 var deffered_type    = deffered.GetType();
  59.            
  60.                 //-------------------------------
  61.                 // handle the resolve
  62.                 //-------------------------------
  63.                 var d_then = DynamicAction.Create(new Type[] { item.response_type }, (results) => {
  64.                     deffered_type.GetMethod("Resolve").Invoke(deffered, results);
  65.                     this.running -= 1;
  66.                     if (this.queue.Count > 0) this.Process();
  67.                 });
  68.  
  69.                 //-------------------------------
  70.                 // handle the reject
  71.                 //-------------------------------
  72.                 var d_error = DynamicAction.Create(new Type[] { typeof(System.Exception) }, (results) => {
  73.                     deffered_type.GetMethod("Reject").Invoke(deffered, results);
  74.                     this.running -= 1;
  75.                     if (this.queue.Count > 0) this.Process();          
  76.                 });
  77.  
  78.                 //-------------------------------
  79.                 // execute
  80.                 //-------------------------------
  81.                 future_type.GetMethod("Then").Invoke  (future, new object[] { d_then });
  82.                 future_type.GetMethod("Error").Invoke (future, new object[] { d_error });
  83.             }
  84.         }
  85.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement