This commit is contained in:
crusader 2017-12-05 14:58:32 +09:00
parent 84a1d91c52
commit b30f3d10ea
16 changed files with 30 additions and 277 deletions

View File

@ -1,15 +0,0 @@
package client
import (
crc "git.loafle.net/commons_go/rpc/client"
crcrs "git.loafle.net/commons_go/rpc/client/rwc/socket"
csc "git.loafle.net/commons_go/server/client"
)
func New(clientHandler ClientHandler, socketBuilder csc.SocketBuilder) crc.Client {
cRWCHandler := crcrs.New(socketBuilder)
c := crc.New(clientHandler, cRWCHandler)
return c
}

View File

@ -1,7 +0,0 @@
package client
import "git.loafle.net/commons_go/rpc/client"
type ClientHandler interface {
client.ClientHandler
}

View File

@ -1,19 +0,0 @@
package client
import (
crc "git.loafle.net/commons_go/rpc/client"
"git.loafle.net/commons_go/rpc/protocol/json"
crr "git.loafle.net/commons_go/rpc/registry"
)
func NewClientHandler(rpcInvoker crr.RPCInvoker) ClientHandler {
ch := &ClientHandlers{}
ch.RPCInvoker = rpcInvoker
ch.Codec = json.NewClientCodec()
return ch
}
type ClientHandlers struct {
crc.ClientHandlers
}

View File

@ -1,17 +0,0 @@
package client
import (
csc "git.loafle.net/commons_go/server/client"
)
func NewSocketBuilder(address string) csc.SocketBuilder {
return newSocketBuilder(address)
}
type SocketBuilders struct {
csc.SocketBuilders
}
func (sb *SocketBuilders) SocketHandler() csc.SocketHandler {
return NewSocketHandler()
}

View File

@ -1,13 +0,0 @@
package client
import (
csc "git.loafle.net/commons_go/server/client"
)
func newSocketBuilder(address string) csc.SocketBuilder {
sb := &SocketBuilders{}
sb.Network = "unix"
sb.Address = address
return sb
}

View File

@ -1,23 +0,0 @@
package client
import (
"net"
csc "git.loafle.net/commons_go/server/client"
"gopkg.in/natefinch/npipe.v2"
)
func newSocketBuilder(address string) csc.SocketBuilder {
sb := &SocketBuilders{}
sb.Network = "pipe"
sb.Address = address
return sb
}
func (sb *SocketBuilders) Dial(network, address string) (net.Conn, error) {
if 0 == sb.HandshakeTimeout {
return npipe.Dial(`\\.\pipe\` + address)
}
return npipe.DialTimeout(`\\.\pipe\`+address, sb.HandshakeTimeout)
}

View File

@ -1,28 +0,0 @@
package client
import (
"log"
"net"
csc "git.loafle.net/commons_go/server/client"
)
type SocketHandlers struct {
csc.SocketHandlers
}
func (sh *SocketHandlers) OnConnect(socketContext csc.SocketContext, conn net.Conn) {
log.Printf("OnConnect res: %v \n", conn)
}
func (sh *SocketHandlers) OnDisconnect(soc csc.Socket) {
log.Printf("OnDisconnect \n")
}
func (sh *SocketHandlers) Validate() {
sh.SocketHandlers.Validate()
}
func NewSocketHandler() csc.SocketHandler {
return &SocketHandlers{}
}

View File

@ -1,9 +0,0 @@
package server
import (
"git.loafle.net/commons_go/rpc/server"
)
type RPCServletHandler interface {
server.ServletHandler
}

View File

@ -1,22 +0,0 @@
package server
import (
crr "git.loafle.net/commons_go/rpc/registry"
"git.loafle.net/commons_go/rpc/server"
)
func newRPCServletHandler(rpcInvoker crr.RPCInvoker) server.ServletHandler {
sh := &RPCServletHandlers{}
sh.RPCInvoker = rpcInvoker
return sh
}
type RPCServletHandlers struct {
server.ServletHandlers
}
func (sh *RPCServletHandlers) Validate() {
sh.ServletHandlers.Validate()
}

View File

@ -1,24 +1,21 @@
package server
import (
crpj "git.loafle.net/commons_go/rpc/protocol/json"
crr "git.loafle.net/commons_go/rpc/registry"
"git.loafle.net/commons_go/server"
"git.loafle.net/overflow/overflow_discovery/service"
oopcs "git.loafle.net/overflow/overflow_probe_container/server"
)
func New(addr string) server.Server {
rpcRegistry := crr.NewRPCRegistry()
service.RegisterRPC(rpcRegistry)
rpcSH := newRPCServletHandler(rpcRegistry)
rpcSH.RegisterCodec(crpj.Name, crpj.NewServerCodec())
rpcSH := oopcs.NewRPCServletHandler(rpcRegistry)
socketHandler := newSocketHandler(rpcSH)
sh := newServerHandler(addr, socketHandler)
s := server.New(sh)
s := oopcs.New(sh)
return s
}

View File

@ -1,7 +1,9 @@
package server
import "git.loafle.net/commons_go/server"
import (
oopcs "git.loafle.net/overflow/overflow_probe_container/server"
)
type ServerHandler interface {
server.ServerHandler
oopcs.ServerHandler
}

View File

@ -1,32 +1,24 @@
package server
import (
"fmt"
"git.loafle.net/commons_go/logging"
"git.loafle.net/overflow/overflow_discovery/discovery"
"git.loafle.net/commons_go/server"
"git.loafle.net/overflow/overflow_discovery/discovery"
oopcs "git.loafle.net/overflow/overflow_probe_container/server"
)
func newServerHandler(addr string, socketHandler SocketHandler) ServerHandler {
sh := &ServerHandlers{
addr: addr,
}
sh := &ServerHandlers{}
sh.ServerHandler = oopcs.NewServerHandler(addr, "Discovery Containter", socketHandler)
sh.Name = "Discovery"
sh.SocketHandler = socketHandler
return sh
}
type ServerHandlers struct {
server.ServerHandlers
addr string
oopcs.ServerHandler
}
func (sh *ServerHandlers) Init(serverCTX server.ServerContext) error {
if err := sh.ServerHandlers.Init(serverCTX); nil != err {
if err := sh.ServerHandler.Init(serverCTX); nil != err {
return err
}
@ -34,7 +26,7 @@ func (sh *ServerHandlers) Init(serverCTX server.ServerContext) error {
}
func (sh *ServerHandlers) OnStart(serverCTX server.ServerContext) {
sh.ServerHandlers.OnStart(serverCTX)
sh.ServerHandler.OnStart(serverCTX)
discovery.DiscoveryInit()
}
@ -43,14 +35,10 @@ func (sh *ServerHandlers) OnStop(serverCTX server.ServerContext) {
discovery.DiscoveryDestroy()
sh.ServerHandlers.OnStop(serverCTX)
sh.ServerHandler.OnStop(serverCTX)
}
func (sh *ServerHandlers) Validate() {
sh.ServerHandlers.Validate()
if "" == sh.addr {
logging.Logger().Panic(fmt.Sprintf("Server: Address of server must be specified"))
}
sh.ServerHandler.Validate()
}

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"
"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
}

View File

@ -1,9 +1,9 @@
package server
import (
"git.loafle.net/commons_go/server"
oopcs "git.loafle.net/overflow/overflow_probe_container/server"
)
type SocketHandler interface {
server.SocketHandler
oopcs.SocketHandler
}

View File

@ -2,36 +2,27 @@ package server
import (
"net"
"sync"
cRPC "git.loafle.net/commons_go/rpc"
crsrs "git.loafle.net/commons_go/rpc/server/rwc/socket"
"git.loafle.net/commons_go/rpc/protocol/json"
"git.loafle.net/commons_go/server"
"git.loafle.net/overflow/overflow_discovery/discovery"
oopc "git.loafle.net/overflow/overflow_probe_container"
oopcs "git.loafle.net/overflow/overflow_probe_container/server"
)
func newSocketHandler(rpcSH RPCServletHandler) SocketHandler {
rpcRWCSH := crsrs.New()
sh := &SocketHandlers{
rpcSH: rpcSH,
rpcRWCSH: rpcRWCSH,
}
func newSocketHandler(rpcSH oopcs.RPCServletHandler) SocketHandler {
sh := &SocketHandlers{}
sh.SocketHandler = oopcs.NewSocketHandler(rpcSH)
return sh
}
type SocketHandlers struct {
server.SocketHandlers
rpcRWCSH cRPC.ServletReadWriteCloseHandler
rpcSH RPCServletHandler
oopcs.SocketHandler
}
func (sh *SocketHandlers) Init(serverCTX server.ServerContext) error {
if err := sh.SocketHandlers.Init(serverCTX); nil != err {
if err := sh.SocketHandler.Init(serverCTX); nil != err {
return err
}
@ -43,61 +34,22 @@ func (sh *SocketHandlers) Handshake(socketCTX server.SocketContext, conn net.Con
}
func (sh *SocketHandlers) OnConnect(soc server.Socket) {
sh.SocketHandlers.OnConnect(soc)
sh.SocketHandler.OnConnect(soc)
soc.Context().SetAttribute(cRPC.ContentTypeKey, json.Name)
}
func (sh *SocketHandlers) Handle(soc server.Socket, stopChan <-chan struct{}, doneChan chan<- error) {
var err error
rpcServlet := retainRPCServlet(sh.rpcSH, sh.rpcRWCSH)
discovery.RPCServlet = rpcServlet
defer func() {
releaseRPCServlet(rpcServlet)
doneChan <- err
}()
rpcDoneChan := make(chan error, 1)
if err = rpcServlet.Start(soc.Context(), soc, rpcDoneChan); nil != err {
return
}
select {
case err = <-rpcDoneChan:
case <-stopChan:
rpcServlet.Stop()
<-rpcDoneChan
}
discovery.RPCServlet = soc.Context().GetAttribute(oopc.RPCServletKey).(cRPC.Servlet)
}
func (sh *SocketHandlers) OnDisconnect(soc server.Socket) {
discovery.RPCServlet = nil
sh.SocketHandlers.OnDisconnect(soc)
sh.SocketHandler.OnDisconnect(soc)
}
func (sh *SocketHandlers) Destroy() {
sh.SocketHandlers.Destroy()
sh.SocketHandler.Destroy()
}
func (sh *SocketHandlers) Validate() {
}
var rpcServletPool sync.Pool
func retainRPCServlet(sh RPCServletHandler, rpcRWCSH cRPC.ServletReadWriteCloseHandler) cRPC.Servlet {
v := rpcServletPool.Get()
if v == nil {
return cRPC.NewServlet(sh, rpcRWCSH)
}
return v.(cRPC.Servlet)
}
func releaseRPCServlet(s cRPC.Servlet) {
rpcServletPool.Put(s)
}