Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
infrastructure changes for multi-user support
  • Loading branch information
pmuellr committed Apr 28, 2011
1 parent 3f28244 commit 55ab5927558e98765775c436aedbf0e2df39c39e
Showing 19 changed files with 198 additions and 125 deletions.
@@ -22,8 +22,11 @@
//-------------------------------------------------------------------
public class Channel {

static public final String AnonymousId = "anonymous";

private String pathPrefix;
private String name;
private String id;
private MessageQueue<String> requestQueue;
private MessageQueue<String> responseQueue;
private boolean isClosed;
@@ -34,9 +37,10 @@ public class Channel {
private String remoteAddress;

//---------------------------------------------------------------
public Channel(String pathPrefix, String name, String remoteHost, String remoteAddress) {
public Channel(String pathPrefix, String name, String id, String remoteHost, String remoteAddress) {
this.pathPrefix = pathPrefix;
this.name = name;
this.id = id;
this.remoteHost = remoteHost;
this.remoteAddress = remoteAddress;
this.requestQueue = new MessageQueue<String>();
@@ -162,6 +166,11 @@ public String getName() {
return name;
}

//---------------------------------------------------------------
public String getId() {
return id;
}

//---------------------------------------------------------------
public long getLastRead() {
return lastRead;
@@ -73,10 +73,10 @@ public List<Channel> getChannels() {
}

//---------------------------------------------------------------
public Channel registerChannel(String pathPrefix, String channelName, String remoteHost, String remoteAddress) {
public Channel registerChannel(String pathPrefix, String channelName, String id, String remoteHost, String remoteAddress) {
if (channelMap.containsKey(channelName)) return null;

Channel channel = new Channel(pathPrefix, channelName, remoteHost, remoteAddress);
Channel channel = new Channel(pathPrefix, channelName, id, remoteHost, remoteAddress);
channelMap.put(channelName, channel);

fireRegisteredEvent(channel);
@@ -65,7 +65,8 @@ public JSONObject getDescription() {
JSONObject result = new JSONObject();

try {
result.put("id", getChannel().getName());
result.put("channel", getChannel().getName());
result.put("id", getChannel().getId());
result.put("hostName", getChannel().getRemoteHost());
result.put("connectedTargetIds", new JSONArray());
}
@@ -55,51 +55,59 @@ public void channelDeregistered(Channel channel) {
//---------------------------------------------------------------
public void addClient(Client client) {
clientMap.put(client.getName(), client);
_sendAllClientsEvent("WeinreClientEvents", "clientRegistered", client.getDescription());
_sendAllClientsEvent("WeinreClientEvents", client.getId(), "clientRegistered", client.getDescription());
}

//---------------------------------------------------------------
public void addTarget(Target target) {
targetMap.put(target.getName(), target);
_sendAllClientsEvent("WeinreClientEvents", "targetRegistered", target.getDescription());
_sendAllClientsEvent("WeinreClientEvents", target.getId(), "targetRegistered", target.getDescription());
}

//---------------------------------------------------------------
private void _removeClient(Client client) {
_sendAllClientsEvent("WeinreClientEvents", "clientUnregistered", client.getName());
_sendAllClientsEvent("WeinreClientEvents", client.getId(), "clientUnregistered", client.getName());
clientMap.remove(client.getName());
}

//---------------------------------------------------------------
private void _removeTarget(Target target) {
_sendAllClientsEvent("WeinreClientEvents", "targetUnregistered", target.getName());
_sendAllClientsEvent("WeinreClientEvents", target.getId(), "targetUnregistered", target.getName());
targetMap.remove(target.getName());
}

//---------------------------------------------------------------
public Client getClient(String id) {
return clientMap.get(id);
public Client getClient(String name) {
return clientMap.get(name);
}

//---------------------------------------------------------------
public Target getTarget(String id) {
return targetMap.get(id);
public Target getTarget(String name) {
return targetMap.get(name);
}

//---------------------------------------------------------------
public List<Client> getClients() {
public List<Client> getClients(String id) {
List<Client> result = new ArrayList<Client>();

result.addAll(clientMap.values());
for (Client client: clientMap.values()) {
if (client.getId().equals(id)) {
result.add(client);
}
}

return result;
}

//---------------------------------------------------------------
public List<Target> getTargets() {
public List<Target> getTargets(String id) {
List<Target> result = new ArrayList<Target>();

result.addAll(targetMap.values());
for (Target target: targetMap.values()) {
if (target.getId().equals(id)) {
result.add(target);
}
}

return result;
}
@@ -144,13 +152,13 @@ private void _sendConnectionEvent(Client client, Target target, String message)
String clientName = client.getChannel().getName();
String targetName = target.getChannel().getName();

_sendAllClientsEvent("WeinreClientEvents", message, clientName, targetName);
_sendAllClientsEvent("WeinreClientEvents", client.getId(), message, clientName, targetName);
target.getChannel().sendEvent("WeinreTargetEvents", message, clientName, targetName);
}

//---------------------------------------------------------------
private void _sendAllClientsEvent(String intfName, String message, Object... args) {
for (Client aClient: getClients()) {
private void _sendAllClientsEvent(String intfName, String id, String message, Object... args) {
for (Client aClient: getClients(id)) {
aClient.getChannel().sendEvent(intfName, message, args);
}
}
@@ -45,6 +45,11 @@ public String getName() {
return channel.getName();
}

//---------------------------------------------------------------
public String getId() {
return channel.getId();
}

//---------------------------------------------------------------
public List<Connector> getConnections() {
List<Connector> result = new ArrayList<Connector>();
@@ -93,7 +93,8 @@ public JSONObject getDescription() {
JSONObject result = new JSONObject();

try {
result.put("id", getChannel().getName());
result.put("channel", getChannel().getName());
result.put("id", getChannel().getId());
result.put("hostName", getChannel().getRemoteHost());
result.put("url", this.url);
result.put("connectedClientIds", new JSONArray());
@@ -13,13 +13,15 @@
import java.io.Reader;
import java.util.List;

import javax.management.RuntimeErrorException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.wink.json4j.JSONArray;
import org.apache.wink.json4j.JSONException;
import org.apache.wink.json4j.JSONObject;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.AbstractHandler;

@@ -138,14 +140,54 @@ public void handleOptions(String target, Request baseRequest, HttpServletRequest
//---------------------------------------------------------------
public void handleCreate(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String channelName = "" + Utility.getNextSequenceNumber();
String id;

if (0 == baseRequest.getContentLength()) {
id = Channel.AnonymousId;
}

ChannelManager.$.registerChannel(pathPrefix, channelName, request.getRemoteHost(), request.getRemoteAddr());
else {
try {
String json = readRequestBody(request.getInputStream());
JSONObject obj = new JSONObject(json);
if (!obj.has("id")) {
id = Channel.AnonymousId;
}
else {
id = obj.getString("id");
if (id.isEmpty()) {
id = Channel.AnonymousId;
}
}
}
catch (IOException e) {
response.setStatus(400);
return;
}
catch (JSONException e) {
response.setStatus(400);
return;
}
}

ChannelManager.$.registerChannel(pathPrefix, channelName, id, request.getRemoteHost(), request.getRemoteAddr());

response.setStatus(200);
response.setContentType("application/json");

ServletOutputStream oStream = response.getOutputStream();
oStream.print("{\"channel\": " + channelName + "}");
JSONObject obj = new JSONObject();

try {
obj.put("channel", channelName);
obj.put("id", id);
}
catch (JSONException e) {
throw new RuntimeException(e);
}

String result = obj.toString();
oStream.print(result);
oStream.close();
}

@@ -11,6 +11,8 @@
import java.util.List;

import org.apache.wink.json4j.JSONArray;
import org.apache.wink.json4j.JSONException;
import org.apache.wink.json4j.JSONObject;

import weinre.server.Channel;
import weinre.server.Client;
@@ -25,13 +27,15 @@ public class WeinreClientCommands {
public void registerClient(Channel channel, String callbackId) throws IOException {
Client client = new Client(channel);

channel.sendCallback("WeinreClientEvents", callbackId, client.getName());
JSONObject description = client.getDescription();

channel.sendCallback("WeinreClientEvents", callbackId, description);
channel.sendEvent("WeinreClientEvents", "serverProperties", Main.getSettings().asProperties());
}

//---------------------------------------------------------------
public void getTargets(Channel channel, String callbackId) throws IOException {
List<Target> targets = ConnectionManager.$.getTargets();
List<Target> targets = ConnectionManager.$.getTargets(channel.getId());
JSONArray targetResults = new JSONArray();

for (Target target: targets) {
@@ -43,7 +47,7 @@ public void getTargets(Channel channel, String callbackId) throws IOException {

//---------------------------------------------------------------
public void getClients(Channel channel, String callbackId) throws IOException {
List<Client> clients = ConnectionManager.$.getClients();
List<Client> clients = ConnectionManager.$.getClients(channel.getId());
JSONArray clientResults = new JSONArray();

for (Client client: clients) {
@@ -10,6 +10,9 @@
import java.io.IOException;
import java.util.List;

import org.apache.wink.json4j.JSONException;
import org.apache.wink.json4j.JSONObject;

import weinre.server.Channel;
import weinre.server.ChannelManager;
import weinre.server.Main;
@@ -22,7 +25,9 @@ public class WeinreTargetCommands {
public void registerTarget(Channel channel, String url, String callbackId) throws IOException {
Target target = new Target(channel, url);

channel.sendCallback("WeinreTargetEvents", callbackId, target.getName());
JSONObject description = target.getDescription();

channel.sendCallback("WeinreTargetEvents", callbackId, description);
}

//---------------------------------------------------------------
@@ -11,5 +11,6 @@
<listEntry value="org.eclipse.debug.ui.launchGroup.run"/>
</listAttribute>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="weinre.server.Main"/>
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="--verbose true --deathTimeout 120"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="weinre.server"/>
</launchConfiguration>
@@ -90,6 +90,8 @@ method _installRemotePanel
//-----------------------------------------------------------------------------
method onLoaded

Weinre.WeinreClientCommands.registerClient(Binding(this, this.cb_registerClient))

this._installRemotePanel()

var messageDispatcher = Weinre.messageDispatcher
@@ -99,16 +101,12 @@ method onLoaded
messageDispatcher.registerInterface("WeinreClientEvents", new WeinreClientEventsImpl(this), false)
messageDispatcher.registerInterface("InspectorFrontendHost", InspectorFrontendHost, false)

Weinre.WeinreClientCommands.registerClient(Binding(this, this.cb_registerClient))

//-----------------------------------------------------------------------------
method cb_registerClient(clientId)
Weinre.clientId = clientId
Weinre.connectorId = clientId
Callback.setConnectorId(clientId)
method cb_registerClient(clientDescription)
Weinre.clientDescription = clientDescription

if (this.uiAvailable()) {
WebInspector.panels.remote.setCurrentClient(clientId)
WebInspector.panels.remote.setCurrentClient(clientDescription.channel)
WebInspector.panels.remote.afterInitialConnection()
}

0 comments on commit 55ab592

Please sign in to comment.