96 lines
2.1 KiB
Go
96 lines
2.1 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
|
|
"git.loafle.net/commons_go/logging"
|
|
"git.loafle.net/commons_go/server"
|
|
)
|
|
|
|
func NewServerHandler(pidPath string, serverName string, socketHandler SocketHandler) ServerHandler {
|
|
pidPathABS, err := filepath.Abs(pidPath)
|
|
if nil != err {
|
|
logging.Logger().Panicf("Container: pid file path[%s] is not valid %v", pidPath, err)
|
|
}
|
|
|
|
sh := &ServerHandlers{
|
|
pidPath: pidPath,
|
|
pidPathABS: pidPathABS,
|
|
}
|
|
|
|
sh.Name = serverName
|
|
sh.SocketHandler = socketHandler
|
|
return sh
|
|
}
|
|
|
|
type ServerHandlers struct {
|
|
server.ServerHandlers
|
|
|
|
pidPath string
|
|
pidPathABS string
|
|
port int
|
|
}
|
|
|
|
func (sh *ServerHandlers) Init(serverCTX server.ServerContext) error {
|
|
if err := sh.ServerHandlers.Init(serverCTX); nil != err {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (sh *ServerHandlers) Listen(serverCTX server.ServerContext) (net.Listener, error) {
|
|
for i := 60000; i < 61000; i++ {
|
|
addr := fmt.Sprintf("localhost:%d", i)
|
|
l, err := net.Listen("tcp", addr)
|
|
if nil == err {
|
|
sh.port = i
|
|
return l, nil
|
|
}
|
|
}
|
|
|
|
return nil, fmt.Errorf("Container: Cannot find availrable port")
|
|
}
|
|
|
|
func (sh *ServerHandlers) OnStart(serverCTX server.ServerContext) {
|
|
sh.ServerHandlers.OnStart(serverCTX)
|
|
|
|
if _, err := os.Stat(sh.pidPathABS); os.IsExist(err) {
|
|
if err := os.Remove(sh.pidPathABS); nil != err {
|
|
logging.Logger().Errorf("Container: Removing pid file has been failed [%v]", err)
|
|
}
|
|
}
|
|
|
|
s := strconv.FormatInt(int64(sh.port), 10)
|
|
ioutil.WriteFile(sh.pidPathABS, []byte(s), os.ModePerm)
|
|
}
|
|
|
|
func (sh *ServerHandlers) OnStop(serverCTX server.ServerContext) {
|
|
if _, err := os.Stat(sh.pidPathABS); os.IsExist(err) {
|
|
if err := os.Remove(sh.pidPathABS); nil != err {
|
|
logging.Logger().Errorf("Container: Removing pid file has been failed [%v]", err)
|
|
}
|
|
}
|
|
|
|
sh.ServerHandlers.OnStop(serverCTX)
|
|
}
|
|
|
|
func (sh *ServerHandlers) Destroy(serverCTX server.ServerContext) {
|
|
|
|
sh.ServerHandlers.Destroy(serverCTX)
|
|
}
|
|
|
|
func (sh *ServerHandlers) Validate() {
|
|
sh.ServerHandlers.Validate()
|
|
|
|
if "" == sh.pidPath {
|
|
logging.Logger().Panicf("Container: The path of pid file must be specified")
|
|
}
|
|
|
|
}
|