Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ConnectionsSource.cs
1using System;
3using System.Threading.Tasks;
4using Waher.Events;
9using Waher.Things;
11
13{
15 {
19 public const string SourceID = "Connections";
20
21 private readonly Dictionary<Guid, string> jidByConnection = new Dictionary<Guid, string>();
22 private readonly ClientConnections clientConnections;
23 private readonly ServerConnections serverConnections;
24 private readonly INode[] nodes;
25
26 public ConnectionsSource()
27 {
28 this.nodes = new INode[]
29 {
30 this.clientConnections = new ClientConnections(),
31 this.serverConnections = new ServerConnections()
32 };
33 }
34
35 string IDataSource.SourceID => SourceID;
36 public bool HasChildren => false;
37 public DateTime LastChanged => DateTime.MinValue;
38 public IEnumerable<IDataSource> ChildSources => null;
39 public IEnumerable<INode> RootNodes => this.nodes;
40 public event EventHandlerAsync<SourceEvent> OnEvent;
41
42 public Task<bool> CanViewAsync(RequestOrigin Caller)
43 {
44 return Task.FromResult(Caller.HasPrivilege("Source." + SourceID + ".View"));
45 }
46
47 public Task<string> GetNameAsync(Language Language)
48 {
49 return Language.GetStringAsync(typeof(XmppServerModule), 9, "Connections");
50 }
51
52 public Task<INode> GetNodeAsync(IThingReference NodeRef)
53 {
54 switch (NodeRef.Partition)
55 {
56 case "C2S":
57 if (XmppServerModule.Server.TryGetClientConnection(NodeRef.NodeId, out IClientConnection C2sConnection))
58 return Task.FromResult<INode>(new ClientConnection(this.clientConnections, C2sConnection));
59 break;
60
61 case "S2S":
63 {
64 if (!XmppServerModule.Server.TryGetS2sEndpoint(NodeRef.NodeId, out IS2SEndpoint S2sConnection))
65 S2sConnection = null;
66
67 return Task.FromResult<INode>(new ServerConnection(this.serverConnections, Stat, S2sConnection));
68 }
69 break;
70
71 default:
72 foreach (INode Node in this.nodes)
73 {
74 if (Node.NodeId == NodeRef.NodeId &&
75 Node.SourceId == NodeRef.SourceId &&
76 Node.Partition == NodeRef.Partition)
77 {
78 return Task.FromResult<INode>(Node);
79 }
80 }
81 break;
82 }
83
84
85 return Task.FromResult<INode>(null);
86 }
87
88 internal async Task ClientConnectionAdded(object Sender, ClientConnectionEventArgs e)
89 {
90 try
91 {
92 ClientConnection Node = new ClientConnection(this.clientConnections, e.Connection);
94
95 lock (this.jidByConnection)
96 {
97 this.jidByConnection[e.Connection.Id] = Event.NodeId;
98 }
99
100 await this.OnEvent.Raise(this, Event);
101 }
102 catch (Exception ex)
103 {
104 Log.Exception(ex);
105 }
106 }
107
108 internal async Task ClientConnectionRemoved(object Sender, ClientConnectionEventArgs e)
109 {
110 lock (this.jidByConnection)
111 {
112 this.jidByConnection.Remove(e.Connection.Id);
113 }
114
115 ClientConnection Node = new ClientConnection(this.clientConnections, e.Connection);
117
118 await this.OnEvent.Raise(this, Event);
119 }
120
121 internal async Task ClientConnectionUpdated(object Sender, ClientConnectionEventArgs e)
122 {
123 try
124 {
125 ClientConnection Node = new ClientConnection(this.clientConnections, e.Connection);
126 string OldId;
127
128 lock (this.jidByConnection)
129 {
130 if (!this.jidByConnection.TryGetValue(e.Connection.Id, out OldId))
131 OldId = string.Empty;
132 }
133
135
136 lock (this.jidByConnection)
137 {
138 this.jidByConnection[e.Connection.Id] = Event.NodeId;
139 }
140
141 await this.OnEvent.Raise(this, Event);
142 }
143 catch (Exception ex)
144 {
145 Log.Exception(ex);
146 }
147 }
148
149 internal async Task ServerConnectionAdded(object _, Networking.XMPP.Server.ServerConnectionEventArgs e)
150 {
151 S2sEndpointStatistics Stat = XmppServerModule.Server.GetS2sStatistics(e.Connection.RemoteDomain, e.Connection.Type);
152 ServerConnection Node = new ServerConnection(this.serverConnections, Stat, e.Connection);
153 await this.OnEvent.Raise(this, await NodeAdded.FromNode(Node, await Translator.GetDefaultLanguageAsync(), RequestOrigin.Empty, Node is ICommunicationLayer));
154 }
155
156 internal async Task ServerConnectionRemoved(object _, Networking.XMPP.Server.ServerConnectionEventArgs e)
157 {
158 S2sEndpointStatistics Stat = XmppServerModule.Server.GetS2sStatistics(e.Connection.RemoteDomain, e.Connection.Type);
159 ServerConnection Node = new ServerConnection(this.serverConnections, Stat, e.Connection);
160 await this.OnEvent.Raise(this, NodeRemoved.FromNode(Node));
161 }
162
163 internal async Task ServerConnectionUpdated(object _, Networking.XMPP.Server.ServerConnectionEventArgs e)
164 {
165 S2sEndpointStatistics Stat = XmppServerModule.Server.GetS2sStatistics(e.Connection.RemoteDomain, e.Connection.Type);
166 ServerConnection Node = new ServerConnection(this.serverConnections, Stat, e.Connection);
167 await this.OnEvent.Raise(this, await NodeUpdated.FromNode(Node, await Translator.GetDefaultLanguageAsync(), RequestOrigin.Empty));
168 }
169
170 }
171}
Class representing an event.
Definition: Event.cs:11
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1657
Abstract base class for XMPP client connections
Abstract base class for server connections.
Mainstains information about connectivity from a specific s2s endpoint.
bool TryGetClientConnection(string FullJID, out IClientConnection Connection)
Tries to get an active client connection.
Definition: XmppServer.cs:844
bool TryGetS2sStatistics(CaseInsensitiveString Endpoint, out S2sEndpointStatistics Stat)
Tries to get available S2S Endpoint statistics for an endpoint.
Definition: XmppServer.cs:715
bool TryGetS2sEndpoint(string RemoteDomain, out IS2SEndpoint Endpoint)
Tries to get a server-to-server connection state object.
Definition: XmppServer.cs:2468
Contains information about a language.
Definition: Language.cs:17
Task< string > GetStringAsync(Type Type, int Id, string Default)
Gets the string value of a string ID. If no such string exists, a string is created with the default ...
Definition: Language.cs:209
Basic access point for runtime language localization.
Definition: Translator.cs:16
static async Task< Language > GetDefaultLanguageAsync()
Gets the default language.
Definition: Translator.cs:223
const string SourceID
Source ID for the connections data source.
bool HasChildren
If the source has any child sources.
IEnumerable< INode > RootNodes
Root node references. If no root nodes are available, null is returned.
Task< string > GetNameAsync(Language Language)
Gets the name of data source.
IEnumerable< IDataSource > ChildSources
Child sources. If no child sources are available, null is returned.
Task< INode > GetNodeAsync(IThingReference NodeRef)
Gets the node, given a reference to it.
Task< bool > CanViewAsync(RequestOrigin Caller)
If the data source is visible to the caller.
DateTime LastChanged
When the source was last updated.
Service Module hosting the XMPP broker and its components.
Tokens available in request.
Definition: RequestOrigin.cs:9
static readonly RequestOrigin Empty
Empty request origin.
bool HasPrivilege(string Privilege)
If the origin has a given privilege.
static Task< NodeAdded > FromNode(INode Node, Language Language, RequestOrigin Caller, bool Sniffable)
Creates an event object from a node object.
Definition: NodeAdded.cs:36
static NodeRemoved FromNode(INode Node)
Creates an event object from a node object.
Definition: NodeRemoved.cs:30
static Task< NodeUpdated > FromNode(INode Node, Language Language, RequestOrigin Caller)
Creates an event object from a node object.
Definition: NodeUpdated.cs:31
Interface for observable classes implementing communication protocols.
Guid Id
Identity of connection object.
string Type
Type of endpoint
Definition: IEndpoint.cs:14
Interface for XMPP S2S endpoints
Definition: IS2sEndpoint.cs:11
Interface for datasources that are published through the concentrator interface.
Definition: IDataSource.cs:14
Interface for nodes that are published through the concentrator interface.
Definition: INode.cs:49
Interface for thing references.
string Partition
Optional partition in which the Node ID is unique.
string SourceId
Optional ID of source containing node.
Definition: ImplTypes.g.cs:58