Skip to content

Commit

Permalink
add net speed api to webservice
Browse files Browse the repository at this point in the history
  • Loading branch information
lonord committed Jun 18, 2018
1 parent b7d9022 commit 77c4d98
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions app/webservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"../base"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"github.com/lonord/sse"
)

type WebService struct {
Expand Down Expand Up @@ -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()
Expand All @@ -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
})
}
67 changes: 67 additions & 0 deletions app/webservice_netspeed.go
Original file line number Diff line number Diff line change
@@ -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()
}
}
3 changes: 1 addition & 2 deletions dnsmasq/dnsmasq.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions netutil/netspeed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 77c4d98

Please sign in to comment.