|
| 1 | +/* |
| 2 | + * This file is part of Arduino. |
| 3 | + * |
| 4 | + * Arduino is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | + * |
| 18 | + * As a special exception, you may use this file as part of a free software |
| 19 | + * library without restriction. Specifically, if other files instantiate |
| 20 | + * templates or use macros or inline functions from this file, or you compile |
| 21 | + * this file and link it with other files to produce an executable, this |
| 22 | + * file does not by itself cause the resulting executable to be covered by |
| 23 | + * the GNU General Public License. This exception does not however |
| 24 | + * invalidate any other reasons why the executable file might be covered by |
| 25 | + * the GNU General Public License. |
| 26 | + * |
| 27 | + * Copyright 2013 Arduino LLC (http://www.arduino.cc/) |
| 28 | + */ |
| 29 | + |
| 30 | +package cc.arduino.packages.discoverers; |
| 31 | + |
| 32 | +//import cc.arduino.packages.BoardPort; |
| 33 | +//import cc.arduino.packages.Discovery; |
| 34 | +import java.io.IOException; |
| 35 | +import java.net.InetAddress; |
| 36 | +import java.util.HashSet; |
| 37 | +import java.util.Hashtable; |
| 38 | +import java.util.Iterator; |
| 39 | +import java.util.Map; |
| 40 | +import java.util.Timer; |
| 41 | + |
| 42 | +import javax.jmdns.JmDNS; |
| 43 | +import javax.jmdns.NetworkTopologyDiscovery; |
| 44 | +import javax.jmdns.ServiceEvent; |
| 45 | +import javax.jmdns.ServiceInfo; |
| 46 | +import javax.jmdns.ServiceListener; |
| 47 | +import javax.jmdns.impl.DNSTaskStarter; |
| 48 | + |
| 49 | +import processing.app.zeroconf.jmdns.ArduinoDNSTaskStarter; |
| 50 | +import cc.arduino.packages.discoverers.network.NetworkChecker; |
| 51 | +//import processing.app.Base; |
| 52 | +//import processing.app.helpers.NetUtils; |
| 53 | +//import processing.app.helpers.PreferencesMap; |
| 54 | +//import processing.app.zeroconf.jmdns.ArduinoDNSTaskStarter; |
| 55 | + |
| 56 | +public class NetworkDiscovery implements ServiceListener, cc.arduino.packages.discoverers.network.NetworkTopologyListener { |
| 57 | + |
| 58 | + private class bonour { |
| 59 | + public String address; |
| 60 | + public String name; |
| 61 | + |
| 62 | + public String board; |
| 63 | + public String distroversion; |
| 64 | + |
| 65 | + public String port; |
| 66 | + |
| 67 | + public bonour() { |
| 68 | + address = ""; |
| 69 | + name = ""; |
| 70 | + board = ""; |
| 71 | + distroversion = ""; |
| 72 | + port = ""; |
| 73 | + } |
| 74 | + |
| 75 | + public String getLabel() { |
| 76 | + return name + " at " + address + " (" + board + ")" + distroversion + " " + port; |
| 77 | + } |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + private Timer timer; |
| 82 | + private final HashSet<bonour> myComPorts; // well not really com ports but we treat them like com ports |
| 83 | + private final Map<InetAddress, JmDNS> mappedJmDNSs; |
| 84 | + |
| 85 | + public NetworkDiscovery() { |
| 86 | + DNSTaskStarter.Factory.setClassDelegate(new ArduinoDNSTaskStarter()); |
| 87 | + this.myComPorts = new HashSet<bonour>(); |
| 88 | + this.mappedJmDNSs = new Hashtable<InetAddress, JmDNS>(); |
| 89 | + } |
| 90 | + |
| 91 | + public String[] getList() { |
| 92 | + String[] ret = new String[myComPorts.size()]; |
| 93 | + int curPort = 0; |
| 94 | + Iterator<bonour> iterator = myComPorts.iterator(); |
| 95 | + while (iterator.hasNext()) { |
| 96 | + bonour board = iterator.next(); |
| 97 | + ret[curPort++] = board.getLabel(); |
| 98 | + } |
| 99 | + return ret; |
| 100 | + } |
| 101 | + |
| 102 | + // @Override |
| 103 | + // public List<BoardPort> discovery() { |
| 104 | + // List<BoardPort> ports = clonePortsList(); |
| 105 | + // Iterator<BoardPort> iterator = ports.iterator(); |
| 106 | + // while (iterator.hasNext()) { |
| 107 | + // try { |
| 108 | + // BoardPort board = iterator.next(); |
| 109 | + // if (!NetUtils.isReachable(InetAddress.getByName(board.getAddress()), Integer.parseInt(board.getPrefs().get("port")))) { |
| 110 | + // iterator.remove(); |
| 111 | + // } |
| 112 | + // } catch (UnknownHostException e) { |
| 113 | + // iterator.remove(); |
| 114 | + // } |
| 115 | + // } |
| 116 | + // return ports; |
| 117 | + // } |
| 118 | + |
| 119 | + // private List<BoardPort> clonePortsList() { |
| 120 | + // synchronized (this) { |
| 121 | + // return new ArrayList<BoardPort>(this.ports); |
| 122 | + // } |
| 123 | + // } |
| 124 | + |
| 125 | + public void start() { |
| 126 | + this.timer = new Timer(this.getClass().getName() + " timer"); |
| 127 | + new NetworkChecker(this, NetworkTopologyDiscovery.Factory.getInstance()).start(timer); |
| 128 | + } |
| 129 | + |
| 130 | + public void stop() { |
| 131 | + timer.purge(); |
| 132 | + // we don't close each JmDNS instance as it's too slow |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void serviceAdded(ServiceEvent serviceEvent) { |
| 137 | + String type = serviceEvent.getType(); |
| 138 | + String name = serviceEvent.getName(); |
| 139 | + |
| 140 | + JmDNS dns = serviceEvent.getDNS(); |
| 141 | + |
| 142 | + dns.requestServiceInfo(type, name); |
| 143 | + ServiceInfo serviceInfo = dns.getServiceInfo(type, name); |
| 144 | + if (serviceInfo != null) { |
| 145 | + dns.requestServiceInfo(type, name); |
| 146 | + } |
| 147 | + |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public void serviceRemoved(ServiceEvent serviceEvent) { |
| 152 | + String name = serviceEvent.getName(); |
| 153 | + synchronized (this) { |
| 154 | + removeBoardswithSameName(name); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public void serviceResolved(ServiceEvent serviceEvent) { |
| 160 | + ServiceInfo info = serviceEvent.getInfo(); |
| 161 | + for (InetAddress inetAddress : info.getInet4Addresses()) { |
| 162 | + bonour newItem = new bonour(); |
| 163 | + newItem.address = inetAddress.getHostAddress(); |
| 164 | + newItem.name = serviceEvent.getName(); |
| 165 | + if (info.hasData()) { |
| 166 | + newItem.board = info.getPropertyString("board"); |
| 167 | + newItem.distroversion = info.getPropertyString("distro_version"); |
| 168 | + } |
| 169 | + newItem.port = Integer.toString(info.getPort()); |
| 170 | + |
| 171 | + synchronized (this) { |
| 172 | + removeBoardswithSameAdress(newItem); |
| 173 | + myComPorts.add(newItem); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + private void removeBoardswithSameAdress(bonour newBoard) { |
| 179 | + Iterator<bonour> iterator = myComPorts.iterator(); |
| 180 | + while (iterator.hasNext()) { |
| 181 | + bonour board = iterator.next(); |
| 182 | + if (newBoard.address.equals(board.address)) { |
| 183 | + iterator.remove(); |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + private void removeBoardswithSameName(String name) { |
| 189 | + Iterator<bonour> iterator = myComPorts.iterator(); |
| 190 | + while (iterator.hasNext()) { |
| 191 | + bonour board = iterator.next(); |
| 192 | + if (name.equals(board.name)) { |
| 193 | + iterator.remove(); |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public void inetAddressAdded(InetAddress address) { |
| 200 | + if (mappedJmDNSs.containsKey(address)) { |
| 201 | + return; |
| 202 | + } |
| 203 | + try { |
| 204 | + JmDNS jmDNS = JmDNS.create(address); |
| 205 | + jmDNS.addServiceListener("_arduino._tcp.local.", this); |
| 206 | + mappedJmDNSs.put(address, jmDNS); |
| 207 | + } catch (Exception e) { |
| 208 | + e.printStackTrace(); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + public void inetAddressRemoved(InetAddress address) { |
| 214 | + JmDNS jmDNS = mappedJmDNSs.remove(address); |
| 215 | + if (jmDNS != null) { |
| 216 | + try { |
| 217 | + jmDNS.close(); |
| 218 | + } catch (IOException e) { |
| 219 | + e.printStackTrace(); |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | +} |
0 commit comments