spring eingebaut. TestController zum testen

This commit is contained in:
Lucas Pleß 2012-03-02 00:51:12 +01:00
parent ffc7352e4d
commit c4f88ec73f
15 changed files with 343 additions and 170 deletions

View File

@ -2,15 +2,11 @@
<classpath> <classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java"/> <classpathentry kind="src" output="target/classes" path="src/main/java"/>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/> <classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER"> <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes> <attributes>
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/> <attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
<attributes>
<attribute name="owner.project.facets" value="java"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/> <classpathentry kind="output" path="target/classes"/>
</classpath> </classpath>

View File

@ -1,10 +1,23 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>ctdo</groupId> <groupId>de.ctdo</groupId>
<artifactId>bunti_server</artifactId> <artifactId>bunti.server</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging> <packaging>war</packaging>
<properties>
<org.springframework.version>3.0.5.RELEASE</org.springframework.version>
</properties>
<repositories>
<repository>
<id>com.springsource.repository.bundles.release</id>
<name>EBR Spring Release Repository</name>
<url>http://repository.springsource.com/maven/bundles/release</url>
</repository>
</repositories>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.glassfish</groupId> <groupId>org.glassfish</groupId>
@ -39,6 +52,23 @@
</dependency> </dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
@ -51,8 +81,36 @@
<source>1.6</source> <source>1.6</source>
<target>1.6</target> <target>1.6</target>
</configuration> </configuration>
</plugin> </plugin><!--
<plugin>
<groupId>org.glassfish.maven.plugin</groupId>
<artifactId>maven-glassfish-plugin</artifactId>
<version>2.1</version>
<configuration>
<glassfishDirectory>/opt/glassfish3/glassfish/</glassfishDirectory>
<user>admin</user>
<adminPassword>foobar2342</adminPassword>
<domain>
<name>${project.artifactId}</name>
<adminPort>4848</adminPort>
<httpPort>8080</httpPort>
<httpsPort>8443</httpsPort>
</domain>
<components>
<component>
<name>${project.artifactId}</name>
<artifact>${project.build.directory}/${project.build.finalName}.war</artifact>
</component>
</components>
</configuration>
</plugin>-->
</plugins> </plugins>
</build> </build>
<url>http://bunti.ctdo.de:8080/bunti/</url>
<organization>
<url>www.ctdo.de</url>
<name>Chaostreff Dortmund</name>
</organization>
</project> </project>

View File

@ -0,0 +1,5 @@
package de.ctdo.control;
public interface BroadcastListener {
void Broadcast(String message);
}

View File

@ -0,0 +1,56 @@
package de.ctdo.control;
import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONObject;
import de.ctdo.dmx.DMXMixer;
public class BuntiController {
static BuntiController instance = new BuntiController();
DMXMixer mixer = null;
protected final List<BroadcastListener> listeners = new ArrayList<BroadcastListener>();
public static BuntiController getInstance() {
return instance;
}
private BuntiController() {
mixer = new DMXMixer();
}
public void addListener(BroadcastListener l) {
synchronized (listeners) {
listeners.add(l);
}
}
public void removeListener(BroadcastListener l) {
synchronized (listeners) {
listeners.remove(l);
}
}
public void performJSONString(String json) {
JSONObject jsonobj = JSONObject.fromObject(json);
if (jsonobj.containsKey("command")) {
String command = jsonobj.get("command").toString();
if (command.equals("setdmxchannels")) {
} else if (command.equals("switchdevice")) {
}
}
}
public void sendBroadcastMessage(String message) {
for (BroadcastListener l : listeners) {
l.Broadcast(message);
}
}
}

View File

@ -12,14 +12,14 @@ import artnet4j.ArtNetException;
import artnet4j.ArtNetNode; import artnet4j.ArtNetNode;
import artnet4j.packets.ArtDmxPacket; import artnet4j.packets.ArtDmxPacket;
public class Mixer { public class DMXMixer {
static Mixer instance = new Mixer();
final int TICKS_BETWEEN_DMX_SEND = 20; final int TICKS_BETWEEN_DMX_SEND = 20;
final int DMX_MIN_CHANNEL = 0; final int DMX_MIN_CHANNEL = 0;
final int DMX_MAX_CHANNEL = 512; final int DMX_MAX_CHANNEL = 512;
final String ARTNET_DEVICE_ADDRESS = "192.168.0.90"; final String ARTNET_DEVICE_ADDRESS = "192.168.0.90";
Logger logger = LoggerFactory.getLogger(Mixer.class); Logger logger = LoggerFactory.getLogger(DMXMixer.class);
int[] dmx512databuffer = new int[512]; int[] dmx512databuffer = new int[512];
static int sequenceID = 0; static int sequenceID = 0;
ArtNet artnet = new ArtNet(); ArtNet artnet = new ArtNet();
@ -28,11 +28,10 @@ public class Mixer {
long ticksLastBufferFlush = 0; long ticksLastBufferFlush = 0;
protected final List<DMXDataChangedListener> listeners = new ArrayList<DMXDataChangedListener>(); protected final List<DMXDataChangedListener> listeners = new ArrayList<DMXDataChangedListener>();
public static Mixer getInstance() {
return instance;
}
public void addListener(DMXDataChangedListener l) { public void addListener(DMXDataChangedListener l) {
synchronized (listeners) { synchronized (listeners) {
listeners.add(l); listeners.add(l);
@ -45,7 +44,7 @@ public class Mixer {
} }
private Mixer() { public DMXMixer() {
artnetDmxPacket = new ArtDmxPacket(); artnetDmxPacket = new ArtDmxPacket();
artnetDmxPacket.setUniverse(0, 0); artnetDmxPacket.setUniverse(0, 0);

View File

@ -0,0 +1,19 @@
package de.ctdo.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class TestController {
@RequestMapping("/foobar")
public ModelAndView blafasel() {
ModelAndView mav = new ModelAndView("hello.jsp");
return mav;
}
}

View File

@ -9,16 +9,15 @@ import com.sun.grizzly.websockets.WebSocket;
import com.sun.grizzly.websockets.WebSocketApplication; import com.sun.grizzly.websockets.WebSocketApplication;
import com.sun.grizzly.websockets.WebSocketListener; import com.sun.grizzly.websockets.WebSocketListener;
import de.ctdo.dmx.Mixer; import de.ctdo.control.BuntiController;
public class BuntiControllerApplication extends WebSocketApplication { public class BuntiControllerApplication extends WebSocketApplication {
Logger logger = LoggerFactory.getLogger(BuntiControllerApplication.class); Logger logger = LoggerFactory.getLogger(BuntiControllerApplication.class);
@Override @Override
public WebSocket createWebSocket(ProtocolHandler protocolHandler, WebSocketListener... listeners) { public WebSocket createWebSocket(ProtocolHandler protocolHandler, WebSocketListener... listeners) {
logger.debug("createSocket ");
BuntiControllerWebSocket socket = new BuntiControllerWebSocket(protocolHandler, listeners); BuntiControllerWebSocket socket = new BuntiControllerWebSocket(protocolHandler, listeners);
Mixer.getInstance().addListener(socket); BuntiController.getInstance().addListener(socket);
return socket; return socket;
} }
@ -31,20 +30,14 @@ public class BuntiControllerApplication extends WebSocketApplication {
@Override @Override
public void onClose(WebSocket socket, com.sun.grizzly.websockets.DataFrame frame) { public void onClose(WebSocket socket, com.sun.grizzly.websockets.DataFrame frame) {
BuntiControllerWebSocket ws = (BuntiControllerWebSocket) socket; BuntiControllerWebSocket ws = (BuntiControllerWebSocket) socket;
Mixer.getInstance().removeListener(ws); BuntiController.getInstance().removeListener(ws);
logger.debug("removed websocket");
} }
@Override @Override
public void onMessage(WebSocket socket, String text) { public void onMessage(WebSocket socket, String text) {
Mixer mixer = Mixer.getInstance();
if(text.startsWith("channel:")) {
text = text.substring(text.indexOf(":")+1);
String[] parts = text.split("=");
mixer.setDMX512Channel(Integer.parseInt(parts[0]), Integer.parseInt(parts[1])); BuntiController.getInstance().performJSONString(text);
}
// for (final WebSocket webSocket : getWebSockets()) { // for (final WebSocket webSocket : getWebSockets()) {

View File

@ -10,7 +10,7 @@ import org.slf4j.LoggerFactory;
import com.sun.grizzly.websockets.WebSocketEngine; import com.sun.grizzly.websockets.WebSocketEngine;
@WebServlet(urlPatterns = "/bunti")
public class BuntiControllerServlet extends HttpServlet { public class BuntiControllerServlet extends HttpServlet {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
Logger logger = LoggerFactory.getLogger(BuntiControllerServlet.class); Logger logger = LoggerFactory.getLogger(BuntiControllerServlet.class);
@ -20,13 +20,13 @@ public class BuntiControllerServlet extends HttpServlet {
@Override @Override
public void init(ServletConfig config) throws ServletException { public void init(ServletConfig config) throws ServletException {
WebSocketEngine.getEngine().register(app); WebSocketEngine.getEngine().register(app);
logger.debug("registered"); logger.debug("registered BuntiControllerApplication");
} }
@Override @Override
public void destroy() { public void destroy() {
WebSocketEngine.getEngine().unregister(app); WebSocketEngine.getEngine().unregister(app);
logger.debug("unregistered"); logger.debug("unregistered BuntiControllerApplication");
} }
} }

View File

@ -1,8 +1,5 @@
package de.ctdo.websocket; package de.ctdo.websocket;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -10,14 +7,14 @@ import com.sun.grizzly.websockets.DefaultWebSocket;
import com.sun.grizzly.websockets.ProtocolHandler; import com.sun.grizzly.websockets.ProtocolHandler;
import com.sun.grizzly.websockets.WebSocketListener; import com.sun.grizzly.websockets.WebSocketListener;
import de.ctdo.dmx.DMXDataChangedListener; import de.ctdo.control.BroadcastListener;
/** /**
* Ein DMXControllerWebSocket gehoert immer zu einem Browserfenster/Tab * Ein DMXControllerWebSocket gehoert immer zu einem Browserfenster/Tab
* @author lucas * @author lucas
* *
*/ */
public class BuntiControllerWebSocket extends DefaultWebSocket implements DMXDataChangedListener { public class BuntiControllerWebSocket extends DefaultWebSocket implements BroadcastListener {
Logger logger = LoggerFactory.getLogger(BuntiControllerWebSocket.class); Logger logger = LoggerFactory.getLogger(BuntiControllerWebSocket.class);
@ -26,14 +23,21 @@ public class BuntiControllerWebSocket extends DefaultWebSocket implements DMXDat
} }
@Override @Override
public void DMXDataChanged(int[] dmx512data) { public void Broadcast(String message) {
// TODO Auto-generated method stub
JSONArray arr = JSONArray.fromObject(dmx512data);
JSONObject obj = new JSONObject();
obj.put("dmx512values", arr);
send(obj.toString());
} }
// @Override
// public void DMXDataChanged(int[] dmx512data) {
//
// JSONArray arr = JSONArray.fromObject(dmx512data);
// JSONObject obj = new JSONObject();
// obj.put("dmx512values", arr);
//
// send(obj.toString());
// }
} }

View File

@ -0,0 +1,96 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>WebSockets</title>
<link type="text/css" href="css/ui-lightness/jquery-ui-1.8.16.custom.css" rel="stylesheet" />
<link type="text/css" href="css/screen.css" rel="stylesheet" />
<script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js"></script>
<script type="text/javascript" src="app.js"></script>
<script type="text/javascript">
var ws;
var dmxData;
$(document).ready(
function() {
var Socket = "MozWebSocket" in window ? MozWebSocket : WebSocket;
ws = new Socket("ws://" + window.location.hostname + ":" + window.location.port + "/bunti_server/bunti");
ws.onmessage = function (message) {
//$("#messages").append("<p>" + message.data + "</p>");
var obj = jQuery.parseJSON(message.data);
if( obj.dmx512values != null) {
dmxData = obj.dmx512values;
// das direkt zu machen ist evtl etwas unklug, da das sliden des sliders im
// gleichen browser dann hier zu ner aenderung fuehrt und der slider dann
// ruckelt. Aber es tut :D
$("#slider1").slider("value", dmxData[1]);
$("#slider2").slider("value", dmxData[2]);
$("#slider3").slider("value", dmxData[3]);
}
};
ws.onopen = function () {
$("#messages").append("<p>WS opened</p>");
};
ws.onclose = function () {
$("#messages").append("<p>WS closed</p>");
};
});
</script>
</head>
<body>
<h1>Hello World!</h1>
<script>
$(function() {
$("#slider1").slider({ min: 0, max: 255, slide: function(event, ui) {
ws.send("channel:2=" + ui.value);
ws.send("channel:7=" + ui.value);
ws.send("channel:12=" + ui.value);
ws.send("channel:17=" + ui.value);
} });
});
$(function() {
$("#slider2").slider({ min: 0, max: 255, slide: function(event, ui) {
ws.send("channel:3=" + ui.value);
ws.send("channel:8=" + ui.value);
ws.send("channel:13=" + ui.value);
ws.send("channel:18=" + ui.value);
} });
});
$(function() {
$("#slider3").slider({ min: 0, max: 255, slide: function(event, ui) {
ws.send("channel:4=" + ui.value);
ws.send("channel:9=" + ui.value);
ws.send("channel:14=" + ui.value);
ws.send("channel:19=" + ui.value);
} });
});
</script>
<div class="demo">
<div id="slider1" style="width: 300px"></div>
<div id="slider2" style="width: 300px; margin-top: 10px"></div>
<div id="slider3" style="width: 300px; margin-top: 10px"></div>
</div>
<div id="messages"></div>
<input type="button" onclick="javascript:alert(dmx512values);" value="Bla" />
</body>
</html>

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="de.ctdo" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
</beans>

View File

@ -1,31 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<display-name>bunti_server</display-name> xmlns="http://java.sun.com/xml/ns/javaee"
<welcome-file-list> xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
<welcome-file>index.html</welcome-file> xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
<welcome-file>index.htm</welcome-file> id="WebApp_ID" version="3.0">
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<description>CTDO bunti control Server</description> <display-name>bunti_server</display-name>
<!-- <welcome-file-list>
<servlet> <welcome-file>index.jsp</welcome-file>
<description></description> </welcome-file-list>
<servlet-name>BuntiControllerServlet</servlet-name>
<servlet-class>de.ctdo.websocket.BuntiControllerServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping> <description>CTDO bunti control Server</description>
<servlet-name>BuntiControllerServlet</servlet-name>
<url-pattern>/bunti</url-pattern> <servlet>
</servlet-mapping> <servlet-name>dispatcher</servlet-name>
--> <servlet-class>
<session-config> org.springframework.web.servlet.DispatcherServlet
<session-timeout>25</session-timeout> </servlet-class>
</session-config> <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>25</session-timeout>
</session-config>
</web-app> </web-app>

View File

@ -0,0 +1,13 @@
<html>
<head><title>Hello :: Spring Application</title></head>
<body>
<h1>Hello - Spring Application</h1>
<p>Greetings.</p>
</body>
</html>

View File

@ -0,0 +1,13 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Bla</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>

View File

@ -1,97 +1 @@
<%@page contentType="text/html" pageEncoding="UTF-8"%> <% response.sendRedirect("index.html"); %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>WebSockets</title>
<link type="text/css" href="css/ui-lightness/jquery-ui-1.8.16.custom.css" rel="stylesheet" />
<link type="text/css" href="css/screen.css" rel="stylesheet" />
<script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js"></script>
<script type="text/javascript" src="app.js"></script>
<script type="text/javascript">
var ws;
var dmxData;
$(document).ready(
function() {
var Socket = "MozWebSocket" in window ? MozWebSocket : WebSocket;
ws = new Socket("ws://" + window.location.hostname + ":" + window.location.port + "/bunti_server/bunti");
ws.onmessage = function (message) {
//$("#messages").append("<p>" + message.data + "</p>");
var obj = jQuery.parseJSON(message.data);
if( obj.dmx512values != null) {
dmxData = obj.dmx512values;
// das direkt zu machen ist evtl etwas unklug, da das sliden des sliders im
// gleichen browser dann hier zu ner aenderung fuehrt und der slider dann
// ruckelt. Aber es tut :D
$("#slider1").slider("value", dmxData[1]);
$("#slider2").slider("value", dmxData[2]);
$("#slider3").slider("value", dmxData[3]);
}
};
ws.onopen = function () {
$("#messages").append("<p>WS opened</p>");
};
ws.onclose = function () {
$("#messages").append("<p>WS closed</p>");
};
});
</script>
</head>
<body>
<h1>Hello World!</h1>
<script>
$(function() {
$("#slider1").slider({ min: 0, max: 255, slide: function(event, ui) {
ws.send("channel:2=" + ui.value);
ws.send("channel:7=" + ui.value);
ws.send("channel:12=" + ui.value);
ws.send("channel:17=" + ui.value);
} });
});
$(function() {
$("#slider2").slider({ min: 0, max: 255, slide: function(event, ui) {
ws.send("channel:3=" + ui.value);
ws.send("channel:8=" + ui.value);
ws.send("channel:13=" + ui.value);
ws.send("channel:18=" + ui.value);
} });
});
$(function() {
$("#slider3").slider({ min: 0, max: 255, slide: function(event, ui) {
ws.send("channel:4=" + ui.value);
ws.send("channel:9=" + ui.value);
ws.send("channel:14=" + ui.value);
ws.send("channel:19=" + ui.value);
} });
});
</script>
<div class="demo">
<div id="slider1" style="width: 300px"></div>
<div id="slider2" style="width: 300px; margin-top: 10px"></div>
<div id="slider3" style="width: 300px; margin-top: 10px"></div>
</div>
<div id="messages"></div>
<input type="button" onclick="javascript:alert(dmx512values);" value="Bla" />
</body>
</html>