Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2bea611
Showing
2 changed files
with
136 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |