From d11b0afd2be9bae21dbf542beff6dca44db83c1d Mon Sep 17 00:00:00 2001 From: crusader Date: Thu, 14 Dec 2017 17:24:03 +0900 Subject: [PATCH] ing --- server/server_handlers.go | 31 ++++++++++++++++++++++++++----- server/server_handlers_unix.go | 17 ----------------- server/server_handlers_windows.go | 16 ---------------- 3 files changed, 26 insertions(+), 38 deletions(-) delete mode 100644 server/server_handlers_unix.go delete mode 100644 server/server_handlers_windows.go diff --git a/server/server_handlers.go b/server/server_handlers.go index c20ecdb..e834657 100644 --- a/server/server_handlers.go +++ b/server/server_handlers.go @@ -2,14 +2,18 @@ package server import ( "fmt" + "io/ioutil" + "net" + "os" + "strconv" "git.loafle.net/commons_go/logging" "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{ - addr: addr, + pidPath: pidPath, } sh.Name = serverName @@ -20,7 +24,8 @@ func NewServerHandler(addr string, serverName string, socketHandler SocketHandle type ServerHandlers struct { server.ServerHandlers - addr string + pidPath string + port int } func (sh *ServerHandlers) Init(serverCTX server.ServerContext) error { @@ -31,12 +36,28 @@ func (sh *ServerHandlers) Init(serverCTX server.ServerContext) error { 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) + s := strconv.FormatInt(int64(sh.port), 10) + ioutil.WriteFile(sh.pidPath, []byte(s), os.ModePerm) } func (sh *ServerHandlers) OnStop(serverCTX server.ServerContext) { + os.Remove(sh.pidPath) sh.ServerHandlers.OnStop(serverCTX) } @@ -44,8 +65,8 @@ func (sh *ServerHandlers) OnStop(serverCTX server.ServerContext) { func (sh *ServerHandlers) Validate() { sh.ServerHandlers.Validate() - if "" == sh.addr { - logging.Logger().Panic(fmt.Sprintf("Server: Address of server must be specified")) + if "" == sh.pidPath { + logging.Logger().Panic(fmt.Sprintf("Server: The path of pid file must be specified")) } } diff --git a/server/server_handlers_unix.go b/server/server_handlers_unix.go deleted file mode 100644 index 4aad065..0000000 --- a/server/server_handlers_unix.go +++ /dev/null @@ -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 -} diff --git a/server/server_handlers_windows.go b/server/server_handlers_windows.go deleted file mode 100644 index a37870e..0000000 --- a/server/server_handlers_windows.go +++ /dev/null @@ -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 -}