Skip to content
This repository has been archived by the owner on Mar 6, 2023. It is now read-only.

Commit

Permalink
Adds Bridge client class, which holds peer and web3 connections and l…
Browse files Browse the repository at this point in the history
…istens for messages
  • Loading branch information
alex-miller-0 committed Feb 5, 2018
1 parent 9a2f180 commit 1d6c49d
Show file tree
Hide file tree
Showing 8 changed files with 2,422 additions and 124 deletions.
46 changes: 0 additions & 46 deletions connect.js/connect.js

This file was deleted.

33 changes: 23 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
const fs = require('fs');
const net = require('net');
const config = require('./src/config.js');
const server = require('./src/server.js');
const peers = require('./src/peers.js');
const Bridge = require('./src/lib/Bridge.js');
const Peers = require('./src/peers.js');
const Clients = require('./src/clients.js');

const argv = require('yargs')
.usage('Usage: $0 <cmd> [options]')
.command('add', 'Add a network group: network_a_host,network_a_address,network_b_host,network_b_address')
Expand All @@ -21,7 +23,7 @@ const argv = require('yargs')
console.log('Bridge Client v0.1\n')

let DIR = `${process.cwd()}/data`;
let NET_INDEX = null;
let INDEX = null;
if (argv.datadir) {
// Change the data directory
DIR = argv.datadir;
Expand All @@ -31,10 +33,10 @@ if (!fs.existsSync(DIR)) { fs.mkdirSync(DIR); }
if(argv.network) {
// Specify two networks being bridged
if (argv.network.length != 2) { console.log('ERROR: Please supply two networks, e.g. `--network 0x...a --network 0x...b`'); }
NET_INDEX = config.getNetIndex(argv.network);
INDEX = config.getNetIndex(argv.network);
} else if (!argv.add) {
try {
NET_INDEX = config.getFirstIndex(DIR)
INDEX = config.getFirstIndex(DIR)
} catch (err) {
console.log('ERROR: Cannot retrieve default index. You can add a bridge with --add network_a_host,network_a_address,network_b_host,network_b_addr');
process.exit(1);
Expand All @@ -55,11 +57,11 @@ if (argv.add) {
if (argv.bootstrap) {
// Add bootstrap peers
const _peers = argv.bootstrap.split(',');
peers.connectToPeers(_peers, (conns) => {
Peers.connectToPeers(_peers, (conns) => {
if (conns.length > 0) {
config.addPeers(conns, DIR, NET_INDEX, (err) => {
config.addPeers(conns, DIR, INDEX, (err) => {
if (err) { console.log(`ERROR: ${err}`); }
else { console.log(`Successfully bootstrapped ${conns.length} peers on bridge ${NET_INDEX}`)}
else { console.log(`Successfully bootstrapped ${conns.length} peers on bridge ${INDEX}`)}
conns.forEach((conn) => { conn.disconnect(); })
})
} else {
Expand All @@ -70,8 +72,19 @@ if (argv.bootstrap) {

if (argv.start) {
// Start listening to peers and blockchains
// peers

let peers;
let clients;
config.getPeers(DIR, INDEX, (err, _peers) => {
peers = _peers;
config.getHosts(DIR, INDEX, (err, _hosts) => {
Clients.connectToClients(_hosts, (err, _clients) => {
// Start a new Bridge client. This consists of a server listening to
// a given port and handling socket messages from peers. The client
// also checks linked web3 hosts for updated blockchain data.
const b = new Bridge({ index: INDEX, peers: _peers, clients: _clients });
})
})
})
}
// // This is the server that peers will connect to and message
// const s = server.createServer(8000);
Expand Down
Loading

0 comments on commit 1d6c49d

Please sign in to comment.