probe_gateway_metric/servlet/data-servlet.go

139 lines
3.1 KiB
Go
Raw Permalink Normal View History

2018-05-11 03:54:30 +00:00
package servlet
import (
"context"
2018-05-11 08:24:12 +00:00
"encoding/json"
2018-05-11 03:54:30 +00:00
"fmt"
"github.com/valyala/fasthttp"
logging "git.loafle.net/commons/logging-go"
"git.loafle.net/commons/server-go"
2018-06-30 17:38:35 +00:00
css "git.loafle.net/commons/server-go/socket"
2018-05-11 03:54:30 +00:00
cssw "git.loafle.net/commons/server-go/socket/web"
occp "git.loafle.net/overflow/commons-go/config/probe"
2018-05-11 08:24:12 +00:00
ocmd "git.loafle.net/overflow/commons-go/model/data"
oe "git.loafle.net/overflow/external-go"
oeg "git.loafle.net/overflow/external-go/grpc"
oek "git.loafle.net/overflow/external-go/kafka"
2018-05-11 03:54:30 +00:00
og "git.loafle.net/overflow/gateway"
)
type DataServlet interface {
cssw.Servlet
}
type DataServlets struct {
cssw.Servlets
}
func (s *DataServlets) Init(serverCtx server.ServerCtx) error {
if err := s.Servlets.Init(serverCtx); nil != err {
return err
}
return nil
}
func (s *DataServlets) OnStart(serverCtx server.ServerCtx) error {
if err := s.Servlets.OnStart(serverCtx); nil != err {
return err
}
return nil
}
func (s *DataServlets) OnStop(serverCtx server.ServerCtx) {
s.Servlets.OnStop(serverCtx)
}
func (s *DataServlets) Destroy(serverCtx server.ServerCtx) {
s.Servlets.Destroy(serverCtx)
}
func (s *DataServlets) Handshake(servletCtx server.ServletCtx, ctx *fasthttp.RequestCtx) (*fasthttp.ResponseHeader, error) {
// probe key extraction
bMethod := ctx.Request.Header.Peek(occp.HTTPRequestHeaderKey_Probe_Method)
if nil == bMethod {
return nil, fmt.Errorf("Unexpected probe method: %v", bMethod)
}
method := string(bMethod)
switch method {
case occp.HTTPRequestHeaderValue_Probe_Method_Connect:
default:
return nil, fmt.Errorf("Unexpected probe method: %s", method)
}
bProbeKey := ctx.Request.Header.Peek(occp.HTTPRequestHeaderKey_Probe_ProbeKey)
if nil == bProbeKey {
return nil, fmt.Errorf("Unexpected probe key : %v", bProbeKey)
}
probeKey := string(bProbeKey)
grpcCTX := context.Background()
2018-05-11 08:24:12 +00:00
_, err := oeg.Exec(grpcCTX, "ProbeService.readByProbeKey", probeKey)
2018-05-11 03:54:30 +00:00
if nil != err {
return nil, fmt.Errorf("grpc call Error: %s", err.Error())
}
servletCtx.SetAttribute(og.SessionIDKey, probeKey)
2018-05-11 08:24:12 +00:00
servletCtx.SetAttribute(og.SessionClientTypeKey, oe.PROBE)
2018-05-11 03:54:30 +00:00
servletCtx.SetAttribute(og.SessionTargetIDKey, probeKey)
return nil, nil
}
2018-06-30 17:38:35 +00:00
func (s *DataServlets) OnConnect(servletCtx server.ServletCtx, conn css.Conn) {
2018-05-11 03:54:30 +00:00
s.Servlets.OnConnect(servletCtx, conn)
}
func (s *DataServlets) OnDisconnect(servletCtx server.ServletCtx) {
s.Servlets.OnDisconnect(servletCtx)
}
func (s *DataServlets) Handle(servletCtx server.ServletCtx,
stopChan <-chan struct{}, doneChan chan<- struct{},
2018-06-30 17:38:35 +00:00
readChan <-chan css.SocketMessage, writeChan chan<- css.SocketMessage) {
2018-05-11 03:54:30 +00:00
defer func() {
doneChan <- struct{}{}
}()
var (
2018-06-30 17:38:35 +00:00
// messageType int
message []byte
err error
2018-05-11 03:54:30 +00:00
)
for {
select {
2018-06-30 17:38:35 +00:00
case socketMessage, ok := <-readChan:
2018-05-11 03:54:30 +00:00
if !ok {
return
}
2018-06-30 17:38:35 +00:00
_, message = socketMessage()
2018-05-11 08:24:12 +00:00
m := &ocmd.Metric{}
2018-06-30 17:38:35 +00:00
err = json.Unmarshal(message, m)
2018-05-11 08:24:12 +00:00
if nil != err {
logging.Logger().Error(err)
break
}
2018-07-04 10:44:51 +00:00
err = oek.Write("metric", []byte(m.SensorID), message)
2018-05-11 03:54:30 +00:00
if nil != err {
logging.Logger().Error(err)
break
}
case <-stopChan:
return
}
}
}