Working on map view

This commit is contained in:
maciejhirsz
2018-07-14 22:56:20 +02:00
parent bcc7145477
commit 2c073eac19
16 changed files with 308 additions and 64 deletions
+30 -3
View File
@@ -1,14 +1,19 @@
import * as WebSocket from 'ws';
import * as EventEmitter from 'events';
import * as iplocation from 'iplocation';
import { timestamp, Maybe, Types, idGenerator } from '@dotstats/common';
import { parseMessage, getBestBlock, Message, BestBlock, SystemInterval } from './message';
import { locate, Location } from './location';
const BLOCK_TIME_HISTORY = 10;
const TIMEOUT = (1000 * 60 * 1) as Types.Milliseconds; // 1 minute
const nextId = idGenerator<Types.NodeId>();
export interface NodeEvents {
on(event: 'location', fn: (location: Location) => void): void;
emit(event: 'location', location: Location): void;
}
export default class Node {
public readonly id: Types.NodeId;
public readonly name: Types.NodeName;
@@ -16,8 +21,9 @@ export default class Node {
public readonly implementation: Types.NodeImplementation;
public readonly version: Types.NodeVersion;
public readonly events = new EventEmitter();
public readonly events = new EventEmitter() as EventEmitter & NodeEvents;
public location: Maybe<Location> = null;
public lastMessage: Types.Timestamp;
public config: string;
public best = '' as Types.BlockHash;
@@ -30,11 +36,13 @@ export default class Node {
private peers = 0 as Types.PeerCount;
private txcount = 0 as Types.TransactionCount;
private readonly ip: string;
private readonly socket: WebSocket;
private blockTimes: Array<number> = new Array(BLOCK_TIME_HISTORY);
private lastBlockAt: Maybe<Date> = null;
constructor(
ip: string,
socket: WebSocket,
name: Types.NodeName,
chain: Types.ChainLabel,
@@ -42,6 +50,7 @@ export default class Node {
implentation: Types.NodeImplementation,
version: Types.NodeVersion,
) {
this.ip = ip;
this.id = nextId();
this.name = name;
this.chain = chain;
@@ -83,6 +92,18 @@ export default class Node {
this.disconnect();
});
locate(ip).then((location) => {
if (!location) {
return;
}
console.log('node', ip, 'located at', location);
this.location = location;
this.events.emit('location', location);
});
}
public static fromSocket(socket: WebSocket, ip: string): Promise<Node> {
@@ -102,7 +123,7 @@ export default class Node {
const { name, chain, config, implementation, version } = message;
resolve(new Node(socket, name, chain, config, implementation, version));
resolve(new Node(ip, socket, name, chain, config, implementation, version));
}
}
@@ -136,6 +157,12 @@ export default class Node {
return [this.height, this.best, this.blockTime, this.blockTimestamp, this.propagationTime];
}
public nodeLocation(): Maybe<Types.NodeLocation> {
const { location } = this;
return location ? [location.lat, location.lon] : null;
}
public get average(): number {
let accounted = 0;
let sum = 0;