// Path: src/main/java/edu/stanford/junction/api/activity/JunctionActor.java
// public abstract class JunctionActor {
// 	protected Junction mJunction;
// 	private String actorID;
// 	private String[] mRoles;
// 	
// 	public String[] getRoles() {
// 		return mRoles;
// 	}
// 	
// 	public JunctionActor() {
// 		actorID = UUID.randomUUID().toString();
// 		mRoles = new String[]{};
// 	}
// 
// 	public JunctionActor(String role) {
// 		actorID = UUID.randomUUID().toString();
// 		mRoles = new String[]{role};
// 	}
// 	
// 	public JunctionActor(String[] roles) {
// 		actorID = UUID.randomUUID().toString();
// 		mRoles=roles;
// 	}
// 	
// 	public void onActivityJoin() {
// 		
// 	}
// 	
// 	public void setJunction(Junction j) {
// 		mJunction=j;
// 	}
// 	
// 	public Junction getJunction() {
// 		return mJunction;
// 	}
// 	
// 	public String getActorID() {
// 		return actorID;
// 	}
// 	
// 	public void onActivityStart() {
// 		
// 	}
// 	
// 	public final void leave() {
// 		Junction jx = getJunction();
// 		if (jx != null) {
// 			jx.disconnect();
// 			setJunction(null);
// 		}
// 	}
// 	
// 	public void onActivityCreate() {
// 		
// 	}
// 
// 	/**
// 	 * Send a message to an individual actor, identified by actorID
// 	 */
// 	public void sendMessageToActor(String actorID, JSONObject message) {
// 		mJunction.sendMessageToActor(actorID, message);
// 	}
// 
// 	/**
// 	 * Send a message for anyone in the Junction session.
// 	 */	
// 	public void sendMessageToSession(JSONObject message) {
// 		mJunction.sendMessageToSession(message);
// 	}
// 	
// 	/**
// 	 * Send a message to an actor claiming a certain role.
// 	 */
// 	public void sendMessageToRole(String role, JSONObject message) {
// 		mJunction.sendMessageToRole(role, message);
// 	}
// 	
// 	/**
// 	 * Asynchronously handle an inbound message.
// 	 */
// 	public abstract void onMessageReceived(MessageHeader header, JSONObject message);
// 	
// 	/**
// 	 * Returns a list of JunctionExtras that should be loaded
// 	 * when the actor joins an activity
// 	 * @return
// 	 */
// 	public List<JunctionExtra>getInitialExtras() {
// 		return new ArrayList<JunctionExtra>();
// 	}
// 	
// 	public void registerExtra(JunctionExtra extra) {
// 		mJunction.registerExtra(extra);
// 	}
// }
// 
// Path: src/main/java/edu/stanford/junction/api/messaging/MessageHeader.java
// public class MessageHeader {
// 	private Junction jx;
// 	private JSONObject message;
// 	public String from;
// 	
// 	public MessageHeader(Junction jx, JSONObject message, String from) {
// 		this.jx=jx;
// 		this.message=message;
// 		this.from=from;
// 	}
// 	
// 	
// 	public MessageTarget getReplyTarget() {
// 		if (message.has(Junction.NS_JX)) {
// 			JSONObject h = message.optJSONObject(Junction.NS_JX);
// 			if (h.has("replyTo")) {
// 				return MessageTargetFactory.getInstance(jx).getTarget(h.optString("replyTo"));
// 			}
// 		}
// 		
// 		return MessageTargetFactory.getInstance(jx).getTarget("actor:"+from);
// 	}
// 	
// 	public String getSender() {
// 		return from;
// 	}
// 	
// 	public Junction getJunction() { return jx; }
// }


import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
import edu.stanford.junction.api.activity.JunctionActor;
import edu.stanford.junction.api.messaging.MessageHeader;

/*
 * Copyright (C) 2010 Stanford University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package edu.stanford.junction.provider.jvm;




public class MultiJunction {
	private static Map<URI,MultiJunction>multiJunctions = 
		Collections.synchronizedMap(
			new HashMap<URI,MultiJunction>());
	
	Map<String,JunctionActor> actorMap;
	URI uri;
	
	public static MultiJunction get(URI uri, JunctionActor actor) {
		MultiJunction j;
		if (!multiJunctions.containsKey(uri)) {
			j = new MultiJunction(uri);
			multiJunctions.put(uri,j);
		} else {
			j = multiJunctions.get(uri);
		}
		j.registerActor(actor);
		return j;
	}
	
	private MultiJunction(URI uri) {
		actorMap = new HashMap<String, JunctionActor>();
		this.uri = uri;
	}
	
	private void registerActor(JunctionActor actor) {
		actorMap.put(actor.getActorID(),actor);
	}
	
	protected void sendMessageToActor(JunctionActor from, String actorID, JSONObject message) {
		if (actorMap.containsKey(actorID)) {
			actorMap.get(actorID).getJunction().triggerMessageReceived(

// Path: src/main/java/edu/stanford/junction/api/activity/JunctionActor.java
// public abstract class JunctionActor {
// 	protected Junction mJunction;
// 	private String actorID;
// 	private String[] mRoles;
// 	
// 	public String[] getRoles() {
// 		return mRoles;
// 	}
// 	
// 	public JunctionActor() {
// 		actorID = UUID.randomUUID().toString();
// 		mRoles = new String[]{};
// 	}
// 
// 	public JunctionActor(String role) {
// 		actorID = UUID.randomUUID().toString();
// 		mRoles = new String[]{role};
// 	}
// 	
// 	public JunctionActor(String[] roles) {
// 		actorID = UUID.randomUUID().toString();
// 		mRoles=roles;
// 	}
// 	
// 	public void onActivityJoin() {
// 		
// 	}
// 	
// 	public void setJunction(Junction j) {
// 		mJunction=j;
// 	}
// 	
// 	public Junction getJunction() {
// 		return mJunction;
// 	}
// 	
// 	public String getActorID() {
// 		return actorID;
// 	}
// 	
// 	public void onActivityStart() {
// 		
// 	}
// 	
// 	public final void leave() {
// 		Junction jx = getJunction();
// 		if (jx != null) {
// 			jx.disconnect();
// 			setJunction(null);
// 		}
// 	}
// 	
// 	public void onActivityCreate() {
// 		
// 	}
// 
// 	/**
// 	 * Send a message to an individual actor, identified by actorID
// 	 */
// 	public void sendMessageToActor(String actorID, JSONObject message) {
// 		mJunction.sendMessageToActor(actorID, message);
// 	}
// 
// 	/**
// 	 * Send a message for anyone in the Junction session.
// 	 */	
// 	public void sendMessageToSession(JSONObject message) {
// 		mJunction.sendMessageToSession(message);
// 	}
// 	
// 	/**
// 	 * Send a message to an actor claiming a certain role.
// 	 */
// 	public void sendMessageToRole(String role, JSONObject message) {
// 		mJunction.sendMessageToRole(role, message);
// 	}
// 	
// 	/**
// 	 * Asynchronously handle an inbound message.
// 	 */
// 	public abstract void onMessageReceived(MessageHeader header, JSONObject message);
// 	
// 	/**
// 	 * Returns a list of JunctionExtras that should be loaded
// 	 * when the actor joins an activity
// 	 * @return
// 	 */
// 	public List<JunctionExtra>getInitialExtras() {
// 		return new ArrayList<JunctionExtra>();
// 	}
// 	
// 	public void registerExtra(JunctionExtra extra) {
// 		mJunction.registerExtra(extra);
// 	}
// }
// 
// Path: src/main/java/edu/stanford/junction/api/messaging/MessageHeader.java
// public class MessageHeader {
// 	private Junction jx;
// 	private JSONObject message;
// 	public String from;
// 	
// 	public MessageHeader(Junction jx, JSONObject message, String from) {
// 		this.jx=jx;
// 		this.message=message;
// 		this.from=from;
// 	}
// 	
// 	
// 	public MessageTarget getReplyTarget() {
// 		if (message.has(Junction.NS_JX)) {
// 			JSONObject h = message.optJSONObject(Junction.NS_JX);
// 			if (h.has("replyTo")) {
// 				return MessageTargetFactory.getInstance(jx).getTarget(h.optString("replyTo"));
// 			}
// 		}
// 		
// 		return MessageTargetFactory.getInstance(jx).getTarget("actor:"+from);
// 	}
// 	
// 	public String getSender() {
// 		return from;
// 	}
// 	
// 	public Junction getJunction() { return jx; }
// }


// Path: src/main/java/edu/stanford/junction/provider/jvm/MultiJunction.java
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
import edu.stanford.junction.api.activity.JunctionActor;
import edu.stanford.junction.api.messaging.MessageHeader;

/*
 * Copyright (C) 2010 Stanford University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package edu.stanford.junction.provider.jvm;




public class MultiJunction {
	private static Map<URI,MultiJunction>multiJunctions = 
		Collections.synchronizedMap(
			new HashMap<URI,MultiJunction>());
	
	Map<String,JunctionActor> actorMap;
	URI uri;
	
	public static MultiJunction get(URI uri, JunctionActor actor) {
		MultiJunction j;
		if (!multiJunctions.containsKey(uri)) {
			j = new MultiJunction(uri);
			multiJunctions.put(uri,j);
		} else {
			j = multiJunctions.get(uri);
		}
		j.registerActor(actor);
		return j;
	}
	
	private MultiJunction(URI uri) {
		actorMap = new HashMap<String, JunctionActor>();
		this.uri = uri;
	}
	
	private void registerActor(JunctionActor actor) {
		actorMap.put(actor.getActorID(),actor);
	}
	
	protected void sendMessageToActor(JunctionActor from, String actorID, JSONObject message) {
		if (actorMap.containsKey(actorID)) {
			actorMap.get(actorID).getJunction().triggerMessageReceived(

					new MessageHeader(from.getJunction(),message,from.getActorID()),
