handler.go 996 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package neighbour
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "imuslab.com/arozos/mod/network/mdns"
  6. )
  7. /*
  8. Static handlers for Cluster Neighbourhood
  9. author: tobychui
  10. */
  11. type ScanResults struct {
  12. LastUpdate int64 //Last update timestamp for the scan results
  13. ThisHost *mdns.NetworkHost //The host information this host is sending out (also looping back)
  14. NearbyHosts []*mdns.NetworkHost //Other hosts in the network
  15. }
  16. //Handle HTTP request for scanning and return the result
  17. func (d *Discoverer) HandleScanningRequest(w http.ResponseWriter, r *http.Request) {
  18. result := new(ScanResults)
  19. hosts := d.GetNearbyHosts()
  20. for _, host := range hosts {
  21. if host.UUID == d.Host.Host.UUID {
  22. //This a loopback signal
  23. result.ThisHost = host
  24. } else {
  25. //This is a signal from other host in the network
  26. result.NearbyHosts = append(result.NearbyHosts, host)
  27. }
  28. }
  29. result.LastUpdate = d.LastScanningTime
  30. js, _ := json.Marshal(result)
  31. sendJSONResponse(w, string(js))
  32. }