Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Session.cs
1using System;
3using System.Threading.Tasks;
4using Waher.Events;
8
10{
15 {
16 private readonly HashSet<string> subscriptions = new HashSet<string>();
17 private readonly ISnifferSet? snifferSet;
18 private readonly bool hasSnifferSet;
19 private InMemorySniffer? unauthenticatedSniffer;
20 private string userName = "Not Authenticated";
21 private bool isAuthenticated = false;
22
36 {
37 this.SessionId = SessionId;
38 this.ClientProtocolVersion = ClientProtocolVersion;
39 this.ClientCapabilities = ClientCapabilities;
40 this.ClientInformation = Implementation;
41 this.RemoteEndpoint = RemoteEndpoint;
42 this.snifferSet = SnifferSet;
43 this.hasSnifferSet = !(SnifferSet is null);
44
45 this.unauthenticatedSniffer = new InMemorySniffer(200, SessionId);
46 }
47
51 public string SessionId { get; }
52
56 public string ClientProtocolVersion { get; }
57
62
67
71 public string RemoteEndpoint { get; }
72
76 public string UserName => this.userName;
77
81 public bool IsAuthenticated => this.isAuthenticated;
82
86 [Obsolete("Use DisposeAsync instead.")]
87 public void Dispose()
88 {
89 this.DisposeAsync().Wait();
90 }
91
95 public async Task DisposeAsync()
96 {
97 if (!(this.unauthenticatedSniffer is null))
98 {
99 await this.unauthenticatedSniffer.DisposeAsync();
100 this.unauthenticatedSniffer = null;
101 }
102
103 lock (this.subscriptions)
104 {
105 this.subscriptions.Clear();
106 }
107 }
108
114 internal async Task SetUserName(string UserName)
115 {
116 this.userName = UserName;
117
118 if (!this.isAuthenticated)
119 {
120 this.isAuthenticated = true;
121
122 if (this.hasSnifferSet)
123 this.unauthenticatedSniffer?.Replay(UserName, this.snifferSet);
124
125 if (!(this.unauthenticatedSniffer is null))
126 {
127 await this.unauthenticatedSniffer.DisposeAsync();
128 this.unauthenticatedSniffer = null;
129 }
130 }
131 }
132
137 internal void ReceiveText(string Text)
138 {
139 if (this.hasSnifferSet)
140 {
141 if (this.isAuthenticated)
142 this.snifferSet!.ReceiveText(this.userName, Text);
143 else
144 this.unauthenticatedSniffer?.ReceiveText(Text);
145 }
146 }
147
152 public void TransmitText(string Text)
153 {
154 if (this.hasSnifferSet)
155 {
156 if (this.isAuthenticated)
157 this.snifferSet!.TransmitText(this.userName, Text);
158 else
159 this.unauthenticatedSniffer?.TransmitText(Text);
160 }
161 }
162
167 internal void Information(string Text)
168 {
169 if (this.hasSnifferSet)
170 {
171 if (this.isAuthenticated)
172 this.snifferSet!.Information(this.userName, Text);
173 else
174 this.unauthenticatedSniffer?.Information(Text);
175 }
176 }
177
182 internal void Warning(string Text)
183 {
184 if (this.hasSnifferSet)
185 {
186 if (this.isAuthenticated)
187 this.snifferSet!.Warning(this.userName, Text);
188 else
189 this.unauthenticatedSniffer?.Warning(Text);
190 }
191 }
192
197 internal void Error(string Text)
198 {
199 if (this.hasSnifferSet)
200 {
201 if (this.isAuthenticated)
202 this.snifferSet!.Error(this.userName, Text);
203 else
204 this.unauthenticatedSniffer?.Error(Text);
205 }
206 }
207
212 internal void Exception(Exception Exception)
213 {
214 if (this.hasSnifferSet)
215 {
216 if (this.isAuthenticated)
217 this.snifferSet!.Exception(this.userName, Exception);
218 else
219 this.unauthenticatedSniffer?.Exception(Exception);
220 }
221 }
222
228 public bool IsSubscribed(string Uri)
229 {
230 lock (this.subscriptions)
231 {
232 return this.subscriptions.Contains(Uri);
233 }
234 }
235
241 public bool Subscribe(string Uri)
242 {
243 lock (this.subscriptions)
244 {
245 return this.subscriptions.Add(Uri);
246 }
247 }
248
254 public bool Unsubscribe(string Uri)
255 {
256 lock (this.subscriptions)
257 {
258 return this.subscriptions.Remove(Uri);
259 }
260 }
261 }
262}
void Dispose()
Disposes the connection
Definition: Session.cs:87
bool Unsubscribe(string Uri)
Unsubscribes from a resource.
Definition: Session.cs:254
string ClientProtocolVersion
Protocol version of client.
Definition: Session.cs:56
bool IsSubscribed(string Uri)
Checks if a subscription to a resource exists.
Definition: Session.cs:228
string UserName
User name used for session.
Definition: Session.cs:76
async Task DisposeAsync()
IDisposableAsync.DisposeAsync()
Definition: Session.cs:95
Implementation? ClientInformation
Information about client, if available.
Definition: Session.cs:66
Session(string SessionId, string ClientProtocolVersion, ClientCapabilities? ClientCapabilities, Implementation? Implementation, string RemoteEndpoint, ISnifferSet? SnifferSet)
MCP Session information.
Definition: Session.cs:33
void TransmitText(string Text)
Text has been transmitted to the client.
Definition: Session.cs:152
string RemoteEndpoint
Client remote endpoint.
Definition: Session.cs:71
bool Subscribe(string Uri)
Subscribes to a resource.
Definition: Session.cs:241
bool IsAuthenticated
If client has been authenticated in the session.
Definition: Session.cs:81
ClientCapabilities? ClientCapabilities
Client capabilities, if available.
Definition: Session.cs:61
Sniffer that stores events in memory.
void Replay(CommunicationLayer ComLayer)
Replays sniffer events.
override Task DisposeAsync()
IDisposableAsync.DisposeAsync
void Warning(string Warning)
Called to inform the viewer of a warning state.
Definition: SnifferBase.cs:324
void Error(string Error)
Called to inform the viewer of an error state.
Definition: SnifferBase.cs:343
void TransmitText(string Text)
Called when text has been transmitted.
Definition: SnifferBase.cs:286
void Exception(string Exception)
Called to inform the viewer of an exception state.
Definition: SnifferBase.cs:362
void Information(string Comment)
Called to inform the viewer of something.
Definition: SnifferBase.cs:305
void ReceiveText(string Text)
Called when text has been received.
Definition: SnifferBase.cs:267
Maintains a set of sniffers.
Definition: SnifferSet.cs:12
Interface for asynchronously disposable objects.
Interface for JSON-RPC session objects.
Interface for sets of sniffers.
Definition: ISnifferSet.cs:9