From b4f64c8662de0dfddc06f6a30c35f4d73bbaa82b Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Sun, 8 Jul 2012 15:19:35 +0300 Subject: [PATCH] json made optional for all rpc encodings --- cmd/cgr-balancer/cgr-balanncer.go | 3 ++- .../{jsonrpc_responder.go => rpc_responder.go} | 16 +++++++++++----- cmd/cgr-rater/cgr-rater.go | 11 +++++++---- cmd/cgr-sessionmanager/cgr-sessionmanager.go | 9 ++++++++- cmd/stress/cgr-raterstress/cgr-raterstress.go | 13 ++++++++++++- 5 files changed, 40 insertions(+), 12 deletions(-) rename cmd/cgr-balancer/{jsonrpc_responder.go => rpc_responder.go} (91%) diff --git a/cmd/cgr-balancer/cgr-balanncer.go b/cmd/cgr-balancer/cgr-balanncer.go index a18332942..8f2f7a770 100644 --- a/cmd/cgr-balancer/cgr-balanncer.go +++ b/cmd/cgr-balancer/cgr-balanncer.go @@ -36,6 +36,7 @@ var ( httpApiAddress = flag.String("httpapiaddr", "127.0.0.1:8000", "Http API server address (localhost:2002)") freeswitchsrv = flag.String("freeswitchsrv", "localhost:8021", "freeswitch address host:port") freeswitchpass = flag.String("freeswitchpass", "ClueCon", "freeswitch address host:port") + js = flag.Bool("json", false, "use JSON for RPC encoding") bal *balancer.Balancer balancerRWMutex sync.RWMutex ) @@ -92,7 +93,7 @@ func main() { go StopSingnalHandler() go listenToRPCRaterRequests() - go listenToJsonRPCRequests() + go listenToRPCRequests() sm := &sessionmanager.FSSessionManager{} sm.Connect(sessionmanager.NewRPCBalancerSessionDelegate(bal), *freeswitchsrv, *freeswitchpass) diff --git a/cmd/cgr-balancer/jsonrpc_responder.go b/cmd/cgr-balancer/rpc_responder.go similarity index 91% rename from cmd/cgr-balancer/jsonrpc_responder.go rename to cmd/cgr-balancer/rpc_responder.go index 364c2a8a0..e343019fa 100644 --- a/cmd/cgr-balancer/jsonrpc_responder.go +++ b/cmd/cgr-balancer/rpc_responder.go @@ -97,7 +97,7 @@ func (r *Responder) Status(arg timespans.CallDescriptor, replay *string) (err er /* Creates the json rpc server. */ -func listenToJsonRPCRequests() { +func listenToRPCRequests() { l, err := net.Listen("tcp", *jsonRpcAddress) defer l.Close() @@ -111,13 +111,19 @@ func listenToJsonRPCRequests() { rpc.Register(responder) for { - c, err := l.Accept() + conn, err := l.Accept() if err != nil { - log.Printf("accept error: %s", c) + log.Printf("accept error: %s", conn) continue } - log.Printf("connection started: %v", c.RemoteAddr()) - go jsonrpc.ServeConn(c) + log.Printf("connection started: %v", conn.RemoteAddr()) + if *js { + // log.Print("json encoding") + go jsonrpc.ServeConn(conn) + } else { + // log.Print("gob encoding") + go rpc.ServeConn(conn) + } } } diff --git a/cmd/cgr-rater/cgr-rater.go b/cmd/cgr-rater/cgr-rater.go index c085db4c9..1b989275e 100644 --- a/cmd/cgr-rater/cgr-rater.go +++ b/cmd/cgr-rater/cgr-rater.go @@ -38,7 +38,9 @@ var ( redissrv = flag.String("redissrv", "127.0.0.1:6379", "redis address host:port") redisdb = flag.Int("redisdb", 10, "redis database number") listen = flag.String("listen", "127.0.0.1:1234", "listening address host:port") - standalone = flag.Bool("standalone", false, "start standalone server (no balancer), and use JSON for RPC encoding") + standalone = flag.Bool("standalone", false, "start standalone server (no balancer)") + freeswitch = flag.Bool("freeswitch", false, "connect to freeswitch server") + json = flag.Bool("json", false, "use JSON for RPC encoding") storage Responder ) @@ -122,10 +124,11 @@ func main() { if err != nil { log.Fatalf("Cannot open storage: %v", err) } - if *standalone { + if *freeswitch { sm := &sessionmanager.FSSessionManager{} sm.Connect(sessionmanager.NewDirectSessionDelegate(getter), *freeswitchsrv, *freeswitchpass) - } else { + } + if !*standalone { go RegisterToServer(balancer, listen) go StopSingnalHandler(balancer, listen, getter) } @@ -155,7 +158,7 @@ func main() { continue } log.Printf("connection started: %v", conn.RemoteAddr()) - if *standalone { + if *json { // log.Print("json encoding") go jsonrpc.ServeConn(conn) } else { diff --git a/cmd/cgr-sessionmanager/cgr-sessionmanager.go b/cmd/cgr-sessionmanager/cgr-sessionmanager.go index 26cf71e78..236a1fcb6 100644 --- a/cmd/cgr-sessionmanager/cgr-sessionmanager.go +++ b/cmd/cgr-sessionmanager/cgr-sessionmanager.go @@ -23,11 +23,13 @@ import ( "github.com/cgrates/cgrates/sessionmanager" "github.com/cgrates/cgrates/timespans" "log" + "net/rpc" "net/rpc/jsonrpc" ) var ( standalone = flag.Bool("standalone", false, "run standalone (run as a rater)") + json = flag.Bool("json", false, "use JSON for RPC encoding") balancer = flag.String("balancer", "127.0.0.1:2000", "balancer address host:port") freeswitchsrv = flag.String("freeswitchsrv", "localhost:8021", "freeswitch address host:port") freeswitchpass = flag.String("freeswitchpass", "ClueCon", "freeswitch address host:port") @@ -46,7 +48,12 @@ func main() { if *standalone { sm.Connect(sessionmanager.NewDirectSessionDelegate(getter), *freeswitchsrv, *freeswitchpass) } else { - client, err := jsonrpc.Dial("tcp", *balancer) + var client *rpc.Client + if *json { + client, err = jsonrpc.Dial("tcp", *balancer) + } else { + client, err = rpc.Dial("tcp", *balancer) + } if err != nil { log.Fatalf("could not connect to balancer: %v", err) } diff --git a/cmd/stress/cgr-raterstress/cgr-raterstress.go b/cmd/stress/cgr-raterstress/cgr-raterstress.go index c797f2417..880365ee1 100644 --- a/cmd/stress/cgr-raterstress/cgr-raterstress.go +++ b/cmd/stress/cgr-raterstress/cgr-raterstress.go @@ -21,6 +21,7 @@ package main import ( "github.com/cgrates/cgrates/timespans" "log" + "net/rpc" "net/rpc/jsonrpc" "time" "flag" @@ -28,6 +29,7 @@ import ( var ( runs = flag.Int("runs", 10000, "stress cycle number") + json = flag.Bool("json", false, "use JSON for RPC encoding") ) func main() { @@ -36,7 +38,16 @@ func main() { t2 := time.Date(2012, time.February, 02, 18, 30, 0, 0, time.UTC) cd := timespans.CallDescriptor{Direction: "OUT", TOR: "0", Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2} result := timespans.CallCost{} - client, _ := jsonrpc.Dial("tcp", "localhost:1234") + var client *rpc.Client + var err error + if *json { + client, err = jsonrpc.Dial("tcp", "localhost:1234") + } else { + client, err = rpc.Dial("tcp", "localhost:1234") + } + if err != nil { + log.Panic("Could not connect to rater: ", err) + } i := 0 start := time.Now() for j := 0; j < *runs; j++ {