From f5e3a8b6db4c136bbb7ecbf73e59b036d6eef954 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Mon, 26 Mar 2012 22:17:02 +0300 Subject: [PATCH] both multiple raters and status page working --- cmd/balancer/http_responder.go | 17 +------------ cmd/balancer/registration.go | 39 +++++++++++++++++++++++++----- cmd/balancer/status_responder.go | 4 +-- cmd/balancer/templates/status.html | 5 ++-- cmd/rater/rater.go | 4 +-- cmd/rater/registration.go | 2 +- 6 files changed, 41 insertions(+), 30 deletions(-) diff --git a/cmd/balancer/http_responder.go b/cmd/balancer/http_responder.go index 7ba73090c..868846086 100644 --- a/cmd/balancer/http_responder.go +++ b/cmd/balancer/http_responder.go @@ -19,7 +19,6 @@ package main import ( "encoding/json" - "fmt" "github.com/rif/cgrates/timespans" "log" "net/http" @@ -30,17 +29,6 @@ type IncorrectParameters struct { Error string } -/* -Handler for the statistics web client -*/ -func statusHandler(w http.ResponseWriter, r *http.Request) { - fmt.Fprint(w, "
    ") - for _, addr := range raterList.clientAddresses { - fmt.Fprint(w, fmt.Sprintf("
  1. Client: %v
  2. ", addr)) - } - fmt.Fprint(w, "
") -} - /* curl "http://127.0.0.1:8000/getcost?cstmid=vdf&subj=rif&dest=0257" */ @@ -216,7 +204,7 @@ func resetUserBudget(w http.ResponseWriter, r *http.Request) { } func listenToHttpRequests() { - http.HandleFunc("/", statusHandler) + http.Handle("/static/", http.FileServer(http.Dir(""))) http.HandleFunc("/getcost", getCostHandler) http.HandleFunc("/debitbalance", debitBalanceHandler) http.HandleFunc("/debitsms", debitSMSHandler) @@ -226,12 +214,9 @@ func listenToHttpRequests() { http.HandleFunc("/resetvolumediscountseconds", resetVolumeDiscountSeconds) http.HandleFunc("/addrecievedcallseconds", addRecievedCallSeconds) http.HandleFunc("/resetuserbudget", resetUserBudget) -<<<<<<< Updated upstream -======= http.HandleFunc("/", statusHandler) http.HandleFunc("/getmem", memoryHandler) http.HandleFunc("/raters", ratersHandler) ->>>>>>> Stashed changes log.Print("The server is listening on ", *httpApiAddress) http.ListenAndServe(*httpApiAddress, nil) } diff --git a/cmd/balancer/registration.go b/cmd/balancer/registration.go index b01d9af16..ffccb31e5 100644 --- a/cmd/balancer/registration.go +++ b/cmd/balancer/registration.go @@ -18,12 +18,13 @@ along with this program. If not, see package main import ( - "os/signal" "fmt" "log" - "net/rpc" + "net" "net/http" + "net/rpc" "os" + "os/signal" "syscall" "time" ) @@ -33,11 +34,37 @@ RPC Server that handles the registering and unregistering of raters. */ type RaterServer byte -func listenToRPCRaterRequests(){ - raterServer := new(RaterServer) - rpc.Register(raterServer) +func listenToRPCRaterRequests() { + /*rpc.Register(new(RaterServer)) rpc.HandleHTTP() - http.ListenAndServe(*raterAddress, nil) + http.ListenAndServe(*raterAddress, nil)*/ + + rpc.Register(new(RaterServer)) + rpc.HandleHTTP() + l, e := net.Listen("tcp", *raterAddress) + if e != nil { + log.Fatal("listen error:", e) + } + go http.Serve(l, nil) + + /*log.Print("Starting Server...") + l, err := net.Listen("tcp", *raterAddress) + defer l.Close() + if err != nil { + log.Fatal(err) + } + log.Print("listening on: ", l.Addr()) + rpc.Register(new(RaterServer)) + for { + log.Print("waiting for connections ...") + conn, err := l.Accept() + if err != nil { + log.Printf("accept error: %s", conn) + continue + } + log.Printf("connection started: %v", conn.RemoteAddr()) + go rpc.ServeConn(conn) + }*/ } /* diff --git a/cmd/balancer/status_responder.go b/cmd/balancer/status_responder.go index 6e23218b1..ebfa22165 100644 --- a/cmd/balancer/status_responder.go +++ b/cmd/balancer/status_responder.go @@ -19,7 +19,6 @@ package main import ( "encoding/json" - "fmt" "html/template" "log" "net/http" @@ -51,5 +50,6 @@ Ajax Handler for current used memory value func memoryHandler(w http.ResponseWriter, r *http.Request) { memstats := new(runtime.MemStats) runtime.ReadMemStats(memstats) - fmt.Fprint(w, memstats.HeapAlloc/1024, memstats.Sys/1024) + enc := json.NewEncoder(w) + enc.Encode([]uint64{memstats.HeapAlloc / 1024, memstats.Sys / 1024}) } diff --git a/cmd/balancer/templates/status.html b/cmd/balancer/templates/status.html index 5c800d08f..2978dc17a 100644 --- a/cmd/balancer/templates/status.html +++ b/cmd/balancer/templates/status.html @@ -92,8 +92,7 @@ limit = limit.slice(1); } - $.get("/getmem", function(memValue){ - memValue = memValue.split(" "); + $.getJSON("/getmem", function(memValue){ now = new Date() timeOffsetInHours = (now.getTimezoneOffset()/60) * (-1); now.setHours(now.getHours() + timeOffsetInHours); @@ -125,7 +124,7 @@ update(); $("#rater-refresh").click(function(){ - $.get("/raters", function(raters){ + $.getJSON("/raters", function(raters){ $("tr", "#rater-table").remove() console.log(raters); for(var i=0; i package main import ( - "os/signal" "github.com/rif/cgrates/timespans" "log" "net/rpc" "os" + "os/signal" "syscall" )