diff --git a/bunti_server/src/main/java/artnet4j/ArtNet.java b/bunti_server/src/main/java/artnet4j/ArtNet.java index 187c0d5..07e4296 100644 --- a/bunti_server/src/main/java/artnet4j/ArtNet.java +++ b/bunti_server/src/main/java/artnet4j/ArtNet.java @@ -25,75 +25,68 @@ import java.net.UnknownHostException; import java.util.logging.Level; import java.util.logging.Logger; -import artnet4j.events.ArtNetServerEventAdapter; -import artnet4j.events.ArtNetServerListener; import artnet4j.packets.ArtNetPacket; -import artnet4j.packets.ArtPollReplyPacket; -import artnet4j.packets.PacketType; public class ArtNet { - public static final Logger logger = - Logger.getLogger(ArtNet.class.getClass().getName()); + public static final Logger logger = Logger.getLogger(ArtNet.class.getClass().getName()); protected static final long ARTPOLL_REPLY_TIMEOUT = 3000; protected static final String VERSION = "0001-20091119"; protected ArtNetServer server; - protected ArtNetNodeDiscovery discovery; +// protected ArtNetNodeDiscovery discovery; - public ArtNet() { - logger.info("Art-Net v" + VERSION); - } +// public ArtNet() { +// logger.info("Art-Net v" + VERSION); +// } - public void addServerListener(ArtNetServerListener l) { - server.addListener(l); - } +// public void addServerListener(ArtNetServerListener l) { +// server.addListener(l); +// } +// +// public void broadcastPacket(ArtNetPacket packet) { +// server.broadcastPacket(packet); +// } - public void broadcastPacket(ArtNetPacket packet) { - server.broadcastPacket(packet); - } - - public ArtNetNodeDiscovery getNodeDiscovery() { - if (discovery == null) { - discovery = new ArtNetNodeDiscovery(this); - } - return discovery; - } +// public ArtNetNodeDiscovery getNodeDiscovery() { +// if (discovery == null) { +// discovery = new ArtNetNodeDiscovery(this); +// } +// return discovery; +// } public void init() { server = new ArtNetServer(); - server.addListener(new ArtNetServerEventAdapter() { - - @Override - public void artNetPacketReceived(ArtNetPacket packet) { - logger.fine("packet received: " + packet.getType()); - if (discovery != null - && packet.getType() == PacketType.ART_POLL_REPLY) { - discovery.discoverNode((ArtPollReplyPacket) packet); - } - } - - @Override - public void artNetServerStarted(ArtNetServer artNetServer) { - logger.fine("server started callback"); - } - - @Override - public void artNetServerStopped(ArtNetServer artNetServer) { - logger.info("server stopped"); - } - }); +// server.addListener(new ArtNetServerEventAdapter() { +// @Override +// public void artNetPacketReceived(ArtNetPacket packet) { +// logger.fine("packet received: " + packet.getType()); +// if (discovery != null && packet.getType() == PacketType.ART_POLL_REPLY) { +// discovery.discoverNode((ArtPollReplyPacket) packet); +// } +// } +// +// @Override +// public void artNetServerStarted(ArtNetServer artNetServer) { +// logger.fine("server started callback"); +// } +// +// @Override +// public void artNetServerStopped(ArtNetServer artNetServer) { +// logger.info("server stopped"); +// } +// }); } - public void removeServerListener(ArtNetServerListener l) { - server.removeListener(l); - } - - public void setBroadCastAddress(String ip) { - server.setBroadcastAddress(ip); - } +// public void removeServerListener(ArtNetServerListener l) { +// server.removeListener(l); +// } +// +// public void setBroadCastAddress(String ip) { +// server.setBroadcastAddress(ip); +// } public void start() throws SocketException, ArtNetException { if (server == null) { @@ -102,38 +95,38 @@ public class ArtNet { server.start(); } - public void startNodeDiscovery() throws ArtNetException { - getNodeDiscovery().start(); - } +// public void startNodeDiscovery() throws ArtNetException { +// getNodeDiscovery().start(); +// } public void stop() { - if (discovery != null) { - discovery.stop(); - } +// if (discovery != null) { +// discovery.stop(); +// } if (server != null) { server.stop(); } } - /** - * Sends the given packet to the specified Art-Net node. - * - * @param packet - * @param node - */ - public void unicastPacket(ArtNetPacket packet, ArtNetNode node) { - server.unicastPacket(packet, node.getIPAddress()); - } - - /** - * Sends the given packet to the specified IP address. - * - * @param packet - * @param adr - */ - public void unicastPacket(ArtNetPacket packet, InetAddress adr) { - server.unicastPacket(packet, adr); - } +// /** +// * Sends the given packet to the specified Art-Net node. +// * +// * @param packet +// * @param node +// */ +// public void unicastPacket(ArtNetPacket packet, ArtNetNode node) { +// server.unicastPacket(packet, node.getIPAddress()); +// } +// +// /** +// * Sends the given packet to the specified IP address. +// * +// * @param packet +// * @param adr +// */ +// public void unicastPacket(ArtNetPacket packet, InetAddress adr) { +// server.unicastPacket(packet, adr); +// } /** * Sends the given packet to the specified IP address. diff --git a/bunti_server/src/main/java/artnet4j/ArtNetNodeDiscovery.java b/bunti_server/src/main/java/artnet4j/ArtNetNodeDiscovery.java index 1ce8db5..7bdc754 100644 --- a/bunti_server/src/main/java/artnet4j/ArtNetNodeDiscovery.java +++ b/bunti_server/src/main/java/artnet4j/ArtNetNodeDiscovery.java @@ -1,132 +1,132 @@ -/* - * This file is part of artnet4j. - * - * Copyright 2009 Karsten Schmidt (PostSpectacular Ltd.) - * - * artnet4j 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 3 of the License, or - * (at your option) any later version. - * - * artnet4j 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 artnet4j. If not, see . - */ - -package artnet4j; - -import java.net.InetAddress; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.ConcurrentHashMap; -import java.util.logging.Logger; - -import artnet4j.events.ArtNetDiscoveryListener; -import artnet4j.packets.ArtPollPacket; -import artnet4j.packets.ArtPollReplyPacket; - -public class ArtNetNodeDiscovery implements Runnable { - - public static final int POLL_INTERVAL = 10000; - - public static final Logger logger = Logger - .getLogger(ArtNetNodeDiscovery.class.getClass().getName()); - - protected final ArtNet artNet; - protected ConcurrentHashMap discoveredNodes = new ConcurrentHashMap(); - protected List lastDiscovered = new ArrayList(); - protected List listeners = new ArrayList(); - - protected boolean isActive = true; - - protected long discoveryInterval; - - private Thread discoveryThread; - - public ArtNetNodeDiscovery(ArtNet artNet) { - this.artNet = artNet; - setInterval(POLL_INTERVAL); - } - - public void addListener(ArtNetDiscoveryListener l) { - synchronized (listeners) { - listeners.add(l); - } - } - - public void discoverNode(ArtPollReplyPacket reply) { - InetAddress nodeIP = reply.getIPAddress(); - ArtNetNode node = discoveredNodes.get(nodeIP); - if (node == null) { - logger.info("discovered new node: " + nodeIP); - node = reply.getNodeStyle().createNode(); - node.extractConfig(reply); - discoveredNodes.put(nodeIP, node); - for (ArtNetDiscoveryListener l : listeners) { - l.discoveredNewNode(node); - } - } else { - node.extractConfig(reply); - } - lastDiscovered.add(node); - } - - public void removeListener(ArtNetDiscoveryListener l) { - synchronized (listeners) { - listeners.remove(l); - } - } - - @Override - public void run() { - try { - while (isActive) { - lastDiscovered.clear(); - ArtPollPacket poll = new ArtPollPacket(); - artNet.broadcastPacket(poll); - Thread.sleep(ArtNet.ARTPOLL_REPLY_TIMEOUT); - if (isActive) { - synchronized (listeners) { - for (ArtNetNode node : discoveredNodes.values()) { - if (!lastDiscovered.contains(node)) { - discoveredNodes.remove(node.getIPAddress()); - for (ArtNetDiscoveryListener l : listeners) { - l.discoveredNodeDisconnected(node); - } - } - } - for (ArtNetDiscoveryListener l : listeners) { - l.discoveryCompleted(new ArrayList( - discoveredNodes.values())); - } - } - Thread.sleep(discoveryInterval - - ArtNet.ARTPOLL_REPLY_TIMEOUT); - } - } - } catch (InterruptedException e) { - logger.warning("node discovery interrupted"); - } - } - - public void setInterval(int interval) { - discoveryInterval = Math.max(interval, ArtNet.ARTPOLL_REPLY_TIMEOUT); - } - - public void start() throws ArtNetException { - if (discoveryThread == null) { - discoveryThread = new Thread(this); - discoveryThread.start(); - } else { - throw new ArtNetException("discovery already started."); - } - } - - public void stop() { - isActive = false; - } -} \ No newline at end of file +///* +// * This file is part of artnet4j. +// * +// * Copyright 2009 Karsten Schmidt (PostSpectacular Ltd.) +// * +// * artnet4j 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 3 of the License, or +// * (at your option) any later version. +// * +// * artnet4j 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 artnet4j. If not, see . +// */ +// +//package artnet4j; +// +//import java.net.InetAddress; +//import java.util.ArrayList; +//import java.util.List; +//import java.util.concurrent.ConcurrentHashMap; +//import java.util.logging.Logger; +// +//import artnet4j.events.ArtNetDiscoveryListener; +//import artnet4j.packets.ArtPollPacket; +//import artnet4j.packets.ArtPollReplyPacket; +// +//public class ArtNetNodeDiscovery implements Runnable { +// +// public static final int POLL_INTERVAL = 10000; +// +// public static final Logger logger = Logger +// .getLogger(ArtNetNodeDiscovery.class.getClass().getName()); +// +// protected final ArtNet artNet; +// protected ConcurrentHashMap discoveredNodes = new ConcurrentHashMap(); +// protected List lastDiscovered = new ArrayList(); +// protected List listeners = new ArrayList(); +// +// protected boolean isActive = true; +// +// protected long discoveryInterval; +// +// private Thread discoveryThread; +// +// public ArtNetNodeDiscovery(ArtNet artNet) { +// this.artNet = artNet; +// setInterval(POLL_INTERVAL); +// } +// +// public void addListener(ArtNetDiscoveryListener l) { +// synchronized (listeners) { +// listeners.add(l); +// } +// } +// +// public void discoverNode(ArtPollReplyPacket reply) { +// InetAddress nodeIP = reply.getIPAddress(); +// ArtNetNode node = discoveredNodes.get(nodeIP); +// if (node == null) { +// logger.info("discovered new node: " + nodeIP); +// node = reply.getNodeStyle().createNode(); +// node.extractConfig(reply); +// discoveredNodes.put(nodeIP, node); +// for (ArtNetDiscoveryListener l : listeners) { +// l.discoveredNewNode(node); +// } +// } else { +// node.extractConfig(reply); +// } +// lastDiscovered.add(node); +// } +// +// public void removeListener(ArtNetDiscoveryListener l) { +// synchronized (listeners) { +// listeners.remove(l); +// } +// } +// +// @Override +// public void run() { +// try { +// while (isActive) { +// lastDiscovered.clear(); +// ArtPollPacket poll = new ArtPollPacket(); +// artNet.broadcastPacket(poll); +// Thread.sleep(ArtNet.ARTPOLL_REPLY_TIMEOUT); +// if (isActive) { +// synchronized (listeners) { +// for (ArtNetNode node : discoveredNodes.values()) { +// if (!lastDiscovered.contains(node)) { +// discoveredNodes.remove(node.getIPAddress()); +// for (ArtNetDiscoveryListener l : listeners) { +// l.discoveredNodeDisconnected(node); +// } +// } +// } +// for (ArtNetDiscoveryListener l : listeners) { +// l.discoveryCompleted(new ArrayList( +// discoveredNodes.values())); +// } +// } +// Thread.sleep(discoveryInterval +// - ArtNet.ARTPOLL_REPLY_TIMEOUT); +// } +// } +// } catch (InterruptedException e) { +// logger.warning("node discovery interrupted"); +// } +// } +// +// public void setInterval(int interval) { +// discoveryInterval = Math.max(interval, ArtNet.ARTPOLL_REPLY_TIMEOUT); +// } +// +// public void start() throws ArtNetException { +// if (discoveryThread == null) { +// discoveryThread = new Thread(this); +// discoveryThread.start(); +// } else { +// throw new ArtNetException("discovery already started."); +// } +// } +// +// public void stop() { +// isActive = false; +// } +//} \ No newline at end of file diff --git a/bunti_server/src/main/java/artnet4j/ArtNetServer.java b/bunti_server/src/main/java/artnet4j/ArtNetServer.java index 69f39a1..a811232 100644 --- a/bunti_server/src/main/java/artnet4j/ArtNetServer.java +++ b/bunti_server/src/main/java/artnet4j/ArtNetServer.java @@ -25,17 +25,11 @@ import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.List; import java.util.logging.Level; -import artnet4j.events.ArtNetServerListener; import artnet4j.packets.ArtNetPacket; -import artnet4j.packets.ArtNetPacketParser; -import artnet4j.packets.ArtPollPacket; -import artnet4j.packets.PacketType; -public class ArtNetServer extends ArtNetNode implements Runnable { +public class ArtNetServer extends ArtNetNode { // implements Runnable { public static final int DEFAULT_PORT = 0x1936; @@ -51,7 +45,7 @@ public class ArtNetServer extends ArtNetNode implements Runnable { protected int receiveBufferSize; protected boolean isRunning; - protected final List listeners; +// protected final List listeners; public ArtNetServer() { this(DEFAULT_PORT, DEFAULT_PORT); @@ -61,68 +55,67 @@ public class ArtNetServer extends ArtNetNode implements Runnable { super(NodeStyle.ST_SERVER); this.port = port; this.sendPort = sendPort; - this.listeners = new ArrayList(); +// this.listeners = new ArrayList(); setBufferSize(2048); } - public void addListener(ArtNetServerListener l) { - synchronized (listeners) { - listeners.add(l); - } - } +// public void addListener(ArtNetServerListener l) { +// synchronized (listeners) { +// listeners.add(l); +// } +// } +// +// public void broadcastPacket(ArtNetPacket ap) { +// try { +// DatagramPacket packet = new DatagramPacket(ap.getData(), ap +// .getLength(), broadCastAddress, sendPort); +// socket.send(packet); +// for (ArtNetServerListener l : listeners) { +// l.artNetPacketBroadcasted(ap); +// } +// } catch (IOException e) { +// logger.warning(e.getMessage()); +// } +// } +// +// public void removeListener(ArtNetServerListener l) { +// synchronized (listeners) { +// listeners.remove(l); +// } +// } - public void broadcastPacket(ArtNetPacket ap) { - try { - DatagramPacket packet = new DatagramPacket(ap.getData(), ap - .getLength(), broadCastAddress, sendPort); - socket.send(packet); - for (ArtNetServerListener l : listeners) { - l.artNetPacketBroadcasted(ap); - } - } catch (IOException e) { - logger.warning(e.getMessage()); - } - } +// @Override +// public void run() { +// byte[] receiveBuffer = new byte[receiveBufferSize]; +// DatagramPacket receivedPacket = new DatagramPacket(receiveBuffer, receiveBuffer.length); +// +// try { +// while (isRunning) { +// socket.receive(receivedPacket); +// logger.finer("received new packet"); +// ArtNetPacket packet = ArtNetPacketParser.parse(receivedPacket); +// if (packet != null) { +// if (packet.getType() == PacketType.ART_POLL) { +// sendArtPollReply(receivedPacket.getAddress(), (ArtPollPacket) packet); +// } +// for (ArtNetServerListener l : listeners) { +// l.artNetPacketReceived(packet); +// } +// } +// } +// socket.close(); +// logger.info("server thread terminated."); +// for (ArtNetServerListener l : listeners) { +// l.artNetServerStopped(this); +// } +// } catch (IOException e) { +// e.printStackTrace(); +// } +// } - public void removeListener(ArtNetServerListener l) { - synchronized (listeners) { - listeners.remove(l); - } - } - - @Override - public void run() { - byte[] receiveBuffer = new byte[receiveBufferSize]; - DatagramPacket receivedPacket = new DatagramPacket(receiveBuffer, - receiveBuffer.length); - try { - while (isRunning) { - socket.receive(receivedPacket); - logger.finer("received new packet"); - ArtNetPacket packet = ArtNetPacketParser.parse(receivedPacket); - if (packet != null) { - if (packet.getType() == PacketType.ART_POLL) { - sendArtPollReply(receivedPacket.getAddress(), - (ArtPollPacket) packet); - } - for (ArtNetServerListener l : listeners) { - l.artNetPacketReceived(packet); - } - } - } - socket.close(); - logger.info("server thread terminated."); - for (ArtNetServerListener l : listeners) { - l.artNetServerStopped(this); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - - private void sendArtPollReply(InetAddress inetAddress, ArtPollPacket packet) { - // TODO send reply with self description - } +// private void sendArtPollReply(InetAddress inetAddress, ArtPollPacket packet) { +// // TODO send reply with self description +// } public void setBroadcastAddress(String address) { try { @@ -144,14 +137,15 @@ public class ArtNetServer extends ArtNetNode implements Runnable { setBroadcastAddress(DEFAULT_BROADCAST_IP); } if (socket == null) { - socket = new DatagramSocket(port); - logger.info("Art-Net server started at port: " + port); - for (ArtNetServerListener l : listeners) { - l.artNetServerStarted(this); - } +// socket = new DatagramSocket(port); + socket = new DatagramSocket(); +// logger.info("Art-Net server started at port: " + port); +// for (ArtNetServerListener l : listeners) { +// l.artNetServerStarted(this); +// } isRunning = true; - serverThread = new Thread(this); - serverThread.start(); +// serverThread = new Thread(this); +// serverThread.start(); } else { throw new ArtNetException( "Couldn't create server socket, server already running?"); @@ -170,13 +164,14 @@ public class ArtNetServer extends ArtNetNode implements Runnable { */ public void unicastPacket(ArtNetPacket ap, InetAddress targetAdress) { try { - DatagramPacket packet = new DatagramPacket(ap.getData(), ap - .getLength(), targetAdress, sendPort); + DatagramPacket packet = new DatagramPacket(ap.getData(), ap.getLength(), targetAdress, sendPort); + socket.send(packet); logger.finer("sent packet to: " + targetAdress); - for (ArtNetServerListener l : listeners) { - l.artNetPacketUnicasted(ap); - } + +// for (ArtNetServerListener l : listeners) { +// l.artNetPacketUnicasted(ap); +// } } catch (IOException e) { logger.warning(e.getMessage()); } diff --git a/bunti_server/src/main/java/de/ctdo/dmx/DMXDataChangedListener.java b/bunti_server/src/main/java/de/ctdo/dmx/DMXDataChangedListener.java new file mode 100644 index 0000000..d060191 --- /dev/null +++ b/bunti_server/src/main/java/de/ctdo/dmx/DMXDataChangedListener.java @@ -0,0 +1,7 @@ +package de.ctdo.dmx; + +public interface DMXDataChangedListener { + + void DMXDataChanged(int[] dmx512data); + +} diff --git a/bunti_server/src/main/java/de/ctdo/dmx/Mixer.java b/bunti_server/src/main/java/de/ctdo/dmx/Mixer.java index 58d3712..8c484ce 100644 --- a/bunti_server/src/main/java/de/ctdo/dmx/Mixer.java +++ b/bunti_server/src/main/java/de/ctdo/dmx/Mixer.java @@ -1,72 +1,52 @@ package de.ctdo.dmx; import java.net.SocketException; +import java.util.ArrayList; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - import artnet4j.ArtNet; import artnet4j.ArtNetException; import artnet4j.ArtNetNode; -import artnet4j.events.ArtNetDiscoveryListener; import artnet4j.packets.ArtDmxPacket; public class Mixer { static Mixer instance = new Mixer(); - final int TICKS_BETWEEN_DMX_SEND = 1000 * 200; + final int TICKS_BETWEEN_DMX_SEND = 20; + final int DMX_MIN_CHANNEL = 0; + final int DMX_MAX_CHANNEL = 512; Logger logger = LoggerFactory.getLogger(Mixer.class); - byte[] dmx512databuffer = new byte[512]; + int[] dmx512databuffer = new int[512]; static int sequenceID = 0; ArtNet artnet = new ArtNet(); ArtNetNode firstNodeFound = null; long ticksLastBufferFlush = 0; - + + protected final List listeners = new ArrayList(); public static Mixer getInstance() { return instance; } + + public void addListener(DMXDataChangedListener l) { + synchronized (listeners) { + listeners.add(l); + } + } + public void removeListener(DMXDataChangedListener l) { + synchronized (listeners) { + listeners.remove(l); + } + } + private Mixer() { try { - artnet.start(); - - artnet.getNodeDiscovery().addListener(new ArtNetDiscoveryListener() { - @Override - public void discoveryCompleted(List nodes) { - for (ArtNetNode artNetNode : nodes) { - logger.debug(artNetNode.toString()); - } - } - - @Override - public void discoveredNodeDisconnected(ArtNetNode node) { - if( node == firstNodeFound) { - firstNodeFound = null; - } - } - - @Override - public void discoveredNewNode(ArtNetNode node) { - if(firstNodeFound == null) { - firstNodeFound = node; - logger.info("found first artnetnode"); - } - } - - @Override - public void discoveryFailed(Throwable t) { - logger.error("ArtNetNode discovery failed"); - } - }); - - artnet.startNodeDiscovery(); - - - + artnet.start(); } catch (ArtNetException e) { logger.error("ArtNetException... " + e.toString()); @@ -79,20 +59,28 @@ public class Mixer { } private void sendOutDMXBuffer() { - if(firstNodeFound == null) { - logger.info("No ArtNetNode to send data to"); - return; - } +// if(firstNodeFound == null) { +// logger.info("No ArtNetNode to send data to"); +// return; +// } ArtDmxPacket dmx = new ArtDmxPacket(); - dmx.setUniverse(firstNodeFound.getSubNet(), firstNodeFound.getDmxOuts()[0]); +// dmx.setUniverse(firstNodeFound.getSubNet(), firstNodeFound.getDmxOuts()[0]); +// dmx.setSequenceID(sequenceID % 255); +// dmx.setDMX(dmx512databuffer, dmx512databuffer.length); +// artnet.unicastPacket(dmx, firstNodeFound.getIPAddress()); + dmx.setUniverse(0, 0); dmx.setSequenceID(sequenceID % 255); - dmx.setDMX(dmx512databuffer, dmx512databuffer.length); - artnet.unicastPacket(dmx, firstNodeFound.getIPAddress()); + byte[] arr = new byte[dmx512databuffer.length]; + for (int i = 0; i < dmx512databuffer.length; i++) { + arr[i] = (byte)dmx512databuffer[i]; + } + dmx.setDMX(arr, arr.length); + artnet.unicastPacket(dmx, "192.168.0.90"); sequenceID++; ticksLastBufferFlush = System.currentTimeMillis(); - logger.debug("Send out DMX Data"); +// logger.debug("Send out DMX Data"); } public void sendOutDMXBufferIfNeeded() { @@ -102,15 +90,20 @@ public class Mixer { } public void setDMX512Channel(int channel, int value) { - if(channel < 0 || channel > dmx512databuffer.length-1) { + if(channel < DMX_MIN_CHANNEL || channel > DMX_MAX_CHANNEL) { return; } if(value < 0) value = 0; if(value > 255) value = 255; - dmx512databuffer[channel] = (byte)value; + dmx512databuffer[channel-1] = value; + sendOutDMXBufferIfNeeded(); + + for (DMXDataChangedListener l : this.listeners) { + l.DMXDataChanged(dmx512databuffer); + } } } diff --git a/bunti_server/src/main/java/de/ctdo/websocket/DMXControllerApplication.java b/bunti_server/src/main/java/de/ctdo/websocket/DMXControllerApplication.java index d379e47..891443f 100644 --- a/bunti_server/src/main/java/de/ctdo/websocket/DMXControllerApplication.java +++ b/bunti_server/src/main/java/de/ctdo/websocket/DMXControllerApplication.java @@ -14,25 +14,31 @@ import de.ctdo.dmx.Mixer; public class DMXControllerApplication extends WebSocketApplication { Logger logger = LoggerFactory.getLogger(DMXControllerApplication.class); - public WebSocket createSocket(ProtocolHandler handler, WebSocketListener... listeners) { + @Override + public WebSocket createWebSocket(ProtocolHandler protocolHandler, WebSocketListener... listeners) { logger.debug("createSocket "); - return new DMXControllerWebSocket(handler, listeners); + DMXControllerWebSocket socket = new DMXControllerWebSocket(protocolHandler, listeners); + Mixer.getInstance().addListener(socket); + return socket; } @Override - public boolean isApplicationRequest(Request arg0) { - logger.debug("isApplicationRequest "); - return true; - } + public boolean isApplicationRequest(Request request) { + final String uri = request.requestURI().toString(); + return uri.endsWith("/dmx"); + } + @Override + public void onClose(WebSocket socket, com.sun.grizzly.websockets.DataFrame frame) { + DMXControllerWebSocket ws = (DMXControllerWebSocket) socket; + Mixer.getInstance().removeListener(ws); + logger.debug("removed websocket"); + } @Override public void onMessage(WebSocket socket, String text) { - Mixer mixer = Mixer.getInstance(); - logger.debug("got message: " + text); - if(text.startsWith("channel:")) { text = text.substring(text.indexOf(":")+1); String[] parts = text.split("="); @@ -42,13 +48,9 @@ public class DMXControllerApplication extends WebSocketApplication { // for (final WebSocket webSocket : getWebSockets()) { -// try { -// // send data to all connected clients (including caller) -// webSocket.send(data); -// } catch (IOException e) { -// e.printStackTrace(); -// webSocket.close(); -// } +// DMXControllerWebSocket ws = (DMXControllerWebSocket) webSocket; +// +// // } //super.onMessage(socket, text); } diff --git a/bunti_server/src/main/java/de/ctdo/websocket/DMXControllerWebSocket.java b/bunti_server/src/main/java/de/ctdo/websocket/DMXControllerWebSocket.java index a7a5f9f..678cacd 100644 --- a/bunti_server/src/main/java/de/ctdo/websocket/DMXControllerWebSocket.java +++ b/bunti_server/src/main/java/de/ctdo/websocket/DMXControllerWebSocket.java @@ -1,21 +1,39 @@ package de.ctdo.websocket; +import net.sf.json.JSONArray; +import net.sf.json.JSONObject; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import com.sun.grizzly.websockets.DefaultWebSocket; import com.sun.grizzly.websockets.ProtocolHandler; import com.sun.grizzly.websockets.WebSocketListener; +import de.ctdo.dmx.DMXDataChangedListener; + /** * Ein DMXControllerWebSocket gehoert immer zu einem Browserfenster/Tab * @author lucas * */ -public class DMXControllerWebSocket extends DefaultWebSocket { - +public class DMXControllerWebSocket extends DefaultWebSocket implements DMXDataChangedListener { + + Logger logger = LoggerFactory.getLogger(DMXControllerWebSocket.class); public DMXControllerWebSocket(ProtocolHandler protocolHandler, WebSocketListener[] listeners) { super(protocolHandler, listeners); } + @Override + public void DMXDataChanged(int[] dmx512data) { + + JSONArray arr = JSONArray.fromObject(dmx512data); + JSONObject obj = new JSONObject(); + obj.put("dmx512values", arr); + + send(obj.toString()); + } } diff --git a/bunti_server/src/main/webapp/index.jsp b/bunti_server/src/main/webapp/index.jsp index f924067..e46f434 100644 --- a/bunti_server/src/main/webapp/index.jsp +++ b/bunti_server/src/main/webapp/index.jsp @@ -16,14 +16,32 @@
-
+
+
+
+