-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
6 changed files
with
187 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package p2p | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
) | ||
|
||
type NodeData struct { | ||
Name string | ||
ID string | ||
TunnelAddress string | ||
LastSeen time.Time | ||
} | ||
|
||
func (d NodeData) IsOnline() bool { | ||
now := time.Now() | ||
// if the node was seen in the last 40 seconds, it's online | ||
return now.Sub(d.LastSeen) < 40*time.Second | ||
} | ||
|
||
var mu sync.Mutex | ||
var nodes = map[string]NodeData{} | ||
|
||
func GetAvailableNodes() []NodeData { | ||
mu.Lock() | ||
defer mu.Unlock() | ||
var availableNodes = []NodeData{} | ||
for _, v := range nodes { | ||
availableNodes = append(availableNodes, v) | ||
} | ||
return availableNodes | ||
} | ||
|
||
func AddNode(node NodeData) { | ||
mu.Lock() | ||
defer mu.Unlock() | ||
nodes[node.ID] = 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.