Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Renamed NetworkIdProvider to WorkerIdProvider

  • Loading branch information...
commit 6605e2edd79dbbedb033c79d09cf08ccb602de14 1 parent f6130fa
@phatboyg phatboyg authored
View
2  src/NewId.Tests/LongTerm_Specs.cs
@@ -10,7 +10,7 @@ public class Generating_ids_over_time
[Test]
public void Should_keep_them_ordered_for_sql_server()
{
- var generator = new NewIdGenerator(new DefaultNetworkIdProvider(), new TimeLapseTickProvider());
+ var generator = new NewIdGenerator(new TimeLapseTickProvider(), new NetworkAddressWorkerIdProvider());
generator.Next();
int limit = 1024;
View
4 src/NewId.Tests/NetworkAddress_Specs.cs
@@ -9,9 +9,9 @@ public class When_getting_a_network_address_for_the_id_generator
[Test]
public void Should_pull_the_network_adapter_mac_address()
{
- var networkIdProvider = new DefaultNetworkIdProvider();
+ var networkIdProvider = new NetworkAddressWorkerIdProvider();
- byte[] networkId = networkIdProvider.NetworkId;
+ byte[] networkId = networkIdProvider.GetWorkerId(0);
Assert.IsNotNull(networkId);
Assert.AreEqual(6, networkId.Length);
View
7 src/NewId/NetworkIdProvider.cs
@@ -1,7 +0,0 @@
-namespace NewId
-{
- public interface NetworkIdProvider
- {
- byte[] NetworkId { get; }
- }
-}
View
12 src/NewId/NewId.cs
@@ -17,7 +17,7 @@ public struct NewId :
{
static NewId _empty;
static NewIdGenerator _generator;
- static NetworkIdProvider _networkIdProvider;
+ static WorkerIdProvider _workerIdProvider;
static TickProvider _tickProvider;
readonly Int32 _a;
@@ -66,12 +66,12 @@ public static NewId Empty
static NewIdGenerator Generator
{
- get { return _generator ?? (_generator = new NewIdGenerator(NetworkIdProvider, TickProvider)); }
+ get { return _generator ?? (_generator = new NewIdGenerator(TickProvider, WorkerIdProvider)); }
}
- static NetworkIdProvider NetworkIdProvider
+ static WorkerIdProvider WorkerIdProvider
{
- get { return _networkIdProvider ?? (_networkIdProvider = new DefaultNetworkIdProvider()); }
+ get { return _workerIdProvider ?? (_workerIdProvider = new NetworkAddressWorkerIdProvider()); }
}
static TickProvider TickProvider
@@ -257,9 +257,9 @@ public static void SetGenerator(NewIdGenerator generator)
_generator = generator;
}
- public static void SetNetworkIdProvider(NetworkIdProvider provider)
+ public static void SetWorkerIdProvider(WorkerIdProvider provider)
{
- _networkIdProvider = provider;
+ _workerIdProvider = provider;
}
public static void SetTickProvider(TickProvider provider)
View
4 src/NewId/NewId.csproj
@@ -35,13 +35,13 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
- <Compile Include="NetworkIdProvider.cs" />
+ <Compile Include="WorkerIdProvider.cs" />
<Compile Include="TickProvider.cs" />
<Compile Include="NewId.cs" />
<Compile Include="NewIdGenerator.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Providers\DateTimeTickProvider.cs" />
- <Compile Include="Providers\DefaultNetworkIdProvider.cs" />
+ <Compile Include="Providers\NetworkAddressWorkerIdProvider.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
View
11 src/NewId/NewIdGenerator.cs
@@ -6,6 +6,7 @@ public class NewIdGenerator
readonly object _sync = new object();
readonly TickProvider _tickProvider;
+ readonly int _workerIndex;
int _a;
int _b;
int _c;
@@ -15,15 +16,21 @@ public class NewIdGenerator
ushort _sequence;
- public NewIdGenerator(NetworkIdProvider networkIdProvider, TickProvider tickProvider)
+ public NewIdGenerator(TickProvider tickProvider, WorkerIdProvider workerIdProvider, int workerIndex)
{
- _networkId = networkIdProvider.NetworkId;
+ _workerIndex = workerIndex;
+ _networkId = workerIdProvider.GetWorkerId(_workerIndex);
_tickProvider = tickProvider;
_c = _networkId[0] << 24 | _networkId[1] << 16 | _networkId[2] << 8 | _networkId[3];
_d = _networkId[4] << 24 | _networkId[5] << 16;
}
+ public NewIdGenerator(TickProvider tickProvider, WorkerIdProvider workerIdProvider)
+ : this(tickProvider, workerIdProvider, 0)
+ {
+ }
+
public NewId Next()
{
ushort sequence;
View
17 ...wId/Providers/DefaultNetworkIdProvider.cs → ...oviders/NetworkAddressWorkerIdProvider.cs
@@ -5,17 +5,15 @@ namespace NewId.Providers
using System.Linq;
using System.Net.NetworkInformation;
- public class DefaultNetworkIdProvider :
- NetworkIdProvider
+ public class NetworkAddressWorkerIdProvider :
+ WorkerIdProvider
{
- byte[] _address;
-
- public byte[] NetworkId
+ public byte[] GetWorkerId(int index)
{
- get { return _address ?? (_address = GetNetworkAddress()); }
+ return GetNetworkAddress(index);
}
- static byte[] GetNetworkAddress()
+ static byte[] GetNetworkAddress(int index)
{
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
@@ -26,7 +24,10 @@ static byte[] GetNetworkAddress()
IEnumerable<NetworkInterface> wireless =
interfaces.Where(x => x.NetworkInterfaceType == NetworkInterfaceType.Wireless80211);
- NetworkInterface network = ethernet.Concat(gigabit).Concat(wireless).FirstOrDefault();
+ NetworkInterface network = ethernet.Concat(gigabit).Concat(wireless)
+ .Skip(index)
+ .FirstOrDefault();
+
if (network == null)
{
throw new InvalidOperationException("Unable to find usable network adapter for unique address");
View
7 src/NewId/WorkerIdProvider.cs
@@ -0,0 +1,7 @@
+namespace NewId
+{
+ public interface WorkerIdProvider
+ {
+ byte[] GetWorkerId(int index);
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.