diff --git a/client/client-context.go b/client/client-context.go new file mode 100644 index 0000000..3cb37db --- /dev/null +++ b/client/client-context.go @@ -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 +} diff --git a/client/client-handler.go b/client/client-handler.go new file mode 100644 index 0000000..376afe0 --- /dev/null +++ b/client/client-handler.go @@ -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 +}