Permalink
Browse files

added live server and console

  • Loading branch information...
also committed Apr 13, 2009
0 parents commit 2bea6114e8978247079f60edfc799fce0a28ee1f
Showing with 136 additions and 0 deletions.
  1. +39 −0 pom.xml
  2. +97 −0 src/main/java/com/ryanberdeen/oneshow/LiveServer.java
39 pom.xml
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<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/maven-v4_0_0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>com.ryanberdeen.oneshow</groupId>
+ <artifactId>live-server</artifactId>
+ <name>oneshow live server</name>
+ <version>0.1-SNAPSHOT</version>
+
+ <build>
+ <pluginManagement>
+ <plugins>
+ <plugin>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <source>1.5</source>
+ <target>1.5</target>
+ </configuration>
+ </plugin>
+ </plugins>
+ </pluginManagement>
+ </build>
+
+ <dependencies>
+ <dependency>
+ <groupId>com.ryanberdeen.rjs</groupId>
+ <artifactId>rjs-server</artifactId>
+ <version>0.1</version>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-nop</artifactId>
+ <version>1.5.2</version>
+ <scope>runtime</scope>
+ </dependency>
+ </dependencies>
+</project>
@@ -0,0 +1,97 @@
+package com.ryanberdeen.oneshow;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+
+import com.ryanberdeen.rjs.server.RjsClient;
+import com.ryanberdeen.rjs.server.RjsHandlerAdapter;
+import com.ryanberdeen.rjs.server.RjsServer;
+
+public class LiveServer extends RjsHandlerAdapter {
+ private static final String CLIENT_PATHS_KEY = "paths";
+ private static final String SERVER_PATH = "server";
+
+ private HashMap<String, HashSet<RjsClient>> listeners;
+ private HashSet<RjsClient> globalListeners;
+
+ public LiveServer() {
+ listeners = new HashMap<String, HashSet<RjsClient>>();
+ globalListeners = new HashSet<RjsClient>();
+ }
+
+ @Override
+ public void clientConnected(RjsClient client) throws Exception {
+ client.setAttribute(CLIENT_PATHS_KEY, new HashSet<String>());
+ }
+
+ @SuppressWarnings("unchecked")
+ private static HashSet<String> getClientPaths(RjsClient client) {
+ return (HashSet<String>) client.getAttribute(CLIENT_PATHS_KEY);
+ }
+
+ private void subscribe(String path, RjsClient client) {
+ HashSet<RjsClient> pathListeners;
+ synchronized (listeners) {
+ pathListeners = listeners.get(path);
+ if (pathListeners == null) {
+ pathListeners = new HashSet<RjsClient>();
+ listeners.put(path, pathListeners);
+ }
+ }
+ pathListeners.add(client);
+ HashSet<String> clientPaths = getClientPaths(client);
+ clientPaths.add(path);
+ }
+
+ @Override
+ public void clientDisconnected(RjsClient client) throws Exception {
+ for (String path : getClientPaths(client)) {
+ listeners.get(path).remove(client);
+ }
+ }
+
+ @Override
+ public void messageReceived(RjsClient client, String message) throws Exception {
+ int spaceIndex = message.indexOf(' ');
+ if (spaceIndex < 1) {
+ throw new RuntimeException("Invalid message: " + message);
+ }
+ String path = message.substring(0, spaceIndex);
+ String targetMessage = message.substring(spaceIndex + 1);
+ if (path.equals(SERVER_PATH)) {
+ serverMessageReceived(client, targetMessage);
+ }
+ HashSet<RjsClient> pathListeners = listeners.get(path);
+ if (pathListeners != null) {
+ sendMessage(pathListeners, targetMessage);
+ }
+ sendMessage(globalListeners, message);
+ }
+
+ private static void sendMessage(Collection<RjsClient> clients, String message) {
+ for (RjsClient client : clients) {
+ client.send(message);
+ }
+ }
+
+ private void serverMessageReceived(RjsClient client, String message) {
+ String[] parts = message.split(" ");
+ if ("subscribe".equals(parts[0])) {
+ if ("*".equals(parts[1])) {
+ globalListeners.add(client);
+ }
+ else {
+ subscribe(parts[1], client);
+ }
+ }
+ }
+
+ public static void main(String[] args) throws IOException {
+ RjsServer server = new RjsServer();
+ server.setPort(1843);
+ server.setHandler(new LiveServer());
+ server.start();
+ }
+}

0 comments on commit 2bea611

Please sign in to comment.