public void AllocateBootstrapServer(int organisationID, BootstrapServer bootstrapServer) { IMongoDatabase database = GetDatabase(DATABASE_NAME, true); IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>("OrganisationBootstrapServer"); FilterDefinition<BsonDocument> query = Builders<BsonDocument>.Filter.Eq("_id", organisationID); BsonDocument doc = new BsonDocument(); BsonHelper.SetValue(doc, "_id", organisationID); BsonHelper.SetValue(doc, "Url", bootstrapServer.Url); UpdateOptions options = new UpdateOptions(); options.IsUpsert = true; collection.ReplaceOne(query, doc, options); }
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 section = configuration.GetSection("LWM2MBootstrap"); if (section != null) { section = section.GetSection("Port"); if (section != null) { if (!int.TryParse(section.Value, out port)) { port = 15683; } } section = section.GetSection("SecureOnly"); if (section != null) { if (!bool.TryParse(section.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()); } }
public List<BootstrapServer> GetBootstrapServers() { List<BootstrapServer> result = _CachedBootstrapServers; if (result == null) { lock (this) { result = _CachedBootstrapServers; if (result == null) { result = new List<BootstrapServer>(); IMongoDatabase database = GetDatabase(DATABASE_NAME, false); IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>("BootstrapServer"); IAsyncCursor<BsonDocument> mongoCursor = collection.FindSync(new BsonDocument()); while (mongoCursor.MoveNext()) { foreach (BsonDocument item in mongoCursor.Current) { BootstrapServer bootstrapServer = new BootstrapServer(); bootstrapServer.Url = BsonHelper.GetString(item, "_id"); 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"); bootstrapServer.AddServerIdentity(pskIdentity); } } } if (item.Contains("ServerCertificate")) { BsonDocument serverCertificateDoc = item["ServerCertificate"].AsBsonDocument; if (serverCertificateDoc != null) { bootstrapServer.ServerCertificate = new Certificate(); bootstrapServer.ServerCertificate.CertificateFormat = (TCertificateFormat)BsonHelper.GetInt32(serverCertificateDoc, "_id"); bootstrapServer.ServerCertificate.RawCertificate = BsonHelper.GetString(serverCertificateDoc, "RawCertificate"); } } result.Add(bootstrapServer); } } _CachedBootstrapServers = result; } } } return result; }
public void SaveBootstrapServer(BootstrapServer bootstrapServer, TObjectState state) { IMongoDatabase database = GetDatabase(DATABASE_NAME, true); IMongoCollection<BsonDocument> collection = database.GetCollection<BsonDocument>("BootstrapServer"); FilterDefinition<BsonDocument> query = Builders<BsonDocument>.Filter.Eq("_id", bootstrapServer.Url); if ((state == TObjectState.Add) || (state == TObjectState.Update)) { BsonDocument doc = new BsonDocument(); BsonHelper.SetValue(doc, "_id", bootstrapServer.Url); if (bootstrapServer.ServerIdentities != null && bootstrapServer.ServerIdentities.Count > 0) { BsonArray array = new BsonArray(); foreach (PSKIdentity pskIdentity in bootstrapServer.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 (bootstrapServer.ServerCertificate != null) { BsonDocument serverCertificateDoc = new BsonDocument(); BsonHelper.SetValue(serverCertificateDoc, "_id", (int)bootstrapServer.ServerCertificate.CertificateFormat); BsonHelper.SetValue(serverCertificateDoc, "RawCertificate", bootstrapServer.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("BootstrapServer", string.Empty); }
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()); } }