public Server(LWM2MServer lwm2mServer) { Lifetime = lwm2mServer.Lifetime; DefaultMinimumPeriod = lwm2mServer.DefaultMinimumPeriod; DefaultMaximumPeriod = lwm2mServer.DefaultMaximumPeriod; DisableTimeout = lwm2mServer.DisableTimeout; NotificationStoringWhenOffline = lwm2mServer.NotificationStoringWhenOffline; Binding = lwm2mServer.Binding; EndPoints = new List<Security>(); if (!string.IsNullOrEmpty(lwm2mServer.Url)) { Security security = new Security(); security.ServerURI = lwm2mServer.Url; if (lwm2mServer.Url.StartsWith("coaps")) { security.SecurityMode = TSecurityMode.Certificate; } else security.SecurityMode = TSecurityMode.NoSecurity; EndPoints.Add(security); } }
public Server(LWM2MServer lwm2mServer) { Lifetime = lwm2mServer.Lifetime; DefaultMinimumPeriod = lwm2mServer.DefaultMinimumPeriod; DefaultMaximumPeriod = lwm2mServer.DefaultMaximumPeriod; DisableTimeout = lwm2mServer.DisableTimeout; NotificationStoringWhenOffline = lwm2mServer.NotificationStoringWhenOffline; Binding = lwm2mServer.Binding; EndPoints = new List <Security>(); if (!string.IsNullOrEmpty(lwm2mServer.Url)) { Security security = new Security(); security.ServerURI = lwm2mServer.Url; if (lwm2mServer.Url.StartsWith("coaps")) { security.SecurityMode = TSecurityMode.Certificate; } else { security.SecurityMode = TSecurityMode.NoSecurity; } EndPoints.Add(security); } }
public void Start() { CoAP.Log.LogManager.Level = CoAP.Log.LogLevel.Error; int port; string apiPort = System.Configuration.ConfigurationManager.AppSettings["APIPort"]; if (!int.TryParse(apiPort, out port)) port = 14080; _ProcessRequestsThread = new Thread(new ThreadStart(ProcessRequests)); if (_ProcessRequestsThread.Name == null) _ProcessRequestsThread.Name = "ProcessRequestsThread"; _ProcessRequestsThread.IsBackground = true; _ProcessRequestsThread.Start(); if (_CoapServer == null) { _CoapServer = new CoapServer(); _CoapServer.MessageDeliverer = this; if (!SecureOnly) _CoapServer.AddEndPoint(new CoAPEndPoint(new FlowChannel(Port), CoapConfig.Default)); _SecureChannel = new FlowSecureChannel(Port + 1); if (System.IO.File.Exists("LWM2MServer.pem")) { _SecureChannel.CertificateFile = "LWM2MServer.pem"; } _SecureChannel.PSKIdentities = _PSKIdentities; _SecureChannel.SupportedCipherSuites.Add(TCipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8); _SecureChannel.SupportedCipherSuites.Add(TCipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256); _SecureChannel.SupportedCipherSuites.Add(TCipherSuite.TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256); _SecureChannel.SupportedCipherSuites.Add(TCipherSuite.TLS_PSK_WITH_AES_128_CCM_8); _SecureChannel.SupportedCipherSuites.Add(TCipherSuite.TLS_PSK_WITH_AES_128_CBC_SHA256); _SecureChannel.ValidatePSK += new EventHandler<ValidatePSKEventArgs>(ValidatePSK); _CoapServer.AddEndPoint(new CoAPEndPoint(_SecureChannel, CoapConfig.Default)); } _CoapServer.Start(); ServiceEventMessage message = new ServiceEventMessage(); Imagination.Model.LWM2MServer lwm2mServer = new Imagination.Model.LWM2MServer(); lwm2mServer.Url = ServiceConfiguration.ExternalUri.ToString(); message.AddParameter("Server", lwm2mServer); BusinessLogicFactory.ServiceMessages.Publish("LWM2MServer.Start", message, TMessagePublishMode.Confirms); _ServerEndPoint = string.Concat("net.tcp://", ServiceConfiguration.Hostname, ":", port.ToString(), "/LWM2MServerService"); if (_NativeServerAPI == null) _NativeServerAPI = new NativeIPCServer(AddressFamily.InterNetwork,port); _NativeServerAPI.Start(); if (_NativeServerAPIv6 == null) _NativeServerAPIv6 = new NativeIPCServer(AddressFamily.InterNetworkV6, port); _NativeServerAPIv6.Start(); //if (_ServiceHost != null) // _ServiceHost.Close(); //_ServiceHost = new ServiceHost(typeof(Imagination.Service.ServerAPI)); //ServiceThrottlingBehavior throttle = _ServiceHost.Description.Behaviors.Find<ServiceThrottlingBehavior>(); //if (throttle == null) //{ // throttle = new ServiceThrottlingBehavior // { // MaxConcurrentCalls = 100, // MaxConcurrentSessions = 100, // MaxConcurrentInstances = int.MaxValue // }; // _ServiceHost.Description.Behaviors.Add(throttle); //} //else //{ // throttle.MaxConcurrentCalls = 100; // throttle.MaxConcurrentSessions = 100; // throttle.MaxConcurrentInstances = int.MaxValue; //} //NetTcpBinding netTcpBinding = new NetTcpBinding(); //_ServiceHost.AddServiceEndpoint(typeof(Imagination.Service.ILWM2MServerService), netTcpBinding, _ServerEndPoint); ////int newLimit = _ServiceHost.IncrementManualFlowControlLimit(100); //_ServiceHost.Open(); }
public void Start() { CoAP.Log.LogManager.Level = CoAP.Log.LogLevel.Error; int port; string apiPort = System.Configuration.ConfigurationManager.AppSettings["APIPort"]; if (!int.TryParse(apiPort, out port)) { port = 14080; } _ProcessRequestsThread = new Thread(new ThreadStart(ProcessRequests)); if (_ProcessRequestsThread.Name == null) { _ProcessRequestsThread.Name = "ProcessRequestsThread"; } _ProcessRequestsThread.IsBackground = true; _ProcessRequestsThread.Start(); if (_CoapServer == null) { _CoapServer = new CoapServer(); _CoapServer.MessageDeliverer = this; if (!SecureOnly) { _CoapServer.AddEndPoint(new CoAPEndPoint(new FlowChannel(Port), CoapConfig.Default)); } _SecureChannel = new FlowSecureChannel(Port + 1); if (System.IO.File.Exists("LWM2MServer.pem")) { _SecureChannel.CertificateFile = "LWM2MServer.pem"; } _SecureChannel.PSKIdentities = _PSKIdentities; _SecureChannel.SupportedCipherSuites.Add(TCipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8); _SecureChannel.SupportedCipherSuites.Add(TCipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256); _SecureChannel.SupportedCipherSuites.Add(TCipherSuite.TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256); _SecureChannel.SupportedCipherSuites.Add(TCipherSuite.TLS_PSK_WITH_AES_128_CCM_8); _SecureChannel.SupportedCipherSuites.Add(TCipherSuite.TLS_PSK_WITH_AES_128_CBC_SHA256); _SecureChannel.ValidatePSK += new EventHandler <ValidatePSKEventArgs>(ValidatePSK); _CoapServer.AddEndPoint(new CoAPEndPoint(_SecureChannel, CoapConfig.Default)); } _CoapServer.Start(); ServiceEventMessage message = new ServiceEventMessage(); Imagination.Model.LWM2MServer lwm2mServer = new Imagination.Model.LWM2MServer(); lwm2mServer.Url = ServiceConfiguration.ExternalUri.ToString(); message.AddParameter("Server", lwm2mServer); BusinessLogicFactory.ServiceMessages.Publish("LWM2MServer.Start", message, TMessagePublishMode.Confirms); _ServerEndPoint = string.Concat("net.tcp://", ServiceConfiguration.Hostname, ":", port.ToString(), "/LWM2MServerService"); if (_NativeServerAPI == null) { _NativeServerAPI = new NativeIPCServer(AddressFamily.InterNetwork, port); } _NativeServerAPI.Start(); if (_NativeServerAPIv6 == null) { _NativeServerAPIv6 = new NativeIPCServer(AddressFamily.InterNetworkV6, port); } _NativeServerAPIv6.Start(); //if (_ServiceHost != null) // _ServiceHost.Close(); //_ServiceHost = new ServiceHost(typeof(Imagination.Service.ServerAPI)); //ServiceThrottlingBehavior throttle = _ServiceHost.Description.Behaviors.Find<ServiceThrottlingBehavior>(); //if (throttle == null) //{ // throttle = new ServiceThrottlingBehavior // { // MaxConcurrentCalls = 100, // MaxConcurrentSessions = 100, // MaxConcurrentInstances = int.MaxValue // }; // _ServiceHost.Description.Behaviors.Add(throttle); //} //else //{ // throttle.MaxConcurrentCalls = 100; // throttle.MaxConcurrentSessions = 100; // throttle.MaxConcurrentInstances = int.MaxValue; //} //NetTcpBinding netTcpBinding = new NetTcpBinding(); //_ServiceHost.AddServiceEndpoint(typeof(Imagination.Service.ILWM2MServerService), netTcpBinding, _ServerEndPoint); ////int newLimit = _ServiceHost.IncrementManualFlowControlLimit(100); //_ServiceHost.Open(); }
public List<Server> GetServers() { List<Server> result = _CachedLWM2MServers; if (result == null) { lock (this) { result = _CachedLWM2MServers; if (result == null) { result = new List<Server>(); IMongoDatabase database = GetDatabase(DATABASE_NAME, false); IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>("Server"); IAsyncCursor<BsonDocument> mongoCursor = collection.FindSync(new BsonDocument()); while (mongoCursor.MoveNext()) { foreach (BsonDocument item in mongoCursor.Current) { LWM2MServer lwm2mServer = new LWM2MServer(); lwm2mServer.Url = BsonHelper.GetString(item, "_id"); lwm2mServer.Lifetime = (uint)BsonHelper.GetInt64(item, "Lifetime"); lwm2mServer.DefaultMinimumPeriod = (uint?)BsonHelper.GetLong(item, "DefaultMinimumPeriod"); lwm2mServer.DefaultMaximumPeriod = (uint?)BsonHelper.GetLong(item, "DefaultMaximumPeriod"); lwm2mServer.DisableTimeout = (uint?)BsonHelper.GetLong(item, "DisableTimeout"); lwm2mServer.NotificationStoringWhenOffline = BsonHelper.GetBoolean(item, "NotificationStoringWhenOffline"); lwm2mServer.Binding = (TBindingMode)BsonHelper.GetInt32(item, "Binding"); if (item.Contains("ServerIdentities")) { BsonArray array = item["ServerIdentities"].AsBsonArray; foreach (BsonValue arrayItem in array) { BsonDocument pskIdentityDoc = arrayItem.AsBsonDocument; if (pskIdentityDoc != null) { PSKIdentity pskIdentity = new PSKIdentity(); pskIdentity.Identity = BsonHelper.GetString(pskIdentityDoc, "_id"); pskIdentity.Secret = BsonHelper.GetString(pskIdentityDoc, "Secret"); lwm2mServer.AddServerIdentity(pskIdentity); } } } if (item.Contains("ServerCertificate")) { BsonDocument serverCertificateDoc = item["ServerCertificate"].AsBsonDocument; if (serverCertificateDoc != null) { lwm2mServer.ServerCertificate = new Certificate(); lwm2mServer.ServerCertificate.CertificateFormat = (TCertificateFormat)BsonHelper.GetInt32(serverCertificateDoc, "_id"); lwm2mServer.ServerCertificate.RawCertificate = BsonHelper.GetString(serverCertificateDoc, "RawCertificate"); } } Server server = new Server(lwm2mServer); server.ShortServerID = result.Count + 1; foreach (Model.Security endPoint in server.EndPoints) { endPoint.ShortServerID = server.ShortServerID; } #if DEBUG if (lwm2mServer.Url.ToLower().Contains(Environment.MachineName.ToLower())) { result.Add(server); } #else result.Add(server); #endif } } _CachedLWM2MServers = result; } } } return result; }
public void SaveLWM2MServer(LWM2MServer lwm2mServer, TObjectState state) { IMongoDatabase database = GetDatabase(DATABASE_NAME, true); IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>("Server"); FilterDefinition<BsonDocument> query = Builders<BsonDocument>.Filter.Eq("_id", lwm2mServer.Url); if ((state == TObjectState.Add) || (state == TObjectState.Update)) { BsonDocument doc = new BsonDocument(); BsonHelper.SetValue(doc, "_id", lwm2mServer.Url); BsonHelper.SetValue(doc, "Lifetime", lwm2mServer.Lifetime); BsonHelper.SetValue(doc, "DefaultMinimumPeriod", lwm2mServer.DefaultMinimumPeriod); BsonHelper.SetValue(doc, "DefaultMaximumPeriod", lwm2mServer.DefaultMaximumPeriod); BsonHelper.SetValue(doc, "DisableTimeout", lwm2mServer.DisableTimeout); BsonHelper.SetValue(doc, "NotificationStoringWhenOffline", lwm2mServer.NotificationStoringWhenOffline); BsonHelper.SetValue(doc, "Binding", (int)lwm2mServer.Binding); if (lwm2mServer.ServerIdentities != null && lwm2mServer.ServerIdentities.Count > 0) { BsonArray array = new BsonArray(); foreach (PSKIdentity pskIdentity in lwm2mServer.ServerIdentities) { BsonDocument pskIdentityDoc = new BsonDocument(); BsonHelper.SetValue(pskIdentityDoc, "_id", pskIdentity.Identity); BsonHelper.SetValue(pskIdentityDoc, "Secret", pskIdentity.Secret); array.Add(pskIdentityDoc); } doc.Add("ServerIdentities", array); } if (lwm2mServer.ServerCertificate != null) { BsonDocument serverCertificateDoc = new BsonDocument(); BsonHelper.SetValue(serverCertificateDoc, "_id", (int)lwm2mServer.ServerCertificate.CertificateFormat); BsonHelper.SetValue(serverCertificateDoc, "RawCertificate", lwm2mServer.ServerCertificate.RawCertificate); doc.Add("ServerCertificate", serverCertificateDoc); } UpdateOptions options = new UpdateOptions(); options.IsUpsert = true; collection.ReplaceOne(query, doc, options); } else if (state == TObjectState.Delete) { collection.DeleteOne(query); } BroadcastTableChange("LWM2MServer", string.Empty); }