Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EdgeHub: Add option to turn off protocol heads #160

Merged
merged 2 commits into from
Aug 15, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,18 @@ namespace Microsoft.Azure.Devices.Edge.Hub.Service

public class Hosting
{
const int SslPortNumber = 443;

Hosting(IWebHost webHost, IContainer container)
{
this.WebHost = webHost;
this.Container = container;
}

public static Hosting Initialize()
public static Hosting Initialize(int port)
{
IWebHostBuilder webHostBuilder = new WebHostBuilder()
.UseKestrel(options =>
{
options.Listen(!Socket.OSSupportsIPv6 ? IPAddress.Any : IPAddress.IPv6Any, SslPortNumber, listenOptions =>
options.Listen(!Socket.OSSupportsIPv6 ? IPAddress.Any : IPAddress.IPv6Any, port, listenOptions =>
{
listenOptions.UseHttps(ServerCertificateCache.X509Certificate);
});
Expand Down
28 changes: 20 additions & 8 deletions edge-hub/src/Microsoft.Azure.Devices.Edge.Hub.Service/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ public static async Task<int> MainAsync(IConfigurationRoot configuration)

// TODO: set certificate for Startup without the cache
ServerCertificateCache.X509Certificate = cert;
Hosting hosting = Hosting.Initialize();

int port = configuration.GetValue("httpSettings:port", 443);
Hosting hosting = Hosting.Initialize(port);

IContainer container = hosting.Container;

Expand Down Expand Up @@ -111,13 +113,23 @@ public static async Task<int> MainAsync(IConfigurationRoot configuration)
(CancellationTokenSource cts, ManualResetEventSlim completed, Option<object> handler)
= ShutdownHandler.Init(ShutdownWaitPeriod, logger);

using (IProtocolHead protocolHead = new EdgeHubProtocolHead(
new IProtocolHead[]
{
new HttpProtocolHead(hosting.WebHost),
await container.Resolve<Task<MqttProtocolHead>>(),
await container.Resolve<Task<AmqpProtocolHead>>()
}, logger))
var protocolHeads = new List<IProtocolHead>();
if (configuration.GetValue("mqttSettings:enabled", true))
{
protocolHeads.Add(await container.Resolve<Task<MqttProtocolHead>>());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This turned out really nice!

}

if (configuration.GetValue("amqpSettings:enabled", true))
{
protocolHeads.Add(await container.Resolve<Task<AmqpProtocolHead>>());
}

if (configuration.GetValue("httpSettings:enabled", true))
{
protocolHeads.Add(new HttpProtocolHead(hosting.WebHost));
}

using (IProtocolHead protocolHead = new EdgeHubProtocolHead(protocolHeads, logger))
{
await protocolHead.StartAsync();
await cts.Token.WhenCanceled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ IContainer BuildContainer(IServiceCollection services)
var routes = this.Configuration.GetSection("routes").Get<Dictionary<string, string>>();
(bool isEnabled, bool usePersistentStorage, StoreAndForwardConfiguration config, string storagePath) storeAndForward = this.GetStoreAndForwardConfiguration();

IConfiguration mqttSettingsConfiguration = this.Configuration.GetSection("appSettings");
IConfiguration mqttSettingsConfiguration = this.Configuration.GetSection("mqttSettings");
Option<UpstreamProtocol> upstreamProtocolOption = Enum.TryParse(this.Configuration.GetValue("UpstreamProtocol", string.Empty), false, out UpstreamProtocol upstreamProtocol)
? Option.Some(upstreamProtocol)
: Option.None<UpstreamProtocol>();
Expand All @@ -116,7 +116,7 @@ IContainer BuildContainer(IServiceCollection services)
// n Clients + 1 Edgehub
int maxConnectedClients = this.Configuration.GetValue("MaxConnectedClients", 100) + 1;

IConfiguration amqpSettings = this.Configuration.GetSection("amqp");
IConfiguration amqpSettings = this.Configuration.GetSection("amqpSettings");

var builder = new ContainerBuilder();
builder.Populate(services);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"appSettings": {
"mqttSettings": {
"enabled": true,
"MaxPendingInboundAcknowledgements": 16,
"DeviceReceiveAckTimeout": "00:00:00",
"MaxInboundMessageSize": "262144",
Expand All @@ -19,10 +20,15 @@
"TableQos2StatePersistenceProvider.StorageConnectionString": "UseDevelopmentStorage=true",
"TableQos2StatePersistenceProvider.StorageTableName": "mqttqos2"
},
"amqp": {
"amqpSettings": {
"enabled": true,
"scheme": "amqps",
"port": 5671
},
"httpSettings": {
"enabled": true,
"port": 443
},
"IotHubConnectionPoolSize": 1,
"IotHubConnectionString": "",
"mqttTopicNameConversion": {
Expand Down