From a1feb25187f937188248d2bd656f3fb8b48fe3fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=C3=9F?= Date: Sat, 10 Mar 2012 21:38:05 +0100 Subject: [PATCH] working on websocket --- pom.xml | 30 ++++++++- .../java/de/ctdo/bunti/dmx/DMXMixerImpl.java | 9 ++- .../AtmosphereResourceArgumentResolver.java | 30 +++++++++ .../ctdo/bunti/websocket/AtmosphereUtils.java | 24 +++++++ .../websocket/BuntiControllerApplication.java | 51 --------------- .../websocket/BuntiControllerServlet.java | 40 ------------ .../websocket/BuntiControllerWebSocket.java | 36 ----------- .../bunti/websocket/WebSocketController.java | 57 +++++++++++++++++ src/main/resources/applicationContext.xml | 27 -------- src/main/resources/log4j.xml | 6 +- .../servlet-context.xml} | 4 +- src/main/webapp/WEB-INF/web.xml | 63 +++++++++++++++---- .../de/ctdo/bunti/dmx/DMXMixerImplTest.java | 49 ++++++++++++--- 13 files changed, 243 insertions(+), 183 deletions(-) create mode 100644 src/main/java/de/ctdo/bunti/websocket/AtmosphereResourceArgumentResolver.java create mode 100644 src/main/java/de/ctdo/bunti/websocket/AtmosphereUtils.java delete mode 100644 src/main/java/de/ctdo/bunti/websocket/BuntiControllerApplication.java delete mode 100644 src/main/java/de/ctdo/bunti/websocket/BuntiControllerServlet.java delete mode 100644 src/main/java/de/ctdo/bunti/websocket/BuntiControllerWebSocket.java create mode 100644 src/main/java/de/ctdo/bunti/websocket/WebSocketController.java delete mode 100644 src/main/resources/applicationContext.xml rename src/main/webapp/WEB-INF/{dispatcher-servlet.xml => spring/servlet-context.xml} (91%) diff --git a/pom.xml b/pom.xml index 5be7ad3..c1f5ffc 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,8 @@ 3.0.5.RELEASE - 7.2.0.v20101020 + + 8.1.0.RC4 1.6.4 @@ -19,6 +20,11 @@ https://repository.jboss.org/nexus/content/repositories/releases false + + org.eclipse.repository.development + Jetty Repo + http://oss.sonatype.org/content/groups/jetty/ + @@ -111,6 +117,28 @@ 1.2 + + org.atmosphere + atmosphere-runtime + 0.7.2 + compile + + + + + + + + + + + org.aspectj + aspectjrt + 1.6.9 + compile + + + junit junit diff --git a/src/main/java/de/ctdo/bunti/dmx/DMXMixerImpl.java b/src/main/java/de/ctdo/bunti/dmx/DMXMixerImpl.java index 5a6cf67..0241d5f 100644 --- a/src/main/java/de/ctdo/bunti/dmx/DMXMixerImpl.java +++ b/src/main/java/de/ctdo/bunti/dmx/DMXMixerImpl.java @@ -9,7 +9,6 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationListener; import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.stereotype.Component; import java.util.Collections; import java.util.HashMap; @@ -27,14 +26,13 @@ public class DMXMixerImpl implements DMXMixer, ApplicationListener options) { + if(device == null || options == null || options.size() == 0) { + return false; + } + BuntiDMXDevice dmxDev = (BuntiDMXDevice) device; if (dmxDev.setValuesFromOptions(options)) { diff --git a/src/main/java/de/ctdo/bunti/websocket/AtmosphereResourceArgumentResolver.java b/src/main/java/de/ctdo/bunti/websocket/AtmosphereResourceArgumentResolver.java new file mode 100644 index 0000000..fe14b33 --- /dev/null +++ b/src/main/java/de/ctdo/bunti/websocket/AtmosphereResourceArgumentResolver.java @@ -0,0 +1,30 @@ +/** + * + */ +package de.ctdo.bunti.websocket; + +import javax.servlet.http.HttpServletRequest; + +import org.atmosphere.cpr.AtmosphereResource; +import org.springframework.core.MethodParameter; +import org.springframework.web.bind.support.WebArgumentResolver; +import org.springframework.web.context.request.NativeWebRequest; + +public class AtmosphereResourceArgumentResolver implements WebArgumentResolver { + + + /* (non-Javadoc) + * @see org.springframework.web.bind.support.WebArgumentResolver#resolveArgument(org.springframework.core.MethodParameter, org.springframework.web.context.request.NativeWebRequest) + */ + @Override + public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) throws Exception { + + if (AtmosphereResource.class.isAssignableFrom(methodParameter.getParameterType())) { + return AtmosphereUtils.getAtmosphereResource(webRequest.getNativeRequest(HttpServletRequest.class)); + } else { + return WebArgumentResolver.UNRESOLVED; + } + + } + +} diff --git a/src/main/java/de/ctdo/bunti/websocket/AtmosphereUtils.java b/src/main/java/de/ctdo/bunti/websocket/AtmosphereUtils.java new file mode 100644 index 0000000..b3f56f1 --- /dev/null +++ b/src/main/java/de/ctdo/bunti/websocket/AtmosphereUtils.java @@ -0,0 +1,24 @@ +package de.ctdo.bunti.websocket; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.atmosphere.cpr.AtmosphereResource; +import org.atmosphere.cpr.AtmosphereServlet; +import org.springframework.util.Assert; + +public final class AtmosphereUtils { + + + public static AtmosphereResource getAtmosphereResource( + HttpServletRequest request) { + + AtmosphereResource resource = + (AtmosphereResource) request.getAttribute(AtmosphereServlet.ATMOSPHERE_RESOURCE); + + Assert.notNull(resource,"AtmosphereResource could not be located for the request. Check that AtmosphereServlet is configured correctly in web.xml"); + + return resource; + } + +} diff --git a/src/main/java/de/ctdo/bunti/websocket/BuntiControllerApplication.java b/src/main/java/de/ctdo/bunti/websocket/BuntiControllerApplication.java deleted file mode 100644 index bd5d1c6..0000000 --- a/src/main/java/de/ctdo/bunti/websocket/BuntiControllerApplication.java +++ /dev/null @@ -1,51 +0,0 @@ -package de.ctdo.bunti.websocket; - -//import org.slf4j.Logger; -//import org.slf4j.LoggerFactory; -//import org.springframework.stereotype.Component; -// -//import com.sun.grizzly.tcp.Request; -//import com.sun.grizzly.websockets.ProtocolHandler; -//import com.sun.grizzly.websockets.WebSocket; -//import com.sun.grizzly.websockets.WebSocketApplication; -//import com.sun.grizzly.websockets.WebSocketListener; -// -//@Component -//public class BuntiControllerApplication extends WebSocketApplication { -// private static final Logger LOGGER = LoggerFactory.getLogger(BuntiControllerApplication.class); -// -// @Override -// public final WebSocket createWebSocket(ProtocolHandler protocolHandler, WebSocketListener... listeners) { -// BuntiControllerWebSocket socket = new BuntiControllerWebSocket(protocolHandler, listeners); -//// BuntiControllerImpl.getInstance().addListener(socket); -// return socket; -// } -// -// @Override -// public final boolean isApplicationRequest(Request request) { -// final String uri = request.requestURI().toString(); -// return uri.endsWith("/bunti"); -// } -// -// @Override -// public final void onClose(WebSocket socket, com.sun.grizzly.websockets.DataFrame frame) { -// BuntiControllerWebSocket ws = (BuntiControllerWebSocket) socket; -//// BuntiControllerImpl.getInstance().removeListener(ws); -// } -// -// @Override -// public void onMessage(WebSocket socket, String text) { -// -// -//// BuntiControllerImpl.getInstance().performJSONString(text); -// -// -//// for (final WebSocket webSocket : getWebSockets()) { -//// DMXControllerWebSocket ws = (DMXControllerWebSocket) webSocket; -//// -//// -//// } -// //super.onMessage(socket, text); -// } -// -//} diff --git a/src/main/java/de/ctdo/bunti/websocket/BuntiControllerServlet.java b/src/main/java/de/ctdo/bunti/websocket/BuntiControllerServlet.java deleted file mode 100644 index f21884d..0000000 --- a/src/main/java/de/ctdo/bunti/websocket/BuntiControllerServlet.java +++ /dev/null @@ -1,40 +0,0 @@ -package de.ctdo.bunti.websocket; - -//import org.slf4j.Logger; -//import org.slf4j.LoggerFactory; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.stereotype.Component; -// -//// das klappt so jedenfalls alles noch nicht :) -//@Component -//public class BuntiControllerServlet { // extends HttpServlet { -// private static final long serialVersionUID = 1L; -// private static final Logger LOGGER = LoggerFactory.getLogger(BuntiControllerServlet.class); -// -// private BuntiControllerApplication app; -// -// @Autowired -// public final void setApp(BuntiControllerApplication app) { -// this.app = app; -// } -// -// -// -// public void buntiControllerApplication() { -//// WebSocketEngine.getEngine().register(app); -//// LOGGER.debug("registered buntiControllerApplication"); -// } -// -//// @Override -//// public void init(ServletConfig config) throws ServletException { -//// WebSocketEngine.getEngine().register(app); -//// LOGGER.debug("registered buntiControllerApplication"); -//// } -//// -//// @Override -//// public void destroy() { -//// WebSocketEngine.getEngine().unregister(app); -//// LOGGER.debug("unregistered buntiControllerApplication"); -//// } -// -//} diff --git a/src/main/java/de/ctdo/bunti/websocket/BuntiControllerWebSocket.java b/src/main/java/de/ctdo/bunti/websocket/BuntiControllerWebSocket.java deleted file mode 100644 index 6e29966..0000000 --- a/src/main/java/de/ctdo/bunti/websocket/BuntiControllerWebSocket.java +++ /dev/null @@ -1,36 +0,0 @@ -package de.ctdo.bunti.websocket; - -//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; -// -///** -// * Ein DMXControllerWebSocket gehoert immer zu einem Browserfenster/Tab -// * @author lucas -// * -// */ -//public class BuntiControllerWebSocket extends DefaultWebSocket { -// -// private static final Logger LOGGER = LoggerFactory.getLogger(BuntiControllerWebSocket.class); -// -// public BuntiControllerWebSocket(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/src/main/java/de/ctdo/bunti/websocket/WebSocketController.java b/src/main/java/de/ctdo/bunti/websocket/WebSocketController.java new file mode 100644 index 0000000..30c51df --- /dev/null +++ b/src/main/java/de/ctdo/bunti/websocket/WebSocketController.java @@ -0,0 +1,57 @@ +package de.ctdo.bunti.websocket; + +import java.util.concurrent.Callable; +import java.util.concurrent.TimeUnit; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.atmosphere.cpr.AtmosphereResource; +import org.atmosphere.cpr.Broadcaster; +import org.codehaus.jackson.map.ObjectMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class WebSocketController { + private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketController.class); + + + @RequestMapping(value="/buntisocket", method=RequestMethod.GET) + @ResponseBody + public void websockets(final AtmosphereResource event) { + + + + final HttpServletRequest req = event.getRequest(); +// final HttpServletResponse res = event.getResponse(); + + LOGGER.debug("call to websockets " + req.toString()); + + final ObjectMapper mapper = new ObjectMapper(); + + event.suspend(); + + final Broadcaster bc = event.getBroadcaster(); + + bc.scheduleFixedBroadcast(new Callable() { + + private long sinceId = 0; + + @Override + public String call() throws Exception { + LOGGER.debug("call was called"); + + return mapper.writeValueAsString("blafaselblubb"); + } + + }, 10, TimeUnit.SECONDS); + + } + +} + diff --git a/src/main/resources/applicationContext.xml b/src/main/resources/applicationContext.xml deleted file mode 100644 index fc3158c..0000000 --- a/src/main/resources/applicationContext.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/main/resources/log4j.xml b/src/main/resources/log4j.xml index 74722c4..59a29c4 100644 --- a/src/main/resources/log4j.xml +++ b/src/main/resources/log4j.xml @@ -19,7 +19,7 @@ - + @@ -32,6 +32,10 @@ + + + + diff --git a/src/main/webapp/WEB-INF/dispatcher-servlet.xml b/src/main/webapp/WEB-INF/spring/servlet-context.xml similarity index 91% rename from src/main/webapp/WEB-INF/dispatcher-servlet.xml rename to src/main/webapp/WEB-INF/spring/servlet-context.xml index f9f7bd5..1c798b6 100644 --- a/src/main/webapp/WEB-INF/dispatcher-servlet.xml +++ b/src/main/webapp/WEB-INF/spring/servlet-context.xml @@ -27,11 +27,11 @@ - + - + diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 7169c5c..6ee0520 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -29,21 +29,60 @@ /* - - dispatcher - - org.springframework.web.servlet.DispatcherServlet - - 1 - + + + + + + + - - dispatcher - / - - 25 + + + + appServlet + org.atmosphere.cpr.MeteorServlet + + org.atmosphere.servlet + org.springframework.web.servlet.DispatcherServlet + + + org.atmosphere.cpr.broadcasterClass + org.atmosphere.cpr.DefaultBroadcaster + + + org.atmosphere.cpr.broadcastFilterClasses + org.atmosphere.client.JavascriptClientFilter + + + org.atmosphere.cpr.CometSupport.maxInactiveActivity + 30 + + + org.atmosphere.useStream + true + + + org.atmosphere.useWebSocket + true + + + org.atmosphere.useNative + true + + + contextConfigLocation + /WEB-INF/spring/servlet-context.xml + + 1 + + + + appServlet + / + \ No newline at end of file diff --git a/src/test/java/de/ctdo/bunti/dmx/DMXMixerImplTest.java b/src/test/java/de/ctdo/bunti/dmx/DMXMixerImplTest.java index 3d4aeb7..50bc1ef 100644 --- a/src/test/java/de/ctdo/bunti/dmx/DMXMixerImplTest.java +++ b/src/test/java/de/ctdo/bunti/dmx/DMXMixerImplTest.java @@ -1,8 +1,13 @@ package de.ctdo.bunti.dmx; +import de.ctdo.bunti.model.BuntiDevice; +import de.ctdo.bunti.model.Par56Spot; import org.junit.Before; import org.junit.Test; +import java.util.HashMap; +import java.util.Map; + import static org.junit.Assert.*; public class DMXMixerImplTest { @@ -20,22 +25,49 @@ public class DMXMixerImplTest { @Test public void testSetArtNetDeviceAddress() throws Exception { - + dut.setArtNetDeviceAddress("172.0.0.1"); } - @Test - public void testInitDMXData() throws Exception { - } - - @Test + @Test(expected = NullPointerException.class) public void testSendOutDMXBuffer() throws Exception { - + dut.sendOutDMXBuffer(); } @Test public void testUpdateDevice() throws Exception { + BuntiDevice device = new Par56Spot(23,42,"deviceName"); + Map options = new HashMap(); + options.put("red", 44); + assertTrue(dut.updateDevice(device, options)); + } + @Test + public void testUpdateDeviceWrong1() throws Exception { + BuntiDevice device = new Par56Spot(23,42,"deviceName"); + assertFalse(dut.updateDevice(device, null)); + } + + @Test + public void testUpdateDeviceWrong2() throws Exception { + Map options = new HashMap(); + options.put("red", 44); + assertFalse(dut.updateDevice(null, options)); + } + + @Test + public void testUpdateDeviceWrong3() throws Exception { + BuntiDevice device = new Par56Spot(23,42,"deviceName"); + Map options = new HashMap(); + assertFalse(dut.updateDevice(device, options)); + } + + @Test + public void testUpdateDeviceWrong4() throws Exception { + BuntiDevice device = new Par56Spot(23,42,"deviceName"); + Map options = new HashMap(); + options.put("rednonexistent", 44); + assertFalse(dut.updateDevice(device, options)); } @Test @@ -73,9 +105,6 @@ public class DMXMixerImplTest { assertFalse(dut.setDMX512Channel(-1, -10)); } - - - @Test public void testOnApplicationEvent() throws Exception {