Ejemplo n.º 1
0
        public void OnDhtGetReturns(IAsyncResult result)
        {
            AsyncResult              ar           = (AsyncResult)result;
            AsyncFragsOpState        agfs         = (AsyncFragsOpState)ar.AsyncState;
            BrunetDhtGetOp           op           = (BrunetDhtGetOp)ar.AsyncDelegate;
            AsyncGetFragsGlobalState global_state =
                (AsyncGetFragsGlobalState)agfs.GlobalState;
            AsyncOpState piece_state = agfs.PieceState;

            DhtGetResult[] returns = op.EndInvoke(ar);

            if (returns != null && returns.Length > 0)
            {
                // We only need the first one.
                DhtGetResult dgr = returns[0];
                Logger.WriteLineIf(LogLevel.Verbose, _log_props,
                                   string.Format("Get succeeded for piece: {0}",
                                                 Encoding.UTF8.GetString(piece_state.Key)));
                lock (global_state.SyncRoot) {
                    byte[] piece_key  = piece_state.Key;
                    int    piece_indx = BrunetDht.GetPieceIndexFromFragmentKey(piece_key);
                    global_state.Fragments[piece_indx] = dgr;
                    global_state.OpSuccCount++;

                    if ((global_state.Concurrency > 1 && global_state.OpSuccCount %
                         global_state.Concurrency == 0) || global_state.OpSuccCount ==
                        global_state.ExpectedPositiveReturnNum)
                    {
                        // put concurrently and a batch finishes.
                        global_state.BatchFinishedEvent.Set();
                    }
                    if (global_state.OpSuccCount == global_state.ExpectedPositiveReturnNum)
                    {
                        Logger.WriteLineIf(LogLevel.Verbose, _log_props,
                                           string.Format("All pieces of {0} successfully got. Firing GetStoppedEvent",
                                                         Encoding.UTF8.GetString(global_state.BaseKey)));
                        global_state.Returns.Enqueue(new GetFragsStoppedEventArgs(global_state.Fragments));
                    }
                }
            }
            else
            {
                Logger.WriteLineIf(LogLevel.Verbose, _log_props,
                                   string.Format("Get failed for piece: {0}. Firing GetStoppedEvent",
                                                 Encoding.UTF8.GetString(piece_state.Key)));
                //No retry at this level currently, stop put operation.
                lock (global_state.SyncRoot) {
                    global_state.Returns.Enqueue(new PutFragsStoppedEventArgs(piece_state));
                }
            }
        }