-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add libp2p module for realtime messages
Signed-off-by: vol4tim <vol4tim@gmail.com>
- Loading branch information
Showing
9 changed files
with
1,635 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as Ipfs } from "./ipfs"; | ||
export { default as Libp2p } from "./libp2p"; | ||
export { default as Remote } from "./remote"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { createNode } from "../utils/libp2p"; | ||
import { measurements as converter } from "../utils/measurement"; | ||
import { getAgents } from "../utils/utils"; | ||
|
||
const topic = "airalab.lighthouse.5.robonomics.eth"; | ||
|
||
class Provider { | ||
constructor(config) { | ||
this.node = null; | ||
this.isReady = false; | ||
this.whiteListAccounts = []; | ||
this.history = {}; | ||
this.init(config).then(() => { | ||
this.isReady = true; | ||
window.pubsubPeers = () => { | ||
console.log( | ||
"peers", | ||
this.node.services.pubsub.getPeers().map((peer) => peer.toString()) | ||
); | ||
console.log( | ||
"pubsub", | ||
this.node.services.pubsub | ||
.getSubscribers(topic) | ||
.map((peer) => peer.toString()) | ||
); | ||
}; | ||
// this.node.addEventListener("peer:connect", (evt) => { | ||
// const peerId = evt.detail; | ||
// console.log("Connection established to:", peerId.toString()); | ||
// }); | ||
// this.node.addEventListener("peer:discovery", (evt) => { | ||
// const peerInfo = evt.detail; | ||
// console.log("Discovered:", peerInfo.id.toString()); | ||
// }); | ||
}); | ||
} | ||
|
||
async init(config) { | ||
this.node = await createNode(config); | ||
this.whiteListAccounts = getAgents(); | ||
} | ||
|
||
ready() { | ||
return new Promise((res) => { | ||
const t = setInterval(() => { | ||
if (this.isReady) { | ||
res(); | ||
clearInterval(t); | ||
} | ||
}, 100); | ||
}); | ||
} | ||
|
||
getHistoryBySensor(sensor) { | ||
return Promise.resolve(this.history[sensor] ? this.history[sensor] : []); | ||
} | ||
|
||
watch(cb) { | ||
this.node.services.pubsub.subscribe(topic); | ||
this.node.services.pubsub.addEventListener("message", (evt) => { | ||
const sender = evt.detail.from.toString(); | ||
if (!this.whiteListAccounts.includes(sender)) { | ||
// console.log(`skip from ${sender}`); | ||
return; | ||
} | ||
|
||
let json; | ||
try { | ||
json = JSON.parse(Buffer.from(evt.detail.data).toString("utf8")); | ||
} catch (e) { | ||
// console.log(sender, Buffer.from(r.data).toString("utf8")); | ||
console.error(e.message); | ||
return; | ||
} | ||
|
||
for (const sensor_id in json) { | ||
const data = json[sensor_id]; | ||
if ( | ||
Object.prototype.hasOwnProperty.call(data, "model") && | ||
(!Object.prototype.hasOwnProperty.call(this.history, sensor_id) || | ||
this.history[sensor_id].find((item) => { | ||
return item.timestamp === data.measurement.timestamp; | ||
}) === undefined) | ||
) { | ||
const { timestamp, ...measurement } = data.measurement; | ||
const measurementLowerCase = {}; | ||
for (var key in measurement) { | ||
const name = key.toLowerCase(); | ||
measurementLowerCase[name] = converter[name]?.calc | ||
? converter[name].calc(measurement[key]) | ||
: measurement[key]; | ||
} | ||
const [lat, lng] = data.geo.split(","); | ||
const donated_by = data.donated_by || undefined; | ||
const point = { | ||
sensor_id, | ||
sender, | ||
model: data.model, | ||
geo: { lat, lng }, | ||
data: measurementLowerCase, | ||
donated_by, | ||
timestamp, | ||
}; | ||
if (!Object.prototype.hasOwnProperty.call(this.history, sensor_id)) { | ||
this.history[sensor_id] = []; | ||
} | ||
this.history[sensor_id].push(point); | ||
|
||
cb(point); | ||
} else { | ||
// console.log(sensor_id, data); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export default Provider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { noise } from "@chainsafe/libp2p-noise"; | ||
import { bootstrap } from "@libp2p/bootstrap"; | ||
import { floodsub } from "@libp2p/floodsub"; | ||
import { mplex } from "@libp2p/mplex"; | ||
import { webRTC } from "@libp2p/webrtc"; | ||
import { webSockets } from "@libp2p/websockets"; | ||
import * as filters from "@libp2p/websockets/filters"; | ||
import { createLibp2p } from "libp2p"; | ||
import { circuitRelayTransport } from "libp2p/circuit-relay"; | ||
import { identifyService } from "libp2p/identify"; | ||
|
||
export const createNode = async (config) => { | ||
const node = await createLibp2p({ | ||
addresses: { | ||
listen: ["/webrtc"], | ||
}, | ||
|
||
transports: [ | ||
webSockets({ | ||
filter: filters.all, | ||
}), | ||
webRTC(), | ||
circuitRelayTransport({ | ||
discoverRelays: 1, | ||
}), | ||
], | ||
|
||
streamMuxers: [mplex()], | ||
connectionEncryption: [noise()], | ||
services: { | ||
pubsub: floodsub(), | ||
identify: identifyService(), | ||
}, | ||
peerDiscovery: [ | ||
bootstrap({ | ||
list: config.bootstrappers, | ||
}), | ||
], | ||
|
||
connectionGater: { | ||
denyDialMultiaddr: () => { | ||
// by default we refuse to dial local addresses from the browser since they | ||
// are usually sent by remote peers broadcasting undialable multiaddrs but | ||
// here we are explicitly connecting to a local node so do not deny dialing | ||
// any discovered address | ||
return false; | ||
}, | ||
}, | ||
}); | ||
|
||
return node; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.