This commit is contained in:
crusader 2017-11-23 18:34:07 +09:00
parent 2f67baeb46
commit 3037d18a5f
8 changed files with 78 additions and 8 deletions

View File

@ -16,5 +16,5 @@ type Port struct {
PortType string `json:"portType,omitempty"`
PortNumber int `json:"portNumber,omitempty"`
UDPLayer gopacket.Layer
UDPLayer gopacket.Layer `json:"-"`
}

View File

@ -9,8 +9,8 @@ type Zone struct {
Iface string `json:"iface"`
Mac string `json:"mac"`
hosts map[string]*Host
mtx sync.RWMutex
hosts map[string]*Host `json:"-"`
mtx sync.RWMutex `json:"-"`
}
func (z *Zone) AddHost(h *Host) {

View File

@ -4,10 +4,10 @@ import (
"fmt"
"sync"
"git.loafle.net/commons_go/util/net/cidr"
"git.loafle.net/commons_go/logging"
"git.loafle.net/commons_go/util/net/cidr"
"git.loafle.net/overflow/overflow_discovery/api/module/discovery/model"
"git.loafle.net/overflow/overflow_discovery/rpc/notify"
)
var discoverer *discovery
@ -80,6 +80,7 @@ func (d *discovery) discoverZone(dz *model.DiscoveryZone) {
func(result interface{}) {
z := result.(*model.Zone)
logging.Logger().Info(fmt.Sprintf("zone: %v", z))
d.sendResult("DiscoveryService.DiscoveredZone", z)
if nil != dz.DiscoveryHost {
cr, _ := cidr.NewCIDRRanger(z.Network)
dh := &model.DiscoveryHost{
@ -102,6 +103,7 @@ func (d *discovery) discoverHost(zone *model.Zone, dh *model.DiscoveryHost) {
h := result.(*model.Host)
zone.AddHost(h)
logging.Logger().Info(fmt.Sprintf("host: %v", h))
d.sendResult("DiscoveryService.DiscoveredHost", h)
if nil != dh.DiscoveryPort {
d.discoverPort(h, dh.DiscoveryPort)
}
@ -117,6 +119,7 @@ func (d *discovery) discoverPort(host *model.Host, dp *model.DiscoveryPort) {
func(result interface{}) {
p := result.(*model.Port)
logging.Logger().Info(fmt.Sprintf("port: %v", p))
d.sendResult("DiscoveryService.DiscoveredPort", p)
if nil != dp.DiscoveryService {
d.discoverService(p, dp.DiscoveryService)
}
@ -131,13 +134,14 @@ func (d *discovery) discoverService(port *model.Port, ds *model.DiscoveryService
},
func(result interface{}) {
s := result.(*model.Service)
d.sendResult("DiscoveryService.DiscoveredService", s)
logging.Logger().Info(fmt.Sprintf("service: %s(%s)[%s:%d]", s.ServiceName, s.CryptoType, port.Host.IP, port.PortNumber))
},
)
}
func (d *discovery) sendResult() {
func (d *discovery) sendResult(method string, args ...interface{}) {
go notify.Notifier.Notify(method, args...)
}
func (d *discovery) sendError() {

View File

@ -52,6 +52,11 @@ func scanServiceTCP(port *model.Port, ds *model.DiscoveryService, resultChan cha
if nil != s {
break
}
select {
case <-stopChan:
return true
}
}
if nil != s {

27
rpc/notify/notify.go Normal file
View File

@ -0,0 +1,27 @@
package notify
import (
"net"
"git.loafle.net/commons_go/rpc/notify"
)
func NotifyInit(conn net.Conn) {
Notifier = New()
Notifier.Start(conn)
}
func NotifyDestroy() {
Notifier.Close()
}
var Notifier notify.Notifier
func New() notify.Notifier {
nh := NewNotifyHandler()
n := notify.New(nh)
return n
}

View File

@ -0,0 +1,7 @@
package notify
import "git.loafle.net/commons_go/rpc/notify"
type NotifyHandler interface {
notify.NotifyHandler
}

View File

@ -0,0 +1,17 @@
package notify
import (
"git.loafle.net/commons_go/rpc/notify"
"git.loafle.net/commons_go/rpc/protocol/json"
)
func NewNotifyHandler() NotifyHandler {
nh := &NotifyHandlers{}
nh.Codec = json.NewClientCodec()
return nh
}
type NotifyHandlers struct {
notify.NotifyHandlers
}

View File

@ -7,6 +7,7 @@ import (
"git.loafle.net/commons_go/logging"
"git.loafle.net/overflow/overflow_discovery/discovery"
"git.loafle.net/overflow/overflow_discovery/rpc/notify"
crs "git.loafle.net/commons_go/rpc/server"
"git.loafle.net/commons_go/server"
@ -43,10 +44,18 @@ func (sh *ServerHandlers) OnConnect(conn net.Conn) (net.Conn, error) {
if conn, err = sh.ServerHandlers.OnConnect(conn); nil != err {
return nil, err
}
return newConn(conn, "jsonrpc"), nil
nConn := newConn(conn, "jsonrpc")
notify.NotifyInit(nConn)
return nConn, nil
}
func (sh *ServerHandlers) Handle(conn net.Conn, stopChan <-chan struct{}, doneChan chan<- struct{}) {
defer func() {
notify.NotifyDestroy()
}()
dConn := conn.(Conn)
contentType := dConn.GetContentType()
codec, err := sh.rpcSH.GetCodec(contentType)
@ -74,6 +83,7 @@ func (sh *ServerHandlers) Handle(conn net.Conn, stopChan <-chan struct{}, doneCh
}
func (sh *ServerHandlers) OnStop() {
discovery.DiscoveryDestroy()
}