Using MAC Address with FlakeGen.IdGuidGenerator

FlakeGen.IdGuidGenerator generator can get it's unique number by specifying 48-bit id which gives 281,474,976,710,656 unique generators. One of the scenarios is to use MAC address of the system hosting a generator.

Sample code (please note that the code fetching MAC Address may not be the best one):

namespace FlakeGenConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            MacAddressGenerator();
        }

        public static void MacAddressGenerator()
        {
            var mac = NetworkInterface.GetAllNetworkInterfaces()
                .Where(i => i.OperationalStatus == OperationalStatus.Up)
                .First().GetPhysicalAddress().GetAddressBytes();

            Console.WriteLine();
            Console.WriteLine(" == Guid ids with MAC Address identifier ({0}) ==",
                BitConverter.ToString(mac));

            if (BitConverter.IsLittleEndian)
                Array.Reverse(mac);

            var generator = new IdGuidGenerator(mac);

            foreach (var id in generator.Take(5).ToArray())
            {
                Console.WriteLine(id);
            }
        }
    }
}

Output:

 == Guid ids with MAC Address identifier (20-5A-B6-A5-BA-3D) ==
0004cdea-65ce-49bd-205a-b6a5ba3d0000
0004cdea-65ce-883d-205a-b6a5ba3d0000
0004cdea-65ce-8c25-205a-b6a5ba3d0000
0004cdea-65ce-8c25-205a-b6a5ba3d0001
0004cdea-65ce-8c25-205a-b6a5ba3d0002

Last edited Nov 8, 2012 at 11:34 AM by tompawlak, version 3

Comments

No comments yet.