-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
1,800 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
module.exports = { | ||
presets: [ | ||
'@vue/cli-plugin-babel/preset' | ||
] | ||
} | ||
presets: ["@vue/cli-plugin-babel/preset"], | ||
plugins: ["@babel/plugin-transform-private-methods"] | ||
}; |
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
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,77 @@ | ||
import { pipe } from "it-pipe"; | ||
import { fromString as uint8ArrayFromString } from "uint8arrays/from-string"; | ||
import { toString as uint8ArrayToString } from "uint8arrays/to-string"; | ||
|
||
export function createHa() { | ||
return (components) => { | ||
async function getRequest(stream) { | ||
return pipe(stream, async function (source) { | ||
let result = ""; | ||
for await (const data of source) { | ||
result += uint8ArrayToString(data.subarray()); | ||
} | ||
return JSON.parse(result); | ||
}); | ||
} | ||
|
||
async function sendResponse(stream, msg) { | ||
return pipe( | ||
[uint8ArrayFromString(JSON.stringify(msg))], | ||
stream.sink | ||
).finally(() => { | ||
stream.close(); | ||
}); | ||
} | ||
|
||
return { | ||
async handle( | ||
protocol, | ||
handler, | ||
options = { | ||
runOnTransientConnection: true | ||
} | ||
) { | ||
await components.registrar.handle( | ||
protocol, | ||
async ({ stream }) => { | ||
handler(await getRequest(stream), stream); | ||
}, | ||
options | ||
); | ||
}, | ||
async request( | ||
connection, | ||
protocol, | ||
data, | ||
options = { | ||
runOnTransientConnection: true | ||
} | ||
) { | ||
if (connection.status !== "open") { | ||
return; | ||
} | ||
const stream = await connection.newStream([protocol], options); | ||
return pipe( | ||
[uint8ArrayFromString(JSON.stringify(data))], | ||
stream, | ||
async function (source) { | ||
let result = ""; | ||
for await (const data of source) { | ||
result += uint8ArrayToString(data.subarray()); | ||
} | ||
try { | ||
// stream.close(); | ||
return JSON.parse(result); | ||
} catch (error) { | ||
return result; | ||
} | ||
} | ||
); | ||
}, | ||
utils: { | ||
getRequest, | ||
sendResponse | ||
} | ||
}; | ||
}; | ||
} |
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,103 @@ | ||
import { noise } from "@chainsafe/libp2p-noise"; | ||
import { mplex } from "@libp2p/mplex"; | ||
import { webRTC } from "@libp2p/webrtc"; | ||
import { webSockets } from "@libp2p/websockets"; | ||
import * as filters from "@libp2p/websockets/filters"; | ||
import { multiaddr } from "@multiformats/multiaddr"; | ||
import { createLibp2p } from "libp2p"; | ||
import { circuitRelayTransport } from "@libp2p/circuit-relay-v2"; | ||
import { identify } from "@libp2p/identify"; | ||
import { createHa } from "./ha"; | ||
|
||
export async function createNode() { | ||
const node = await createLibp2p({ | ||
addresses: { | ||
listen: ["/webrtc"] | ||
}, | ||
transports: [ | ||
webSockets({ | ||
filter: filters.all | ||
}), | ||
webRTC(), | ||
circuitRelayTransport() | ||
], | ||
streamMuxers: [mplex()], | ||
connectionEncryption: [noise()], | ||
services: { | ||
identify: identify(), | ||
ha: createHa() | ||
}, | ||
connectionGater: { | ||
denyDialMultiaddr: () => { | ||
return false; | ||
} | ||
}, | ||
connectionManager: { | ||
minConnections: 0 | ||
} | ||
}); | ||
|
||
return node; | ||
} | ||
|
||
let node = null; | ||
let connections = []; | ||
let connection = null; | ||
|
||
export async function start() { | ||
if (node) { | ||
return; | ||
} | ||
node = await createNode(); | ||
await node.start(); | ||
console.log(`Node started with id ${node.peerId.toString()}`); | ||
|
||
function updateConnectionsList() { | ||
connections = node.getConnections().map((item) => { | ||
return item.remoteAddr.toString(); | ||
}); | ||
console.log("Update Connections List", connections); | ||
} | ||
|
||
node.addEventListener("connection:open", (event) => { | ||
console.log("connected", event.detail.remoteAddr.toString()); | ||
updateConnectionsList(); | ||
}); | ||
|
||
node.addEventListener("connection:close", (event) => { | ||
console.log("disconected", event.detail.remoteAddr.toString()); | ||
updateConnectionsList(); | ||
if ( | ||
event.detail.remoteAddr.toString() === connection.remoteAddr.toString() | ||
) { | ||
reconnect(connection.remoteAddr.toString()); | ||
} | ||
}); | ||
|
||
return node; | ||
} | ||
|
||
export async function reconnect(addr) { | ||
try { | ||
await connect(addr); | ||
} catch (error) { | ||
console.log(error); | ||
setTimeout(async () => { | ||
if (addr && !connections.includes(addr)) { | ||
await reconnect(addr); | ||
} | ||
}, 3000); | ||
} | ||
} | ||
|
||
export async function connect(addr) { | ||
const listenerMultiaddr = multiaddr(addr); | ||
connection = await node.dial(listenerMultiaddr); | ||
} | ||
|
||
export function request(data) { | ||
if (node && connection) { | ||
return node.services.ha.request(connection, "/call", data); | ||
} | ||
throw new Error("error"); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<template> | ||
<robo-template-devices-layout | ||
:config="config" | ||
:datalog="data" | ||
:updateTime="updateTime" | ||
/> | ||
</template> | ||
|
||
<script> | ||
import { watch } from "vue"; | ||
import { useStore } from "vuex"; | ||
import { useData } from "./launch"; | ||
export default { | ||
props: { | ||
config: Object | ||
}, | ||
setup() { | ||
const { data, updateTime, run, launch } = useData(); | ||
const store = useStore(); | ||
run(); | ||
watch( | ||
() => store.state.robonomicsUIvue.rws.launch, | ||
(value) => { | ||
try { | ||
launch(JSON.parse(value)); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
} | ||
); | ||
return { data, updateTime }; | ||
} | ||
}; | ||
</script> |
Oops, something went wrong.