Ignore:
Timestamp:
Jun 17, 2010, 5:11:02 PM (11 years ago)
Author:
Paul Lelgemann
Message:

+ CrypP2P: Return types of synchronous methods Store/Retrieve/Remove changed
o Work on the distributed KeySearcher

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/CrypP2P/Internal/P2PBase.cs

    r1645 r1665  
    351351        /// </summary>
    352352        /// <param name = "key">Key of DHT Entry</param>
     353        /// <param name = "value">Value of DHT Entry</param>
     354        /// <returns>True, when storing is completed!</returns>
     355        public RequestResult SynchStore(string key, string value)
     356        {
     357            return SynchStore(key, Encoding.UTF8.GetBytes(value));
     358        }
     359
     360        /// <summary>
     361        ///   Stores a value in the DHT at the given key
     362        /// </summary>
     363        /// <param name = "key">Key of DHT Entry</param>
    353364        /// <param name = "data">Value of DHT Entry</param>
    354365        /// <returns>True, when storing is completed!</returns>
    355         public bool SynchStore(string key, byte[] data)
     366        public RequestResult SynchStore(string key, byte[] data)
    356367        {
    357368            var autoResetEvent = new AutoResetEvent(false);
     
    360371            LogToMonitor("Begin: SynchStore. Key: " + key + ", " + data.Length + " bytes");
    361372
    362             var responseWait = new ResponseWait {WaitHandle = autoResetEvent, key = key, value = data};
    363             VersionedDht.Store(OnSynchStoreCompleted, key, data, responseWait);
     373            var requestResult = new RequestResult {WaitHandle = autoResetEvent, Key = key, Data = data};
     374            VersionedDht.Store(OnSynchStoreCompleted, key, data, requestResult);
    364375
    365376            // blocking till response
    366377            autoResetEvent.WaitOne();
    367378
    368             LogToMonitor("End: SynchStore. Key: " + key + ". Success: " + responseWait.success);
    369 
    370             return responseWait.success;
    371         }
    372 
    373         /// <summary>
    374         ///   Stores a value in the DHT at the given key
    375         /// </summary>
    376         /// <param name = "key">Key of DHT Entry</param>
    377         /// <param name = "value">Value of DHT Entry</param>
    378         /// <returns>True, when storing is completed!</returns>
    379         public bool SynchStore(string key, string value)
    380         {
    381             return SynchStore(key, Encoding.UTF8.GetBytes(value));
     379            LogToMonitor("End: SynchStore. Key: " + key + ". Status: " + requestResult.Status);
     380
     381            return requestResult;
    382382        }
    383383
     
    388388        private static void OnSynchStoreCompleted(StoreResult storeResult)
    389389        {
    390             var responseWait = storeResult.AsyncState as ResponseWait;
    391             if (responseWait == null)
    392             {
    393                 LogToMonitor("Received OnSynchStoreCompleted, but ResponseWait object is missing. Discarding.");
     390            var requestResult = storeResult.AsyncState as RequestResult;
     391            if (requestResult == null)
     392            {
     393                LogToMonitor("Received OnSynchStoreCompleted, but RequestResult object is missing. Discarding.");
    394394                return;
    395395            }
    396396
    397             responseWait.success = storeResult.Status == OperationStatus.Success;
    398             responseWait.operationStatus = storeResult.Status;
    399             responseWait.Message = Encoding.UTF8.GetBytes(storeResult.Status.ToString());
     397            requestResult.Parse(storeResult);
    400398
    401399            // unblock WaitHandle in the synchronous method
    402             responseWait.WaitHandle.Set();
     400            requestResult.WaitHandle.Set();
    403401        }
    404402
     
    408406        /// <param name = "key">Key of DHT Entry</param>
    409407        /// <returns>Value of DHT Entry</returns>
    410         public byte[] SynchRetrieve(string key)
     408        public RequestResult SynchRetrieve(string key)
    411409        {
    412410            LogToMonitor("Begin: SynchRetrieve. Key: " + key);
    413411
    414412            var autoResetEvent = new AutoResetEvent(false);
    415             var responseWait = new ResponseWait {WaitHandle = autoResetEvent, key = key};
    416             Dht.Retrieve(OnSynchRetrieveCompleted, key, responseWait);
     413            var requestResult = new RequestResult() {WaitHandle = autoResetEvent, Key = key};
     414            Dht.Retrieve(OnSynchRetrieveCompleted, key, requestResult);
    417415
    418416            // blocking till response
    419417            autoResetEvent.WaitOne();
    420418
    421             LogToMonitor("End: SynchRetrieve. Key: " + key + ". Success: " + responseWait.success);
    422 
    423             return responseWait.Message;
     419            LogToMonitor("End: SynchRetrieve. Key: " + key + ". Status: " + requestResult.Status);
     420
     421            return requestResult;
    424422        }
    425423
     
    430428        private static void OnSynchRetrieveCompleted(RetrieveResult retrieveResult)
    431429        {
    432             var responseWait = retrieveResult.AsyncState as ResponseWait;
    433             if (responseWait == null)
    434             {
    435                 LogToMonitor("Received OnSynchRetrieveCompleted, but ResponseWait object is missing. Discarding.");
     430            var requestResult = retrieveResult.AsyncState as RequestResult;
     431            if (requestResult == null)
     432            {
     433                LogToMonitor("Received OnSynchRetrieveCompleted, but RequestResult object is missing. Discarding.");
    436434                return;
    437435            }
    438436
    439             switch (retrieveResult.Status)
    440             {
    441                 case OperationStatus.Success:
    442                     responseWait.success = true;
    443                     responseWait.Message = retrieveResult.Data;
    444                     break;
    445                 case OperationStatus.KeyNotFound:
    446                     responseWait.success = true;
    447                     responseWait.Message = null;
    448                     break;
    449                 default:
    450                     responseWait.success = false;
    451                     responseWait.Message = null;
    452                     break;
    453             }
    454 
    455             responseWait.operationStatus = retrieveResult.Status;
     437            requestResult.Parse(retrieveResult);
    456438
    457439            // unblock WaitHandle in the synchronous method
    458             responseWait.WaitHandle.Set();
     440            requestResult.WaitHandle.Set();
    459441        }
    460442
     
    464446        /// <param name = "key">Key of the DHT Entry</param>
    465447        /// <returns>True, when removing is completed!</returns>
    466         public bool SynchRemove(string key)
     448        public RequestResult SynchRemove(string key)
    467449        {
    468450            LogToMonitor("Begin SynchRemove. Key: " + key);
    469451
    470452            var autoResetEvent = new AutoResetEvent(false);
    471             var responseWait = new ResponseWait {WaitHandle = autoResetEvent, key = key};
    472             VersionedDht.Remove(OnSynchRemoveCompleted, key, responseWait);
     453            var requestResult = new RequestResult { WaitHandle = autoResetEvent, Key = key };
     454            VersionedDht.Remove(OnSynchRemoveCompleted, key, requestResult);
    473455
    474456            // blocking till response
    475457            autoResetEvent.WaitOne();
    476458
    477             LogToMonitor("End: SynchRemove. Key: " + key + ". Success: " + responseWait.success);
    478 
    479             return responseWait.success;
     459            LogToMonitor("End: SynchRemove. Key: " + key + ". Status: " + requestResult.Status);
     460
     461            return requestResult;
    480462        }
    481463
     
    486468        private static void OnSynchRemoveCompleted(RemoveResult removeResult)
    487469        {
    488             var responseWait = removeResult.AsyncState as ResponseWait;
    489             if (responseWait == null)
    490             {
    491                 LogToMonitor("Received OnSynchRemoveCompleted, but ResponseWait object is missing. Discarding.");
     470            var requestResult = removeResult.AsyncState as RequestResult;
     471            if (requestResult == null)
     472            {
     473                LogToMonitor("Received OnSynchRemoveCompleted, but RequestResult object is missing. Discarding.");
    492474                return;
    493475            }
    494476
    495             responseWait.success = removeResult.Status != OperationStatus.Failure &&
    496                                    removeResult.Status != OperationStatus.VersionMismatch;
    497             responseWait.operationStatus = removeResult.Status;
    498             responseWait.Message = Encoding.UTF8.GetBytes(removeResult.Status.ToString());
     477            requestResult.Parse(removeResult);
    499478
    500479            // unblock WaitHandle in the synchronous method
    501             responseWait.WaitHandle.Set();
     480            requestResult.WaitHandle.Set();
    502481        }
    503482
Note: See TracChangeset for help on using the changeset viewer.