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;
7
9{
14 {
15 private readonly CaseInsensitiveString domain;
16 private CaseInsensitiveString componentAddress;
17
23 : base(Uri)
24 {
25 this.domain = Domain;
26 }
27
31 public CaseInsensitiveString Domain => this.domain;
32
37 protected override async Task<bool> DoPrepare()
38 {
39 this.componentAddress = await this.Uri.EDaler.Server.FindComponentAsync(this.domain, EDalerComponent.NamespaceEDaler);
40
41 StringBuilder Xml = new StringBuilder();
42 TaskCompletionSource<bool> Result = new TaskCompletionSource<bool>();
43
44 Xml.Append("<uri xmlns=\"");
46
47 if (this.Uri.ToType == EntityType.NetworkJid)
48 {
49 Xml.Append("\" for=\"");
50 Xml.Append(XML.Encode(this.Uri.To.Address));
51 }
52 else if (!this.Uri.State.Relayed)
53 {
54 Xml.Append("\" for=\"");
55 Xml.Append(XML.Encode(this.Uri.State.Sender));
56 }
57
58 Xml.Append("\">");
59 Xml.Append(XML.Encode(this.Uri.UriString));
60 Xml.Append("</uri>");
61
62 if (!await this.Uri.EDaler.Server.SendIqRequest("set",
64 new XmppAddress(this.componentAddress),
65 string.Empty, Xml.ToString(), (sender2, e2) =>
66 {
67 if (!e2.Ok)
68 this.Uri.State.Error(e2.ErrorType.ToString().ToLower(), string.Empty, this.Uri.PrincipalDomain + " reports: " + e2.ErrorText);
69
70 Result.TrySetResult(e2.Ok);
71
72 return Task.CompletedTask;
73
74 }, null))
75 {
76 this.Uri.State.Error(Uris.States.EDalerUriErrorType.ServiceUnavailable, "Unable to relay URI to secondary domain.", false);
77 return false;
78 }
79
80 return await Result.Task;
81 }
82
87 protected override Task<bool> DoExecute()
88 {
89 return this.DoCommand("execute");
90 }
91
92 private async Task<bool> DoCommand(string Command)
93 {
94 StringBuilder Xml = new StringBuilder();
95 TaskCompletionSource<string> Result = new TaskCompletionSource<string>();
96
97 Xml.Append('<');
98 Xml.Append(Command);
99 Xml.Append(" xmlns=\"");
101 Xml.Append("\" id=\"");
102 Xml.Append(this.Uri.Id.ToString());
103 Xml.Append("\"/>");
104
105 if (!await this.Uri.EDaler.Server.SendIqRequest("set",
107 new XmppAddress(this.componentAddress),
108 string.Empty, Xml.ToString(), (sender2, e2) =>
109 {
110 if (e2.Ok)
111 Result.TrySetResult(null);
112 else
113 Result.TrySetResult(string.IsNullOrEmpty(e2.ErrorText) ? "Unable to process request." : e2.ErrorText);
114
115 return Task.CompletedTask;
116
117 }, null))
118 {
119 return false;
120 }
121
122 string Msg = await Result.Task;
123 if (string.IsNullOrEmpty(Msg))
124 return true;
125
126 this.Uri.State.Error(Uris.States.EDalerUriErrorType.ServiceUnavailable, "Secondary domain rejected the request: " + Msg, false);
127 return false;
128 }
129
134 protected override Task<bool> DoCommit()
135 {
136 return this.DoCommand("commit");
137 }
138
143 protected override Task<bool> DoRollback()
144 {
145 return this.DoCommand("rollback");
146 }
147 }
148}
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Encode(string s)
Encodes a string for use in XML.
Definition: XML.cs:27
XmppServer Server
XMPP Server.
Definition: Component.cs:96
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:3317
CaseInsensitiveString Domain
Domain name.
Definition: XmppServer.cs:882
async Task< CaseInsensitiveString > FindComponentAsync(CaseInsensitiveString Jid, CaseInsensitiveString Feature)
Finds a component having a specific feature, servicing a JID.
Definition: XmppServer.cs:6382
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