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;
31 
32 import cc.arduino.packages.discoverers.NetworkDiscovery;
33 import cc.arduino.packages.discoverers.SerialDiscovery;
34 
35 import java.util.ArrayList;
36 import java.util.List;
37 
38 import static processing.app.I18n.tr;
39 
40 public class DiscoveryManager {
41 
42   private final List<Discovery> discoverers;
43   private final SerialDiscovery serialDiscoverer = new SerialDiscovery();
44   private final NetworkDiscovery networkDiscoverer = new NetworkDiscovery();
45 
DiscoveryManager()46   public DiscoveryManager() {
47     discoverers = new ArrayList<>();
48     discoverers.add(serialDiscoverer);
49     discoverers.add(networkDiscoverer);
50 
51     // Start all discoverers
52     for (Discovery d : discoverers) {
53       try {
54         d.start();
55       } catch (Exception e) {
56         System.err.println(tr("Error starting discovery method: ") + d.getClass());
57         e.printStackTrace();
58       }
59     }
60 
61     Thread closeHook = new Thread(() -> {
62       for (Discovery d : discoverers) {
63         try {
64           d.stop();
65         } catch (Exception e) {
66           e.printStackTrace(); //just printing as the JVM is terminating
67         }
68       }
69     });
70     closeHook.setName("DiscoveryManager closeHook");
71     Runtime.getRuntime().addShutdownHook(closeHook);
72   }
73 
getSerialDiscoverer()74   public SerialDiscovery getSerialDiscoverer() {
75     return serialDiscoverer;
76   }
77 
discovery()78   public List<BoardPort> discovery() {
79     List<BoardPort> res = new ArrayList<>();
80     for (Discovery d : discoverers) {
81       res.addAll(d.listDiscoveredBoards());
82     }
83     return res;
84   }
85 
discovery(boolean complete)86   public List<BoardPort> discovery(boolean complete) {
87     List<BoardPort> res = new ArrayList<>();
88     for (Discovery d : discoverers) {
89       res.addAll(d.listDiscoveredBoards(complete));
90     }
91     return res;
92   }
93 
find(String address)94   public BoardPort find(String address) {
95     for (BoardPort boardPort : discovery()) {
96       if (boardPort.getAddress().equals(address)) {
97         return boardPort;
98       }
99     }
100     return null;
101   }
102 
find(String address, boolean complete)103   public BoardPort find(String address, boolean complete) {
104     for (BoardPort boardPort : discovery(complete)) {
105       if (boardPort.getAddress().equals(address)) {
106         return boardPort;
107       }
108     }
109     return null;
110   }
111 
112 }
113