forked from Com-AugustCellars/CoAP-CSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CoapServer.cs
327 lines (281 loc) · 10.1 KB
/
CoapServer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* Copyright (c) 2011-2014, Longxiang He <[email protected]>,
* SmeshLink Technology Co.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY.
*
* This file is part of the CoAP.NET, a CoAP framework in C#.
* Please see README for more information.
*/
using System;
using System.Collections.Generic;
using System.Net;
using Com.AugustCellars.CoAP.Log;
using Com.AugustCellars.CoAP.Net;
using Com.AugustCellars.CoAP.OSCOAP;
using Com.AugustCellars.CoAP.Server.Resources;
namespace Com.AugustCellars.CoAP.Server
{
/// <summary>
/// Represents an execution environment for CoAP <see cref="IResource"/>s.
/// </summary>
public class CoapServer : IServer
{
private static readonly ILogger _Log = Logging.GetLogger(typeof(CoapServer));
private readonly IResource _root;
private readonly List<IEndPoint> _endpoints = new List<IEndPoint>();
private readonly System.Net.EndPoint _endPointSupplied;
private IMessageDeliverer _deliverer;
public SecurityContextSet SecurityContexts { get; } = new SecurityContextSet();
/// <summary>
/// Constructs a server with default configuration.
/// </summary>
public CoapServer()
: this((ICoapConfig) null)
{
}
/// <summary>
/// Constructs a server that listens to the specified port(s).
/// </summary>
/// <param name="ports">the ports to bind to</param>
public CoapServer(params Int32[] ports)
: this(null, ports)
{
}
/// <summary>
/// Constructs a server with the specified configuration that
/// listens to the given ports.
/// </summary>
/// <param name="config">the configuration, or <code>null</code> for default</param>
/// <param name="ports">the ports to bind to</param>
public CoapServer(ICoapConfig config, params Int32[] ports)
: this(config, null, null, ports)
{
}
public CoapServer(ICoapConfig config, System.Net.EndPoint endPoint, params Int32[] ports)
: this(config, null, endPoint, ports)
{
}
/// <summary>
/// Constructs a server with the specified configuration that
/// listens to the given ports.
/// </summary>
/// <param name="config">the configuration, or <code>null</code> for default</param>
/// <param name="rootResource">root resource object to use</param>
/// <param name="ports">the ports to bind to</param>
public CoapServer(ICoapConfig config, IResource rootResource, params int[] ports)
{
Config = config ?? CoapConfig.Default;
_root = rootResource ?? new RootResource(this);
_deliverer = new ServerMessageDeliverer(Config, _root);
Resource wellKnown = new Resource(".well-known", false);
wellKnown.Add(new DiscoveryResource(_root));
_root.Add(wellKnown);
foreach (int port in ports) {
Bind(port);
}
}
public CoapServer(ICoapConfig config, IResource rootResource, System.Net.EndPoint endPoint, params int[] ports)
{
Config = config ?? CoapConfig.Default;
_root = rootResource ?? new RootResource(this);
_deliverer = new ServerMessageDeliverer(Config, _root);
Resource wellKnown = new Resource(".well-known", false);
wellKnown.Add(new DiscoveryResource(_root));
_root.Add(wellKnown);
_endPointSupplied = endPoint;
foreach (int port in ports) {
Bind(port);
}
}
/// <summary>
/// Return the configuration interface used by the server.
/// </summary>
public ICoapConfig Config { get; }
private void Bind(Int32 port)
{
if (_endPointSupplied != null) {
AddEndPoint(new CoAPEndPoint(new System.Net.IPEndPoint(((System.Net.IPEndPoint) _endPointSupplied).Address, port), Config));
}
else {
AddEndPoint(new CoAPEndPoint(port, Config));
}
}
/// <inheritdoc/>
public IEnumerable<IEndPoint> EndPoints
{
get => _endpoints;
}
/// <summary>
/// Get/Set the message deliverer object to use.
/// </summary>
public IMessageDeliverer MessageDeliverer
{
get => _deliverer;
set
{
_deliverer = value;
foreach (IEndPoint endpoint in _endpoints) {
endpoint.MessageDeliverer = value;
}
}
}
/// <inheritdoc/>
public void AddEndPoint(IEndPoint endpoint)
{
endpoint.MessageDeliverer = _deliverer;
endpoint.SecurityContexts = SecurityContexts;
_endpoints.Add(endpoint);
}
/// <inheritdoc/>
public void AddEndPoint(IPEndPoint ep)
{
AddEndPoint(new CoAPEndPoint(ep, Config));
}
/// <inheritdoc/>
public void AddEndPoint(IPAddress address, Int32 port)
{
AddEndPoint(new CoAPEndPoint(new IPEndPoint(address, port), Config));
}
#if !NETSTANDARD1_3
public void AddMulticastAddress(IPEndPoint ep)
{
foreach (IEndPoint endpoint in _endpoints) {
if (endpoint.AddMulticastAddress(ep)) {
return;
}
}
throw new CoAPException("No UDP Endpoint exists.");
}
#endif
/// <inheritdoc/>
public IEndPoint FindEndPoint(System.Net.EndPoint ep)
{
foreach (IEndPoint endpoint in _endpoints) {
if (endpoint.LocalEndPoint.Equals(ep)) {
return endpoint;
}
}
return null;
}
/// <inheritdoc/>
public IEndPoint FindEndPoint(Int32 port)
{
foreach (IEndPoint endpoint in _endpoints) {
if (((IPEndPoint) endpoint.LocalEndPoint).Port == port) {
return endpoint;
}
}
return null;
}
/// <inheritdoc/>
public IServer Add(IResource resource)
{
_root.Add(resource);
return this;
}
/// <summary>
/// Add a resource using the path node as the parent
/// </summary>
/// <param name="resourcePath">path to parent node</param>
/// <param name="resource">resource to add</param>
/// <returns>current server</returns>
public IServer Add(string resourcePath, IResource resource)
{
IResource parent = FindResource(resourcePath);
if (parent == null) throw new ArgumentException("resource path not found", nameof(resourcePath));
parent.Add(resource);
return this;
}
/// <inheritdoc/>
public IServer Add(params IResource[] resources)
{
foreach (IResource resource in resources) {
_root.Add(resource);
}
return this;
}
/// <summary>
/// Add resources as children of the given path
/// </summary>
/// <param name="resourcePath">path of parent</param>
/// <param name="resources">Array of resources to be added</param>
/// <returns>the server</returns>
public IServer Add(string resourcePath, IResource[] resources)
{
IResource parent = FindResource(resourcePath);
if (parent == null) {
throw new ArgumentException("resource path not found", nameof(resourcePath));
}
foreach (IResource resource in resources) {
parent.Add(resource);
}
return this;
}
/// <summary>
/// Return a resource from the server given a path to the resource
/// </summary>
/// <param name="resourcePath">path to follow</param>
/// <returns>interface to resource</returns>
public IResource FindResource(string resourcePath)
{
string[] pathStrings = resourcePath.Split(',');
IResource resource = _root;
foreach (string path in pathStrings) {
if (string.IsNullOrEmpty(path)) continue;
resource = resource.GetChild(path);
if (resource == null) return null;
}
return resource;
}
/// <inheritdoc/>
public Boolean Remove(IResource resource)
{
return _root.Remove(resource);
}
/// <inheritdoc/>
public void Start()
{
_Log.Debug("Starting CoAP server");
if (_endpoints.Count == 0) {
Bind(Config.DefaultPort);
}
Int32 started = 0;
foreach (IEndPoint endpoint in _endpoints) {
try {
endpoint.Start();
started++;
}
catch (Exception e) {
if (_Log.IsWarnEnabled) _Log.Warn("Could not start endpoint " + endpoint.LocalEndPoint, e);
}
}
if (started == 0) {
throw new InvalidOperationException("None of the server's endpoints could be started");
}
}
/// <inheritdoc/>
public void Stop()
{
_Log.Debug("Stopping CoAP server");
_endpoints.ForEach(ep => ep.Stop());
}
/// <inheritdoc/>
public void Dispose()
{
_endpoints.ForEach(ep => ep.Dispose());
}
class RootResource : Resource
{
public RootResource(CoapServer server)
: base(string.Empty)
{
}
protected override void DoGet(CoapExchange exchange)
{
exchange.Respond("Ni Hao from Com.AugustCellars.CoAP " + Spec.Name);
}
}
}
}