overflow_discovery/server/server_handlers.go

90 lines
1.6 KiB
Go
Raw Normal View History

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"
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() {
// no op
}
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
}
return newConn(conn, "jsonrpc"), nil
}
func (sh *ServerHandlers) Handle(conn net.Conn, stopChan <-chan struct{}, doneChan chan<- struct{}) {
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() {
// no op
}
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
}