server/server_handlers.go
2018-03-21 20:07:11 +09:00

68 lines
1.5 KiB
Go

package server
import (
"errors"
"fmt"
"net"
"git.loafle.net/commons_go/logging"
)
func NewServerHandler() ServerHandler {
sh := &ServerHandlers{}
return sh
}
type ServerHandlers struct {
// Server name for sending in response headers.
//
// Default server name is used if left blank.
Name string
SocketHandler SocketHandler
}
func (sh *ServerHandlers) ServerContext() ServerContext {
return newServerContext()
}
func (sh *ServerHandlers) Init(serverCTX ServerContext) error {
if err := sh.SocketHandler.Init(serverCTX); nil != err {
return err
}
return nil
}
func (sh *ServerHandlers) Listen(serverCTX ServerContext) (net.Listener, error) {
return nil, errors.New("Server: Handler method[Listen] of Server is not implement")
}
func (sh *ServerHandlers) OnStart(serverCTX ServerContext) {
// no op
}
func (sh *ServerHandlers) OnError(serverCTX ServerContext, conn net.Conn, status int, reason error) {
logging.Logger().Errorf("Server: error status: %d, reason: %v, [%v]", status, reason, conn)
}
// OnStop invoked when server is stopped
// If you override ths method, must call
func (sh *ServerHandlers) OnStop(serverCTX ServerContext) {
sh.SocketHandler.Destroy()
}
func (sh *ServerHandlers) GetName() string {
return sh.Name
}
func (sh *ServerHandlers) GetSocketHandler() SocketHandler {
return sh.SocketHandler
}
func (sh *ServerHandlers) Validate() {
if nil == sh.SocketHandler {
logging.Logger().Panicf("Server: SocketHandler must be specified")
}
}