コード例 #1
0
 public CampfireNetClient(Identity identity, IBluetoothAdapter bluetoothAdapter, BroadcastMessageSerializer broadcastMessageSerializer, ClientMerkleTreeFactory merkleTreeFactory)
 {
     this.identity                   = identity;
     this.bluetoothAdapter           = bluetoothAdapter;
     this.broadcastMessageSerializer = broadcastMessageSerializer;
     this.merkleTreeFactory          = merkleTreeFactory;
     this.localMerkleTree            = merkleTreeFactory.CreateForLocal();
 }
コード例 #2
0
        public static void Main()
        {
//         // Generate root pk
//         var rsa = new RSACryptoServiceProvider(CryptoUtil.ASYM_KEY_SIZE_BITS);
//		   var bytes = __HackPrivateKeyUtilities.SerializePrivateKey(rsa);
//         Console.WriteLine($"new byte[] {{ {string.Join(", ", bytes)} }}");

            var rootRsa      = __HackPrivateKeyUtilities.DeserializePrivateKey(__HackPrivateKeyUtilities.__HACK_ROOT_PRIVATE_KEY);
            var rootIdentity = new Identity(rootRsa, new IdentityManager(), "hack_root");

            rootIdentity.GenerateRootChain();

//         Console.WriteLine("Enter key to begin");
            Console.ReadLine();
            Console.Clear();

            using (var adapter = new WindowsBluetoothAdapter()) {
                var broadcastMessageSerializer = new BroadcastMessageSerializer();
                var objectStore = new InMemoryCampfireNetObjectStore();
                //            var objectStore = new FileSystemCampfireNetObjectStore(Path.Combine(Directory.GetParent(Assembly.GetExecutingAssembly().Location).FullName, "demo_store"));

                var identity = new Identity(new IdentityManager(), "Windows_Client");
                identity.AddTrustChain(rootIdentity.GenerateNewChain(identity.PublicIdentity, Permission.None, Permission.None, identity.Name));
                Console.WriteLine($"I am {string.Join(" > ", identity.TrustChain.Select(n => n.ThisId.ToHexString()))}");

                var clientMerkleTreeFactory = new ClientMerkleTreeFactory(broadcastMessageSerializer, objectStore);
                var client = new CampfireNetClient(identity, adapter, broadcastMessageSerializer, clientMerkleTreeFactory);
                client.RunAsync().Forget();

                client.MessageReceived += e => {
                    var s = Encoding.UTF8.GetString(e.Message.DecryptedPayload, 0, e.Message.DecryptedPayload.Length);
                    DebugConsole.WriteLine(new string(' ', Console.BufferWidth - 1), ConsoleColor.White, ConsoleColor.Red);
                    DebugConsole.WriteLine(("RECV: " + s).PadRight(Console.BufferWidth - 1), ConsoleColor.White, ConsoleColor.Red);
                    DebugConsole.WriteLine(new string(' ', Console.BufferWidth - 1), ConsoleColor.White, ConsoleColor.Red);
                };

                Console.WriteLine("My adapter id is: " + adapter.AdapterId + " AKA " + string.Join(" ", adapter.AdapterId.ToByteArray()));
                while (true)
                {
                    var line = Console.ReadLine();
                    client.BroadcastAsync(Encoding.UTF8.GetBytes(line)).Forget();
                }

                new ManualResetEvent(false).WaitOne();
            }
        }
コード例 #3
0
        public CampfireNetClient Build()
        {
            if (bluetoothAdapter == null)
            {
                throw new InvalidStateException($"{nameof(bluetoothAdapter)} Null");
            }
            if (identity == null)
            {
                identity = new Identity(new IdentityManager(), null);
            }
            var broadcastMessageSerializer = new BroadcastMessageSerializer();
            var objectStore             = new InMemoryCampfireNetObjectStore();
            var clientMerkleTreeFactory = new ClientMerkleTreeFactory(broadcastMessageSerializer, objectStore);
            var client = new CampfireNetClient(identity, bluetoothAdapter, broadcastMessageSerializer, clientMerkleTreeFactory);

            return(client);
        }
コード例 #4
0
        public NeighborConnectionContext(
            Identity identity,
            IBluetoothAdapter bluetoothAdapter,
            IBluetoothNeighbor neighbor,
            BroadcastMessageSerializer broadcastMessageSerializer,
            MerkleTree <BroadcastMessageDto> localMerkleTree,
            MerkleTree <BroadcastMessageDto> remoteMerkleTree
            )
        {
            this.haveChannel  = new DisconnectableChannel <HavePacket, NotConnectedException>(disconnectLatchChannel, ChannelFactory.Nonblocking <HavePacket>());
            this.needChannel  = new DisconnectableChannel <NeedPacket, NotConnectedException>(disconnectLatchChannel, ChannelFactory.Nonblocking <NeedPacket>());
            this.giveChannel  = new DisconnectableChannel <GivePacket, NotConnectedException>(disconnectLatchChannel, ChannelFactory.Nonblocking <GivePacket>());
            this.whoisChannel = new DisconnectableChannel <WhoisPacket, NotConnectedException>(disconnectLatchChannel, ChannelFactory.Nonblocking <WhoisPacket>());
            this.identChannel = new DisconnectableChannel <IdentPacket, NotConnectedException>(disconnectLatchChannel, ChannelFactory.Nonblocking <IdentPacket>());
            this.doneChannel  = new DisconnectableChannel <DonePacket, NotConnectedException>(disconnectLatchChannel, ChannelFactory.Nonblocking <DonePacket>());

            this.identity                   = identity;
            this.bluetoothAdapter           = bluetoothAdapter;
            this.neighbor                   = neighbor;
            this.broadcastMessageSerializer = broadcastMessageSerializer;
            this.localMerkleTree            = localMerkleTree;
            this.remoteMerkleTree           = remoteMerkleTree;
        }
コード例 #5
0
 public ClientMerkleTreeFactory(BroadcastMessageSerializer broadcastMessageSerializer, ICampfireNetObjectStore objectStore)
 {
     this.broadcastMessageSerializer = broadcastMessageSerializer;
     this.objectStore = objectStore;
 }
コード例 #6
0
        private static DeviceAgent[] ConstructAgents(SimulatorConfiguration configuration)
        {
            var random = new Random(2);

            var agents = new DeviceAgent[configuration.AgentCount];

            for (int i = 0; i < agents.Length; i++)
            {
                agents[i] = new DeviceAgent {
                    BluetoothAdapterId = Guid.NewGuid(),
                    Position           = new Vector2(
                        random.Next(configuration.AgentRadius, configuration.FieldWidth - configuration.AgentRadius),
                        random.Next(configuration.AgentRadius, configuration.FieldHeight - configuration.AgentRadius)
                        ),
                    Velocity = Vector2.Transform(new Vector2(10, 0), Matrix.CreateRotationZ((float)(random.NextDouble() * Math.PI * 2)))
                };
            }

            agents[0].Position = new Vector2(300, 300);
            agents[1].Position = new Vector2(310, 300);

            var agentsPerRow = (4 * (int)Math.Sqrt(agents.Length)) / 3;
            var hSPacing     = 60;
            var vSPacing     = 60;
            var gw           = agentsPerRow * hSPacing;
            var gh           = (agents.Length / agentsPerRow) * vSPacing;
            var ox           = (configuration.FieldWidth - gw) / 2;
            var oy           = (configuration.FieldHeight - gh) / 2;

            for (var i = 0; i < agents.Length; i++)
            {
                agents[i].Position = new Vector2((i % agentsPerRow) * hSPacing + ox, (i / agentsPerRow) * vSPacing + oy);
            }

            for (var i = 0; i < agents.Length; i++)
            {
                agents[i].Position += agents[i].Velocity * 10;
                agents[i].Velocity  = Vector2.Zero;
            }

            var agentIndexToNeighborsByAdapterId = Enumerable.Range(0, agents.Length).ToDictionary(
                i => i,
                i => new Dictionary <Guid, SimulationBluetoothAdapter.SimulationBluetoothNeighbor>());

            for (int i = 0; i < agents.Length; i++)
            {
                var agent            = agents[i];
                var bluetoothAdapter = agent.BluetoothAdapter = new SimulationBluetoothAdapter(agent, agentIndexToNeighborsByAdapterId[i]);
                agent.BluetoothAdapter.Permit(SimulationBluetoothAdapter.MAX_RATE_LIMIT_TOKENS * (float)random.NextDouble());

                var broadcastMessageSerializer = new BroadcastMessageSerializer();
                var merkleTreeFactory          = new ClientMerkleTreeFactory(broadcastMessageSerializer, new InMemoryCampfireNetObjectStore());
                var identity = agent.CampfireNetIdentity = (Identity) new Identity(new IdentityManager(), $"Agent_{i}");
                if (i == 0 || i == 1)
                {
                    agent.CampfireNetIdentity.GenerateRootChain();
                }
                else
                {
                    var rootAgent = agents[i % 2];
                    agent.CampfireNetIdentity.AddTrustChain(rootAgent.CampfireNetIdentity.GenerateNewChain(identity.PublicIdentity, Permission.All, Permission.None, identity.Name));
                }

                var client = agent.Client = new CampfireNetClient(identity, bluetoothAdapter, broadcastMessageSerializer, merkleTreeFactory);
                client.MessageReceived += e => {
                    var epoch = BitConverter.ToInt32(e.Message.DecryptedPayload, 0);
//               Console.WriteLine($"{client.AdapterId:n} recv {epoch}");
                    agent.Value = Math.Max(agent.Value, epoch);
                };
                client.RunAsync().ContinueWith(task => {
                    if (task.IsFaulted)
                    {
                        Console.WriteLine(task.Exception);
                    }
                });
            }

            return(agents);
        }