Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
HttpProtocol.cs
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Text;
5using System.Threading.Tasks;
12using Waher.Things;
14
16{
18 {
19 private readonly HttpServer server;
20
21 public HttpProtocol(HttpServer Server)
22 {
23 this.server = Server;
24 }
25
26 #region INode
27
28 public string LocalId => this.NodeId;
29 public string LogId => this.NodeId;
30 public string NodeId => "HTTP";
31 public string SourceId => "Protocols";
32 public string Partition => string.Empty;
33 public bool HasChildren => false;
34 public bool ChildrenOrdered => false;
35 public bool IsReadable => false;
36 public bool IsControllable => false;
37 public INode Parent => null;
38 public DateTime LastChanged => DateTime.MinValue;
39 public Task<IEnumerable<INode>> ChildNodes => null;
40
41 public NodeState State => NodeState.None; // TODO: Check HTTP Server state.
42
43 public Task<bool> AcceptsChildAsync(INode Child)
44 {
45 return Task.FromResult(false);
46 }
47
48 public Task<bool> AcceptsParentAsync(INode Parent)
49 {
50 return Task.FromResult(false);
51 }
52
53 public Task AddAsync(INode Child)
54 {
55 throw new NotSupportedException();
56 }
57
58 public Task<bool> CanAddAsync(RequestOrigin Caller)
59 {
60 return Task.FromResult(false);
61 }
62
63 public Task<bool> CanDestroyAsync(RequestOrigin Caller)
64 {
65 return Task.FromResult(false);
66 }
67
68 public Task<bool> CanEditAsync(RequestOrigin Caller)
69 {
70 return XmppServerModule.IsAdmin(Caller.From);
71 }
72
73 public Task<bool> CanViewAsync(RequestOrigin Caller)
74 {
75 return XmppServerModule.IsAdmin(Caller.From);
76 }
77
78 public Task DestroyAsync()
79 {
80 throw new NotSupportedException();
81 }
82
83 public async Task<IEnumerable<Parameter>> GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
84 {
85 LinkedList<Parameter> Parameters = new LinkedList<Parameter>();
86
87 Parameters.AddLast(new StringParameter("HttpPorts", await Language.GetStringAsync(typeof(XmppServerModule), 3, "HTTP Ports"), PortsToString(this.server.OpenHttpPorts)));
88 Parameters.AddLast(new StringParameter("HttpsPorts", await Language.GetStringAsync(typeof(XmppServerModule), 4, "HTTPS Ports"), PortsToString(this.server.OpenHttpsPorts)));
89
90 return Parameters;
91 }
92
93 internal static string PortsToString(int[] Ports)
94 {
95 StringBuilder sb = new StringBuilder();
96 bool First = true;
97
98 foreach (int Port in Ports)
99 {
100 if (First)
101 First = false;
102 else
103 sb.Append(", ");
104
105 sb.Append(Port.ToString());
106 }
107
108 return sb.ToString();
109 }
110
111 public Task<IEnumerable<Message>> GetMessagesAsync(RequestOrigin Caller)
112 {
113 return Task.FromResult<IEnumerable<Message>>(null);
114 }
115
116 public Task<string> GetTypeNameAsync(Language Language)
117 {
118 return Language.GetStringAsync(typeof(XmppServerModule), 28, "HTTP Protocol");
119 }
120
121 public Task<bool> MoveDownAsync(RequestOrigin Caller)
122 {
123 return Task.FromResult(false);
124 }
125
126 public Task<bool> MoveUpAsync(RequestOrigin Caller)
127 {
128 return Task.FromResult(false);
129 }
130
131 public Task UpdateAsync()
132 {
133 return Task.CompletedTask;
134 }
135
136 public Task<bool> RemoveAsync(INode Child)
137 {
138 return Task.FromResult(false);
139 }
140
141 public bool HasCommands => true;
142
143 public Task<IEnumerable<ICommand>> Commands
144 {
145 get
146 {
147 return Task.FromResult<IEnumerable<ICommand>>(new ICommand[]
148 {
149 new HttpStatistics()
150 });
151 }
152 }
153
154 #endregion
155
156 #region ICommunicationLayer
157
163
168 public void Add(ISniffer Sniffer)
169 {
170 Gateway.HttpServer?.Add(Sniffer);
171 Gateway.ScheduleEvent((_) => this.Remove(Sniffer), DateTime.Now.AddHours(1), null);
172 }
173
178 public void AddRange(IEnumerable<ISniffer> Sniffers)
179 {
182 {
183 foreach (ISniffer Sniffer in Sniffers)
184 this.Remove(Sniffer);
185 }, DateTime.Now.AddHours(1), null);
186 }
187
193 public bool Remove(ISniffer Sniffer) => Gateway.HttpServer?.Remove(Sniffer) ?? false;
194
199
203 public bool HasSniffers => Gateway.HttpServer?.HasSniffers ?? false;
204
209 public IEnumerator<ISniffer> GetEnumerator() => Gateway.HttpServer?.GetEnumerator() ?? ((IEnumerable<ISniffer>)(new ISniffer[0])).GetEnumerator();
210
215 IEnumerator IEnumerable.GetEnumerator() => Gateway.HttpServer?.GetEnumerator() ?? (new ISniffer[0]).GetEnumerator();
216
221 public Task ReceiveBinary(byte[] Data) => Gateway.HttpServer?.ReceiveBinary(Data) ?? Task.CompletedTask;
222
227 public Task TransmitBinary(byte[] Data) => Gateway.HttpServer?.TransmitBinary(Data) ?? Task.CompletedTask;
228
233 public Task ReceiveText(string Text) => Gateway.HttpServer?.ReceiveText(Text) ?? Task.CompletedTask;
234
239 public Task TransmitText(string Text) => Gateway.HttpServer?.TransmitText(Text) ?? Task.CompletedTask;
240
245 public Task Information(string Comment) => Gateway.HttpServer?.Information(Comment) ?? Task.CompletedTask;
246
251 public Task Warning(string Warning) => Gateway.HttpServer?.Warning(Warning) ?? Task.CompletedTask;
252
257 public Task Error(string Error) => Gateway.HttpServer?.Error(Error) ?? Task.CompletedTask;
258
263 public Task Exception(Exception Exception) => Gateway.HttpServer?.Exception(Exception) ?? Task.CompletedTask;
264
269 public Task Exception(string Exception) => Gateway.HttpServer?.Exception(Exception) ?? Task.CompletedTask;
270
276 public Task ReceiveBinary(DateTime Timestamp, byte[] Data) => Gateway.HttpServer?.ReceiveBinary(Timestamp, Data) ?? Task.CompletedTask;
277
283 public Task TransmitBinary(DateTime Timestamp, byte[] Data) => Gateway.HttpServer?.TransmitBinary(Timestamp, Data) ?? Task.CompletedTask;
284
290 public Task ReceiveText(DateTime Timestamp, string Text) => Gateway.HttpServer?.ReceiveText(Timestamp, Text) ?? Task.CompletedTask;
291
297 public Task TransmitText(DateTime Timestamp, string Text) => Gateway.HttpServer?.TransmitText(Timestamp, Text) ?? Task.CompletedTask;
298
304 public Task Information(DateTime Timestamp, string Comment) => Gateway.HttpServer?.Information(Timestamp, Comment) ?? Task.CompletedTask;
305
311 public Task Warning(DateTime Timestamp, string Warning) => Gateway.HttpServer?.Warning(Timestamp, Warning) ?? Task.CompletedTask;
312
318 public Task Error(DateTime Timestamp, string Error) => Gateway.HttpServer?.Error(Timestamp, Error) ?? Task.CompletedTask;
319
325 public Task Exception(DateTime Timestamp, string Exception) => Gateway.HttpServer?.Exception(Timestamp, Exception) ?? Task.CompletedTask;
326
332 public Task Exception(DateTime Timestamp, Exception Exception) => Gateway.HttpServer?.Exception(Timestamp, Exception) ?? Task.CompletedTask;
333
334 #endregion
335 }
336}
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static HttpServer HttpServer
HTTP Server
Definition: Gateway.cs:3299
static DateTime ScheduleEvent(ScheduledEventCallback Callback, DateTime When, object State)
Schedules a one-time event.
Definition: Gateway.cs:3452
Task TransmitBinary(byte[] Data)
Called when binary data has been transmitted.
Task Error(string Error)
Called to inform the viewer of an error state.
Task Exception(Exception Exception)
Called to inform the viewer of an exception state.
bool DecoupledEvents
If events raised from the communication layer are decoupled, i.e. executed in parallel with the sourc...
ISniffer[] Sniffers
Registered sniffers.
Task Information(string Comment)
Called to inform the viewer of something.
Task TransmitText(string Text)
Called when text has been transmitted.
bool HasSniffers
If there are sniffers registered on the object.
Task ReceiveText(string Text)
Called when text has been received.
Task ReceiveBinary(byte[] Data)
Called when binary data has been received.
Task Warning(string Warning)
Called to inform the viewer of a warning state.
IEnumerator< ISniffer > GetEnumerator()
Gets a typed enumerator.
Implements an HTTP server.
Definition: HttpServer.cs:36
override void AddRange(IEnumerable< ISniffer > Sniffers)
ICommunicationLayer.AddRange
Definition: HttpServer.cs:1209
override void Add(ISniffer Sniffer)
ICommunicationLayer.Add
Definition: HttpServer.cs:1195
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
string LogId
If provided, an ID for the node, as it would appear or be used in system logs. Can be null,...
Definition: HttpProtocol.cs:29
Task Exception(DateTime Timestamp, string Exception)
Called to inform the viewer of an exception state.
Task ReceiveBinary(byte[] Data)
Called when binary data has been received.
bool ChildrenOrdered
If the children of the node have an intrinsic order (true), or if the order is not important (false).
Definition: HttpProtocol.cs:34
Task Information(string Comment)
Called to inform the viewer of something.
Task Warning(DateTime Timestamp, string Warning)
Called to inform the viewer of a warning state.
Task TransmitText(string Text)
Called when text has been transmitted.
bool HasChildren
If the source has any child sources.
Definition: HttpProtocol.cs:33
Task UpdateAsync()
Updates the node (in persisted storage).
Task< bool > CanViewAsync(RequestOrigin Caller)
If the node is visible to the caller.
Definition: HttpProtocol.cs:73
bool Remove(ISniffer Sniffer)
Removes a sniffer, if registered.
Task< bool > AcceptsChildAsync(INode Child)
If the node accepts a presumptive child, i.e. can receive as a child (if that child accepts the node ...
Definition: HttpProtocol.cs:43
Task ReceiveText(DateTime Timestamp, string Text)
Called when text has been received.
Task Exception(Exception Exception)
Called to inform the viewer of an exception state.
Task Exception(string Exception)
Called to inform the viewer of an exception state.
Task ReceiveBinary(DateTime Timestamp, byte[] Data)
Called when binary data has been received.
Task< IEnumerable< ICommand > > Commands
Available command objects. If no commands are available, null is returned.
Task< IEnumerable< INode > > ChildNodes
Child nodes. If no child nodes are available, null is returned.
Definition: HttpProtocol.cs:39
Task AddAsync(INode Child)
Adds a new child to the node.
Definition: HttpProtocol.cs:53
string SourceId
Optional ID of source containing node.
Definition: HttpProtocol.cs:31
string Partition
Optional partition in which the Node ID is unique.
Definition: HttpProtocol.cs:32
Task< bool > MoveDownAsync(RequestOrigin Caller)
Tries to move the node down.
Task< bool > AcceptsParentAsync(INode Parent)
If the node accepts a presumptive parent, i.e. can be added to that parent (if that parent accepts th...
Definition: HttpProtocol.cs:48
void AddRange(IEnumerable< ISniffer > Sniffers)
Adds a range of sniffers to the node.
Task TransmitBinary(DateTime Timestamp, byte[] Data)
Called when binary data has been transmitted.
Task Warning(string Warning)
Called to inform the viewer of a warning state.
DateTime LastChanged
When the node was last updated.
Definition: HttpProtocol.cs:38
async Task< IEnumerable< Parameter > > GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
Gets displayable parameters.
Definition: HttpProtocol.cs:83
IEnumerator< ISniffer > GetEnumerator()
Returns an enumerator that iterates through the collection.
Task< bool > CanAddAsync(RequestOrigin Caller)
If the node can be added to by the caller.
Definition: HttpProtocol.cs:58
bool HasCommands
If the node has registered commands or not.
bool DecoupledEvents
If events raised from the communication layer are decoupled, i.e. executed in parallel with the sourc...
Task DestroyAsync()
Destroys the node. If it is a child to a parent node, it is removed from the parent first.
Definition: HttpProtocol.cs:78
Task ReceiveText(string Text)
Called when text has been received.
string LocalId
If provided, an ID for the node, but unique locally between siblings. Can be null,...
Definition: HttpProtocol.cs:28
Task< string > GetTypeNameAsync(Language Language)
Gets the type name of the node.
Task TransmitText(DateTime Timestamp, string Text)
Called when text has been transmitted.
void Add(ISniffer Sniffer)
Adds a sniffer to the node.
Task Error(string Error)
Called to inform the viewer of an error state.
Task< bool > CanEditAsync(RequestOrigin Caller)
If the node can be edited by the caller.
Definition: HttpProtocol.cs:68
Task Error(DateTime Timestamp, string Error)
Called to inform the viewer of an error state.
NodeState State
Current overall state of the node.
Definition: HttpProtocol.cs:41
bool IsControllable
If the node can be controlled.
Definition: HttpProtocol.cs:36
Task< bool > CanDestroyAsync(RequestOrigin Caller)
If the node can be destroyed to by the caller.
Definition: HttpProtocol.cs:63
Task< IEnumerable< Message > > GetMessagesAsync(RequestOrigin Caller)
Gets messages logged on the node.
Task< bool > RemoveAsync(INode Child)
Removes a child from the node.
Task< bool > MoveUpAsync(RequestOrigin Caller)
Tries to move the node up.
INode Parent
Parent Node, or null if a root node.
Definition: HttpProtocol.cs:37
Task Information(DateTime Timestamp, string Comment)
Called to inform the viewer of something.
Task TransmitBinary(byte[] Data)
Called when binary data has been transmitted.
Task Exception(DateTime Timestamp, Exception Exception)
Called to inform the viewer of an exception state.
bool HasSniffers
If there are sniffers registered on the object.
Service Module hosting the XMPP broker and its components.
Tokens available in request.
Definition: RequestOrigin.cs:9
string From
Address of caller.
Interface for observable classes implementing communication protocols.
Interface for sniffers. Sniffers can be added to ICommunicationLayer classes to eavesdrop on communic...
Definition: ISniffer.cs:11
Interface for commands.
Definition: ICommand.cs:32
Interface for nodes that are published through the concentrator interface.
Definition: INode.cs:49
NodeState
State of a node.
Definition: INode.cs:13