@xyo-network/p2p
Advanced tools
Comparing version 0.19.0 to 0.19.1
@@ -14,2 +14,3 @@ "use strict"; | ||
const { topic, message } = xyo_topic_buffer_1.decodeXyoTopicBuffer(msg); | ||
this.logInfo(`Topic received ${topic}`); | ||
this.listener.publish(topic, connection.publicKey, message); | ||
@@ -23,2 +24,3 @@ }); | ||
publishMessageToPeer(topic, message, publicKey) { | ||
this.logInfo(`Sending message to peer with topic ${topic}`); | ||
const connection = this.discoveryService.getPeerConnection(publicKey); | ||
@@ -31,2 +33,3 @@ if (connection) { | ||
publish(topic, message) { | ||
this.logInfo(`Publishing message with topic ${topic}`); | ||
this.discoveryService.getListOfKnownPublicKeys().forEach(publicKey => this.publishMessageToPeer(topic, message, publicKey)); | ||
@@ -36,3 +39,6 @@ return Promise.resolve(); | ||
subscribe(topic, cb) { | ||
return this.listener.subscribe(topic, cb); | ||
return this.listener.subscribe(topic, (senderPublicKey, message) => { | ||
this.logInfo(`Message subscription with ${topic} received`); | ||
cb(senderPublicKey, message); | ||
}); | ||
} | ||
@@ -39,0 +45,0 @@ } |
import { IXyoPeerTransport, IXyoPeerConnection, IXyoPeerDiscoveryService, Callback } from "./@types"; | ||
import { XyoBase } from "@xyo-network/base"; | ||
declare enum Attrs { | ||
@@ -6,3 +7,3 @@ address = "address", | ||
} | ||
export declare class XyoPeerDiscoveryService implements IXyoPeerDiscoveryService { | ||
export declare class XyoPeerDiscoveryService extends XyoBase implements IXyoPeerDiscoveryService { | ||
private publicKey; | ||
@@ -9,0 +10,0 @@ private address; |
@@ -14,2 +14,3 @@ "use strict"; | ||
const xyo_peer_connection_pool_1 = require("./xyo-peer-connection-pool"); | ||
const base_1 = require("@xyo-network/base"); | ||
var Attrs; | ||
@@ -20,4 +21,5 @@ (function (Attrs) { | ||
})(Attrs || (Attrs = {})); | ||
class XyoPeerDiscoveryService { | ||
class XyoPeerDiscoveryService extends base_1.XyoBase { | ||
constructor(publicKey, address, transport) { | ||
super(); | ||
this.publicKey = publicKey; | ||
@@ -42,5 +44,12 @@ this.address = address; | ||
}; | ||
this.onDiscovery(connection => this.pool.addPeerConnection(connection)); | ||
this.onDisconnected(connection => this.pool.removePeerConnection(connection)); | ||
this.onDiscovery((connection) => { | ||
this.logInfo(`Discovered Peer`); | ||
this.pool.addPeerConnection(connection); | ||
}); | ||
this.onDisconnected((connection) => { | ||
this.logInfo('Disconnected from Peer'); | ||
this.pool.removePeerConnection(connection); | ||
}); | ||
this.transport.onConnection((connection) => { | ||
this.logInfo(`Connected to Peer ${connection.publicKey}`); | ||
this.handleBootstrap(connection); | ||
@@ -63,2 +72,3 @@ this.handleClose(connection); | ||
addBootstrapNodes(addresses) { | ||
this.logInfo(`Adding bootstrap nodes ${addresses.map(a => `\n${a}`)}`); | ||
this.bootstrapAddresses.push(...addresses.filter(this.isValidBootstrapAddress)); | ||
@@ -70,2 +80,3 @@ if (this.running) | ||
return __awaiter(this, void 0, void 0, function* () { | ||
this.logInfo(`Starting discovery`); | ||
if (this.running) | ||
@@ -81,2 +92,3 @@ return this.starting; | ||
stop() { | ||
this.logInfo(`Stopping discovery`); | ||
this.running = false; | ||
@@ -102,2 +114,3 @@ this.transport.stop(); | ||
dialBootstrapNodes() { | ||
this.logInfo(`Dialing bootstrap nodes`); | ||
this.bootstrapAddresses.forEach((address) => { | ||
@@ -117,2 +130,3 @@ this.transport.dial(address).then((connection) => { | ||
const { publicKey, address, peers } = JSON.parse(message); // TODO sanitize / validate input | ||
this.logInfo(`Discovery topic received for publicKey ${publicKey}`); | ||
connection.setPublicKey(publicKey); | ||
@@ -119,0 +133,0 @@ connection.setMultiAddress(address); |
{ | ||
"name": "@xyo-network/p2p", | ||
"version": "0.19.0", | ||
"version": "0.19.1", | ||
"description": "A peer to peer network module", | ||
@@ -22,3 +22,3 @@ "main": "dist/index.js", | ||
}, | ||
"gitHead": "b7c5908827bdf1d628e7625738a1e60ccd5bb8ab" | ||
"gitHead": "8a7b48eb185d223c5fa53fd851decec2724f72fc" | ||
} |
@@ -19,2 +19,3 @@ import { IXyoP2PService, unsubscribeFn, IXyoPeerDiscoveryService } from './@types' | ||
const { topic, message } = decodeXyoTopicBuffer(msg) | ||
this.logInfo(`Topic received ${topic}`) | ||
this.listener.publish(topic, connection.publicKey, message) | ||
@@ -30,2 +31,3 @@ }) | ||
public publishMessageToPeer(topic: string, message: Buffer, publicKey: string) { | ||
this.logInfo(`Sending message to peer with topic ${topic}`) | ||
const connection = this.discoveryService.getPeerConnection(publicKey) | ||
@@ -39,2 +41,3 @@ if (connection) { | ||
public publish(topic: string, message: Buffer) { | ||
this.logInfo(`Publishing message with topic ${topic}`) | ||
this.discoveryService.getListOfKnownPublicKeys().forEach(publicKey => | ||
@@ -47,4 +50,7 @@ this.publishMessageToPeer(topic, message, publicKey)) | ||
public subscribe(topic: string, cb: Callback): unsubscribeFn { | ||
return this.listener.subscribe(topic, cb) | ||
return this.listener.subscribe(topic, (senderPublicKey, message) => { | ||
this.logInfo(`Message subscription with ${topic} received`) | ||
cb(senderPublicKey, message) | ||
}) | ||
} | ||
} |
@@ -6,3 +6,3 @@ import { IXyoP2PService, IXyoPeer, IXyoPeerTransport, unsubscribeFn, IXyoPeerConnection, IXyoPeerDiscoveryService, Callback } from "./@types" | ||
import { XyoPeerConnectionPool } from "./xyo-peer-connection-pool" | ||
import { Server } from 'net' | ||
import { XyoBase } from "@xyo-network/base" | ||
@@ -14,3 +14,3 @@ enum Attrs { | ||
export class XyoPeerDiscoveryService implements IXyoPeerDiscoveryService { | ||
export class XyoPeerDiscoveryService extends XyoBase implements IXyoPeerDiscoveryService { | ||
private running = false | ||
@@ -27,5 +27,13 @@ private bootstrapAddresses: string[] = [] | ||
) { | ||
this.onDiscovery(connection => this.pool.addPeerConnection(connection)) | ||
this.onDisconnected(connection => this.pool.removePeerConnection(connection)) | ||
super() | ||
this.onDiscovery((connection) => { | ||
this.logInfo(`Discovered Peer`) | ||
this.pool.addPeerConnection(connection) | ||
}) | ||
this.onDisconnected((connection) => { | ||
this.logInfo('Disconnected from Peer') | ||
this.pool.removePeerConnection(connection) | ||
}) | ||
this.transport.onConnection((connection) => { | ||
this.logInfo(`Connected to Peer ${connection.publicKey}`) | ||
this.handleBootstrap(connection) | ||
@@ -59,2 +67,3 @@ this.handleClose(connection) | ||
public addBootstrapNodes(addresses: string[]) { | ||
this.logInfo(`Adding bootstrap nodes ${addresses.map(a => `\n${a}`)}`) | ||
this.bootstrapAddresses.push(...addresses.filter(this.isValidBootstrapAddress)) | ||
@@ -65,2 +74,3 @@ if (this.running) this.dialBootstrapNodes() | ||
public async start() { | ||
this.logInfo(`Starting discovery`) | ||
if (this.running) return this.starting | ||
@@ -77,2 +87,3 @@ | ||
public stop() { | ||
this.logInfo(`Stopping discovery`) | ||
this.running = false | ||
@@ -106,2 +117,3 @@ this.transport.stop() | ||
private dialBootstrapNodes () { | ||
this.logInfo(`Dialing bootstrap nodes`) | ||
this.bootstrapAddresses.forEach((address) => { | ||
@@ -122,2 +134,3 @@ this.transport.dial(address).then((connection) => { | ||
const { publicKey, address, peers } = JSON.parse(message) // TODO sanitize / validate input | ||
this.logInfo(`Discovery topic received for publicKey ${publicKey}`) | ||
connection.setPublicKey(publicKey) | ||
@@ -124,0 +137,0 @@ connection.setMultiAddress(address) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
84146
1591