view java/src/uk/ac/qmul/eecs/ccmi/network/LockMessage.java @ 1:e3935c01cde2 tip

moved license of PdPersistenceManager to the beginning of the file
author Fiore Martin <f.martin@qmul.ac.uk>
date Tue, 08 Jul 2014 19:52:03 +0100
parents 78b7fc5391a2
children
line wrap: on
line source
/*  
 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
  
 Copyright (C) 2011  Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/  

package uk.ac.qmul.eecs.ccmi.network;

/**
 * This class represents a lock message,through which the clients get exclusivity on  
 * editing the elements of the diagram. The class is used by both the client, 
 * to request a lock, and the server, to acknowledge the success/failure of the request.
 * 
 * The argument of the message can be either the path to a three node or the id of a diagram element.
 * The former is used for lock for notes editing which can concern every tree node in the tree, 
 * the latter for all the other types of lock as they only concern diagram elements.   
 * The path to a tree node is a sequence of integers representing the index of the children 
 * from the root to the affected node. So for instance 4,2,3 would be the third son of the 
 * second son of the fourth son of the root node. 
 */
public class LockMessage extends Message {

	/**
	 * Creates a lock message for the given diagram and with the given timestamp.
	 * 
	 * @param name the message name  
	 * @param timestamp the time when the message was issued 
	 * @param diagram the name of the diagram this message refers to 
	 * @param args arguments of the message 
	 * @param source the source that generated this message see {@link DiagramEventActionSource}
	 */
	public LockMessage(Name name, long timestamp, String diagram, Object[] args, Object source) {
		super(timestamp, diagram,source);
		this.args = args;
		this.name = name;
	}

	/**
	 * Creates a lock message for the given diagram and timestamp of the moment 
	 * the message is created.
	 * 
	 * @param name the message name  
	 * @param diagram the name of the diagram this message refers to 
	 * @param args arguments of the message 
	 * @param source the source that generated this message see {@link DiagramEventActionSource}
	 */
	public LockMessage(Name name, String diagram, Object[] args, DiagramEventActionSource source) {
		super(diagram,source);
		this.args = args;
		this.name = name;
	}
	
	public LockMessage(Name name, long timestamp, String diagram, long id, DiagramEventActionSource source) {
		this(name,timestamp,diagram,new Object[]{id},source);
	}
	
	public LockMessage(Name name, String diagram, long id, DiagramEventActionSource source){
		this(name,diagram,new Object[]{id},source);
	}
	
	@Override
	public Name getName() {
		return name;
	}
	
	public Object getArgAt(int index) {
		return args[index];
	}
	
	public Object[] getArgs(){
		return args;
	}

	public int getArgNum(){
		return args.length;
	}
	
	@Override
	public DiagramEventActionSource getSource(){
		return (DiagramEventActionSource)super.getSource();
	}
	
	public static LockMessage.Name valueOf(String n){
		Name name = Name.NONE_L;
		try {
			name = Name.valueOf(n);
		}catch(IllegalArgumentException iae){
			iae.printStackTrace();
			return Name.NONE_L; 
		}
		return name;
	}

	/** used to distinguish between different kinds of messages. */
	public static final String NAME_POSTFIX = "_L";
	public static final String GET_LOCK_PREFIX = "GET_";
	public static final String YIELD_LOCK_PREFIX = "YIELD_";
	private Name name;
	private Object[] args;
	
	/**
	 * enum containing all the possible lock messages that can be exchanged 
	 * between server and client in either direction.
	 */
	public static enum Name implements Message.MessageName {
		GET_DELETE_L,
		GET_NAME_L,
		GET_PROPERTIES_L,
		GET_EDGE_END_L,
		GET_MOVE_L,
		GET_NOTES_L,
		GET_BOOKMARK_L,
		GET_MUST_EXIST_L,
		
		YIELD_DELETE_L,
		YIELD_NAME_L,
		YIELD_PROPERTIES_L,
		YIELD_EDGE_END_L,
		YIELD_MOVE_L,
		YIELD_NOTES_L,
		YIELD_BOOKMARK_L,
		YIELD_MUST_EXISTS_L,
		
		YES_L,
		NO_L,
		NONE_L;
	}
}