Merge pull request #835 from kotovalexarian/test-and-refactor-proto-handler

Really tiny refactoring of "src/core"
This commit is contained in:
Arceliar 2021-09-24 04:43:06 -05:00 committed by GitHub
commit f2d1eff8f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 37 deletions

View File

@ -39,7 +39,7 @@ type AdminSocketResponse struct {
type handler struct { type handler struct {
args []string // List of human-readable argument names args []string // List of human-readable argument names
handler func(json.RawMessage) (interface{}, error) // First is input map, second is output handler core.AddHandlerFunc // First is input map, second is output
} }
type ListResponse struct { type ListResponse struct {
@ -51,7 +51,7 @@ type ListEntry struct {
} }
// AddHandler is called for each admin function to add the handler and help documentation to the API. // AddHandler is called for each admin function to add the handler and help documentation to the API.
func (a *AdminSocket) AddHandler(name string, args []string, handlerfunc func(json.RawMessage) (interface{}, error)) error { func (a *AdminSocket) AddHandler(name string, args []string, handlerfunc core.AddHandlerFunc) error {
if _, ok := a.handlers[strings.ToLower(name)]; ok { if _, ok := a.handlers[strings.ToLower(name)]; ok {
return errors.New("handler already exists") return errors.New("handler already exists")
} }

View File

@ -242,9 +242,11 @@ func (c *Core) PublicKey() ed25519.PublicKey {
// Hack to get the admin stuff working, TODO something cleaner // Hack to get the admin stuff working, TODO something cleaner
type AddHandler interface { type AddHandler interface {
AddHandler(name string, args []string, handlerfunc func(json.RawMessage) (interface{}, error)) error AddHandler(name string, args []string, handlerfunc AddHandlerFunc) error
} }
type AddHandlerFunc func(json.RawMessage) (interface{}, error)
// SetAdmin must be called after Init and before Start. // SetAdmin must be called after Init and before Start.
// It sets the admin handler for NodeInfo and the Debug admin functions. // It sets the admin handler for NodeInfo and the Debug admin functions.
func (c *Core) SetAdmin(a AddHandler) error { func (c *Core) SetAdmin(a AddHandler) error {

View File

@ -34,21 +34,26 @@ type keyArray [ed25519.PublicKeySize]byte
type protoHandler struct { type protoHandler struct {
phony.Inbox phony.Inbox
core *Core core *Core
nodeinfo nodeinfo nodeinfo nodeinfo
sreqs map[keyArray]*reqInfo
preqs map[keyArray]*reqInfo selfRequests map[keyArray]*reqInfo
dreqs map[keyArray]*reqInfo peersRequests map[keyArray]*reqInfo
dhtRequests map[keyArray]*reqInfo
} }
func (p *protoHandler) init(core *Core) { func (p *protoHandler) init(core *Core) {
p.core = core p.core = core
p.nodeinfo.init(p) p.nodeinfo.init(p)
p.sreqs = make(map[keyArray]*reqInfo)
p.preqs = make(map[keyArray]*reqInfo) p.selfRequests = make(map[keyArray]*reqInfo)
p.dreqs = make(map[keyArray]*reqInfo) p.peersRequests = make(map[keyArray]*reqInfo)
p.dhtRequests = make(map[keyArray]*reqInfo)
} }
// Common functions
func (p *protoHandler) handleProto(from phony.Actor, key keyArray, bs []byte) { func (p *protoHandler) handleProto(from phony.Actor, key keyArray, bs []byte) {
if len(bs) == 0 { if len(bs) == 0 {
return return
@ -85,22 +90,29 @@ func (p *protoHandler) _handleDebug(key keyArray, bs []byte) {
} }
} }
func (p *protoHandler) _sendDebug(key keyArray, dType uint8, data []byte) {
bs := append([]byte{typeSessionProto, typeProtoDebug, dType}, data...)
_, _ = p.core.PacketConn.WriteTo(bs, iwt.Addr(key[:]))
}
// Get self
func (p *protoHandler) sendGetSelfRequest(key keyArray, callback func([]byte)) { func (p *protoHandler) sendGetSelfRequest(key keyArray, callback func([]byte)) {
p.Act(nil, func() { p.Act(nil, func() {
if info := p.sreqs[key]; info != nil { if info := p.selfRequests[key]; info != nil {
info.timer.Stop() info.timer.Stop()
delete(p.sreqs, key) delete(p.selfRequests, key)
} }
info := new(reqInfo) info := new(reqInfo)
info.callback = callback info.callback = callback
info.timer = time.AfterFunc(time.Minute, func() { info.timer = time.AfterFunc(time.Minute, func() {
p.Act(nil, func() { p.Act(nil, func() {
if p.sreqs[key] == info { if p.selfRequests[key] == info {
delete(p.sreqs, key) delete(p.selfRequests, key)
} }
}) })
}) })
p.sreqs[key] = info p.selfRequests[key] = info
p._sendDebug(key, typeDebugGetSelfRequest, nil) p._sendDebug(key, typeDebugGetSelfRequest, nil)
}) })
} }
@ -119,29 +131,31 @@ func (p *protoHandler) _handleGetSelfRequest(key keyArray) {
} }
func (p *protoHandler) _handleGetSelfResponse(key keyArray, bs []byte) { func (p *protoHandler) _handleGetSelfResponse(key keyArray, bs []byte) {
if info := p.sreqs[key]; info != nil { if info := p.selfRequests[key]; info != nil {
info.timer.Stop() info.timer.Stop()
info.callback(bs) info.callback(bs)
delete(p.sreqs, key) delete(p.selfRequests, key)
} }
} }
// Get peers
func (p *protoHandler) sendGetPeersRequest(key keyArray, callback func([]byte)) { func (p *protoHandler) sendGetPeersRequest(key keyArray, callback func([]byte)) {
p.Act(nil, func() { p.Act(nil, func() {
if info := p.preqs[key]; info != nil { if info := p.peersRequests[key]; info != nil {
info.timer.Stop() info.timer.Stop()
delete(p.preqs, key) delete(p.peersRequests, key)
} }
info := new(reqInfo) info := new(reqInfo)
info.callback = callback info.callback = callback
info.timer = time.AfterFunc(time.Minute, func() { info.timer = time.AfterFunc(time.Minute, func() {
p.Act(nil, func() { p.Act(nil, func() {
if p.preqs[key] == info { if p.peersRequests[key] == info {
delete(p.preqs, key) delete(p.peersRequests, key)
} }
}) })
}) })
p.preqs[key] = info p.peersRequests[key] = info
p._sendDebug(key, typeDebugGetPeersRequest, nil) p._sendDebug(key, typeDebugGetPeersRequest, nil)
}) })
} }
@ -161,29 +175,31 @@ func (p *protoHandler) _handleGetPeersRequest(key keyArray) {
} }
func (p *protoHandler) _handleGetPeersResponse(key keyArray, bs []byte) { func (p *protoHandler) _handleGetPeersResponse(key keyArray, bs []byte) {
if info := p.preqs[key]; info != nil { if info := p.peersRequests[key]; info != nil {
info.timer.Stop() info.timer.Stop()
info.callback(bs) info.callback(bs)
delete(p.preqs, key) delete(p.peersRequests, key)
} }
} }
// Get DHT
func (p *protoHandler) sendGetDHTRequest(key keyArray, callback func([]byte)) { func (p *protoHandler) sendGetDHTRequest(key keyArray, callback func([]byte)) {
p.Act(nil, func() { p.Act(nil, func() {
if info := p.dreqs[key]; info != nil { if info := p.dhtRequests[key]; info != nil {
info.timer.Stop() info.timer.Stop()
delete(p.dreqs, key) delete(p.dhtRequests, key)
} }
info := new(reqInfo) info := new(reqInfo)
info.callback = callback info.callback = callback
info.timer = time.AfterFunc(time.Minute, func() { info.timer = time.AfterFunc(time.Minute, func() {
p.Act(nil, func() { p.Act(nil, func() {
if p.dreqs[key] == info { if p.dhtRequests[key] == info {
delete(p.dreqs, key) delete(p.dhtRequests, key)
} }
}) })
}) })
p.dreqs[key] = info p.dhtRequests[key] = info
p._sendDebug(key, typeDebugGetDHTRequest, nil) p._sendDebug(key, typeDebugGetDHTRequest, nil)
}) })
} }
@ -203,19 +219,14 @@ func (p *protoHandler) _handleGetDHTRequest(key keyArray) {
} }
func (p *protoHandler) _handleGetDHTResponse(key keyArray, bs []byte) { func (p *protoHandler) _handleGetDHTResponse(key keyArray, bs []byte) {
if info := p.dreqs[key]; info != nil { if info := p.dhtRequests[key]; info != nil {
info.timer.Stop() info.timer.Stop()
info.callback(bs) info.callback(bs)
delete(p.dreqs, key) delete(p.dhtRequests, key)
} }
} }
func (p *protoHandler) _sendDebug(key keyArray, dType uint8, data []byte) { // Admin socket stuff for "Get self"
bs := append([]byte{typeSessionProto, typeProtoDebug, dType}, data...)
_, _ = p.core.PacketConn.WriteTo(bs, iwt.Addr(key[:]))
}
// Admin socket stuff
type DebugGetSelfRequest struct { type DebugGetSelfRequest struct {
Key string `json:"key"` Key string `json:"key"`
@ -255,6 +266,8 @@ func (p *protoHandler) getSelfHandler(in json.RawMessage) (interface{}, error) {
} }
} }
// Admin socket stuff for "Get peers"
type DebugGetPeersRequest struct { type DebugGetPeersRequest struct {
Key string `json:"key"` Key string `json:"key"`
} }
@ -303,6 +316,8 @@ func (p *protoHandler) getPeersHandler(in json.RawMessage) (interface{}, error)
} }
} }
// Admin socket stuff for "Get DHT"
type DebugGetDHTRequest struct { type DebugGetDHTRequest struct {
Key string `json:"key"` Key string `json:"key"`
} }