Home Reference Source Repository

src/controller.js

import EventEmitter from 'events';
import {createWatcher} from 'ip-monitor';

class Emitter extends EventEmitter {
	constructor(ipfs) {
		super();
    this.ipfs = ipfs;
		// bind methods
		this._announceAddress = this._announceAddress.bind(this);
		// this._onBlockAdded = this._onBlockAdded.bind(this);
		// setup ip monitor
		this._ipWatcher = createWatcher();
		this._ipWatcher.on('IP:change', this._announceAddress);

		this._ipWatcher.start();

		this.on('block-added', () => {

		});

		this.on('peer', () => {
			switch (type) {
			case 'added':
			case 'removed':
				// notifyClient
				break;
			case 'add':
				// add peer
			case 'remove':
				// notifyClient
				break;
			}
		});
	}

	async _announceAddress(oldIP, newIP) {
    const peerinfo = await this.ipfs.id();
		const address = `/ip4/${newIP}/tcp/4001/ipfs/${peerinfo.id}`;
    global.multiAddress = address;
		if (oldIP !== newIP) this.ipfs.pubsub.publish('peer-added', new Buffer(JSON.stringify({id: peerinfo.id, address})));
	}


}

export default ipfs => new Emitter(ipfs);