From 77c4d98c4740c3973b1f3df47907d658757fadfd Mon Sep 17 00:00:00 2001 From: lonord Date: Mon, 18 Jun 2018 22:02:38 +0800 Subject: [PATCH] add net speed api to webservice --- app/context.go | 4 +-- app/webservice.go | 7 ++++ app/webservice_netspeed.go | 67 ++++++++++++++++++++++++++++++++++++++ dnsmasq/dnsmasq.go | 3 +- netutil/netspeed.go | 6 ++-- 5 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 app/webservice_netspeed.go diff --git a/app/context.go b/app/context.go index bf2cab4..e714d28 100644 --- a/app/context.go +++ b/app/context.go @@ -73,8 +73,8 @@ func (a *MainAction) RestartDnsmasq() error { } type NetStatus struct { - InnerNetSpeed netutil.NetSpeed - OuterNetSpeed netutil.NetSpeed + InnerNetSpeed netutil.NetSpeed `json:"inner"` + OuterNetSpeed netutil.NetSpeed `json:"outer"` } type WrappedNetSpeedReader struct { diff --git a/app/webservice.go b/app/webservice.go index 001444d..903a773 100644 --- a/app/webservice.go +++ b/app/webservice.go @@ -10,6 +10,7 @@ import ( "../base" "github.com/labstack/echo" "github.com/labstack/echo/middleware" + "github.com/lonord/sse" ) type WebService struct { @@ -55,6 +56,7 @@ func createEcho() *echo.Echo { } func bindRouters(ec *echo.Echo, action *MainAction) { + nss := newNetSpeedService(action) // get dnsmasq leases ec.GET("/clients", func(c echo.Context) error { r, err := action.GetOnlineClients() @@ -71,4 +73,9 @@ func bindRouters(ec *echo.Echo, action *MainAction) { } return c.String(http.StatusOK, "OK") }) + // sse net speed + ec.Any("/netspeed", func(c echo.Context) error { + nss.handleClient(sse.GenerateClientID(), c.Response().Writer) + return nil + }) } diff --git a/app/webservice_netspeed.go b/app/webservice_netspeed.go new file mode 100644 index 0000000..4669023 --- /dev/null +++ b/app/webservice_netspeed.go @@ -0,0 +1,67 @@ +package app + +import ( + "log" + "net/http" + "sync" + "time" + + "github.com/lonord/sse" +) + +const readInterval = time.Second * 3 + +type netSpeedService struct { + sse *sse.Service + action *MainAction + reader *WrappedNetSpeedReader + lck sync.Mutex +} + +func newNetSpeedService(action *MainAction) *netSpeedService { + return &netSpeedService{ + action: action, + } +} + +func (s *netSpeedService) handleClient(clientID interface{}, w http.ResponseWriter) error { + c, err := s.sse.HandleClient(clientID, w) + if err != nil { + return err + } + go s.runNetSpeedReader() + <-c + return nil +} + +func (s *netSpeedService) runNetSpeedReader() { + s.lck.Lock() + if s.reader == nil { + rd, err := s.action.CreateNetSpeedReader() + if err != nil { + log.Println(err) + } else { + s.reader = rd + } + s.lck.Unlock() + timer := time.NewTimer(readInterval) + for { + select { + case <-timer.C: + if s.sse.GetClientCount() == 0 { + s.lck.Lock() + s.reader = nil + s.lck.Unlock() + return + } + result, _ := s.reader.Read() + s.sse.Broadcast(sse.Event{ + Data: result, + }) + timer.Reset(readInterval) + } + } + } else { + s.lck.Unlock() + } +} diff --git a/dnsmasq/dnsmasq.go b/dnsmasq/dnsmasq.go index 7af3951..1f4f5a2 100644 --- a/dnsmasq/dnsmasq.go +++ b/dnsmasq/dnsmasq.go @@ -119,8 +119,7 @@ func collectInternalArgs(fileReaderFn ba.FileReaderFn, c *ba.Config) []string { args = append(args, a) } } - dhcpIPChunks := strings.Split(c.BridgeAddr, ".") - ipPrefix := strings.Join(dhcpIPChunks[:3], ".") + ipPrefix := ba.GetSubnetPrefix(c.BridgeAddr) args = append(args, fmt.Sprintf("--dhcp-range=%s.50,%s.250,12h", ipPrefix, ipPrefix)) return args } diff --git a/netutil/netspeed.go b/netutil/netspeed.go index e82ffb3..d9b1a2b 100644 --- a/netutil/netspeed.go +++ b/netutil/netspeed.go @@ -7,9 +7,9 @@ import ( ) type NetSpeed struct { - DevName string - ReceiveSpeed uint64 - TransmitSpeed uint64 + DevName string `json:"name"` + ReceiveSpeed uint64 `json:"recv"` + TransmitSpeed uint64 `json:"send"` } type NetSpeedReader struct {