This commit is contained in:
crusader 2018-05-11 13:16:30 +09:00
parent 35e8be57fd
commit 528e27a4ab
2 changed files with 91 additions and 0 deletions

19
client/client-context.go Normal file
View File

@ -0,0 +1,19 @@
package client
import (
cuc "git.loafle.net/commons/util-go/context"
)
type ClientCtx interface {
cuc.Context
}
func NewClientCtx(parent cuc.Context) ClientCtx {
return &clientCtx{
Context: cuc.NewContext(parent),
}
}
type clientCtx struct {
cuc.Context
}

72
client/client-handler.go Normal file
View File

@ -0,0 +1,72 @@
package client
import (
"fmt"
"sync/atomic"
)
type ClientHandler interface {
GetName() string
GetConnector() Connector
ClientCtx() ClientCtx
Init(clientCtx ClientCtx) error
OnStart(clientCtx ClientCtx) error
OnStop(clientCtx ClientCtx)
Destroy(clientCtx ClientCtx)
Validate() error
}
type ClientHandlers struct {
Name string `json:"name,omitempty"`
Connector Connector `json:"-"`
validated atomic.Value
}
func (ch *ClientHandlers) ClientCtx() ClientCtx {
return NewClientCtx(nil)
}
func (ch *ClientHandlers) Init(clientCtx ClientCtx) error {
return nil
}
func (ch *ClientHandlers) OnStart(clientCtx ClientCtx) error {
return nil
}
func (ch *ClientHandlers) OnStop(clientCtx ClientCtx) {
}
func (ch *ClientHandlers) Destroy(clientCtx ClientCtx) {
}
func (ch *ClientHandlers) GetName() string {
return ch.Name
}
func (ch *ClientHandlers) GetConnector() Connector {
return ch.Connector
}
func (ch *ClientHandlers) Validate() error {
if nil != ch.validated.Load() {
return nil
}
ch.validated.Store(true)
if nil == ch.Connector {
return fmt.Errorf("Connector is not valid")
}
if err := ch.Connector.Validate(); nil != err {
return err
}
return nil
}