All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mxgraph.sharing.mxSession Maven / Gradle / Ivy

Go to download

JGraphX Swing Component - Java Graph Visualization Library This is a binary & source redistribution of the original, unmodified JGraphX library originating from: "https://github.com/jgraph/jgraphx/archive/v3.4.1.3.zip". The purpose of this redistribution is to make the library available to other Maven projects.

There is a newer version: 3.4.1.3
Show newest version
/**
 * $Id: mxSession.java,v 1.15 2012/01/13 11:07:37 david Exp $
 * Copyright (c) 2007-2012, JGraph Ltd
 */
package com.mxgraph.sharing;

import org.w3c.dom.Node;

import com.mxgraph.sharing.mxSharedState.mxDiagramChangeListener;
import com.mxgraph.util.mxUtils;

/**
 * Implements a session that may be attached to a shared diagram. The session
 * contains a synchronized buffer which is used to hold the pending edits which
 * are to be sent to a specific client. The update mechnism between the server
 * and the client uses HTTP requests (polling). The request is kept on the server
 * for an amount of time or wakes up / returns immediately if the buffer is no
 * longer empty.
 */
public class mxSession implements mxDiagramChangeListener
{
	/**
	 * Default timeout is 10000 ms.
	 */
	public static int DEFAULT_TIMEOUT = 10000;

	/**
	 * Holds the session ID.
	 */
	protected String id;

	/**
	 * Reference to the shared diagram.
	 */
	protected mxSharedState diagram;

	/**
	 * Holds the send buffer for this session.
	 */
	protected StringBuffer buffer = new StringBuffer();

	/**
	 * Holds the last active time millis.
	 */
	protected long lastTimeMillis = 0;

	/**
	 * Constructs a new session with the given ID.
	 * 
	 * @param id Specifies the session ID to be used.
	 * @param diagram Reference to the shared diagram.
	 */
	public mxSession(String id, mxSharedState diagram)
	{
		this.id = id;
		this.diagram = diagram;
		this.diagram.addDiagramChangeListener(this);

		lastTimeMillis = System.currentTimeMillis();
	}

	/**
	 * Returns the session ID.
	 */
	public String getId()
	{
		return id;
	}

	/**
	 * Initializes the session buffer and returns a string that represents the
	 * state of the session.
	 *
	 * @return Returns the initial state of the session.
	 */
	public synchronized String init()
	{
		synchronized (this)
		{
			buffer = new StringBuffer();
			notify();
		}

		return getInitialMessage();
	}

	/**
	 * Returns an XML string that represents the current state of the session
	 * and the shared diagram. A globally unique ID is used as the session's
	 * namespace, which is used on the client side to prefix IDs of newly
	 * created cells.
	 */
	public String getInitialMessage()
	{
		String ns = mxUtils.getMd5Hash(id);

		StringBuffer result = new StringBuffer("");
		result.append("");
		result.append(diagram.getState());
		result.append("");
		result.append("");
		result.append(diagram.getDelta());
		result.append("");
		result.append("");

		return result.toString();
	}

	/**
	 * Posts the change represented by the given XML string to the shared diagram.
	 * 
	 * @param message XML that represents the change.
	 */
	public void receive(Node message)
	{
		//System.out.println(getId() + ": " + mxUtils.getPrettyXml(message));
		Node child = message.getFirstChild();

		while (child != null)
		{
			if (child.getNodeName().equals("delta"))
			{
				diagram.processDelta(this, child);
			}

			child = child.getNextSibling();
		}
		/*System.out.println(mxUtils.getPrettyXml(new mxCodec()
				.encode(((mxSharedGraphModel) diagram).getModel())));*/
	}

	/**
	 * Returns the changes received by other sessions for the shared diagram.
	 * The method returns an empty XML node if no change was received within
	 * 10 seconds.
	 * 
	 * @return Returns a string representing the changes to the shared diagram.
	 */
	public String poll() throws InterruptedException
	{
		return poll(DEFAULT_TIMEOUT);
	}

	/**
	 * Returns the changes received by other sessions for the shared diagram.
	 * The method returns an empty XML node if no change was received within
	 * the given timeout.
	 * 
	 * @param timeout Time in milliseconds to wait for changes.
	 * @return Returns a string representing the changes to the shared diagram.
	 */
	public String poll(long timeout) throws InterruptedException
	{
		lastTimeMillis = System.currentTimeMillis();
		StringBuffer result = new StringBuffer("");

		synchronized (this)
		{
			if (buffer.length() == 0)
			{
				wait(timeout);
			}

			if (buffer.length() > 0)
			{
				result.append("");
				result.append(buffer.toString());
				result.append("");
				
				buffer = new StringBuffer();
			}

			notify();
		}

		result.append("");

		return result.toString();
	}

	/*
	 * (non-Javadoc)
	 * @see com.mxgraph.sharing.mxSharedDiagram.mxDiagramChangeListener#diagramChanged(java.lang.Object, org.w3c.dom.Node)
	 */
	public synchronized void diagramChanged(Object sender, String edits)
	{
		if (sender != this)
		{
			synchronized (this)
			{
				buffer.append(edits);
				notify();
			}
		}
	}

	/**
	 * Returns the number of milliseconds this session has been inactive.
	 */
	public long inactiveTimeMillis()
	{
		return System.currentTimeMillis() - lastTimeMillis;
	}

	/**
	 * Destroys the session and removes its listener from the shared diagram.
	 */
	public void destroy()
	{
		diagram.removeDiagramChangeListener(this);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy