Skel of the new SM-Generic

This commit is contained in:
DanB
2015-11-06 19:31:29 +01:00
parent c143849c37
commit 640336d7b9
6 changed files with 201 additions and 11 deletions

View File

@@ -27,12 +27,14 @@ import (
"net/rpc"
"net/rpc/jsonrpc"
"github.com/cenkalti/rpc2"
"golang.org/x/net/websocket"
)
type Server struct {
rpcEnabled bool
httpEnabled bool
bijsonSrv *rpc2.Server
}
func (s *Server) RpcRegister(rcvr interface{}) {
@@ -50,13 +52,20 @@ func (s *Server) RegisterHttpFunc(pattern string, handler func(http.ResponseWrit
s.httpEnabled = true
}
func (s *Server) BijsonRegisterName(method string, handlerFunc interface{}) {
if s.bijsonSrv == nil {
s.bijsonSrv = rpc2.NewServer()
}
s.bijsonSrv.Handle(method, handlerFunc)
}
func (s *Server) ServeJSON(addr string) {
if !s.rpcEnabled {
return
}
lJSON, e := net.Listen("tcp", addr)
if e != nil {
log.Fatal("listen error:", e)
log.Fatal("ServeJSON listen error:", e)
}
Logger.Info(fmt.Sprintf("Starting CGRateS JSON server at %s.", addr))
for {
@@ -65,7 +74,6 @@ func (s *Server) ServeJSON(addr string) {
Logger.Err(fmt.Sprintf("<CGRServer> Accept error: %v", conn))
continue
}
//utils.Logger.Info(fmt.Sprintf("<CGRServer> New incoming connection: %v", conn.RemoteAddr()))
go jsonrpc.ServeConn(conn)
}
@@ -78,7 +86,7 @@ func (s *Server) ServeGOB(addr string) {
}
lGOB, e := net.Listen("tcp", addr)
if e != nil {
log.Fatal("listen error:", e)
log.Fatal("ServeGOB listen error:", e)
}
Logger.Info(fmt.Sprintf("Starting CGRateS GOB server at %s.", addr))
for {
@@ -113,6 +121,18 @@ func (s *Server) ServeHTTP(addr string) {
http.ListenAndServe(addr, nil)
}
func (s *Server) ServeBiJSON(addr string) {
if s.bijsonSrv == nil {
return
}
lBiJSON, e := net.Listen("tcp", addr)
if e != nil {
log.Fatal("ServeBiJSON listen error:", e)
}
Logger.Info(fmt.Sprintf("Starting CGRateS BiJSON server at %s.", addr))
s.bijsonSrv.Accept(lBiJSON)
}
// rpcRequest represents a RPC request.
// rpcRequest implements the io.ReadWriteCloser interface.
type rpcRequest struct {