started http listener

This commit is contained in:
Radu Ioan Fericean
2012-02-21 14:19:45 +02:00
parent 55ee9ffa8e
commit 69140bc2c3
5 changed files with 51 additions and 27 deletions

View File

@@ -1 +1,24 @@
package main
import(
"fmt"
"log"
"net/http"
)
/*
Handler for the statistics web client
*/
func statusHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "<html><body><ol>")
for _, addr := range raterList.clientAddresses {
fmt.Fprint(w, fmt.Sprintf("<li>Client: %v</li>", addr))
}
fmt.Fprint(w, "</ol></body></html>")
}
func listenToHttpRequests(){
http.HandleFunc("/status", statusHandler)
log.Print("The server is listening on ", *httpApiAddress)
http.ListenAndServe(*httpApiAddress, nil)
}

View File

@@ -2,33 +2,20 @@ package main
import (
"errors"
"fmt"
"github.com/rif/cgrates/timespans"
"log"
"net/http"
"net/rpc"
"runtime"
"time"
"flag"
)
var (
raterAddress = flag.String("rateraddr", ":2000", "Rater server address (localhost:2000)")
jsonRpcAddress = flag.String("jsonrpcaddr", ":2001", "Json RPC server address (localhost:2001)")
htpApiAddress = flag.String("httpapiaddr", ":2002", "Http API server address (localhost:2002)")
raterAddress = flag.String("rateraddr", "127.0.0.1:2000", "Rater server address (localhost:2000)")
jsonRpcAddress = flag.String("jsonrpcaddr", "127.0.0.1:2001", "Json RPC server address (localhost:2001)")
httpApiAddress = flag.String("httpapiaddr", "127.0.0.1:8000", "Http API server address (localhost:2002)")
raterList *RaterList
)
/*
Handler for the statistics web client
*/
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "<html><body><ol>")
for _, addr := range raterList.clientAddresses {
fmt.Fprint(w, fmt.Sprintf("<li>Client: %v</li>", addr))
}
fmt.Fprint(w, "</ol></body></html>")
}
/*
The function that gets the information from the raters using balancer.
@@ -55,17 +42,12 @@ func CallRater(key *timespans.CallDescriptor) (reply *timespans.CallCost) {
func main() {
flag.Parse()
runtime.GOMAXPROCS(runtime.NumCPU() - 1)
raterList = NewRaterList()
raterServer := new(RaterServer)
rpc.Register(raterServer)
rpc.HandleHTTP()
go StopSingnalHandler()
go listenToRPCRaterRequests()
go listenToJsonRPCRequests()
runtime.GOMAXPROCS(runtime.NumCPU() - 1)
http.HandleFunc("/", handler)
log.Print("The server is listening...")
http.ListenAndServe(*raterAddress, nil)
listenToHttpRequests()
}

View File

@@ -10,6 +10,9 @@ import (
type Responder byte
/*
Creates the json rpc server.
*/
func listenToJsonRPCRequests() {
l, err := net.Listen("tcp", *jsonRpcAddress)
defer l.Close()
@@ -18,7 +21,7 @@ func listenToJsonRPCRequests() {
log.Fatal(err)
}
log.Print("listening:", l.Addr())
log.Print("Listening for incomming json RPC requests on ", l.Addr())
responder := new(Responder)
rpc.Register(responder)

View File

@@ -4,7 +4,9 @@ import (
"os/signal"
"fmt"
"log"
"net"
"net/rpc"
"net/http"
"os"
"syscall"
"time"
@@ -15,6 +17,19 @@ RPC Server that handles the registering and unregistering of raters.
*/
type RaterServer byte
func listenToRPCRaterRequests(){
raterServer := new(RaterServer)
rpc.Register(raterServer)
rpc.HandleHTTP()
l, e := net.Listen("tcp", *raterAddress)
if e != nil {
log.Fatal("listen error:", e)
}
log.Print("Listening for raters on ", *raterAddress)
http.Serve(l, nil)
}
/*
Listens for SIGTERM, SIGINT, SIGQUIT system signals and shuts down all the registered raters.
*/
@@ -22,7 +37,7 @@ func StopSingnalHandler() {
log.Print("Handling stop signals...")
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
sig := <-c
log.Printf("Caught signal %v, sending shutdownto raters\n", sig)
var reply string

View File

@@ -15,7 +15,7 @@ Listens for the SIGTERM, SIGINT, SIGQUIT system signals and gracefuly unregiste
func StopSingnalHandler(server, listen *string, sg timespans.StorageGetter) {
log.Print("Handling stop signals...")
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
signal.Notify(c, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
sig := <-c
log.Printf("Caught signal %v, unregistering from server\n", sig)
@@ -58,4 +58,5 @@ func RegisterToServer(server, listen *string) {
log.Print("Could not close server registration!")
os.Exit(1)
}
log.Print("Registration finished!")
}