New /fleet command to change target Status fleet

This commit is contained in:
Franck Royer 2021-07-15 14:25:59 +10:00
parent 9ffb33cdde
commit 53412e2fb4
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
2 changed files with 47 additions and 10 deletions

View File

@ -81,19 +81,17 @@ export default function App() {
const persistedNick = window.localStorage.getItem('nick'); const persistedNick = window.localStorage.getItem('nick');
return persistedNick !== null ? persistedNick : generate(); return persistedNick !== null ? persistedNick : generate();
}); });
const [fleetEnv] = useState<Environment>(defaultFleetEnv); const [fleetEnv, setFleetEnv] = useState<Environment>(defaultFleetEnv);
useEffect(() => { useEffect(() => {
localStorage.setItem('nick', nick); localStorage.setItem('nick', nick);
}, [nick]); }, [nick]);
useEffect(() => { useEffect(() => {
if (waku) return;
initWaku(fleetEnv, setWaku) initWaku(fleetEnv, setWaku)
.then(() => console.log('Waku init done')) .then(() => console.log('Waku init done'))
.catch((e) => console.log('Waku init failed ', e)); .catch((e) => console.log('Waku init failed ', e));
}, [fleetEnv, waku]); }, [fleetEnv]);
useEffect(() => { useEffect(() => {
if (!waku) return; if (!waku) return;
@ -163,7 +161,13 @@ export default function App() {
newMessages={newMessages} newMessages={newMessages}
archivedMessages={archivedMessages} archivedMessages={archivedMessages}
commandHandler={(input: string) => { commandHandler={(input: string) => {
const { command, response } = handleCommand(input, waku, setNick); const { command, response } = handleCommand(
input,
waku,
setNick,
fleetEnv,
setFleetEnv
);
const commandMessages = response.map((msg) => { const commandMessages = response.map((msg) => {
return Message.fromUtf8String(command, msg); return Message.fromUtf8String(command, msg);
}); });

View File

@ -1,12 +1,13 @@
import { multiaddr } from 'multiaddr'; import { multiaddr } from 'multiaddr';
import PeerId from 'peer-id'; import PeerId from 'peer-id';
import { Waku } from 'js-waku'; import { Environment, Waku } from 'js-waku';
function help(): string[] { function help(): string[] {
return [ return [
'/nick <nickname>: set a new nickname', '/nick <nickname>: set a new nickname',
'/info: some information about the node', '/info: some information about the node',
'/connect <Multiaddr>: connect to the given peer', '/connect <Multiaddr>: connect to the given peer',
'/fleet <prod|test>: connect to this fleet; beware it restarts waku node.',
'/help: Display this help', '/help: Display this help',
]; ];
} }
@ -22,11 +23,14 @@ function nick(
return [`New nick: ${nick}`]; return [`New nick: ${nick}`];
} }
function info(waku: Waku | undefined): string[] { function info(waku: Waku | undefined, fleetEnv: Environment): string[] {
if (!waku) { if (!waku) {
return ['Waku node is starting']; return ['Waku node is starting'];
} }
return [`PeerId: ${waku.libp2p.peerId.toB58String()}`]; return [
`PeerId: ${waku.libp2p.peerId.toB58String()}`,
`Fleet environment: ${fleetEnv}`,
];
} }
function connect(peer: string | undefined, waku: Waku | undefined): string[] { function connect(peer: string | undefined, waku: Waku | undefined): string[] {
@ -78,6 +82,28 @@ function peers(waku: Waku | undefined): string[] {
return response; return response;
} }
function fleet(
newFleetEnv: string | undefined,
currFleetEnv: Environment,
setFleetEnv: (fleetEnv: Environment) => void
): string[] {
switch (newFleetEnv) {
case Environment.Test:
setFleetEnv(newFleetEnv);
break;
case Environment.Prod:
setFleetEnv(newFleetEnv);
break;
default:
return [
`Incorrect values, acceptable values are ${Environment.Test}, ${Environment.Prod}`,
`Current fleet environment is ${currFleetEnv}`,
];
}
return [`New fleet Environment: ${newFleetEnv}`];
}
function connections(waku: Waku | undefined): string[] { function connections(waku: Waku | undefined): string[] {
if (!waku) { if (!waku) {
return ['Waku node is starting']; return ['Waku node is starting'];
@ -107,7 +133,9 @@ function connections(waku: Waku | undefined): string[] {
export default function handleCommand( export default function handleCommand(
input: string, input: string,
waku: Waku | undefined, waku: Waku | undefined,
setNick: (nick: string) => void setNick: (nick: string) => void,
currFleetEnv: Environment,
setFleetEnv: (fleetEnv: Environment) => void
): { command: string; response: string[] } { ): { command: string; response: string[] } {
let response: string[] = []; let response: string[] = [];
const args = parseInput(input); const args = parseInput(input);
@ -120,7 +148,7 @@ export default function handleCommand(
nick(args.shift(), setNick).map((str) => response.push(str)); nick(args.shift(), setNick).map((str) => response.push(str));
break; break;
case '/info': case '/info':
info(waku).map((str) => response.push(str)); info(waku, currFleetEnv).map((str) => response.push(str));
break; break;
case '/connect': case '/connect':
connect(args.shift(), waku).map((str) => response.push(str)); connect(args.shift(), waku).map((str) => response.push(str));
@ -131,6 +159,11 @@ export default function handleCommand(
case '/connections': case '/connections':
connections(waku).map((str) => response.push(str)); connections(waku).map((str) => response.push(str));
break; break;
case '/fleet':
fleet(args.shift(), currFleetEnv, setFleetEnv).map((str) =>
response.push(str)
);
break;
default: default:
response.push(`Unknown Command '${command}'`); response.push(`Unknown Command '${command}'`);
} }