Exemplo n.º 1
0
        public static async Task Run()
        {
            var dataPath = new FileInfo(Assembly.GetEntryAssembly().Location).DirectoryName;

            IItemOperations <int>   intOperations = new IntItemOperations();
            ICampfireNetObjectStore storeA        = new FileSystemCampfireNetObjectStore(Path.Combine(dataPath, "a"));
            ICampfireNetObjectStore storeB        = new InMemoryCampfireNetObjectStore(); // new FileSystemCampfireNetObjectStore(Path.Combine(dataPath, "b"));
            var a = new MerkleTree <int>("ns_a", intOperations, storeA);
            var b = new MerkleTree <int>("ns_b", intOperations, storeB);

            Go(async() => {
                while (true)
                {
                    await a.InsertAsync((int)DateTime.Now.ToFileTimeUtc());
//               await Task.Delay(100);
                }
            }).Forget();

            while (true)
            {
                var upstreamRootHash = await a.GetRootHashAsync();

                if (upstreamRootHash == null)
                {
                    continue;
                }

                var nodesToImport = new List <Tuple <string, MerkleNode> >();

                var s = new Stack <string>();
                s.Push(upstreamRootHash);
                while (s.Any())
                {
                    var hash = s.Pop();
                    if (hash == CampfireNetHash.ZERO_HASH_BASE64)
                    {
                        continue;
                    }

                    if (await b.GetNodeAsync(hash) != null)
                    {
                        continue;
                    }

                    var node = await a.GetNodeAsync(hash);

                    nodesToImport.Add(Tuple.Create(hash, node));

                    s.Push(node.LeftHash);
                    s.Push(node.RightHash);
                }

                await b.ImportAsync(upstreamRootHash, nodesToImport);

                Console.WriteLine("Imported " + nodesToImport.Count + " nodes!");
            }
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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);
        }