Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 4x 4x 4x 4x 4x 1x 1x 1x 1x 1x 1x | "use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AddressBook = void 0;
const fs_1 = require("fs");
const util_1 = require("util");
const logger_1 = require("./logger");
const client_1 = require("./client");
const database_1 = require("./database");
const contact_1 = require("./contact");
class AddressBook extends database_1.Database {
constructor(_filePath) {
super(_filePath);
this._typex = client_1.Client;
this._clientsById = new Map();
this._clientsByAddrPort = new Map();
this._logger = logger_1.LoggerFactory.getInstance().createLogger('address_book');
this._logger.debug((0, util_1.format)('constructor(%s)', this._filePath));
}
postLoad() {
this._logger.debug('postLoad()');
const raw = Array.from(this._data.values());
// Clients by ID
this._clientsById = raw
.filter((client) => client.id !== undefined && client.id !== null)
.reduce((map, client) => {
map.set(client.id, client);
return map;
}, new Map());
// Clients by Address:Port
this._clientsByAddrPort = raw
.filter((client) => client.hasContact())
.reduce((map, client) => {
map.set(`${client.address}:${client.port}`, client);
return map;
}, new Map());
}
loadBootstrap(path, clean = true) {
return __awaiter(this, void 0, void 0, function* () {
this._logger.debug((0, util_1.format)('loadBootstrap(%s)', path));
if (!(0, fs_1.existsSync)(path)) {
return;
}
const data = JSON.parse((0, fs_1.readFileSync)(path, 'utf8'));
// console.log('loadBootstrap', data);
for (const _contact of data) {
const contact = yield contact_1.Contact.resolve(_contact);
if (contact.is_valid) {
const client = new client_1.Client();
client.debugAdd = 'bootstrap';
client.isBootstrap = true;
if (contact.address) {
client.address = contact.address;
}
if (contact.port) {
client.port = contact.port;
}
this.addClient(client);
}
}
if (clean) {
(0, fs_1.writeFileSync)(path, '[]', 'utf8');
}
});
}
addClient(client) {
this._logger.debug((0, util_1.format)('addClient(%s)', client));
this.add(client.uuid, client);
this._clientsById.set(client.id, client);
this._clientsByAddrPort.set(`${client.address}:${client.port}`, client);
}
// TODO: tests
getNearestTo(node, withContactInfos = null, limit = 20) {
/* eslint-disable */
const sorted = [...this._data.values()]
.filter((client) => {
return client.node !== null;
})
.sort((a, b) => {
const aNode = a.node;
const bNode = b.node;
const aDistance = aNode.distance(node);
const bDistance = bNode.distance(node);
return aDistance.distance - bDistance.distance;
})
.slice(0, limit);
if (withContactInfos !== null) {
// TODO withContactInfos
if (withContactInfos) { }
else { }
}
return [];
}
getClientById(id) {
return this._clientsById.get(id) || null;
}
getClientByAddrPort(address, port) {
return this._clientsByAddrPort.get(`${address}:${port}`) || null;
}
}
exports.AddressBook = AddressBook;
|