java-org.hwo/src/org/hwo/beacon/Beacon.java

244 lines
5.0 KiB
Java

package org.hwo.beacon;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
import javax.net.SocketFactory;
public class Beacon extends Thread{
public class BeaconSender
{
private Properties properties;
private InetAddress inetAddress;
private Long lastBeacon;
public BeaconSender()
{
properties = new Properties();
lastBeacon = 0l;
}
public InetAddress getInetAddress() {
return inetAddress;
}
public void setInetAddress(InetAddress inetAddress) {
this.inetAddress = inetAddress;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public Long getLastBeacon() {
return lastBeacon;
}
public void setLastBeacon(Long lastBeacon) {
this.lastBeacon = lastBeacon;
}
}
public static void main(String[] args)
{
int mode = 0;
for (String arg:args)
{
if (arg.equals("-s"))
mode = 1;
}
Beacon beacon = new Beacon(44999);
beacon.setBeaconName("Test beacon");
if (mode == 1)
{
beacon.start();
System.console().printf("Press Enter to exit!");
System.console().readLine();
System.console().printf("exiting...");
beacon.exit();
} else
{
beacon.ping();
}
}
private int port;
private boolean exit;
private DatagramSocket socket;
private Properties properties;
private Integer intervall;
private String beaconName;
private UUID uuid;
private Hashtable<String, BeaconSender> beaconSenders;
public Beacon(int port)
{
this.port = port;
this.socket = null;
this.properties = new Properties();
this.intervall = 5000;
this.beaconSenders = new Hashtable<String, Beacon.BeaconSender>();
this.beaconName = "DefaultBeacon";
this.uuid = UUID.randomUUID();
}
public synchronized void exit()
{
exit = true;
}
public synchronized boolean getExit()
{
return exit;
}
@Override
public void run() {
Timer pingTimer = new Timer();
pingTimer.schedule(new TimerTask() {
@Override
public void run() {
ping();
}
}, intervall, intervall);
try {
socket = new DatagramSocket(this.port);
DatagramPacket packet = new DatagramPacket(new byte[1500], 1500);
while (!getExit())
{
try {
socket.receive(packet);
ByteArrayInputStream bis = new ByteArrayInputStream(packet.getData(),0,packet.getLength());
BeaconSender bs = beaconSenders.get(packet.getAddress().getHostAddress());
if (bs == null)
{
bs = new BeaconSender();
bs.setInetAddress(packet.getAddress());
}
bs.setLastBeacon((new Date()).getTime());
bs.getProperties().load(bis);
if (bs.getProperties().getProperty("beacon.uuid").equals(uuid.toString()))
{
System.err.println("Received Local Echo.");
} else
{
System.err.println(String.format("Beacon from %s",packet.getAddress().getHostAddress()));
System.err.println(String.format("Beacon: %s", bs.getProperties().getProperty("beacon.name")));
beaconSenders.put(packet.getAddress().getHostAddress(), bs);
};
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (SocketException e) {
e.printStackTrace();
}
pingTimer.cancel();
}
public void ping()
{
DatagramSocket sock = socket;
try {
if (sock == null)
sock = new DatagramSocket();
} catch (SocketException e1) {
e1.printStackTrace();
}
properties.setProperty("beacon.name", beaconName);
properties.setProperty("beacon.uuid", uuid.toString());
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
properties.store(bos, null);
byte[] bytes = bos.toByteArray();
DatagramPacket packet = new DatagramPacket(bytes,bytes.length);
packet.setPort(this.port);
packet.setAddress(InetAddress.getByName("255.255.255.255"));
sock.send(packet);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public Integer getIntervall() {
return intervall;
}
public void setIntervall(Integer intervall) {
this.intervall = intervall;
}
public Hashtable<String, BeaconSender> getBeaconSenders() {
return beaconSenders;
}
public void setBeaconSenders(Hashtable<String, BeaconSender> beaconSenders) {
this.beaconSenders = beaconSenders;
}
public String getBeaconName() {
return beaconName;
}
public void setBeaconName(String beaconName) {
this.beaconName = beaconName;
}
}