|
| 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 cc.arduino.packages.discoverers.simpleudp.UdpRunnable; |
| 35 | + |
| 36 | +import java.util.LinkedList; |
| 37 | +import java.util.List; |
| 38 | +import java.util.Timer; |
| 39 | + |
| 40 | +public class SimpleUDPDiscovery implements Discovery { |
| 41 | + |
| 42 | + private Timer udpBoardsListerTimer; |
| 43 | + private final List<BoardPort> udpBoardPorts; |
| 44 | + private Thread thread; |
| 45 | + private UdpRunnable runner; |
| 46 | + //private SerialBoardsLister serialBoardsLister = new SerialBoardsLister((SerialBoardsLister)this); |
| 47 | + |
| 48 | + public SimpleUDPDiscovery() { |
| 49 | + this.udpBoardPorts = new LinkedList<>(); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public List<BoardPort> listDiscoveredBoards() { |
| 54 | + return getudpBoardPorts(false); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public List<BoardPort> listDiscoveredBoards(boolean complete) { |
| 59 | + return getudpBoardPorts(complete); |
| 60 | + } |
| 61 | + |
| 62 | + private List<BoardPort> getudpBoardPorts(boolean complete) { |
| 63 | + if (complete) { |
| 64 | + return new LinkedList<>(udpBoardPorts); |
| 65 | + } |
| 66 | + List<BoardPort> onlineBoardPorts = new LinkedList<>(); |
| 67 | + for (BoardPort port : runner.udpBoardPorts) { |
| 68 | + if (port.isOnline() == true) { |
| 69 | + onlineBoardPorts.add(port); |
| 70 | + } |
| 71 | + } |
| 72 | + return onlineBoardPorts; |
| 73 | + } |
| 74 | + |
| 75 | + public void setudpBoardPorts(List<BoardPort> newudpBoardPorts) { |
| 76 | + udpBoardPorts.clear(); |
| 77 | + udpBoardPorts.addAll(newudpBoardPorts); |
| 78 | + } |
| 79 | + |
| 80 | + public void forceRefresh() { |
| 81 | + runner.udpBoardPorts.clear(); |
| 82 | + } |
| 83 | + |
| 84 | + public void setUploadInProgress(boolean param) { |
| 85 | + //serialBoardsLister.uploadInProgress = param; |
| 86 | + } |
| 87 | + |
| 88 | + public void pausePolling(boolean param) { |
| 89 | + //serialBoardsLister.pausePolling = param; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void start() { |
| 94 | +//System.out.println("SimpleUDPDiscovery.start!!!"); |
| 95 | + |
| 96 | + runner = new UdpRunnable(); |
| 97 | + thread = new Thread(runner); |
| 98 | + thread.start(); |
| 99 | + |
| 100 | + forceRefresh(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void stop() { |
| 105 | + runner.terminate(); |
| 106 | + } |
| 107 | +} |
| 108 | + |
0 commit comments