source: trunk/CrypP2P/Internal/RequestResult.cs @ 2438

Last change on this file since 2438 was 1919, checked in by Paul Lelgemann, 11 years ago

o Updated peers@play libraries to revision 5611: Updated PapsClient supports OverlayMessages needed for Christian Arnolds KeySearcher
o Improved handling of SystemLeft events, allowing to reconnect with P2PEditor using Paps

File size: 3.8 KB
Line 
1using System;
2using System.Threading;
3using PeersAtPlay.P2PStorage.DHT;
4
5namespace Cryptool.P2P.Internal
6{
7    public class RequestResult
8    {
9        /// <summary>
10        /// Key used for querying the DHT
11        /// </summary>
12        public string Key;
13
14        /// <summary>
15        /// Type of the request
16        /// </summary>
17        public RequestType RequestType;
18
19        /// <summary>
20        /// Result of the request as reported by Peers@Play
21        ///
22        /// The result is an enum. If new result types are added by Peers@Play,
23        /// this field will be set to Unknown.
24        /// </summary>
25        public RequestResultType Status;
26
27        /// <summary>
28        /// Data used in the request.
29        ///
30        /// For Store requests, this is the data that was attempted to store.
31        /// For Retrieve requests, this is the data that was received.
32        /// For Remove results, this is always <code>null</code>.
33        /// </summary>
34        public byte[] Data;
35
36        /// <summary>
37        /// Unique identifier for each request. Is assigned by Peers@Play.
38        /// </summary>
39        public Guid Guid;
40
41        /// <summary>
42        /// Indicates if the request was successful. The value depends on the request type.
43        ///
44        /// For Store, this is only true if Status equals Success.
45        /// For Retrieve, this is only if Status equals Success or KeyNotFound.
46        /// For Remove, this is only true if Status does not equal Failure or VersionMismatch.
47        /// </summary>
48        /// <returns>bool indicating the success of the request</returns>
49        public bool IsSuccessful()
50        {
51            switch (RequestType)
52            {
53                case RequestType.Store:
54                    return Status == RequestResultType.Success;
55                case RequestType.Retrieve:
56                    return Status == RequestResultType.Success || Status == RequestResultType.KeyNotFound;
57                case RequestType.Remove:
58                    return Status != RequestResultType.Failure && Status != RequestResultType.VersionMismatch;
59                default:
60                    return false;
61            }
62        }
63
64        internal AutoResetEvent WaitHandle;
65
66        internal void Parse(RemoveResult removeResult)
67        {
68            RequestType = RequestType.Remove;
69            Guid = removeResult.Guid;
70            ParseResultStatus(removeResult.Status);
71        }
72
73        internal void Parse(RetrieveResult retrieveResult)
74        {
75            RequestType = RequestType.Retrieve;
76            Guid = retrieveResult.Guid;
77            ParseResultStatus(retrieveResult.Status);
78
79            if (Status == RequestResultType.Success && retrieveResult.Data != null)
80                Data = retrieveResult.Data;
81        }
82
83        internal void Parse(StoreResult storeResult)
84        {
85            RequestType = RequestType.Store;
86            Guid = storeResult.Guid;
87            ParseResultStatus(storeResult.Status);
88        }
89
90        private void ParseResultStatus(OperationStatus status)
91        {
92            switch (status)
93            {
94                case OperationStatus.Failure:
95                    Status = RequestResultType.Failure;
96                    return;
97                case OperationStatus.KeyNotFound:
98                    Status = RequestResultType.KeyNotFound;
99                    return;
100                case OperationStatus.Success:
101                    Status = RequestResultType.Success;
102                    return;
103                case OperationStatus.VersionMismatch:
104                    Status = RequestResultType.VersionMismatch;
105                    return;
106                default:
107                    Status = RequestResultType.Unknown;
108                    return;
109            }
110        }
111    }
112}
Note: See TracBrowser for help on using the repository browser.