This commit is contained in:
crusader 2017-12-14 17:24:03 +09:00
parent 0fc50612a4
commit d11b0afd2b
3 changed files with 26 additions and 38 deletions

View File

@ -2,14 +2,18 @@ package server
import ( import (
"fmt" "fmt"
"io/ioutil"
"net"
"os"
"strconv"
"git.loafle.net/commons_go/logging" "git.loafle.net/commons_go/logging"
"git.loafle.net/commons_go/server" "git.loafle.net/commons_go/server"
) )
func NewServerHandler(addr string, serverName string, socketHandler SocketHandler) ServerHandler { func NewServerHandler(pidPath string, serverName string, socketHandler SocketHandler) ServerHandler {
sh := &ServerHandlers{ sh := &ServerHandlers{
addr: addr, pidPath: pidPath,
} }
sh.Name = serverName sh.Name = serverName
@ -20,7 +24,8 @@ func NewServerHandler(addr string, serverName string, socketHandler SocketHandle
type ServerHandlers struct { type ServerHandlers struct {
server.ServerHandlers server.ServerHandlers
addr string pidPath string
port int
} }
func (sh *ServerHandlers) Init(serverCTX server.ServerContext) error { func (sh *ServerHandlers) Init(serverCTX server.ServerContext) error {
@ -31,12 +36,28 @@ func (sh *ServerHandlers) Init(serverCTX server.ServerContext) error {
return nil 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) { func (sh *ServerHandlers) OnStart(serverCTX server.ServerContext) {
sh.ServerHandlers.OnStart(serverCTX) sh.ServerHandlers.OnStart(serverCTX)
s := strconv.FormatInt(int64(sh.port), 10)
ioutil.WriteFile(sh.pidPath, []byte(s), os.ModePerm)
} }
func (sh *ServerHandlers) OnStop(serverCTX server.ServerContext) { func (sh *ServerHandlers) OnStop(serverCTX server.ServerContext) {
os.Remove(sh.pidPath)
sh.ServerHandlers.OnStop(serverCTX) sh.ServerHandlers.OnStop(serverCTX)
} }
@ -44,8 +65,8 @@ func (sh *ServerHandlers) OnStop(serverCTX server.ServerContext) {
func (sh *ServerHandlers) Validate() { func (sh *ServerHandlers) Validate() {
sh.ServerHandlers.Validate() sh.ServerHandlers.Validate()
if "" == sh.addr { if "" == sh.pidPath {
logging.Logger().Panic(fmt.Sprintf("Server: Address of server must be specified")) logging.Logger().Panic(fmt.Sprintf("Server: The path of pid file must be specified"))
} }
} }

View File

@ -1,17 +0,0 @@
package server
import (
"net"
"os"
"git.loafle.net/commons_go/server"
)
func (sh *ServerHandlers) Listen(serverCTX server.ServerContext) (net.Listener, error) {
os.Remove(sh.addr)
l, err := net.ListenUnix("unix", &net.UnixAddr{Name: sh.addr, Net: "unix"})
if nil == err {
os.Chmod(sh.addr, 0777)
}
return l, err
}

View File

@ -1,16 +0,0 @@
package server
import (
"net"
"git.loafle.net/commons_go/server"
npipe "gopkg.in/natefinch/npipe.v2"
)
func (sh *ServerHandlers) Listen(serverCTX server.ServerContext) (net.Listener, error) {
ln, err := npipe.Listen(`\\.\pipe\` + sh.addr)
if err != nil {
// handle error
}
return ln, err
}