probe/server/server-handler.go

81 lines
1.6 KiB
Go
Raw Normal View History

2018-08-23 09:21:48 +00:00
package server
import (
"fmt"
"net"
"reflect"
2018-08-23 10:00:34 +00:00
"go.uber.org/zap"
olog "git.loafle.net/overflow/log-go"
2018-08-23 09:21:48 +00:00
"git.loafle.net/overflow/server-go"
ossw "git.loafle.net/overflow/server-go/socket/web"
)
type ServerHandler interface {
ossw.ServerHandler
}
type ServerHandlers struct {
ossw.ServerHandlers
Services []interface{}
OrderedServices []reflect.Type
}
func (sh *ServerHandlers) Init(serverCtx server.ServerCtx) error {
if err := sh.ServerHandlers.Init(serverCtx); nil != err {
return err
}
return nil
}
func (sh *ServerHandlers) OnStart(serverCtx server.ServerCtx) error {
if err := sh.ServerHandlers.OnStart(serverCtx); nil != err {
return err
}
return nil
}
func (sh *ServerHandlers) OnStop(serverCtx server.ServerCtx) {
sh.ServerHandlers.OnStop(serverCtx)
}
func (sh *ServerHandlers) Destroy(serverCtx server.ServerCtx) {
sh.ServerHandlers.Destroy(serverCtx)
}
func (sh *ServerHandlers) Listener(serverCtx server.ServerCtx) (net.Listener, error) {
for i := 60000; i < 61000; i++ {
addr := fmt.Sprintf("localhost:%d", i)
l, err := net.Listen("tcp", addr)
if nil == err {
2018-08-23 10:00:34 +00:00
olog.Logger().Debug("Listener", zap.Int("port", i))
2018-08-23 09:21:48 +00:00
// config.ProbePortNumber = int64(i)
return l, nil
}
}
return nil, fmt.Errorf("Cannot find availrable port")
}
func (sh *ServerHandlers) Validate() error {
if err := sh.ServerHandlers.Validate(); nil != err {
return err
}
if nil == sh.Services {
return fmt.Errorf("Services must be specified")
}
if nil == sh.OrderedServices {
return fmt.Errorf("OrderedServices must be specified")
}
return nil
}