Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RelayToSecondary.cs
1using System.Text;
2using System.Threading.Tasks;
8
10{
15 {
16 private readonly CaseInsensitiveString domain;
17 private CaseInsensitiveString componentAddress;
18
24 : base(Uri)
25 {
26 this.domain = Domain;
27 }
28
32 public CaseInsensitiveString Domain => this.domain;
33
38 protected override async Task<bool> DoPrepare()
39 {
40 if (Gateway.HasDomain &&
41 !await this.Uri.EDaler.Legal.InSameTreeOfTrust(this.Uri.SecondaryDomain.Value))
42 {
43 this.Uri.State.Error(Uris.States.EDalerUriErrorType.Forbidden,
44 "Secondary domain (" + this.Uri.SecondaryDomain.Value +
45 ") not in same tree of trust.", false);
46 return false;
47 }
48
49 this.componentAddress = await this.Uri.EDaler.Server.FindComponentAsync(this.domain, EDalerComponent.NamespaceEDaler);
50
51 StringBuilder Xml = new StringBuilder();
52 TaskCompletionSource<bool> Result = new TaskCompletionSource<bool>();
53
54 Xml.Append("<uri xmlns=\"");
56
57 if (this.Uri.ToType == EntityType.NetworkJid)
58 {
59 Xml.Append("\" for=\"");
60 Xml.Append(XML.Encode(this.Uri.To.Address));
61 }
62 else if (!this.Uri.State.Relayed)
63 {
64 Xml.Append("\" for=\"");
65 Xml.Append(XML.Encode(this.Uri.State.Sender));
66 }
67
68 Xml.Append("\">");
69 Xml.Append(XML.Encode(this.Uri.UriString));
70 Xml.Append("</uri>");
71
72 if (!await this.Uri.EDaler.Server.SendIqRequest("set",
74 new XmppAddress(this.componentAddress),
75 string.Empty, Xml.ToString(), false, (sender2, e2) =>
76 {
77 if (!e2.Ok)
78 this.Uri.State.Error(e2.ErrorType.ToString().ToLower(), string.Empty, this.Uri.PrincipalDomain + " reports: " + e2.ErrorText);
79
80 Result.TrySetResult(e2.Ok);
81
82 return Task.CompletedTask;
83
84 }, null))
85 {
86 this.Uri.State.Error(Uris.States.EDalerUriErrorType.ServiceUnavailable, "Unable to relay URI to secondary domain.", false);
87 return false;
88 }
89
90 return await Result.Task;
91 }
92
97 protected override Task<bool> DoExecute()
98 {
99 return this.DoCommand("execute");
100 }
101
102 private async Task<bool> DoCommand(string Command)
103 {
104 StringBuilder Xml = new StringBuilder();
105 TaskCompletionSource<string> Result = new TaskCompletionSource<string>();
106
107 Xml.Append('<');
108 Xml.Append(Command);
109 Xml.Append(" xmlns=\"");
111 Xml.Append("\" id=\"");
112 Xml.Append(this.Uri.Id.ToString());
113 Xml.Append("\"/>");
114
115 if (!await this.Uri.EDaler.Server.SendIqRequest("set",
117 new XmppAddress(this.componentAddress),
118 string.Empty, Xml.ToString(), false, (sender2, e2) =>
119 {
120 if (e2.Ok)
121 Result.TrySetResult(null);
122 else
123 Result.TrySetResult(string.IsNullOrEmpty(e2.ErrorText) ? "Unable to process request." : e2.ErrorText);
124
125 return Task.CompletedTask;
126
127 }, null))
128 {
129 return false;
130 }
131
132 string Msg = await Result.Task;
133 if (string.IsNullOrEmpty(Msg))
134 return true;
135
136 this.Uri.State.Error(Uris.States.EDalerUriErrorType.ServiceUnavailable, "Secondary domain rejected the request: " + Msg, false);
137 return false;
138 }
139
144 protected override Task<bool> DoCommit()
145 {
146 return this.DoCommand("commit");
147 }
148
153 protected override Task<bool> DoRollback()
154 {
155 return this.DoCommand("rollback");
156 }
157 }
158}
Helps with common XML-related tasks.
Definition: XML.cs:21
static string Encode(string s)
Encodes a string for use in XML.
Definition: XML.cs:29
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:147
static bool HasDomain
If a domain name is configured.
Definition: Gateway.cs:3093
XmppServer Server
XMPP Server.
Definition: Component.cs:97
Contains information about one XMPP address.
Definition: XmppAddress.cs:9
override string ToString()
object.ToString()
Definition: XmppAddress.cs:190
Task< bool > SendIqRequest(string Type, string From, string To, string Language, string ContentXml, EventHandlerAsync< IqResultEventArgs > Callback, object State)
Sends an IQ stanza to a recipient.
Definition: XmppServer.cs:3668
CaseInsensitiveString Domain
Domain name.
Definition: XmppServer.cs:922
async Task< CaseInsensitiveString > FindComponentAsync(CaseInsensitiveString Jid, CaseInsensitiveString Feature)
Finds a component having a specific feature, servicing a JID.
Definition: XmppServer.cs:6832
Represents a case-insensitive string.
Manages eDaler on accounts connected to the broker.
const string NamespaceEDaler
Namespace of eDaler component.
Abstract base class for eDaler transactions.
Relays partial processing of the URI to the secondary domain.
override async Task< bool > DoPrepare()
Performs actual preparation.
override Task< bool > DoExecute()
Performs actual execution.
RelayToSecondary(EDalerUri Uri, CaseInsensitiveString Domain)
Relays partial processing of the URI to the secondary domain.
override Task< bool > DoRollback()
Performs actual rollback.
override Task< bool > DoCommit()
Performs actual commit.
Abstract base class for eDaler URIs
Definition: EDalerUri.cs:20
EntityType ToType
Type of recipient.
Definition: EDalerUri.cs:191
EDalerUriState State
URI State object.
Definition: EDalerUri.cs:173
EDalerComponent EDaler
eDaler component reference
Definition: EDalerUri.cs:167
EntityType
Type of entity referred to in transaction.
Definition: Transaction.cs:15