WIP
This commit is contained in:
@@ -10,22 +10,20 @@ import (
|
||||
"vppn/m"
|
||||
)
|
||||
|
||||
type hubPoller struct {
|
||||
client *http.Client
|
||||
req *http.Request
|
||||
versions [256]int64
|
||||
localIP byte
|
||||
netName string
|
||||
handleControlMsg func(fromIP byte, msg any)
|
||||
type HubPoller struct {
|
||||
Globals
|
||||
client *http.Client
|
||||
req *http.Request
|
||||
versions [256]int64
|
||||
netName string
|
||||
}
|
||||
|
||||
func newHubPoller(
|
||||
localIP byte,
|
||||
func NewHubPoller(
|
||||
g Globals,
|
||||
netName,
|
||||
hubURL,
|
||||
apiKey string,
|
||||
handleControlMsg func(byte, any),
|
||||
) (*hubPoller, error) {
|
||||
) (*HubPoller, error) {
|
||||
u, err := url.Parse(hubURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -41,20 +39,19 @@ func newHubPoller(
|
||||
}
|
||||
req.SetBasicAuth("", apiKey)
|
||||
|
||||
return &hubPoller{
|
||||
client: client,
|
||||
req: req,
|
||||
localIP: localIP,
|
||||
netName: netName,
|
||||
handleControlMsg: handleControlMsg,
|
||||
return &HubPoller{
|
||||
Globals: g,
|
||||
client: client,
|
||||
req: req,
|
||||
netName: netName,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (hp *hubPoller) logf(s string, args ...any) {
|
||||
func (hp *HubPoller) logf(s string, args ...any) {
|
||||
log.Printf("[HubPoller] "+s, args...)
|
||||
}
|
||||
|
||||
func (hp *hubPoller) Run() {
|
||||
func (hp *HubPoller) Run() {
|
||||
state, err := loadNetworkState(hp.netName)
|
||||
if err != nil {
|
||||
hp.logf("Failed to load network state: %v", err)
|
||||
@@ -69,7 +66,7 @@ func (hp *hubPoller) Run() {
|
||||
}
|
||||
}
|
||||
|
||||
func (hp *hubPoller) pollHub() {
|
||||
func (hp *HubPoller) pollHub() {
|
||||
var state m.NetworkState
|
||||
|
||||
resp, err := hp.client.Do(hp.req)
|
||||
@@ -89,22 +86,26 @@ func (hp *hubPoller) pollHub() {
|
||||
return
|
||||
}
|
||||
|
||||
hp.applyNetworkState(state)
|
||||
|
||||
if err := storeNetworkState(hp.netName, state); err != nil {
|
||||
hp.logf("Failed to store network state: %v", err)
|
||||
}
|
||||
|
||||
hp.applyNetworkState(state)
|
||||
}
|
||||
|
||||
func (hp *hubPoller) applyNetworkState(state m.NetworkState) {
|
||||
func (hp *HubPoller) applyNetworkState(state m.NetworkState) {
|
||||
for i, peer := range state.Peers {
|
||||
if i != int(hp.localIP) {
|
||||
if peer == nil || peer.Version != hp.versions[i] {
|
||||
hp.handleControlMsg(byte(i), peerUpdateMsg{Peer: state.Peers[i]})
|
||||
if peer != nil {
|
||||
hp.versions[i] = peer.Version
|
||||
}
|
||||
}
|
||||
if i == int(hp.LocalPeerIP) {
|
||||
continue
|
||||
}
|
||||
|
||||
if peer != nil && peer.Version == hp.versions[i] {
|
||||
continue
|
||||
}
|
||||
|
||||
hp.RemotePeers[i].Load().HandlePeerUpdate(peerUpdateMsg{Peer: state.Peers[i]})
|
||||
if peer != nil {
|
||||
hp.versions[i] = peer.Version
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user