handler.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. }
  33. func (d *Discoverer) HandleScanRecord(w http.ResponseWriter, r *http.Request) {
  34. offlineNodes, err := d.GetOfflineHosts()
  35. if err != nil {
  36. sendErrorResponse(w, err.Error())
  37. return
  38. }
  39. js, err := json.Marshal(offlineNodes)
  40. if err != nil {
  41. sendErrorResponse(w, err.Error())
  42. return
  43. }
  44. sendJSONResponse(w, string(js))
  45. }