handler.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. //Get networkHosts that are offline
  34. func (d *Discoverer) HandleScanRecord(w http.ResponseWriter, r *http.Request) {
  35. offlineNodes, err := d.GetOfflineHosts()
  36. if err != nil {
  37. sendErrorResponse(w, err.Error())
  38. return
  39. }
  40. js, err := json.Marshal(offlineNodes)
  41. if err != nil {
  42. sendErrorResponse(w, err.Error())
  43. return
  44. }
  45. sendJSONResponse(w, string(js))
  46. }
  47. //Send wake on land to target
  48. func (d *Discoverer) HandleWakeOnLan(w http.ResponseWriter, r *http.Request) {
  49. mac, err := mv(r, "mac", false)
  50. if err != nil {
  51. sendErrorResponse(w, "Invalid mac address")
  52. return
  53. }
  54. err = d.SendWakeOnLan(mac)
  55. if err != nil {
  56. sendErrorResponse(w, err.Error())
  57. return
  58. }
  59. sendOK(w)
  60. }