3using System.Collections.Specialized;
4using System.Security.Cryptography.X509Certificates;
6using System.Threading.Tasks;
22 private static string defaultPurpose =
string.Empty;
24 private readonly Dictionary<string, TaskCompletionSource<ContractPetitionResponseEventArgs>> contractPetitions =
new Dictionary<string, TaskCompletionSource<ContractPetitionResponseEventArgs>>();
25 private readonly
object syncObj =
new object();
26 private bool handlerAdded =
false;
40 get => defaultPurpose;
43 if (
string.IsNullOrEmpty(value))
44 throw new ArgumentException(
"Default purpose cannot be empty.", nameof(
DefaultPurpose));
46 defaultPurpose = value;
63 if (
string.Compare(Uri.Scheme,
"iotsc",
true) == 0)
82 if (this.handlerAdded)
84 Gateway.ContractsClient.PetitionedContractResponseReceived -= this.Client_PetitionedContractResponseReceived;
85 this.handlerAdded =
false;
98 public Task<ContentResponse>
GetAsync(Uri Uri, X509Certificate Certificate,
99 EventHandler<RemoteCertificateEventArgs> RemoteCertificateValidator, params KeyValuePair<string, string>[] Headers)
112 public async Task<ContentResponse>
GetAsync(Uri Uri, X509Certificate Certificate,
113 EventHandler<RemoteCertificateEventArgs> RemoteCertificateValidator,
int TimeoutMs, params KeyValuePair<string, string>[] Headers)
117 return new ContentResponse(
new NotSupportedException(
"Contracts feature not supported or activated on this system."));
121 if (!this.handlerAdded)
123 Client.PetitionedContractResponseReceived += this.Client_PetitionedContractResponseReceived;
124 this.handlerAdded =
true;
128 string ContractId = Uri.AbsolutePath;
137 TaskCompletionSource<ContractPetitionResponseEventArgs> Result =
new TaskCompletionSource<ContractPetitionResponseEventArgs>();
138 string PetitionId = Guid.NewGuid().ToString();
139 string Purpose =
null;
141 if (!
string.IsNullOrEmpty(Uri.Query))
143 NameValueCollection Parameters = HttpUtility.ParseQueryString(Uri.Query);
145 foreach (
string Parameter in Parameters.AllKeys)
157 if (
string.IsNullOrEmpty(Purpose))
158 Purpose =
"Processing referenced contract.";
162 this.contractPetitions[PetitionId] = Result;
169 Task
_ = Task.Delay(TimeoutMs).ContinueWith((T) =>
171 Result.TrySetException(
new TimeoutException(
"Response to petition not received within allotted time."));
172 return Task.CompletedTask;
179 this.contractPetitions.Remove(PetitionId);
195 if (this.contractPetitions.TryGetValue(e.
PetitionId, out TaskCompletionSource<ContractPetitionResponseEventArgs> Result))
196 Result.TrySetResult(e);
199 return Task.CompletedTask;
211 EventHandler<RemoteCertificateEventArgs> RemoteCertificateValidator, params KeyValuePair<string, string>[] Headers)
226 EventHandler<RemoteCertificateEventArgs> RemoteCertificateValidator,
TemporaryStream Destination, params KeyValuePair<string, string>[] Headers)
241 EventHandler<RemoteCertificateEventArgs> RemoteCertificateValidator,
int TimeoutMs, params KeyValuePair<string, string>[] Headers)
243 return this.
GetTempStreamAsync(Uri, Certificate, RemoteCertificateValidator, TimeoutMs,
null, Headers);
257 EventHandler<RemoteCertificateEventArgs> RemoteCertificateValidator,
int TimeoutMs,
TemporaryStream Destination,
258 params KeyValuePair<string, string>[] Headers)
260 ContentResponse Content = await this.
GetAsync(Uri, Certificate, RemoteCertificateValidator, TimeoutMs, Headers);
265 StringBuilder Xml =
new StringBuilder();
267 byte[] Bin = Encoding.UTF8.GetBytes(Xml.ToString());
268 bool DestinationCreated =
false;
270 if (Destination is
null)
273 DestinationCreated =
true;
278 await Destination.
WriteAsync(Bin, 0, Bin.Length);
282 if (DestinationCreated)
Contains information about a response to a content request.
bool HasError
If an error occurred.
object Decoded
Decoded object.
Exception Error
Error response.
Contains information about a stream response to a content request.
Static class managing encoding and decoding of internet content.
static int DefaultTimeout
Default timeout of internet access methods, in milliseconds.
Static class managing the runtime environment of the IoT Gateway.
static ContractsClient ContractsClient
XMPP Contracts Client, if such a compoent is available on the XMPP broker.
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...
Task< ContentResponse > GetAsync(Uri Uri, X509Certificate Certificate, EventHandler< RemoteCertificateEventArgs > RemoteCertificateValidator, params KeyValuePair< string, string >[] Headers)
Gets a resource, using a Uniform Resource Identifier (or Locator).
ContractGetter()
Gets smart contracts.
bool CanGet(Uri Uri, out Grade Grade)
If the getter is able to get a resource, given its URI.
Task< ContentStreamResponse > GetTempStreamAsync(Uri Uri, X509Certificate Certificate, EventHandler< RemoteCertificateEventArgs > RemoteCertificateValidator, int TimeoutMs, params KeyValuePair< string, string >[] Headers)
Gets a (possibly big) resource, using a Uniform Resource Identifier (or Locator).
async Task< ContentStreamResponse > GetTempStreamAsync(Uri Uri, X509Certificate Certificate, EventHandler< RemoteCertificateEventArgs > RemoteCertificateValidator, int TimeoutMs, TemporaryStream Destination, params KeyValuePair< string, string >[] Headers)
Gets a (possibly big) resource, using a Uniform Resource Identifier (or Locator).
void Dispose()
IDisposable.Dispose
async Task< ContentResponse > GetAsync(Uri Uri, X509Certificate Certificate, EventHandler< RemoteCertificateEventArgs > RemoteCertificateValidator, int TimeoutMs, params KeyValuePair< string, string >[] Headers)
Gets a resource, using a Uniform Resource Identifier (or Locator).
Task< ContentStreamResponse > GetTempStreamAsync(Uri Uri, X509Certificate Certificate, EventHandler< RemoteCertificateEventArgs > RemoteCertificateValidator, params KeyValuePair< string, string >[] Headers)
Gets a (possibly big) resource, using a Uniform Resource Identifier (or Locator).
string[] UriSchemes
Supported URI schemes.
Task< ContentStreamResponse > GetTempStreamAsync(Uri Uri, X509Certificate Certificate, EventHandler< RemoteCertificateEventArgs > RemoteCertificateValidator, TemporaryStream Destination, params KeyValuePair< string, string >[] Headers)
Gets a (possibly big) resource, using a Uniform Resource Identifier (or Locator).
static string DefaultPurpose
Default purpose string.
Basic interface for Internet Content getters. A class implementing this interface and having a defaul...