2using System.Collections.Generic;
3using System.Collections.Specialized;
4using System.Runtime.ExceptionServices;
5using System.Security.Cryptography.X509Certificates;
7using System.Threading.Tasks;
23 private static string defaultPurpose =
string.Empty;
24 private static int defaultTimeoutMilliseconds = 60000;
26 private readonly Dictionary<string, TaskCompletionSource<ContractPetitionResponseEventArgs>> contractPetitions =
new Dictionary<string, TaskCompletionSource<ContractPetitionResponseEventArgs>>();
27 private readonly
object syncObj =
new object();
28 private bool handlerAdded =
false;
42 get => defaultPurpose;
45 if (
string.IsNullOrEmpty(value))
46 throw new ArgumentException(
"Default purpose cannot be empty.", nameof(
DefaultPurpose));
48 defaultPurpose = value;
57 get => defaultTimeoutMilliseconds;
63 defaultTimeoutMilliseconds = value;
80 if (
string.Compare(Uri.Scheme,
"iotsc",
true) == 0)
99 if (this.handlerAdded)
101 Gateway.ContractsClient.PetitionedContractResponseReceived -= this.Client_PetitionedContractResponseReceived;
102 this.handlerAdded =
false;
115 public Task<object>
GetAsync(Uri Uri, X509Certificate Certificate,
118 return this.
GetAsync(Uri, Certificate, RemoteCertificateValidator, defaultTimeoutMilliseconds, Headers);
129 public async Task<object>
GetAsync(Uri Uri, X509Certificate Certificate,
133 ??
throw new NotSupportedException(
"Contracts feature not supported or activated on this system.");
137 if (!this.handlerAdded)
139 Client.PetitionedContractResponseReceived += this.Client_PetitionedContractResponseReceived;
140 this.handlerAdded =
true;
144 string ContractId = Uri.AbsolutePath;
152 TaskCompletionSource<ContractPetitionResponseEventArgs> Result =
new TaskCompletionSource<ContractPetitionResponseEventArgs>();
153 string PetitionId = Guid.NewGuid().ToString();
154 string Purpose =
null;
156 if (!
string.IsNullOrEmpty(Uri.Query))
158 NameValueCollection Parameters = HttpUtility.ParseQueryString(Uri.Query);
160 foreach (
string Parameter in Parameters.AllKeys)
172 if (
string.IsNullOrEmpty(Purpose))
173 Purpose =
"Processing referenced contract.";
177 this.contractPetitions[PetitionId] = Result;
184 Task _ = Task.Delay(TimeoutMs).ContinueWith((T) =>
186 Result.TrySetException(
new TimeoutException(
"Response to petition not received within allotted time."));
187 return Task.CompletedTask;
194 this.contractPetitions.Remove(PetitionId);
197 ExceptionDispatchInfo.Capture(ex).Throw();
210 if (this.contractPetitions.TryGetValue(e.
PetitionId, out TaskCompletionSource<ContractPetitionResponseEventArgs> Result))
211 Result.TrySetResult(e);
214 return Task.CompletedTask;
225 public Task<KeyValuePair<string, TemporaryStream>>
GetTempStreamAsync(Uri Uri, X509Certificate Certificate,
228 return this.
GetTempStreamAsync(Uri, Certificate, RemoteCertificateValidator, defaultTimeoutMilliseconds, Headers);
240 public async Task<KeyValuePair<string, TemporaryStream>>
GetTempStreamAsync(Uri Uri, X509Certificate Certificate,
244 StringBuilder Xml =
new StringBuilder();
246 byte[] Bin = Encoding.UTF8.GetBytes(Xml.ToString());
256 ExceptionDispatchInfo.Capture(ex).Throw();
260 return new KeyValuePair<string, TemporaryStream>(
"application/xml; charset=utf-8", Result);
Contains the definition of a contract
void Serialize(StringBuilder Xml, bool IncludeNamespace, bool IncludeIdAttribute, bool IncludeClientSignatures, bool IncludeAttachments, bool IncludeStatus, bool IncludeServerSignature, bool IncludeAttachmentReferences)
Serializes the Contract, in normalized form.
Adds support for legal identities, smart contracts and signatures to an XMPP client.
Task PetitionContractAsync(string ContractId, string PetitionId, string Purpose)
Sends a petition to the parts of a smart contract, to access the information in the contract....
Task< Contract > GetContractAsync(string ContractId)
Gets a contract
Event arguments for smart contract petition responses
string PetitionId
Petition ID
Contract RequestedContract
Requested contract, if accepted, null if rejected.
Abstract base class for contractual parameters
Manages a temporary stream. Contents is kept in-memory, if below a memory threshold,...
override void Dispose(bool disposing)
Releases the unmanaged resources used by the System.IO.Stream and optionally releases the managed res...
override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
Asynchronously writes a sequence of bytes to the current stream, advances the current position within...
ContractGetter()
Gets smart contracts.
async Task< KeyValuePair< string, TemporaryStream > > GetTempStreamAsync(Uri Uri, X509Certificate Certificate, RemoteCertificateEventHandler RemoteCertificateValidator, int TimeoutMs, params KeyValuePair< string, string >[] Headers)
Gets a (possibly big) resource, using a Uniform Resource Identifier (or Locator).
bool CanGet(Uri Uri, out Grade Grade)
If the getter is able to get a resource, given its URI.
Task< KeyValuePair< string, TemporaryStream > > GetTempStreamAsync(Uri Uri, X509Certificate Certificate, RemoteCertificateEventHandler RemoteCertificateValidator, params KeyValuePair< string, string >[] Headers)
Gets a (possibly big) resource, using a Uniform Resource Identifier (or Locator).
void Dispose()
IDisposable.Dispose
Task< object > GetAsync(Uri Uri, X509Certificate Certificate, RemoteCertificateEventHandler RemoteCertificateValidator, params KeyValuePair< string, string >[] Headers)
Gets a resource, using a Uniform Resource Identifier (or Locator).
async Task< object > GetAsync(Uri Uri, X509Certificate Certificate, RemoteCertificateEventHandler RemoteCertificateValidator, int TimeoutMs, params KeyValuePair< string, string >[] Headers)
Gets a resource, using a Uniform Resource Identifier (or Locator).
static int DefaultTimeoutMilliseconds
Default petition timeout, in milliseconds.
string[] UriSchemes
Supported URI schemes.
static string DefaultPurpose
Default purpose string.
Basic interface for Internet Content getters. A class implementing this interface and having a defaul...
delegate void RemoteCertificateEventHandler(object Sender, RemoteCertificateEventArgs e)
Delegate for remote certificate event handlers.