Skip to content

Commit

Permalink
Add discovery toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Feb 29, 2024
1 parent 66985de commit 9e8943c
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 13 deletions.
5 changes: 2 additions & 3 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,5 @@ Add a connect command to connect to a specific host directly, eg:
### Pinning
We should probably handle pinning. Maybe not for DID Docs, as they have timestamp, so wont be reused.


## BUGS

## Features
En|disable discovery process
6 changes: 0 additions & 6 deletions cmd/actor/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"context"
"fmt"
"os"

Expand Down Expand Up @@ -39,11 +38,6 @@ func main() {
}
fmt.Println("done.")

// Now we can start continuous discovery in the background.
fmt.Print("Starting discovery loop...")
go p.DiscoveryLoop(context.Background())
fmt.Println("done.")

if config.RelayMode() {
fmt.Println("Starting relay mode...")
startWebServer(p, nil)
Expand Down
4 changes: 2 additions & 2 deletions ui/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,6 @@ func (ui *ChatUI) handleSetBroadcastCommand(args []string) {
}

func (ui *ChatUI) handleHelpSetBroadcastCommand(args []string) {
ui.displaySystemMessage("Usage: /set broadcast on|off")
ui.displaySystemMessage("For now toggles broadcast messages on and off")
ui.displayHelpUsage("/set broadcast on|off")
ui.displayHelpText("Toggles broadcast messages on and off")
}
36 changes: 36 additions & 0 deletions ui/discovery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ui

import (
"context"
)

func (ui *ChatUI) handleSetDiscoveryCommand(args []string) {

if len(args) == 3 {

toggle := args[2]

switch toggle {
case "on":

// Now we can start continuous discovery in the background.
ui.discoveryLoopCtx, ui.discoveryLoopCancel = context.WithCancel(context.Background())
go ui.p.DiscoveryLoop(context.Background())
ui.displaySystemMessage("Discovery is on")
case "off":
if ui.discoveryLoopCancel != nil {
ui.discoveryLoopCancel()
ui.displaySystemMessage("Discovery is off")
}
default:
ui.handleHelpSetDiscoveryCommand(args)
}
} else {
ui.handleHelpSetDiscoveryCommand(args)
}
}

func (ui *ChatUI) handleHelpSetDiscoveryCommand(args []string) {
ui.displayHelpUsage("/set discovery on|off")
ui.displayHelpText("Toggles the discovery loop on and off")
}
6 changes: 4 additions & 2 deletions ui/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ func (ui *ChatUI) handleSetCommand(args []string) {
switch args[1] {
case "broadcast":
ui.handleSetBroadcastCommand(args)
case "discovery":
ui.handleSetDiscoveryCommand(args)
}
} else {
ui.handleHelpSetCommands(args)
Expand All @@ -14,6 +16,6 @@ func (ui *ChatUI) handleSetCommand(args []string) {
}

func (ui *ChatUI) handleHelpSetCommands(args []string) {
ui.displaySystemMessage("Usage: /set broadcast on|off")
ui.displaySystemMessage("For now toggles broadcast messages on and off")
ui.displayHelpUsage("/set broadcast|discovery on|off")
ui.displayHelpText("Toggles broadcast and peer discovery on and off")
}
10 changes: 10 additions & 0 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ type ChatUI struct {
broadcastCtx context.Context
broadcastCancel context.CancelFunc

// DiscoveryLoop
discoveryLoopCtx context.Context
discoveryLoopCancel context.CancelFunc

// History of entries
inputField *tview.InputField
inputHistory []string
Expand Down Expand Up @@ -133,6 +137,12 @@ func (ui *ChatUI) Run() error {

defer ui.end()

// Now we can start continuous discovery in the background.
fmt.Print("Starting discovery loop...")
ui.discoveryLoopCtx, ui.discoveryLoopCancel = context.WithCancel(context.Background())
go ui.p.DiscoveryLoop(context.Background())
fmt.Println("done.")

// The actor should just run in the background for ever.
// It will handle incoming messages and envelopes.
// It shouldn't change - ever.
Expand Down

0 comments on commit 9e8943c

Please sign in to comment.