[enh] add BMA watcher + reorganize code
This commit is contained in:
51
src/lib/watchers/bma/bma-watcher.ts
Normal file
51
src/lib/watchers/bma/bma-watcher.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import {watcherLoop} from "../../watcherLoop";
|
||||
import {Conf, ConfBMA} from "../../types/conf";
|
||||
import Axios from "axios";
|
||||
import {mail} from "../../mail";
|
||||
|
||||
export function bmaWatcher(conf: Conf) {
|
||||
|
||||
const URL_PATH = '/blockchain/current'
|
||||
|
||||
return async (bmaServer: ConfBMA) => {
|
||||
|
||||
let nodeDownRes: () => void
|
||||
let nodeDownPromise: Promise<void> = new Promise(res => nodeDownRes = res)
|
||||
|
||||
await watcherLoop(
|
||||
async () => {
|
||||
await Axios.get(bmaServer.address + URL_PATH)
|
||||
let interval = setInterval(async () => {
|
||||
try {
|
||||
await Axios.get(bmaServer.address + URL_PATH)
|
||||
} catch (e) {
|
||||
clearInterval(interval)
|
||||
nodeDownRes()
|
||||
// Re-create down promise for future connection trial
|
||||
nodeDownPromise = new Promise(res => nodeDownRes = res)
|
||||
}
|
||||
}, bmaServer.frequency)
|
||||
},
|
||||
|
||||
() => nodeDownPromise,
|
||||
|
||||
conf.reconnectionDelays,
|
||||
|
||||
mail.onEstablished(conf, bmaServer.address),
|
||||
|
||||
// When a disconnection is detected
|
||||
mail.onDisconnect(conf, bmaServer.address),
|
||||
|
||||
async () => {
|
||||
console.log('Trying to connect to %s', bmaServer.address)
|
||||
},
|
||||
|
||||
mail.onRestartSuccess(conf, bmaServer.address),
|
||||
|
||||
async (e) => {
|
||||
console.error(e.message)
|
||||
}
|
||||
)
|
||||
|
||||
}
|
||||
}
|
||||
20
src/lib/watchers/ws2p/message-handler.ts
Normal file
20
src/lib/watchers/ws2p/message-handler.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import {WS2PMessageHandler} from 'duniter/app/modules/ws2p/lib/impl/WS2PMessageHandler'
|
||||
import {WS2PConnection} from 'duniter/app/modules/ws2p/lib/WS2PConnection'
|
||||
import {WS2PResponse} from 'duniter/app/modules/ws2p/lib/impl/WS2PResponse'
|
||||
import {NewLogger} from 'duniter/app/lib/logger'
|
||||
|
||||
export class MessageHandler implements WS2PMessageHandler {
|
||||
|
||||
async answerToRequest(json: any, c: WS2PConnection): Promise<WS2PResponse> {
|
||||
NewLogger().info('Request from %s = %s', c.pubkey, JSON.stringify(json))
|
||||
return {}
|
||||
}
|
||||
|
||||
/**
|
||||
* THAT'S THE CORE OF DWATCHER: tracing push messages
|
||||
*/
|
||||
async handlePushMessage(json: any, c: WS2PConnection): Promise<void> {
|
||||
NewLogger().info('Push from %s = %s', c.pubkey, JSON.stringify(json))
|
||||
}
|
||||
|
||||
}
|
||||
62
src/lib/watchers/ws2p/ws2p-watcher.ts
Normal file
62
src/lib/watchers/ws2p/ws2p-watcher.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import {WS2PConnection, WS2PPubkeyLocalAuth, WS2PPubkeyRemoteAuth} from "duniter/app/modules/ws2p/lib/WS2PConnection";
|
||||
import {watcherLoop} from "../../watcherLoop";
|
||||
import {MessageHandler} from "./message-handler";
|
||||
import {Conf, ConfWS2P} from "../../types/conf";
|
||||
import {Scrypt} from "duniter/app/modules/keypair/lib/scrypt";
|
||||
import {Key} from "duniter/app/lib/common-libs/crypto/keyring";
|
||||
import {mail} from "../../mail";
|
||||
|
||||
export function ws2pWatcher(conf: Conf) {
|
||||
|
||||
let c: WS2PConnection
|
||||
|
||||
return async (wserver: ConfWS2P) => {
|
||||
|
||||
const keys = await Scrypt(wserver.salt, wserver.passwd)
|
||||
const keypair = new Key(keys.pub, keys.sec)
|
||||
|
||||
await watcherLoop(
|
||||
async () => {
|
||||
|
||||
const localAuth = new WS2PPubkeyLocalAuth(wserver.currency, keypair, "", async () => true)
|
||||
const remoteAuth = new WS2PPubkeyRemoteAuth(wserver.currency, keypair, async () => true)
|
||||
|
||||
c = WS2PConnection.newConnectionToAddress(
|
||||
1,
|
||||
wserver.address,
|
||||
new MessageHandler(),
|
||||
localAuth,
|
||||
remoteAuth,
|
||||
undefined,
|
||||
{
|
||||
connectionTimeout: conf.connectionTimeout,
|
||||
requestTimeout: 0 // No request anyway
|
||||
},
|
||||
wserver.expectedKey
|
||||
)
|
||||
|
||||
await c.connectAsInitiator()
|
||||
},
|
||||
|
||||
() => c.closed,
|
||||
|
||||
conf.reconnectionDelays,
|
||||
|
||||
mail.onEstablished(conf, wserver.expectedKey),
|
||||
|
||||
// When a disconnection is detected
|
||||
mail.onDisconnect(conf, wserver.expectedKey),
|
||||
|
||||
async () => {
|
||||
console.log('Trying to connect to %s', wserver.expectedKey)
|
||||
},
|
||||
|
||||
mail.onRestartSuccess(conf, wserver.expectedKey),
|
||||
|
||||
async (e) => {
|
||||
console.error(e)
|
||||
}
|
||||
)
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user