AddressBook
1 import std.exception; 2 import core.exception; 3 import std.algorithm; 4 5 enum address_content = ` 6 @AzZPqaMsYOwXVgitRRVe7XlyCCSdBeFK6b8mTnv8IDfU node_3 7 @AoL9_T3JJ09fnPKo7Y1in9mpKkjgxSQ_sD0t0CPCcLKk node_4 8 @AumexnPXMa0mKVsYQeEKvY4Y640DXNCuBU6XdzFOicWC node_5 9 @AxEDiWOgvaTLn-zMs62msv-54RwVNA7x7xE0rtLrCd3o node_2 10 @A5VO5-Nk5fUR7Yta7aSIpcXwWzN6cIkbKvg2-So0G52H node_1`; 11 12 immutable(NetworkNodeRecord)*[] nnrs = parseAddressFile(address_content.splitLines); 13 14 shared(AddressBook) unitbook = new shared(AddressBook)(); 15 16 // Can set the address book from a list of nnr records 17 unitbook = nnrs; 18 assert(unitbook.length == 5); 19 20 // It can only be set once 21 assertThrown!AssertError(unitbook = nnrs); 22 assert(unitbook.length == 5); 23 24 // Otherwise it should be explicitly cleared before it can be set again 25 unitbook.clear(); 26 assert(unitbook.length == 0); 27 unitbook = nnrs; 28 assert(unitbook.length == 5); 29 30 // Get all of the active channels 31 Pubkey[] channels = unitbook.keys; 32 foreach(channel; channels) { 33 assert(unitbook.exists(channel)); 34 } 35 36 // We can update/add a channel 37 { 38 import tagion.utils.StdTime; 39 auto nnr = nnrs[0]; 40 assert(!unitbook.isActive(nnr.channel)); 41 42 immutable mod_nnr = new NetworkNodeRecord( 43 nnr.channel, 44 "name", 45 sdt_t(0), 46 NetworkNodeRecord.State.ACTIVE, 47 nnr.address 48 ); 49 50 unitbook.set(mod_nnr); 51 52 assert(unitbook.isActive(nnr.channel)); 53 } 54 55 // Remove channels 56 { 57 Pubkey channel = nnrs[0].channel; 58 assert(unitbook.exists(channel)); 59 unitbook.remove(channel); 60 assert(!unitbook.exists(channel)); 61 } 62 63 // Retrieving a channels result which doesn't exists will throw 64 { 65 Pubkey channel = [0, 1, 2, 3, 4]; 66 assert(!unitbook.exists(channel)); 67 68 auto result = unitbook[channel]; 69 assertThrown!AddressException(result.get); 70 }
* This function is used in dev mode when reading from an address file instead of the dart. * * Params: * address_file_content = A range of strings formatted like <base64url pubkey> <address>