Example #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));
                }
            }
        }
Example #2
0
        public void TestPutAndGetFragments()
        {
            MockBrunetDht     mock_dht  = new MockBrunetDht();
            BrunetDht         dht       = new BrunetDht(mock_dht);
            FragmentationInfo frag_info = new FragmentationInfo();

            byte[] b_key = MakeByteArray(20);
            string key   = Encoding.UTF8.GetString(b_key);

            frag_info.BaseKey = Encoding.UTF8.GetString(b_key);
            Logger.WriteLineIf(LogLevel.Verbose, _log_props,
                               string.Format("key : {0}", Base32.Encode(b_key)));
            byte[]         expected = MakeByteArray(1024 * 10);
            int            ttl      = 1000;
            BrunetDhtEntry bde      = new BrunetDhtEntry(key, expected, ttl);

            dht.PutFragments(bde, frag_info);
            Hashtable ht = mock_dht.HTStorage;

            Assert.AreEqual(11, ht.Count);
            ////
            byte[]            serialized_fragInfo = ((DhtGetResult[])dht.Get(key))[0].value;
            FragmentationInfo frag_info_actual    =
                DictionaryData.CreateDictionaryData(serialized_fragInfo) as FragmentationInfo;

            Logger.WriteLineIf(LogLevel.Verbose, _log_props,
                               string.Format("BaseKey: {0}", Base32.Encode(Encoding.UTF8.GetBytes(frag_info_actual.BaseKey))));
            BrunetDhtEntry actual = dht.GetFragments(frag_info) as BrunetDhtEntry;
            MemBlock       mb     = MemBlock.Reference(actual.Value);
            MemBlock       mb_exp = MemBlock.Reference(expected);

            Logger.WriteLineIf(LogLevel.Verbose, _log_props,
                               mb.ToBase32String().Substring(0, 50));
            Logger.WriteLineIf(LogLevel.Verbose, _log_props,
                               mb_exp.ToBase32String().Substring(0, 50));
            Assert.AreEqual(mb_exp, mb);
        }
Example #3
0
 public void TestPutAndGetFragments()
 {
     MockBrunetDht mock_dht = new MockBrunetDht();
       BrunetDht dht = new BrunetDht(mock_dht);
       FragmentationInfo frag_info = new FragmentationInfo();
       byte[] b_key = MakeByteArray(20);
       string key = Encoding.UTF8.GetString(b_key);
       frag_info.BaseKey = Encoding.UTF8.GetString(b_key);
       Logger.WriteLineIf(LogLevel.Verbose, _log_props,
       string.Format("key : {0}", Base32.Encode(b_key)));
       byte[] expected = MakeByteArray(1024 * 10);
       int ttl = 1000;
       BrunetDhtEntry bde = new BrunetDhtEntry(key, expected, ttl);
       dht.PutFragments(bde, frag_info);
       Hashtable ht = mock_dht.HTStorage;
       Assert.AreEqual(11, ht.Count);
       ////
       byte[] serialized_fragInfo = ((DhtGetResult[])dht.Get(key))[0].value;
       FragmentationInfo frag_info_actual =
     DictionaryData.CreateDictionaryData(serialized_fragInfo) as FragmentationInfo;
       Logger.WriteLineIf(LogLevel.Verbose, _log_props,
       string.Format("BaseKey: {0}", Base32.Encode(Encoding.UTF8.GetBytes(frag_info_actual.BaseKey))));
       BrunetDhtEntry actual = dht.GetFragments(frag_info) as BrunetDhtEntry;
       MemBlock mb = MemBlock.Reference(actual.Value);
       MemBlock mb_exp = MemBlock.Reference(expected);
       Logger.WriteLineIf(LogLevel.Verbose, _log_props,
       mb.ToBase32String().Substring(0, 50));
       Logger.WriteLineIf(LogLevel.Verbose, _log_props,
       mb_exp.ToBase32String().Substring(0, 50));
       Assert.AreEqual(mb_exp, mb);
 }