Example #1
0
        private IAsyncResult RemoteAsyncCallTakeSelect(TupleSpace space, ISchema schema, string id, int seq)
        {
            AsyncTakeSelectDelegate RemoteDel = new AsyncTakeSelectDelegate(space.TakeSelect);
            IAsyncResult            RemAr     = RemoteDel.BeginInvoke(schema, id, seq, null, null);

            return(RemAr);
        }
Example #2
0
        public ITuple Take(ISchema schema, string id)
        {
            bool             allAck = false;
            ITuple           result;
            HashSet <ITuple> intersection = null;
            Dictionary <string, HashSet <ITuple> > tupleSets = null;
            Dictionary <string, TupleSpace>        view;

            view = GetViews();
            Seq++;
            Console.WriteLine("TakeSelect");
            while (true)
            {
                try
                {
                    //List<Pingable> view = viewManager.RequestView().pingables;

                    Dictionary <string, KeyValuePair <IAsyncResult, WaitHandle> > results = new Dictionary <string, KeyValuePair <IAsyncResult, WaitHandle> > ();
                    List <WaitHandle> waits = new List <WaitHandle>();
                    foreach (KeyValuePair <string, TupleSpace> entry in view)
                    {
                        //IAsyncResult ar = RemoteAsyncCallTakeSelect(space, schema, id, seq);
                        IAsyncResult ar = RemoteAsyncCallTakeSelect(entry.Value, schema, id, Seq);
                        results.Add(entry.Key, new KeyValuePair <IAsyncResult, WaitHandle>(ar, ar.AsyncWaitHandle));
                        waits.Add(ar.AsyncWaitHandle);
                    }

                    //WaitHandle.WaitAll(waits.ToArray(), 2000);
                    WaitHandle.WaitAll(waits.ToArray());
                    tupleSets = new Dictionary <string, HashSet <ITuple> >();
                    foreach (var entry in results)
                    {
                        if (entry.Value.Value.WaitOne(0))
                        {
                            AsyncTakeSelectDelegate del = (AsyncTakeSelectDelegate)((AsyncResult)entry.Value.Key).AsyncDelegate;
                            tupleSets.Add(entry.Key, del.EndInvoke(entry.Value.Key));
                        }
                    }
                    if (!tupleSets.Values.Any(item => item == null))
                    {
                        intersection = IntersectAll(tupleSets.Values.ToList());
                        if (intersection.Count() > 0)
                        {
                            break;
                        }
                    }

                    foreach (var entry in tupleSets)
                    {
                        if (entry.Value != null)
                        {
                            RemoteAsyncCallUnlock((TupleSpace)TupleSpaces[entry.Key], entry.Value, id, Seq++);
                        }
                    }
                } catch (Exception e) {
                    continue;
                }
                Thread.Sleep(100);
            }
            result = intersection.First();
            Seq++;
            while (!allAck)
            {
                try
                {
                    //Dictionary<string, TupleSpace> view = GetViews();
                    List <IAsyncResult> results = new List <IAsyncResult>();
                    List <WaitHandle>   waits   = new List <WaitHandle>();
                    foreach (var entry in view)
                    {
                        IAsyncResult ar = RemoteAsyncCallTakeRemove(entry.Value, result, tupleSets[entry.Key], id, Seq);
                        results.Add(ar);
                        waits.Add(ar.AsyncWaitHandle);
                    }
                    allAck = WaitHandle.WaitAll(waits.ToArray(), 2000);
                } catch (Exception e)
                {
                    Console.WriteLine(e);
                    continue;
                }
                Thread.Sleep(100);
                view = GetViews();
            }
            return(result);
        }