public void SaveSubscription(Subscription subscription, TObjectState state) { ServiceEventMessage message = new ServiceEventMessage(); message.AddParameter("SubscriptionID", StringUtils.GuidEncode(subscription.SubscriptionID)); switch (state) { case TObjectState.Add: DataAccessFactory.Subscriptions.SaveSubscription(subscription, state); BusinessLogicFactory.ServiceMessages.Publish(RouteKeys.SUBSCRIPTION_CREATE, message, TMessagePublishMode.Confirms); break; case TObjectState.Update: DataAccessFactory.Subscriptions.SaveSubscription(subscription, state); BusinessLogicFactory.ServiceMessages.Publish(RouteKeys.SUBSCRIPTION_UPDATE, message, TMessagePublishMode.Confirms); break; case TObjectState.Delete: BusinessLogicFactory.ServiceMessages.Publish(RouteKeys.SUBSCRIPTION_DELETE, message, TMessagePublishMode.Confirms); break; default: DataAccessFactory.Subscriptions.SaveSubscription(subscription, state); break; } }
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 static void Main(string[] args) { try { int workerThreads; int completionPortThreads; System.Threading.ThreadPool.GetMinThreads(out workerThreads, out completionPortThreads); if (workerThreads < 16) { workerThreads = 16; System.Threading.ThreadPool.SetMinThreads(workerThreads, completionPortThreads); } IConfigurationBuilder builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .AddCommandLine(args); IConfigurationRoot configuration = builder.Build(); ServiceConfiguration.LoadConfig(configuration.GetSection("ServiceConfiguration")); int port = 15683; bool secureOnly = true; IConfigurationSection sectionBootstrap = configuration.GetSection("LWM2MBootstrap"); if (sectionBootstrap != null) { IConfigurationSection sectionPort = sectionBootstrap.GetSection("Port"); if (sectionPort != null) { if (!int.TryParse(sectionPort.Value, out port)) port = 15683; } IConfigurationSection sectionSecure = sectionBootstrap.GetSection("SecureOnly"); if (sectionSecure != null) { if (!bool.TryParse(sectionSecure.Value, out secureOnly)) secureOnly = true; } } Version version = Assembly.GetExecutingAssembly().GetName().Version; Console.Write("LWM2M Bootstrap ("); Console.Write(version.ToString()); Console.WriteLine(")"); if (ServiceConfiguration.ExternalUri == null) { ServiceConfiguration.ExternalUri = new Uri(string.Concat("coaps://", ServiceConfiguration.Hostname, ":", (port+1).ToString())); } ServiceConfiguration.DisplayConfig(); BusinessLogicFactory.Initialise(); BootstrapServer bootstrapServer = new BootstrapServer(); //bootstrapServer.PSKIdentities.LoadFromFile("PSKIdentities.xml"); bootstrapServer.Port = port; bootstrapServer.SecureOnly = secureOnly; bootstrapServer.Start(); ServiceEventMessage message = new ServiceEventMessage(); Imagination.Model.BootstrapServer bootstrap = new Imagination.Model.BootstrapServer(); bootstrap.Url = ServiceConfiguration.ExternalUri.ToString(); //PSKIdentity pskIdentity = new PSKIdentity(); //pskIdentity.Identity = "Test1"; //pskIdentity.Secret = "TestSecret"; //bootstrap.AddServerIdentity(pskIdentity); message.AddParameter("BootstrapServer", bootstrap); BusinessLogicFactory.ServiceMessages.Publish("Bootstrap.Start", message, TMessagePublishMode.Confirms); _ShutdownEvent = new ManualResetEvent(false); Console.CancelKeyPress += delegate (object sender, ConsoleCancelEventArgs e) { _ShutdownEvent.Set(); e.Cancel = true; }; Console.Write("Listening on port "); Console.WriteLine(port.ToString()); Console.WriteLine("Press Ctrl+C to stop the server."); _ShutdownEvent.WaitOne(); Console.WriteLine("Exiting."); bootstrapServer.Stop(); BusinessLogicFactory.ServiceMessages.Stop(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }
private static void NotifySubscriber(Guid clientID, Subscription subscription, Model.Object changedObject) { #if DEBUG Console.WriteLine("Publishing Subscription.Webhook message for subscription " + subscription.SubscriptionID); #endif ServiceEventMessage message = new ServiceEventMessage(); message.AddParameter("AcceptContentType", subscription.AcceptContentType); message.AddParameter("SubscriptionID", StringUtils.GuidEncode(subscription.SubscriptionID)); message.AddParameter("SubscriptionType", subscription.SubscriptionType.ToString()); message.AddParameter("ClientID", StringUtils.GuidEncode(clientID)); message.AddParameter("Url", subscription.Url); message.AddParameter("TimeTriggered", DateTime.Now); if (changedObject != null) message.AddParameter("Object", changedObject); BusinessLogicFactory.ServiceMessages.Publish(RouteKeys.SUBSCRIPTION_NOTIFICATION, message, TMessagePublishMode.Confirms); }