package org.craft.atom.protocol.rpc.model;

import java.io.Serializable;
import java.net.InetSocketAddress;
import java.util.Map;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * RPC messages use generic message format for transferring data.
 * <pre>
 *      rpc-message = rpc-header + [ rpc-body ]
 * </pre>
 * 
 * @param <T> the type of the interface class.
 * 
 * @author mindwind
 * @version 1.0, Jul 17, 2014
 */
@ToString
@EqualsAndHashCode(of = { "header", "body" })
public class RpcMessage implements Serializable {

	
	private static final long serialVersionUID = 5138100956693144357L;
	
	
	@Getter @Setter private RpcHeader  header;
	@Getter @Setter private RpcBody    body  ;
	
	
	// ~ ---------------------------------------------------------------------------------------------------------

	
	public boolean isOneway() {
		return header.isOw();
	}
	
	public void setOneway(boolean oneway) {
		if (oneway) header.setOw();
	}
	
	public boolean isResponse() {
		return header.isRp();
	}
	
	public void setResponse(boolean response) {
		if (response) header.setRp();
	}
	
	public boolean isHeartbeat() {
		return header.isHb();
	}
	
	public void setHeartbeat(boolean heartbeat) {
		if (heartbeat) header.setHb();
	}
	
	public long getId() {
		return header.getId();
	}
	
	public void setId(long id) {
		header.setId(id);
	}
	
	public Exception getException() {
		return body.getException();
	}
	
	public void setException(Exception e) {
		body.setException(e);
	}
	
	public int getRpcTimeoutInMillis() {
		return body.getRpcOption().getRpcTimeoutInMillis();
	}
	
	public void setRpcTimeoutInMillis(int rpcTimeoutInMillis) {
		body.getRpcOption().setRpcTimeoutInMillis(rpcTimeoutInMillis);
	}
	
	public void setServerAddress(InetSocketAddress serverAddress) {
		body.getRpcOption().setServerAddress(serverAddress);
	}
	
	public InetSocketAddress getServerAddress() {
		return body.getRpcOption().getServerAddress();
	}
	
	public void setClientAddress(InetSocketAddress clientAddress) {
		body.getRpcOption().setClientAddress(clientAddress);
	}
	
	public InetSocketAddress getClientAddress() {
		return body.getRpcOption().getClientAddress();
	}
	
	public Object getReturnObject() {
		return body.getReturnObject();
	}
	
	public void setReturnObject(Object returnObject) {
		body.setReturnObject(returnObject);
	}
	
	public void setAttachments(Map<String, String> attachments) {
		body.setAttachments(attachments);
	}
	
	public Map<String, String> getAttachments() {
		return body.getAttachments();
	}
	
	public void setRpcId(String rpcId) {
		body.setRpcId(rpcId);
	}
	
	public String getRpcId() {
		return body.getRpcId();
	}
	
}

@@ -972,16 +972,18 @@
 (oneway)
+ %7B
  header.
@@ -986,24 +986,26 @@
 der.setOw();
+ %7D
 %0A%09%7D%0A%09%0A%09publi
@@ -1118,16 +1118,18 @@
 esponse)
+ %7B
  header.
@@ -1132,24 +1132,26 @@
 der.setRp();
+ %7D
 %0A%09%7D%0A%09%0A%09publi
@@ -1268,16 +1268,18 @@
 artbeat)
+ %7B
  header.
@@ -1282,24 +1282,26 @@
 der.setHb();
+ %7D
 %0A%09%7D%0A%09%0A%09publi

improve - add brace for code section