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
import (
"fmt"
"sync"
"time"
@ -144,70 +143,12 @@ func (d *ofDiscoverer) discover(req types.DiscoveryRequest) {
session.ReleaseDiscoverySession(s)
}()
params := req.Params()
switch req.RequestType() {
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)
if err := s.InitWithRequest(req); nil != err {
req.SendMessage(types.DiscoveryMessageTypeError, nil, err)
return
}
d.preDiscovery(s)
d.layerDiscovery(s)
}

View File

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

View File

@ -2,7 +2,6 @@ package session
import (
omd "git.loafle.net/overflow/model/discovery"
"git.loafle.net/overflow_scanner/probe/discovery/types"
)
func MockDiscoverySession() *mockDiscoverySession {
@ -13,21 +12,6 @@ type mockDiscoverySession struct {
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 {
return host
}