|
1 |
| -gf |
| 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.simpleudp; |
| 31 | + |
| 32 | +import cc.arduino.packages.BoardPort; |
| 33 | +import processing.app.BaseNoGui; |
| 34 | +import processing.app.Platform; |
| 35 | +import processing.app.debug.TargetBoard; |
| 36 | + |
| 37 | +import java.net.DatagramSocket; |
| 38 | +import java.net.DatagramPacket; |
| 39 | +import java.net.InetAddress; |
| 40 | +import java.net.UnknownHostException; |
| 41 | + |
| 42 | +import java.util.ArrayList; |
| 43 | +import java.util.Iterator; |
| 44 | +import java.util.List; |
| 45 | +import java.util.Collections; |
| 46 | + |
| 47 | + |
| 48 | +import java.util.*; |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +public class UdpRunnable implements Runnable { |
| 53 | + |
| 54 | + private boolean running = true; |
| 55 | + // local, thread save list |
| 56 | + public final List<BoardPort> udpBoardPorts = Collections.synchronizedList(new LinkedList<>()); |
| 57 | + |
| 58 | + private int hasip(String ip) { |
| 59 | + int i = 0; |
| 60 | + for (BoardPort port : udpBoardPorts) { |
| 61 | + if (port.getAddress().equals(ip)) { |
| 62 | + return i; |
| 63 | + } |
| 64 | + i++; |
| 65 | + } |
| 66 | + |
| 67 | + return -1; |
| 68 | + } |
| 69 | + |
| 70 | + public void run(){ |
| 71 | + System.out.println("MyRunnable running"); |
| 72 | + |
| 73 | + while (running) |
| 74 | + { |
| 75 | + try |
| 76 | + { |
| 77 | + DatagramSocket socket = new DatagramSocket(8531, InetAddress.getByName("0.0.0.0")); |
| 78 | + socket.setBroadcast(true); |
| 79 | + System.out.println("Listen on " + socket.getLocalAddress() + " from " + socket.getInetAddress() + " port " + socket.getBroadcast()); |
| 80 | + byte[] buf = new byte[512]; |
| 81 | + DatagramPacket packet = new DatagramPacket(buf, buf.length); |
| 82 | + while (true) { |
| 83 | + System.out.println("Waiting for data"); |
| 84 | + socket.receive(packet); |
| 85 | + System.out.print(packet.getLength()); |
| 86 | + System.out.print(" Data received from "); |
| 87 | + |
| 88 | + String board = null; |
| 89 | + InetAddress senderip = packet.getAddress(); |
| 90 | + System.out.println(senderip); |
| 91 | + |
| 92 | + String msg = new String(packet.getData()); |
| 93 | + String[] lines = msg.split("\\n"); |
| 94 | + |
| 95 | + System.out.println(""); |
| 96 | + |
| 97 | + System.out.print(">>>"); |
| 98 | + System.out.print(lines[0]); |
| 99 | + System.out.println("<<<<"); |
| 100 | + |
| 101 | + // msg typ 1 |
| 102 | + if (lines[0].equals("1")) { |
| 103 | + if (hasip(senderip.toString())==-1) { |
| 104 | + BoardPort port = new BoardPort(); |
| 105 | + |
| 106 | + port.setAddress(senderip.toString()); |
| 107 | + port.setProtocol("network"); |
| 108 | + port.setOnlineStatus(true); |
| 109 | + port.setLabel(lines[1]+" at "+senderip.toString()); |
| 110 | + |
| 111 | + udpBoardPorts.add(port); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + } |
| 117 | + catch (UnknownHostException e) { |
| 118 | + e.printStackTrace(); |
| 119 | + } |
| 120 | + catch ( Exception e ) |
| 121 | + { |
| 122 | + running=false; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + } |
| 127 | + |
| 128 | + public void terminate() |
| 129 | + { |
| 130 | + running = false; |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | +} |
| 135 | + |
| 136 | +/* |
| 137 | + even more simple device discovery protocol |
| 138 | + |
| 139 | + send a broadcase to port 8531 |
| 140 | + |
| 141 | + message format |
| 142 | + |
| 143 | + 1\Ndisplayname\N |
| 144 | + |
| 145 | + 1 - id of this message type |
| 146 | + future protocols can choose different numbers to implement more details |
| 147 | + |
| 148 | +
|
| 149 | + Use Collections.synchronizedList(). |
| 150 | + |
| 151 | + |
| 152 | +*/ |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | + |
0 commit comments