This commit is contained in:
crusader 2018-08-30 03:20:51 +09:00
parent 92370bffc2
commit 42c3ad9ab4
4 changed files with 60 additions and 101 deletions

View File

@ -1,7 +1,6 @@
package discovery package discovery
import ( import (
"fmt"
"sync" "sync"
"time" "time"
@ -144,70 +143,12 @@ func (d *ofDiscoverer) discover(req types.DiscoveryRequest) {
session.ReleaseDiscoverySession(s) session.ReleaseDiscoverySession(s)
}() }()
params := req.Params() if err := s.InitWithRequest(req); nil != err {
req.SendMessage(types.DiscoveryMessageTypeError, nil, err)
switch req.RequestType() { return
case types.DiscoveryRequestTypeHost:
if nil == params || 2 != len(params) {
req.SendMessage(types.DiscoveryMessageTypeError, nil, fmt.Errorf("Parameter is not valid"))
break
}
zone, ok := params[0].(*omd.Zone)
if !ok {
req.SendMessage(types.DiscoveryMessageTypeError, nil, fmt.Errorf("Zone of parameter is not valid"))
break
}
dh, ok := params[1].(*omd.DiscoverHost)
if !ok {
req.SendMessage(types.DiscoveryMessageTypeError, nil, fmt.Errorf("DiscoverHost of parameter is not valid"))
break
}
s.InitWithDiscoverHost(req, zone, dh)
case types.DiscoveryRequestTypePort:
if nil == params || 2 != len(params) {
req.SendMessage(types.DiscoveryMessageTypeError, nil, fmt.Errorf("Parameter is not valid"))
break
}
host, ok := params[0].(*omd.Host)
if !ok {
req.SendMessage(types.DiscoveryMessageTypeError, nil, fmt.Errorf("Host of parameter is not valid"))
break
}
dp, ok := params[1].(*omd.DiscoverPort)
if !ok {
req.SendMessage(types.DiscoveryMessageTypeError, nil, fmt.Errorf("DiscoverPort of parameter is not valid"))
break
}
s.InitWithDiscoverPort(req, host, dp)
case types.DiscoveryRequestTypeService:
if nil == params || 2 != len(params) {
req.SendMessage(types.DiscoveryMessageTypeError, nil, fmt.Errorf("Parameter is not valid"))
break
}
port, ok := params[0].(*omd.Port)
if !ok {
req.SendMessage(types.DiscoveryMessageTypeError, nil, fmt.Errorf("Port of parameter is not valid"))
break
}
ds, ok := params[1].(*omd.DiscoverService)
if !ok {
req.SendMessage(types.DiscoveryMessageTypeError, nil, fmt.Errorf("DiscoverService of parameter is not valid"))
break
}
s.InitWithDiscoverService(req, port, ds)
} }
d.preDiscovery(s) d.preDiscovery(s)
d.layerDiscovery(s) d.layerDiscovery(s)
} }

View File

@ -18,9 +18,8 @@ type DiscoverySession interface {
DiscoverPort() *omd.DiscoverPort DiscoverPort() *omd.DiscoverPort
DiscoverService() *omd.DiscoverService DiscoverService() *omd.DiscoverService
InitWithDiscoverHost(request types.DiscoveryRequest, zone *omd.Zone, discoverHost *omd.DiscoverHost) InitWithRequest(request types.DiscoveryRequest) error
InitWithDiscoverPort(request types.DiscoveryRequest, host *omd.Host, discoverPort *omd.DiscoverPort)
InitWithDiscoverService(request types.DiscoveryRequest, port *omd.Port, discoverService *omd.DiscoverService)
AddHost(host *omd.Host) *omd.Host AddHost(host *omd.Host) *omd.Host
AddPort(port *omd.Port) *omd.Port AddPort(port *omd.Port) *omd.Port
AddService(service *omd.Service) *omd.Service AddService(service *omd.Service) *omd.Service
@ -42,8 +41,8 @@ type ofDiscoverySession struct {
services map[*omd.Port]map[string]map[string]*omd.Service services map[*omd.Port]map[string]map[string]*omd.Service
} }
func (ds *ofDiscoverySession) init() { func (ds *ofDiscoverySession) init(request types.DiscoveryRequest) {
ds.discoveryRequest = nil ds.discoveryRequest = request
ds.zone = nil ds.zone = nil
ds.host = nil ds.host = nil
ds.port = nil ds.port = nil
@ -56,33 +55,68 @@ func (ds *ofDiscoverySession) init() {
ds.services = make(map[*omd.Port]map[string]map[string]*omd.Service) ds.services = make(map[*omd.Port]map[string]map[string]*omd.Service)
} }
func (ds *ofDiscoverySession) initWithRequest(request types.DiscoveryRequest) { func (ds *ofDiscoverySession) InitWithRequest(request types.DiscoveryRequest) error {
ds.init() ds.init(request)
ds.discoveryRequest = request params := request.Params()
switch request.RequestType() {
case types.DiscoveryRequestTypeHost:
if nil == params || 2 != len(params) {
return fmt.Errorf("Parameter is not valid")
} }
func (ds *ofDiscoverySession) InitWithDiscoverHost(request types.DiscoveryRequest, zone *omd.Zone, discoverHost *omd.DiscoverHost) { zone, ok := params[0].(*omd.Zone)
ds.initWithRequest(request) if !ok {
return fmt.Errorf("Zone of parameter is not valid")
}
discoverHost, ok := params[1].(*omd.DiscoverHost)
if !ok {
return fmt.Errorf("DiscoverHost of parameter is not valid")
}
ds.setZone(zone) ds.setZone(zone)
ds.setDiscoverHost(discoverHost) ds.setDiscoverHost(discoverHost)
case types.DiscoveryRequestTypePort:
if nil == params || 2 != len(params) {
return fmt.Errorf("Parameter is not valid")
} }
func (ds *ofDiscoverySession) InitWithDiscoverPort(request types.DiscoveryRequest, host *omd.Host, discoverPort *omd.DiscoverPort) { host, ok := params[0].(*omd.Host)
ds.initWithRequest(request) if !ok {
return fmt.Errorf("Host of parameter is not valid")
}
discoverPort, ok := params[1].(*omd.DiscoverPort)
if !ok {
return fmt.Errorf("DiscoverPort of parameter is not valid")
}
ds.setHost(host) ds.setHost(host)
ds.setDiscoverPort(discoverPort) ds.setDiscoverPort(discoverPort)
case types.DiscoveryRequestTypeService:
if nil == params || 2 != len(params) {
return fmt.Errorf("Parameter is not valid")
} }
func (ds *ofDiscoverySession) InitWithDiscoverService(request types.DiscoveryRequest, port *omd.Port, discoverService *omd.DiscoverService) { port, ok := params[0].(*omd.Port)
ds.initWithRequest(request) if !ok {
return fmt.Errorf("Port of parameter is not valid")
}
discoverService, ok := params[1].(*omd.DiscoverService)
if !ok {
return fmt.Errorf("DiscoverService of parameter is not valid")
}
ds.setPort(port) ds.setPort(port)
ds.setDiscoverService(discoverService) ds.setDiscoverService(discoverService)
} }
return nil
}
func (ds *ofDiscoverySession) Zone() *omd.Zone { func (ds *ofDiscoverySession) Zone() *omd.Zone {
return ds.zone return ds.zone
} }

View File

@ -2,7 +2,6 @@ package session
import ( import (
omd "git.loafle.net/overflow/model/discovery" omd "git.loafle.net/overflow/model/discovery"
"git.loafle.net/overflow_scanner/probe/discovery/types"
) )
func MockDiscoverySession() *mockDiscoverySession { func MockDiscoverySession() *mockDiscoverySession {
@ -13,21 +12,6 @@ type mockDiscoverySession struct {
ofDiscoverySession ofDiscoverySession
} }
func (ds *mockDiscoverySession) InitWithDiscoverHost(request types.DiscoveryRequest, zone *omd.Zone, discoverHost *omd.DiscoverHost) {
ds.setZone(zone)
ds.setDiscoverHost(discoverHost)
}
func (ds *mockDiscoverySession) InitWithDiscoverPort(request types.DiscoveryRequest, host *omd.Host, discoverPort *omd.DiscoverPort) {
ds.setHost(host)
ds.setDiscoverPort(discoverPort)
}
func (ds *mockDiscoverySession) InitWithDiscoverService(request types.DiscoveryRequest, port *omd.Port, discoverService *omd.DiscoverService) {
ds.setPort(port)
ds.setDiscoverService(discoverService)
}
func (ds *mockDiscoverySession) AddHost(host *omd.Host) *omd.Host { func (ds *mockDiscoverySession) AddHost(host *omd.Host) *omd.Host {
return host return host
} }