view java/src/uk/ac/qmul/eecs/ccmi/network/Reply.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;

import uk.ac.qmul.eecs.ccmi.gui.DiagramEventSource;
import uk.ac.qmul.eecs.ccmi.utils.InteractionLog;

/**
 * A {@code Reply} message is sent from the server to a client from which it 
 * has just received a command. The reply acknowledges the client that the command has 
 * successfully been executed on the server. By broadcasting the command to the other client and by 
 * ending the reply to the client which issued the command itself the server keeps all the clients 
 * synchronized on its diagram model. 
 *
 */
public class Reply extends Message {
	
	public Reply(Name name, String diagram, String message, long timestamp, DiagramEventSource source){
		super(timestamp,diagram, source);
		this.name = name;
		this.message = message;
	}
	
	public Reply(Name name, String diagram, String message, DiagramEventSource source){
		super(diagram,source);
		this.name = name;
		this.message = message;
	}
	
	public Name getName() {
		return name;
	}
	
	public String getMessage() {
		return message;
	}
	
	@Override
	public DiagramEventSource getSource(){
		return (DiagramEventSource)super.getSource();
	}
	
	/**
	 * @return the length of the String message conveyed by this Reply 
	 */
	public int getMessageLen(){
		return message.length();
	}
	
	@Override
	public String toString(){
		StringBuilder builder = new StringBuilder();
		builder.append(timestamp).append(' ').append(name).append('\n').append(message);
		return builder.toString();
	}

	public static Name valueOf(String n){
		Name name = Name.NONE_R;
		try {
			name = Name.valueOf(n);
		}catch(IllegalArgumentException iae){
			iae.printStackTrace();
		}
		return name;
	}
	
	public static void log(Reply reply){
		if(reply.getName() != Reply.Name.TRANSLATE_EDGE_R && reply.getName() != Reply.Name.TRANSLATE_NODE_R && reply.getName() != Reply.Name.BEND_R){
			StringBuilder builder = new StringBuilder(reply.getName().toString());
			builder.append(' ').append(reply.getDiagram());
			builder.append(' ').append(reply.getMessage());
			InteractionLog.log("SERVER", "reply received", builder.toString());
		}
	}
	
	private Name name;
	private String message;
	private long timestamp;
	public static final String NAME_POSTFIX = "_R";
	public static enum Name implements Message.MessageName {
		NONE_R,
		OK_R,
		ERROR_R,
		LIST_R,
		GET_R,
		INSERT_NODE_R,
		REMOVE_NODE_R,
		INSERT_EDGE_R,
		REMOVE_EDGE_R,
		SET_NODE_NAME_R,
		SET_EDGE_NAME_R,
		SET_PROPERTY_R,
		SET_PROPERTIES_R,
		CLEAR_PROPERTIES_R,
		SET_NOTES_R,
		ADD_PROPERTY_R,
		REMOVE_PROPERTY_R,
		SET_MODIFIERS_R,
		SET_ENDDESCRIPTION_R,
		SET_ENDLABEL_R,
		TRANSLATE_NODE_R,
		TRANSLATE_EDGE_R,
		BEND_R,
		STOP_EDGE_MOVE_R,
		STOP_NODE_MOVE_R;
	};
}