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

Commit

Permalink
Update pin when active terminal session ends
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaavi committed Nov 15, 2021
1 parent a9c0506 commit ac1d949
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 14 deletions.
18 changes: 15 additions & 3 deletions docks/terminal_expansion.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type ExpansionTerminal struct {
opID uint32
relayOp terminal.OpTerminal
relayOpEnded *abool.AtomicBool

changeNotifyFuncReady *abool.AtomicBool
changeNotifyFunc func()
}

func ExpandTo(t terminal.OpTerminal, routeTo string, encryptFor *hub.Hub) (*ExpansionTerminal, *terminal.Error) {
Expand All @@ -32,9 +35,10 @@ func ExpandTo(t terminal.OpTerminal, routeTo string, encryptFor *hub.Hub) (*Expa
return nil, tErr.Wrap("failed to create expansion terminal base")
}
expansion := &ExpansionTerminal{
TerminalBase: tBase,
relayOp: t,
relayOpEnded: abool.New(),
TerminalBase: tBase,
relayOp: t,
relayOpEnded: abool.New(),
changeNotifyFuncReady: abool.New(),
}
expansion.TerminalBase.SetTerminalExtension(expansion)
expansion.TerminalBase.SetTimeout(expansionClientTimeout)
Expand Down Expand Up @@ -120,6 +124,14 @@ func (t *ExpansionTerminal) IsAbandoned() bool {
return t.Abandoned.IsSet()
}

func (t *ExpansionTerminal) SetChangeNotifyFunc(f func()) {
if t.changeNotifyFuncReady.IsSet() {
return
}
t.changeNotifyFunc = f
t.changeNotifyFuncReady.Set()
}

func (t *ExpansionTerminal) FmtID() string {
return fmt.Sprintf("%s#%d", t.relayOp.FmtID(), t.opID)
}
16 changes: 16 additions & 0 deletions navigator/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,19 @@ func (m *Map) pushPinChangesWorker(ctx context.Context) error {

return nil
}

// pushChange pushes changes of the pin, if the pushChanges flag is set.
func (pin *Pin) pushChange() {
// Check before starting the worker.
if pin.pushChanges.IsNotSet() {
return
}

// Start worker to push changes.
module.StartWorker("push pin change", func(ctx context.Context) error {
if pin.pushChanges.SetToIf(true, false) {
mapDBController.PushUpdate(pin.Export())
}
return nil
})
}
14 changes: 13 additions & 1 deletion navigator/pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type Pin struct {
pushChanges *abool.AtomicBool
}

// Session represents a terminal
// PinConnection represents a connection to a terminal on the Hub.
type PinConnection struct {
// Terminal holds the active terminal session.
Terminal *docks.ExpansionTerminal
Expand Down Expand Up @@ -122,6 +122,10 @@ func (pin *Pin) SetActiveTerminal(pc *PinConnection) {
defer pin.Unlock()

pin.Connection = pc
if pin.Connection.Terminal != nil {
pin.Connection.Terminal.SetChangeNotifyFunc(pin.NotifyTerminalChange)
}

pin.pushChanges.Set()
}

Expand All @@ -146,3 +150,11 @@ func (pin *Pin) hasActiveTerminal() bool {
return pin.Connection != nil &&
!pin.Connection.Terminal.IsAbandoned()
}

func (pin *Pin) NotifyTerminalChange() {
if !pin.HasActiveTerminal() {
pin.pushChanges.Set()
}

pin.pushChange()
}
22 changes: 12 additions & 10 deletions navigator/pin_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ type PinExport struct {
States []string // From pin.State
HopDistance int

ConnectedTo map[string]*LaneExport // Key is Hub ID.
Route []string // Includes Home Hub and this Pin's ID
ConnectedTo map[string]*LaneExport // Key is Hub ID.
Route []string // Includes Home Hub and this Pin's ID.
SessionActive bool
}

// LaneExport is the exportable version of a Lane.
Expand All @@ -48,14 +49,15 @@ func (pin *Pin) Export() *PinExport {

// Shallow copy static values.
export := &PinExport{
ID: pin.Hub.ID,
Name: pin.Hub.Info.Name,
Map: pin.Hub.Map,
FirstSeen: pin.Hub.FirstSeen,
EntityV4: pin.EntityV4,
EntityV6: pin.EntityV6,
States: pin.State.Export(),
HopDistance: pin.HopDistance,
ID: pin.Hub.ID,
Name: pin.Hub.Info.Name,
Map: pin.Hub.Map,
FirstSeen: pin.Hub.FirstSeen,
EntityV4: pin.EntityV4,
EntityV6: pin.EntityV6,
States: pin.State.Export(),
HopDistance: pin.HopDistance,
SessionActive: pin.hasActiveTerminal(),
}

// Export lanes.
Expand Down

0 comments on commit ac1d949

Please sign in to comment.