|
| 1 | +package discovery |
| 2 | + |
| 3 | +import ( |
| 4 | + "net" |
| 5 | + "strings" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/juju/errors" |
| 9 | + "github.com/oleksandr/bonjour" |
| 10 | +) |
| 11 | + |
| 12 | +// Merge updates the device with the new one, returning false if the operation |
| 13 | +// didn't change anything |
| 14 | +func (d *NetworkDevice) merge(dev NetworkDevice) bool { |
| 15 | + changed := false |
| 16 | + if d.Port != dev.Port { |
| 17 | + changed = true |
| 18 | + d.Port = dev.Port |
| 19 | + } |
| 20 | + if d.Address != dev.Address { |
| 21 | + changed = true |
| 22 | + d.Address = dev.Address |
| 23 | + } |
| 24 | + if d.Info != dev.Info { |
| 25 | + changed = true |
| 26 | + d.Info = dev.Info |
| 27 | + } |
| 28 | + if d.Name != dev.Name { |
| 29 | + changed = true |
| 30 | + d.Name = dev.Name |
| 31 | + } |
| 32 | + |
| 33 | + return changed |
| 34 | +} |
| 35 | + |
| 36 | +// IsUp checks if the device is listening on the given port |
| 37 | +// the timeout is 1.5 seconds |
| 38 | +// It checks up to the number of times specified. If at least one of the attempt |
| 39 | +// is successful it returns true |
| 40 | +func (d *NetworkDevice) isUp(times int) bool { |
| 41 | + for i := 0; i < times; i++ { |
| 42 | + if d._isUp() { |
| 43 | + return true |
| 44 | + } |
| 45 | + } |
| 46 | + return false |
| 47 | +} |
| 48 | + |
| 49 | +func (d *NetworkDevice) _isUp() bool { |
| 50 | + timeout := time.Duration(1500 * time.Millisecond) |
| 51 | + conn, err := net.DialTimeout("tcp", d.Address+":"+string(d.Port), timeout) |
| 52 | + if err != nil { |
| 53 | + // Check if the port 22 is open |
| 54 | + conn, err = net.DialTimeout("tcp", d.Address+":22", timeout) |
| 55 | + if err != nil { |
| 56 | + return false |
| 57 | + } |
| 58 | + conn.Close() |
| 59 | + return true |
| 60 | + } |
| 61 | + conn.Close() |
| 62 | + return true |
| 63 | +} |
| 64 | + |
| 65 | +func (m *Monitor) networkDiscover() error { |
| 66 | + entries, err := listEntries() |
| 67 | + if err != nil { |
| 68 | + return errors.Annotatef(err, "while listing the network ports") |
| 69 | + } |
| 70 | + |
| 71 | + for _, entry := range entries { |
| 72 | + m.addNetwork(&entry) |
| 73 | + |
| 74 | + } |
| 75 | + m.pruneNetwork() |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +// listEntries returns a list of bonjour entries. It's convoluted because for |
| 80 | +// some reason they decided they wanted to make it asynchronous. Seems like writing |
| 81 | +// javascript, bleah. |
| 82 | +func listEntries() ([]bonjour.ServiceEntry, error) { |
| 83 | + // Define some helper channels that don't have to survive the function |
| 84 | + finished := make(chan bool) // allows us to communicate that the reading has been completed |
| 85 | + errs := make(chan error) // allows us to communicate that an error has occurred |
| 86 | + defer func() { |
| 87 | + close(finished) |
| 88 | + close(errs) |
| 89 | + }() |
| 90 | + |
| 91 | + // Instantiate the bonjour controller |
| 92 | + resolver, err := bonjour.NewResolver(nil) |
| 93 | + if err != nil { |
| 94 | + return nil, errors.Annotatef(err, "When initializing the bonjour resolver") |
| 95 | + } |
| 96 | + |
| 97 | + results := make(chan *bonjour.ServiceEntry) |
| 98 | + |
| 99 | + // entries is the list of entries we have to return |
| 100 | + entries := []bonjour.ServiceEntry{} |
| 101 | + |
| 102 | + // Exit if after two seconds there was no response |
| 103 | + go func(exitCh chan<- bool) { |
| 104 | + time.Sleep(4 * time.Second) |
| 105 | + exitCh <- true |
| 106 | + close(results) |
| 107 | + }(resolver.Exit) |
| 108 | + |
| 109 | + // Loop through the results |
| 110 | + go func(results chan *bonjour.ServiceEntry, exit chan<- bool) { |
| 111 | + for e := range results { |
| 112 | + entries = append(entries, *e) |
| 113 | + } |
| 114 | + finished <- true |
| 115 | + }(results, resolver.Exit) |
| 116 | + |
| 117 | + // Start the resolving |
| 118 | + err = resolver.Browse("_arduino._tcp", "", results) |
| 119 | + if err != nil { |
| 120 | + close(results) |
| 121 | + errs <- errors.Annotatef(err, "When browsing the services") |
| 122 | + } |
| 123 | + |
| 124 | + select { |
| 125 | + case <-finished: |
| 126 | + return entries, nil |
| 127 | + case err := <-errs: |
| 128 | + return nil, err |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func (m *Monitor) addNetwork(e *bonjour.ServiceEntry) { |
| 133 | + device := NetworkDevice{ |
| 134 | + Name: e.Instance, |
| 135 | + Address: e.AddrIPv4.String(), |
| 136 | + Info: strings.Join(e.Text, " "), |
| 137 | + Port: e.Port, |
| 138 | + } |
| 139 | + for address, dev := range m.network { |
| 140 | + if address == device.Address { |
| 141 | + changed := dev.merge(device) |
| 142 | + if changed { |
| 143 | + m.Events <- Event{Name: "change", NetworkDevice: dev} |
| 144 | + } |
| 145 | + return |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + m.network[device.Address] = &device |
| 150 | + m.Events <- Event{Name: "add", NetworkDevice: &device} |
| 151 | +} |
| 152 | + |
| 153 | +func (m *Monitor) pruneNetwork() { |
| 154 | + toPrune := []string{} |
| 155 | + for address, dev := range m.network { |
| 156 | + if !dev.isUp(2) { |
| 157 | + toPrune = append(toPrune, address) |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + for _, port := range toPrune { |
| 162 | + m.Events <- Event{Name: "remove", NetworkDevice: m.network[port]} |
| 163 | + delete(m.network, port) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +// filter returns a new slice containing all NetworkDevice in the slice that satisfy the predicate f. |
| 168 | +func filter(vs []NetworkDevice, f func(NetworkDevice) bool) []NetworkDevice { |
| 169 | + var vsf []NetworkDevice |
| 170 | + for _, v := range vs { |
| 171 | + if f(v) { |
| 172 | + vsf = append(vsf, v) |
| 173 | + } |
| 174 | + } |
| 175 | + return vsf |
| 176 | +} |
0 commit comments