From 538ee13669bed5c3637888d0b3ae9e49d833d20c Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Wed, 1 Sep 2021 06:16:57 +0500 Subject: [PATCH 1/4] Add type core.AddHandlerFunc --- src/admin/admin.go | 6 +++--- src/core/api.go | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/admin/admin.go b/src/admin/admin.go index d41a48e1..ce831395 100644 --- a/src/admin/admin.go +++ b/src/admin/admin.go @@ -38,8 +38,8 @@ type AdminSocketResponse struct { } type handler struct { - args []string // List of human-readable argument names - handler func(json.RawMessage) (interface{}, error) // First is input map, second is output + args []string // List of human-readable argument names + handler core.AddHandlerFunc // First is input map, second is output } 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. -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 { return errors.New("handler already exists") } diff --git a/src/core/api.go b/src/core/api.go index c312923d..ae13d499 100644 --- a/src/core/api.go +++ b/src/core/api.go @@ -242,9 +242,11 @@ func (c *Core) PublicKey() ed25519.PublicKey { // Hack to get the admin stuff working, TODO something cleaner 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. // It sets the admin handler for NodeInfo and the Debug admin functions. func (c *Core) SetAdmin(a AddHandler) error { From a5f2ba80a2d4aecd61f06e85bc1e69995d821f75 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Wed, 1 Sep 2021 06:24:25 +0500 Subject: [PATCH 2/4] Organize code in "src/core/proto.go" --- src/core/proto.go | 83 ++++++++++++++++++++++++++++------------------- 1 file changed, 49 insertions(+), 34 deletions(-) diff --git a/src/core/proto.go b/src/core/proto.go index e60caeff..da9d9b99 100644 --- a/src/core/proto.go +++ b/src/core/proto.go @@ -34,21 +34,26 @@ type keyArray [ed25519.PublicKeySize]byte type protoHandler struct { phony.Inbox - core *Core + nodeinfo nodeinfo - sreqs map[keyArray]*reqInfo - preqs map[keyArray]*reqInfo - dreqs map[keyArray]*reqInfo + core *Core + + getSelfRequests map[keyArray]*reqInfo + getPeersRequests map[keyArray]*reqInfo + getDHTRequests map[keyArray]*reqInfo } func (p *protoHandler) init(core *Core) { p.core = core p.nodeinfo.init(p) - p.sreqs = make(map[keyArray]*reqInfo) - p.preqs = make(map[keyArray]*reqInfo) - p.dreqs = make(map[keyArray]*reqInfo) + + p.getSelfRequests = make(map[keyArray]*reqInfo) + p.getPeersRequests = make(map[keyArray]*reqInfo) + p.getDHTRequests = make(map[keyArray]*reqInfo) } +// Common functions + func (p *protoHandler) handleProto(from phony.Actor, key keyArray, bs []byte) { if len(bs) == 0 { 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)) { p.Act(nil, func() { - if info := p.sreqs[key]; info != nil { + if info := p.getSelfRequests[key]; info != nil { info.timer.Stop() - delete(p.sreqs, key) + delete(p.getSelfRequests, key) } info := new(reqInfo) info.callback = callback info.timer = time.AfterFunc(time.Minute, func() { p.Act(nil, func() { - if p.sreqs[key] == info { - delete(p.sreqs, key) + if p.getSelfRequests[key] == info { + delete(p.getSelfRequests, key) } }) }) - p.sreqs[key] = info + p.getSelfRequests[key] = info p._sendDebug(key, typeDebugGetSelfRequest, nil) }) } @@ -119,29 +131,31 @@ func (p *protoHandler) _handleGetSelfRequest(key keyArray) { } func (p *protoHandler) _handleGetSelfResponse(key keyArray, bs []byte) { - if info := p.sreqs[key]; info != nil { + if info := p.getSelfRequests[key]; info != nil { info.timer.Stop() info.callback(bs) - delete(p.sreqs, key) + delete(p.getSelfRequests, key) } } +// Get peers + func (p *protoHandler) sendGetPeersRequest(key keyArray, callback func([]byte)) { p.Act(nil, func() { - if info := p.preqs[key]; info != nil { + if info := p.getPeersRequests[key]; info != nil { info.timer.Stop() - delete(p.preqs, key) + delete(p.getPeersRequests, key) } info := new(reqInfo) info.callback = callback info.timer = time.AfterFunc(time.Minute, func() { p.Act(nil, func() { - if p.preqs[key] == info { - delete(p.preqs, key) + if p.getPeersRequests[key] == info { + delete(p.getPeersRequests, key) } }) }) - p.preqs[key] = info + p.getPeersRequests[key] = info p._sendDebug(key, typeDebugGetPeersRequest, nil) }) } @@ -161,29 +175,31 @@ func (p *protoHandler) _handleGetPeersRequest(key keyArray) { } func (p *protoHandler) _handleGetPeersResponse(key keyArray, bs []byte) { - if info := p.preqs[key]; info != nil { + if info := p.getPeersRequests[key]; info != nil { info.timer.Stop() info.callback(bs) - delete(p.preqs, key) + delete(p.getPeersRequests, key) } } +// Get DHT + func (p *protoHandler) sendGetDHTRequest(key keyArray, callback func([]byte)) { p.Act(nil, func() { - if info := p.dreqs[key]; info != nil { + if info := p.getDHTRequests[key]; info != nil { info.timer.Stop() - delete(p.dreqs, key) + delete(p.getDHTRequests, key) } info := new(reqInfo) info.callback = callback info.timer = time.AfterFunc(time.Minute, func() { p.Act(nil, func() { - if p.dreqs[key] == info { - delete(p.dreqs, key) + if p.getDHTRequests[key] == info { + delete(p.getDHTRequests, key) } }) }) - p.dreqs[key] = info + p.getDHTRequests[key] = info p._sendDebug(key, typeDebugGetDHTRequest, nil) }) } @@ -203,19 +219,14 @@ func (p *protoHandler) _handleGetDHTRequest(key keyArray) { } func (p *protoHandler) _handleGetDHTResponse(key keyArray, bs []byte) { - if info := p.dreqs[key]; info != nil { + if info := p.getDHTRequests[key]; info != nil { info.timer.Stop() info.callback(bs) - delete(p.dreqs, key) + delete(p.getDHTRequests, key) } } -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[:])) -} - -// Admin socket stuff +// Admin socket stuff for "Get self" type DebugGetSelfRequest struct { 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 { 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 { Key string `json:"key"` } From 3c89781057bed6e87daf3f40523c21d0a569d59b Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Wed, 1 Sep 2021 07:57:45 +0500 Subject: [PATCH 3/4] Align and reorder code for lesser diff --- src/core/proto.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/proto.go b/src/core/proto.go index da9d9b99..1151d006 100644 --- a/src/core/proto.go +++ b/src/core/proto.go @@ -35,8 +35,8 @@ type keyArray [ed25519.PublicKeySize]byte type protoHandler struct { phony.Inbox + core *Core nodeinfo nodeinfo - core *Core getSelfRequests map[keyArray]*reqInfo getPeersRequests map[keyArray]*reqInfo From 571186ca772e96b1f4226b2dd56252b9aa3e5a30 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Fri, 3 Sep 2021 01:45:30 +0500 Subject: [PATCH 4/4] Rename protohandler attributes --- src/core/proto.go | 54 +++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/core/proto.go b/src/core/proto.go index 1151d006..3045972e 100644 --- a/src/core/proto.go +++ b/src/core/proto.go @@ -38,18 +38,18 @@ type protoHandler struct { core *Core nodeinfo nodeinfo - getSelfRequests map[keyArray]*reqInfo - getPeersRequests map[keyArray]*reqInfo - getDHTRequests map[keyArray]*reqInfo + selfRequests map[keyArray]*reqInfo + peersRequests map[keyArray]*reqInfo + dhtRequests map[keyArray]*reqInfo } func (p *protoHandler) init(core *Core) { p.core = core p.nodeinfo.init(p) - p.getSelfRequests = make(map[keyArray]*reqInfo) - p.getPeersRequests = make(map[keyArray]*reqInfo) - p.getDHTRequests = make(map[keyArray]*reqInfo) + p.selfRequests = make(map[keyArray]*reqInfo) + p.peersRequests = make(map[keyArray]*reqInfo) + p.dhtRequests = make(map[keyArray]*reqInfo) } // Common functions @@ -99,20 +99,20 @@ func (p *protoHandler) _sendDebug(key keyArray, dType uint8, data []byte) { func (p *protoHandler) sendGetSelfRequest(key keyArray, callback func([]byte)) { p.Act(nil, func() { - if info := p.getSelfRequests[key]; info != nil { + if info := p.selfRequests[key]; info != nil { info.timer.Stop() - delete(p.getSelfRequests, key) + delete(p.selfRequests, key) } info := new(reqInfo) info.callback = callback info.timer = time.AfterFunc(time.Minute, func() { p.Act(nil, func() { - if p.getSelfRequests[key] == info { - delete(p.getSelfRequests, key) + if p.selfRequests[key] == info { + delete(p.selfRequests, key) } }) }) - p.getSelfRequests[key] = info + p.selfRequests[key] = info p._sendDebug(key, typeDebugGetSelfRequest, nil) }) } @@ -131,10 +131,10 @@ func (p *protoHandler) _handleGetSelfRequest(key keyArray) { } func (p *protoHandler) _handleGetSelfResponse(key keyArray, bs []byte) { - if info := p.getSelfRequests[key]; info != nil { + if info := p.selfRequests[key]; info != nil { info.timer.Stop() info.callback(bs) - delete(p.getSelfRequests, key) + delete(p.selfRequests, key) } } @@ -142,20 +142,20 @@ func (p *protoHandler) _handleGetSelfResponse(key keyArray, bs []byte) { func (p *protoHandler) sendGetPeersRequest(key keyArray, callback func([]byte)) { p.Act(nil, func() { - if info := p.getPeersRequests[key]; info != nil { + if info := p.peersRequests[key]; info != nil { info.timer.Stop() - delete(p.getPeersRequests, key) + delete(p.peersRequests, key) } info := new(reqInfo) info.callback = callback info.timer = time.AfterFunc(time.Minute, func() { p.Act(nil, func() { - if p.getPeersRequests[key] == info { - delete(p.getPeersRequests, key) + if p.peersRequests[key] == info { + delete(p.peersRequests, key) } }) }) - p.getPeersRequests[key] = info + p.peersRequests[key] = info p._sendDebug(key, typeDebugGetPeersRequest, nil) }) } @@ -175,10 +175,10 @@ func (p *protoHandler) _handleGetPeersRequest(key keyArray) { } func (p *protoHandler) _handleGetPeersResponse(key keyArray, bs []byte) { - if info := p.getPeersRequests[key]; info != nil { + if info := p.peersRequests[key]; info != nil { info.timer.Stop() info.callback(bs) - delete(p.getPeersRequests, key) + delete(p.peersRequests, key) } } @@ -186,20 +186,20 @@ func (p *protoHandler) _handleGetPeersResponse(key keyArray, bs []byte) { func (p *protoHandler) sendGetDHTRequest(key keyArray, callback func([]byte)) { p.Act(nil, func() { - if info := p.getDHTRequests[key]; info != nil { + if info := p.dhtRequests[key]; info != nil { info.timer.Stop() - delete(p.getDHTRequests, key) + delete(p.dhtRequests, key) } info := new(reqInfo) info.callback = callback info.timer = time.AfterFunc(time.Minute, func() { p.Act(nil, func() { - if p.getDHTRequests[key] == info { - delete(p.getDHTRequests, key) + if p.dhtRequests[key] == info { + delete(p.dhtRequests, key) } }) }) - p.getDHTRequests[key] = info + p.dhtRequests[key] = info p._sendDebug(key, typeDebugGetDHTRequest, nil) }) } @@ -219,10 +219,10 @@ func (p *protoHandler) _handleGetDHTRequest(key keyArray) { } func (p *protoHandler) _handleGetDHTResponse(key keyArray, bs []byte) { - if info := p.getDHTRequests[key]; info != nil { + if info := p.dhtRequests[key]; info != nil { info.timer.Stop() info.callback(bs) - delete(p.getDHTRequests, key) + delete(p.dhtRequests, key) } }