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;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using Waher.Events;
10using Waher.Things;
12
14{
16 {
17 private readonly Dictionary<Guid, string> jidByConnection = new Dictionary<Guid, string>();
18 private readonly ClientConnections clientConnections;
19 private readonly ServerConnections serverConnections;
20 private readonly INode[] nodes;
21
22 public ConnectionsSource()
23 {
24 this.nodes = new INode[]
25 {
26 this.clientConnections = new ClientConnections(),
27 this.serverConnections = new ServerConnections()
28 };
29 }
30
31 public string SourceID => "Connections";
32 public bool HasChildren => false;
33 public DateTime LastChanged => DateTime.MinValue;
34 public IEnumerable<IDataSource> ChildSources => null;
35 public IEnumerable<INode> RootNodes => this.nodes;
36 public event EventHandlerAsync<SourceEvent> OnEvent;
37
38 public Task<bool> CanViewAsync(RequestOrigin Caller)
39 {
40 return XmppServerModule.IsAdmin(Caller.From);
41 }
42
43 public Task<string> GetNameAsync(Language Language)
44 {
45 return Language.GetStringAsync(typeof(XmppServerModule), 9, "Connections");
46 }
47
48 public Task<INode> GetNodeAsync(IThingReference NodeRef)
49 {
50 switch (NodeRef.Partition)
51 {
52 case "C2S":
53 if (XmppServerModule.Server.TryGetClientConnection(NodeRef.NodeId, out IClientConnection C2sConnection))
54 return Task.FromResult<INode>(new ClientConnection(this.clientConnections, C2sConnection));
55 break;
56
57 case "S2S":
59 {
60 if (!XmppServerModule.Server.TryGetS2sEndpoint(NodeRef.NodeId, out IS2SEndpoint S2sConnection))
61 S2sConnection = null;
62
63 return Task.FromResult<INode>(new ServerConnection(this.serverConnections, Stat, S2sConnection));
64 }
65 break;
66
67 default:
68 foreach (INode Node in this.nodes)
69 {
70 if (Node.NodeId == NodeRef.NodeId &&
71 Node.SourceId == NodeRef.SourceId &&
72 Node.Partition == NodeRef.Partition)
73 {
74 return Task.FromResult<INode>(Node);
75 }
76 }
77 break;
78 }
79
80
81 return Task.FromResult<INode>(null);
82 }
83
84 internal async Task ClientConnectionAdded(object Sender, ClientConnectionEventArgs e)
85 {
86 try
87 {
88 ClientConnection Node = new ClientConnection(this.clientConnections, e.Connection);
90
91 lock (this.jidByConnection)
92 {
93 this.jidByConnection[e.Connection.Id] = Event.NodeId;
94 }
95
96 await this.OnEvent.Raise(this, Event);
97 }
98 catch (Exception ex)
99 {
100 Log.Exception(ex);
101 }
102 }
103
104 internal async Task ClientConnectionRemoved(object Sender, ClientConnectionEventArgs e)
105 {
106 lock (this.jidByConnection)
107 {
108 this.jidByConnection.Remove(e.Connection.Id);
109 }
110
111 ClientConnection Node = new ClientConnection(this.clientConnections, e.Connection);
113
114 await this.OnEvent.Raise(this, Event);
115 }
116
117 internal async Task ClientConnectionUpdated(object Sender, ClientConnectionEventArgs e)
118 {
119 try
120 {
121 ClientConnection Node = new ClientConnection(this.clientConnections, e.Connection);
122 string OldId;
123
124 lock (this.jidByConnection)
125 {
126 if (!this.jidByConnection.TryGetValue(e.Connection.Id, out OldId))
127 OldId = string.Empty;
128 }
129
131
132 lock (this.jidByConnection)
133 {
134 this.jidByConnection[e.Connection.Id] = Event.NodeId;
135 }
136
137 await this.OnEvent.Raise(this, Event);
138 }
139 catch (Exception ex)
140 {
141 Log.Exception(ex);
142 }
143 }
144
145 internal async Task ServerConnectionAdded(object _, Networking.XMPP.Server.ServerConnectionEventArgs e)
146 {
147 S2sEndpointStatistics Stat = XmppServerModule.Server.GetS2sStatistics(e.Connection.RemoteDomain, e.Connection.Type);
148 ServerConnection Node = new ServerConnection(this.serverConnections, Stat, e.Connection);
149 await this.OnEvent.Raise(this, await NodeAdded.FromNode(Node, await Translator.GetDefaultLanguageAsync(), RequestOrigin.Empty, Node is ICommunicationLayer));
150 }
151
152 internal async Task ServerConnectionRemoved(object _, Networking.XMPP.Server.ServerConnectionEventArgs e)
153 {
154 S2sEndpointStatistics Stat = XmppServerModule.Server.GetS2sStatistics(e.Connection.RemoteDomain, e.Connection.Type);
155 ServerConnection Node = new ServerConnection(this.serverConnections, Stat, e.Connection);
156 await this.OnEvent.Raise(this, NodeRemoved.FromNode(Node));
157 }
158
159 internal async Task ServerConnectionUpdated(object _, Networking.XMPP.Server.ServerConnectionEventArgs e)
160 {
161 S2sEndpointStatistics Stat = XmppServerModule.Server.GetS2sStatistics(e.Connection.RemoteDomain, e.Connection.Type);
162 ServerConnection Node = new ServerConnection(this.serverConnections, Stat, e.Connection);
163 await this.OnEvent.Raise(this, await NodeUpdated.FromNode(Node, await Translator.GetDefaultLanguageAsync(), RequestOrigin.Empty));
164 }
165
166 }
167}
Class representing an event.
Definition: Event.cs:10
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
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:1647
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:807
bool TryGetS2sStatistics(CaseInsensitiveString Endpoint, out S2sEndpointStatistics Stat)
Tries to get available S2S Endpoint statistics for an endpoint.
Definition: XmppServer.cs:708
bool TryGetS2sEndpoint(string RemoteDomain, out IS2SEndpoint Endpoint)
Tries to get a server-to-server connection state object.
Definition: XmppServer.cs:2259
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
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.
string From
Address of caller.
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:15
Interface for XMPP S2S endpoints
Definition: IS2sEndpoint.cs:10
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.