server-go/socket/client/client-handler.go

73 lines
1.2 KiB
Go
Raw Normal View History

2018-05-11 04:16:30 +00:00
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
}