This commit is contained in:
crusader 2017-12-02 01:17:44 +09:00
parent d938e396f9
commit 6bcb527545
6 changed files with 262 additions and 127 deletions

128
probe/_probe.go Normal file
View File

@ -0,0 +1,128 @@
package probe
// import (
// "context"
// "fmt"
// "net/http"
// "git.loafle.net/commons_go/logging"
// "git.loafle.net/overflow/overflow_probes/central/api/module"
// "git.loafle.net/overflow/overflow_probes/central/client"
// "git.loafle.net/overflow/overflow_probes/commons"
// "git.loafle.net/overflow/overflow_probes/config"
// opuu "git.loafle.net/overflow/overflow_probes/util/url"
// )
// const (
// probeEntryPoint = "/probe"
// fileEntryPoint = "/file"
// metricEntryPoint = "/metric"
// )
// type Prober interface {
// commons.Starter
// commons.Shutdowner
// }
// type probe struct {
// probeEntryURL string
// fileEntryURL string
// metricEntryURL string
// probeClient client.Client
// fileClient client.Client
// metricClient client.Client
// shutdown chan bool
// }
// func New() (Prober, error) {
// p := &probe{
// shutdown: make(chan bool),
// }
// var err error
// if p.probeEntryURL, err = opuu.Join(config.Config.Central.URL, probeEntryPoint); nil != err {
// return nil, err
// }
// if p.fileEntryURL, err = opuu.Join(config.Config.Central.URL, fileEntryPoint); nil != err {
// return nil, err
// }
// if p.metricEntryURL, err = opuu.Join(config.Config.Central.URL, metricEntryPoint); nil != err {
// return nil, err
// }
// p.probeClient = client.New()
// p.fileClient = client.New()
// p.metricClient = client.New()
// return p, nil
// }
// func (p *probe) Start() error {
// if err := p.connectToCentral(); nil != err {
// return err
// }
// p.listen()
// return nil
// }
// func (p *probe) listen() {
// go func() {
// for {
// select {
// case <-p.shutdown:
// break
// }
// }
// }()
// }
// func (p *probe) connectToCentral() error {
// var err error
// var res *http.Response
// if res, err = client.ConnectToCentralAsProbe(p.probeClient, p.probeEntryURL); nil != err {
// return err
// }
// encryptionKey := res.Header.Get(module.ProbeHeader_Probe_EncryptionKey)
// config.EncryptionKey = &encryptionKey
// p.probeClient.OnNotify(p.onNotify)
// // if _, err = client.ConnectToCentralAsProbe(p.metricClient, p.metricEntryURL); nil != err {
// // return err
// // }
// return nil
// }
// func (p *probe) sendNotifyToCentral(method string, params ...string) {
// if err := p.probeClient.Notify(method, params); nil != err {
// logging.Logger().Error(fmt.Sprintf("Probe notify: %v", err))
// }
// }
// func (p *probe) Shutdown(ctx context.Context) error {
// for {
// p.stop(fmt.Errorf("Shutdown"))
// select {
// case <-ctx.Done():
// return ctx.Err()
// }
// }
// }
// func (p *probe) stop(err error) {
// defer close(p.shutdown)
// ctx := context.Background()
// if err := p.probeClient.Shutdown(ctx); nil != err {
// logging.Logger().Error(fmt.Sprintf("Client of Probe: %v", err))
// }
// }

11
probe/client/client.go Normal file
View File

@ -0,0 +1,11 @@
package client
import (
cwfc "git.loafle.net/commons_go/websocket_fasthttp/client"
oogwc "git.loafle.net/overflow/overflow_gateway_websocket/client"
)
func NewClient(ch oogwc.ClientHandler, sb cwfc.SocketBuilder) oogwc.Client {
return oogwc.New(ch, sb)
}

View File

@ -0,0 +1,31 @@
package client
import (
"fmt"
"git.loafle.net/commons_go/logging"
crc "git.loafle.net/commons_go/rpc/client"
crr "git.loafle.net/commons_go/rpc/registry"
oogwc "git.loafle.net/overflow/overflow_gateway_websocket/client"
)
func NewClientHandler(rpcInvoker crr.RPCInvoker) oogwc.ClientHandler {
ch := &ClientHandlers{}
ch.ClientHandler = oogwc.NewClientHandler(rpcInvoker)
return ch
}
type ClientHandlers struct {
oogwc.ClientHandler
}
func (ch *ClientHandlers) Init(clientCTX crc.ClientContext) error {
logging.Logger().Info(fmt.Sprintf("Probe: client has been initialized"))
return nil
}
func (ch *ClientHandlers) Destroy(clientCTX crc.ClientContext) {
logging.Logger().Info(fmt.Sprintf("Probe: client has been destroyed"))
}

View File

@ -0,0 +1,51 @@
package client
import (
"net/http"
"git.loafle.net/commons_go/logging"
cwfc "git.loafle.net/commons_go/websocket_fasthttp/client"
oocmn "git.loafle.net/overflow/overflow_commons_go/modules/noauthprobe"
oopar "git.loafle.net/overflow/overflow_probes/auth/rpc"
oopcc "git.loafle.net/overflow/overflow_probes/central/client"
)
func NewSocketBuilder(napService *oopar.NoAuthProbeService) cwfc.SocketBuilder {
sb := &SocketBuilders{
napService: napService,
}
sb.SocketBuilders = oopcc.NewSocketBuilder(oocmn.HTTPEntry_NoAuthProbe)
if nil == sb.SocketBuilders {
return nil
}
return sb
}
type SocketBuilders struct {
*oopcc.SocketBuilders
napService *oopar.NoAuthProbeService
}
func (sb *SocketBuilders) SocketHandler() cwfc.SocketHandler {
return newSocketHandler(sb.napService)
}
func (sb *SocketBuilders) GetRequestHeader() http.Header {
h := sb.napService.GetRequestHeader()
header := http.Header{}
for k, v := range h {
header[k] = v
}
return header
}
func (sb *SocketBuilders) Validate() {
sb.SocketBuilders.Validate()
if nil == sb.napService {
logging.Logger().Panic("Auth: NoAuthProbeService must be specified")
}
}

View File

@ -0,0 +1,41 @@
package client
import (
"fmt"
"net/http"
"git.loafle.net/commons_go/logging"
cwfc "git.loafle.net/commons_go/websocket_fasthttp/client"
ooccn "git.loafle.net/overflow/overflow_commons_go/config/noauthprobe"
oocmn "git.loafle.net/overflow/overflow_commons_go/modules/noauthprobe"
oopar "git.loafle.net/overflow/overflow_probes/auth/rpc"
)
type SocketHandlers struct {
cwfc.SocketHandlers
napService *oopar.NoAuthProbeService
}
func (sh *SocketHandlers) OnConnect(socketContext cwfc.SocketContext, res *http.Response) {
logging.Logger().Info(fmt.Sprintf("Auth: client has been connected res[%v]", res))
switch sh.napService.Config.State() {
case ooccn.NoAuthProbeStateTypeNotRegisterd:
tempProbeKey := res.Header.Get(oocmn.HTTPResponseHeaderKey_NoAuthProbe_SetTempProbeKey)
sh.napService.SetTempProbeKey(tempProbeKey)
case ooccn.NoAuthProbeStateTypeRegisterd:
}
}
func (sh *SocketHandlers) OnDisconnect(soc cwfc.Socket) {
logging.Logger().Info(fmt.Sprintf("Auth: client has been disconnected soc[%v]", soc))
}
func newSocketHandler(napService *oopar.NoAuthProbeService) cwfc.SocketHandler {
return &SocketHandlers{
napService: napService,
}
}

View File

@ -1,128 +1 @@
package probe
// import (
// "context"
// "fmt"
// "net/http"
// "git.loafle.net/commons_go/logging"
// "git.loafle.net/overflow/overflow_probes/central/api/module"
// "git.loafle.net/overflow/overflow_probes/central/client"
// "git.loafle.net/overflow/overflow_probes/commons"
// "git.loafle.net/overflow/overflow_probes/config"
// opuu "git.loafle.net/overflow/overflow_probes/util/url"
// )
// const (
// probeEntryPoint = "/probe"
// fileEntryPoint = "/file"
// metricEntryPoint = "/metric"
// )
// type Prober interface {
// commons.Starter
// commons.Shutdowner
// }
// type probe struct {
// probeEntryURL string
// fileEntryURL string
// metricEntryURL string
// probeClient client.Client
// fileClient client.Client
// metricClient client.Client
// shutdown chan bool
// }
// func New() (Prober, error) {
// p := &probe{
// shutdown: make(chan bool),
// }
// var err error
// if p.probeEntryURL, err = opuu.Join(config.Config.Central.URL, probeEntryPoint); nil != err {
// return nil, err
// }
// if p.fileEntryURL, err = opuu.Join(config.Config.Central.URL, fileEntryPoint); nil != err {
// return nil, err
// }
// if p.metricEntryURL, err = opuu.Join(config.Config.Central.URL, metricEntryPoint); nil != err {
// return nil, err
// }
// p.probeClient = client.New()
// p.fileClient = client.New()
// p.metricClient = client.New()
// return p, nil
// }
// func (p *probe) Start() error {
// if err := p.connectToCentral(); nil != err {
// return err
// }
// p.listen()
// return nil
// }
// func (p *probe) listen() {
// go func() {
// for {
// select {
// case <-p.shutdown:
// break
// }
// }
// }()
// }
// func (p *probe) connectToCentral() error {
// var err error
// var res *http.Response
// if res, err = client.ConnectToCentralAsProbe(p.probeClient, p.probeEntryURL); nil != err {
// return err
// }
// encryptionKey := res.Header.Get(module.ProbeHeader_Probe_EncryptionKey)
// config.EncryptionKey = &encryptionKey
// p.probeClient.OnNotify(p.onNotify)
// // if _, err = client.ConnectToCentralAsProbe(p.metricClient, p.metricEntryURL); nil != err {
// // return err
// // }
// return nil
// }
// func (p *probe) sendNotifyToCentral(method string, params ...string) {
// if err := p.probeClient.Notify(method, params); nil != err {
// logging.Logger().Error(fmt.Sprintf("Probe notify: %v", err))
// }
// }
// func (p *probe) Shutdown(ctx context.Context) error {
// for {
// p.stop(fmt.Errorf("Shutdown"))
// select {
// case <-ctx.Done():
// return ctx.Err()
// }
// }
// }
// func (p *probe) stop(err error) {
// defer close(p.shutdown)
// ctx := context.Background()
// if err := p.probeClient.Shutdown(ctx); nil != err {
// logging.Logger().Error(fmt.Sprintf("Client of Probe: %v", err))
// }
// }