Adds a simple golang udp server
This will add a simple udp server to the http server which recieves packets and counts them. It will also send back a udp message with the id. Change-Id: Ia65df76747fd6b9fc74d9799a86ee9c30e297fbe
This commit is contained in:
parent
cb2c5fc1a5
commit
fe646c62c2
|
@ -4,6 +4,7 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
@ -17,6 +18,7 @@ type ConnectionCount struct {
|
||||||
cur_conn int
|
cur_conn int
|
||||||
max_conn int
|
max_conn int
|
||||||
total_conn int
|
total_conn int
|
||||||
|
packets int
|
||||||
}
|
}
|
||||||
|
|
||||||
var scoreboard ConnectionCount
|
var scoreboard ConnectionCount
|
||||||
|
@ -39,11 +41,18 @@ func (cc *ConnectionCount) close() {
|
||||||
cc.cur_conn--
|
cc.cur_conn--
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cc *ConnectionCount) stats() (int, int) {
|
func (cc *ConnectionCount) stats() (int, int, int) {
|
||||||
cc.mu.Lock()
|
cc.mu.Lock()
|
||||||
defer cc.mu.Unlock()
|
defer cc.mu.Unlock()
|
||||||
|
|
||||||
return cc.max_conn, cc.total_conn
|
return cc.max_conn, cc.total_conn, cc.packets
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cc *ConnectionCount) inc() {
|
||||||
|
cc.mu.Lock()
|
||||||
|
defer cc.mu.Unlock()
|
||||||
|
|
||||||
|
cc.packets++
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cc *ConnectionCount) reset() {
|
func (cc *ConnectionCount) reset() {
|
||||||
|
@ -52,6 +61,7 @@ func (cc *ConnectionCount) reset() {
|
||||||
|
|
||||||
cc.max_conn = 0
|
cc.max_conn = 0
|
||||||
cc.total_conn = 0
|
cc.total_conn = 0
|
||||||
|
cc.packets = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func root_handler(w http.ResponseWriter, r *http.Request) {
|
func root_handler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -78,8 +88,8 @@ func slow_handler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func stats_handler(w http.ResponseWriter, r *http.Request) {
|
func stats_handler(w http.ResponseWriter, r *http.Request) {
|
||||||
http.SetCookie(w, &sess_cookie)
|
http.SetCookie(w, &sess_cookie)
|
||||||
max_conn, total_conn := scoreboard.stats()
|
max_conn, total_conn, packets := scoreboard.stats()
|
||||||
fmt.Fprintf(w, "max_conn=%d\ntotal_conn=%d\n", max_conn, total_conn)
|
fmt.Fprintf(w, "max_conn=%d\ntotal_conn=%d\npackets=%d\n", max_conn, total_conn, packets)
|
||||||
}
|
}
|
||||||
|
|
||||||
func reset_handler(w http.ResponseWriter, r *http.Request) {
|
func reset_handler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
@ -88,9 +98,22 @@ func reset_handler(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprintf(w, "reset\n")
|
fmt.Fprintf(w, "reset\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func udp_handler(port *int) {
|
||||||
|
ServerConn, _ := net.ListenUDP("udp", &net.UDPAddr{IP: []byte{0, 0, 0, 0}, Port: *port, Zone: ""})
|
||||||
|
defer ServerConn.Close()
|
||||||
|
buf := make([]byte, 1024)
|
||||||
|
for {
|
||||||
|
n, addr, _ := ServerConn.ReadFromUDP(buf)
|
||||||
|
scoreboard.inc()
|
||||||
|
fmt.Println("Received ", string(buf[0:n]), " from ", addr)
|
||||||
|
ServerConn.WriteTo([]byte(resp), addr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
portPtr := flag.Int("port", 8080, "TCP port to listen on")
|
portPtr := flag.Int("port", 8080, "TCP port to listen on")
|
||||||
idPtr := flag.String("id", "1", "Server ID")
|
idPtr := flag.String("id", "1", "Server ID")
|
||||||
|
udpPortPtr := flag.Int("udpPort", -1, "UDP port to listen on. If port <=0 UDP server won't start")
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
@ -103,5 +126,9 @@ func main() {
|
||||||
http.HandleFunc("/stats", stats_handler)
|
http.HandleFunc("/stats", stats_handler)
|
||||||
http.HandleFunc("/reset", reset_handler)
|
http.HandleFunc("/reset", reset_handler)
|
||||||
portStr := fmt.Sprintf(":%d", *portPtr)
|
portStr := fmt.Sprintf(":%d", *portPtr)
|
||||||
|
if *udpPortPtr > 0 {
|
||||||
|
// only start server if flag is set
|
||||||
|
go udp_handler(udpPortPtr)
|
||||||
|
}
|
||||||
http.ListenAndServe(portStr, nil)
|
http.ListenAndServe(portStr, nil)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue