2013-07-22 17:47:54 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 Arduino LLC. All right reserved.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software Foundation,
|
|
|
|
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2013-06-02 19:02:57 +02:00
|
|
|
package cc.arduino.packages;
|
|
|
|
|
2013-06-25 16:13:14 +02:00
|
|
|
import cc.arduino.packages.discoverers.NetworkDiscovery;
|
|
|
|
import cc.arduino.packages.discoverers.SerialDiscovery;
|
2013-06-02 19:02:57 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2013-06-25 16:13:14 +02:00
|
|
|
import static processing.app.I18n._;
|
2013-06-02 19:02:57 +02:00
|
|
|
|
|
|
|
public class DiscoveryManager {
|
|
|
|
|
2013-07-10 09:42:27 +02:00
|
|
|
private final List<Discovery> discoverers;
|
2013-06-02 19:02:57 +02:00
|
|
|
|
|
|
|
public DiscoveryManager() {
|
2013-07-10 09:42:27 +02:00
|
|
|
discoverers = new ArrayList<Discovery>();
|
2013-06-02 19:02:57 +02:00
|
|
|
discoverers.add(new SerialDiscovery());
|
|
|
|
discoverers.add(new NetworkDiscovery());
|
|
|
|
|
|
|
|
// Start all discoverers
|
|
|
|
for (Discovery d : discoverers) {
|
|
|
|
try {
|
|
|
|
d.start();
|
|
|
|
} catch (Exception e) {
|
|
|
|
System.err.println(_("Error starting discovery method: ") + d.getClass());
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2013-06-25 16:13:14 +02:00
|
|
|
|
2013-07-10 09:42:27 +02:00
|
|
|
Thread closeHook = new Thread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
for (Discovery d : discoverers) {
|
|
|
|
try {
|
|
|
|
d.stop();
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-06-02 19:02:57 +02:00
|
|
|
Runtime.getRuntime().addShutdownHook(closeHook);
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<BoardPort> discovery() {
|
|
|
|
List<BoardPort> res = new ArrayList<BoardPort>();
|
|
|
|
for (Discovery d : discoverers)
|
|
|
|
res.addAll(d.discovery());
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|