2017-10-26 12:55:55 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2017-11-15 06:59:43 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2017-11-01 06:35:03 +00:00
|
|
|
"net"
|
2017-10-26 12:55:55 +00:00
|
|
|
|
2017-11-15 06:59:43 +00:00
|
|
|
"git.loafle.net/commons_go/logging"
|
2017-11-15 12:09:38 +00:00
|
|
|
"git.loafle.net/overflow/overflow_discovery/discovery"
|
2017-11-23 09:34:07 +00:00
|
|
|
"git.loafle.net/overflow/overflow_discovery/rpc/notify"
|
2017-11-15 06:59:43 +00:00
|
|
|
|
|
|
|
crs "git.loafle.net/commons_go/rpc/server"
|
|
|
|
"git.loafle.net/commons_go/server"
|
2017-10-26 12:55:55 +00:00
|
|
|
)
|
|
|
|
|
2017-11-15 06:59:43 +00:00
|
|
|
func newServerHandler(addr string, rpcSH RPCServerHandler) ServerHandler {
|
|
|
|
sh := &ServerHandlers{
|
|
|
|
addr: addr,
|
|
|
|
rpcSH: rpcSH,
|
|
|
|
}
|
|
|
|
sh.Name = "Discovery"
|
2017-11-01 06:35:03 +00:00
|
|
|
|
2017-10-26 12:55:55 +00:00
|
|
|
return sh
|
|
|
|
}
|
|
|
|
|
|
|
|
type ServerHandlers struct {
|
2017-11-01 06:35:03 +00:00
|
|
|
server.ServerHandlers
|
2017-10-26 12:55:55 +00:00
|
|
|
|
2017-11-15 06:59:43 +00:00
|
|
|
rpcSH RPCServerHandler
|
|
|
|
addr string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) Init() error {
|
|
|
|
|
|
|
|
return nil
|
2017-10-26 12:55:55 +00:00
|
|
|
}
|
|
|
|
|
2017-11-01 06:35:03 +00:00
|
|
|
func (sh *ServerHandlers) OnStart() {
|
2017-11-15 12:09:38 +00:00
|
|
|
discovery.DiscoveryInit()
|
2017-11-01 06:35:03 +00:00
|
|
|
}
|
2017-10-26 13:18:20 +00:00
|
|
|
|
2017-11-15 06:59:43 +00:00
|
|
|
func (sh *ServerHandlers) OnConnect(conn net.Conn) (net.Conn, error) {
|
|
|
|
var err error
|
|
|
|
if conn, err = sh.ServerHandlers.OnConnect(conn); nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-23 09:34:07 +00:00
|
|
|
nConn := newConn(conn, "jsonrpc")
|
|
|
|
|
|
|
|
notify.NotifyInit(nConn)
|
|
|
|
|
|
|
|
return nConn, nil
|
2017-11-15 06:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sh *ServerHandlers) Handle(conn net.Conn, stopChan <-chan struct{}, doneChan chan<- struct{}) {
|
2017-11-23 09:34:07 +00:00
|
|
|
defer func() {
|
|
|
|
notify.NotifyDestroy()
|
|
|
|
}()
|
|
|
|
|
2017-11-15 06:59:43 +00:00
|
|
|
dConn := conn.(Conn)
|
|
|
|
contentType := dConn.GetContentType()
|
|
|
|
codec, err := sh.rpcSH.GetCodec(contentType)
|
|
|
|
if nil != err {
|
|
|
|
log.Printf("RPC Handle: %v", err)
|
|
|
|
doneChan <- struct{}{}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
if err := crs.Handle(sh.rpcSH, codec, conn, conn); nil != err {
|
|
|
|
if server.IsClientDisconnect(err) {
|
|
|
|
doneChan <- struct{}{}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Printf("RPC: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-stopChan:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-01 06:35:03 +00:00
|
|
|
func (sh *ServerHandlers) OnStop() {
|
2017-11-23 09:34:07 +00:00
|
|
|
|
2017-11-15 12:09:38 +00:00
|
|
|
discovery.DiscoveryDestroy()
|
2017-11-01 06:35:03 +00:00
|
|
|
}
|
2017-10-26 13:18:20 +00:00
|
|
|
|
2017-11-15 06:59:43 +00:00
|
|
|
func (sh *ServerHandlers) Validate() {
|
|
|
|
sh.ServerHandlers.Validate()
|
|
|
|
|
|
|
|
if "" == sh.addr {
|
|
|
|
logging.Logger().Panic(fmt.Sprintf("Server: Address of server must be specified"))
|
2017-10-26 13:18:20 +00:00
|
|
|
}
|
2017-11-01 06:35:03 +00:00
|
|
|
|
2017-11-15 06:59:43 +00:00
|
|
|
if nil == sh.rpcSH {
|
|
|
|
logging.Logger().Panic(fmt.Sprintf("Server: RPC Server Handler must be specified"))
|
|
|
|
}
|
2017-11-01 06:35:03 +00:00
|
|
|
}
|