com.openteach.diamond.network.waverider.command.Command.java Source code

Java tutorial

Introduction

Here is the source code for com.openteach.diamond.network.waverider.command.Command.java

Source

/**
 * Copyright 2013 openteach
 *
 *  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 com.openteach.diamond.network.waverider.command;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.openteach.diamond.network.waverider.session.Session;
import com.openteach.diamond.network.waverider.slave.SlaveState;

/**
 * <p>
 * 
 * </p>
 * 
 * @author <a href="mailto:sihai@taobao.com">sihai</a>
 *
 */
public class Command {

    private static Log logger = LogFactory.getLog(Command.class);

    public static Long HEART_BEAT_COMMAND = 0L; // Command Type
    public static Long AVAILABLE_COMMAND_START = 10L; // ?Command Type

    //=====================================================================
    //         Header
    //=====================================================================
    private Long type; // , , 0L-10L?
    private Integer length; // 

    //=====================================================================
    //         Body()
    //=====================================================================
    private ByteBuffer payLoad; // , 

    /**
     * ?Session, Master
     */
    private Session session;

    public Command() {
        this(null, null, null);
    }

    /**
     * 
     * @param type
     * @param payLoad
     */
    public Command(Long type, ByteBuffer payLoad) {
        this(null, type, payLoad);
    }

    /**
     * 
     * @param type
     * @param payLoad
     */
    public Command(Session session, Long type, ByteBuffer payLoad) {
        this.session = session;
        this.type = type;
        this.payLoad = payLoad;
    }

    /**
     * ?
     * @return
     */
    public Long getType() {
        return type;
    }

    /**
     * 
     * @param type
     */
    public void setType(Long type) {
        this.type = type;
    }

    /**
     * ?
     * @return
     */
    public Integer getLength() {
        return length;
    }

    /**
     * 
     * @param length
     */
    public void setLength(Integer length) {
        this.length = length;
    }

    /**
     * 
     * @param payLoad
     */
    public void setPayLoad(ByteBuffer payLoad) {
        this.payLoad = payLoad;
    }

    /**
     * ?
     * @return
     */
    public ByteBuffer getPayLoad() {
        return payLoad;
    }

    /**
     * ??Session, Master
     * @return
     */
    public Session getSession() {
        return session;
    }

    /**
     * ?Session, Master
     * @return
     */
    public void setSession(Session session) {
        this.session = session;
    }

    /**
     * ?
     * @return
     */
    public int getSize() {
        int size = 0;
        size += getHeaderSize();
        size += payLoad.remaining();

        return size;
    }

    //=====================================================================
    //         
    //=====================================================================

    /**
     * 
     * @return
     */
    public static int getHeaderSize() {
        int size = 0;
        size += Long.SIZE / Byte.SIZE;
        size += Integer.SIZE / Byte.SIZE;
        return size;
    }

    /**
     * ByteBuffer
     * @return
     */
    public ByteBuffer marshall() {
        int length = getSize();
        ByteBuffer buffer = ByteBuffer.allocate(length);
        buffer.putLong(type);
        buffer.putInt(length);
        buffer.put(payLoad);
        buffer.flip();
        payLoad.clear();
        return buffer;
    }

    /**
     * ByteBuffer??
     * @param buffer
     * @return
     */
    public static Command unmarshall(ByteBuffer buffer) {
        if (buffer.remaining() < getHeaderSize()) {
            throw new RuntimeException("Wrong command.");
        }
        Command command = new Command();
        command.setType(buffer.getLong());
        command.setLength(buffer.getInt());
        // (payLoad)array()
        command.setPayLoad(buffer.slice());
        return command;
    }

    public static void main(String[] args) {

        ByteArrayOutputStream bout = null;
        ObjectOutputStream objOutputStream = null;

        try {
            bout = new ByteArrayOutputStream();
            objOutputStream = new ObjectOutputStream(bout);
            SlaveState slaveState = new SlaveState();
            slaveState.setId(1L);
            slaveState.setIsMasterCandidate(false);
            objOutputStream.writeObject(slaveState);
            objOutputStream.flush();
            Command command = CommandFactory.createHeartbeatCommand(ByteBuffer.wrap(bout.toByteArray()));

            ByteBuffer buffer = command.marshall();
            Command cmd = Command.unmarshall(buffer);
            SlaveState ss = SlaveState.fromByteBuffer(cmd.getPayLoad());
            System.out.println(cmd.toString());
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                if (objOutputStream != null) {
                    objOutputStream.close();
                }
                if (bout != null) {
                    bout.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}