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

com.generallycloud.baseio.codec.protobase.ProtobaseProtocolEncoder Maven / Gradle / Ivy

There is a newer version: 3.2.9-BETA-2
Show newest version
/*
 * Copyright 2015-2017 GenerallyCloud.com
 *  
 * 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.generallycloud.baseio.codec.protobase;

import java.io.IOException;
import java.nio.charset.Charset;

import com.generallycloud.baseio.buffer.ByteBuf;
import com.generallycloud.baseio.buffer.ByteBufAllocator;
import com.generallycloud.baseio.buffer.EmptyByteBuf;
import com.generallycloud.baseio.codec.protobase.future.ProtobaseReadFuture;
import com.generallycloud.baseio.common.StringUtil;
import com.generallycloud.baseio.component.ByteArrayBuffer;
import com.generallycloud.baseio.protocol.ChannelReadFuture;
import com.generallycloud.baseio.protocol.ChannelWriteFuture;
import com.generallycloud.baseio.protocol.ChannelWriteFutureImpl;
import com.generallycloud.baseio.protocol.ProtocolEncoder;
import com.generallycloud.baseio.protocol.ProtocolException;

public class ProtobaseProtocolEncoder implements ProtocolEncoder {

	private static final byte[] EMPTY_ARRAY = EmptyByteBuf.getInstance().array();

	@Override
	public ChannelWriteFuture encode(ByteBufAllocator allocator, ChannelReadFuture future)
			throws IOException {

		if (future.isHeartbeat()) {

			byte b = future.isPING() ? 
					ProtobaseProtocolDecoder.PROTOCOL_PING
					: ProtobaseProtocolDecoder.PROTOCOL_PONG ;

			ByteBuf buf = allocator.allocate(1);

			buf.putByte(b);

			return new ChannelWriteFutureImpl(future, buf.flip());
		}

		ProtobaseReadFuture f = (ProtobaseReadFuture) future;

		String future_name = f.getFutureName();

		if (StringUtil.isNullOrBlank(future_name)) {
			throw new ProtocolException("future name is empty");
		}

		Charset charset = future.getContext().getEncoding();

		byte[] future_name_array = future_name.getBytes(charset);

		if (future_name_array.length > 255) {
			throw new IllegalArgumentException("service name too long ," + future_name);
		}

		byte future_name_length = (byte) future_name_array.length;

		ByteArrayBuffer binary = f.getWriteBinaryBuffer();

		ByteArrayBuffer buffer = f.getWriteBuffer();

		byte[] text_array;
		int text_length;
		if (buffer == null) {
			text_array = EMPTY_ARRAY;
			text_length = 0;
		} else {
			text_array = buffer.array();
			text_length = buffer.size();
		}

		if (binary != null) {
			return encode(allocator, f, future_name_array, text_array, binary);
		}
		
		int header_length = getHeaderLengthNoBinary();
		
		byte byte0 = getFirstByte();

		int all_length = header_length + future_name_length + text_length;

		ByteBuf buf = allocator.allocate(all_length);

		buf.putByte(byte0);
		buf.putByte(future_name_length);
		buf.putInt(f.getFutureId());
		putHeaderExtend(f,buf);
		buf.putUnsignedShort(text_length);

		buf.put(future_name_array);

		if (text_length > 0) {
			buf.put(text_array, 0, text_length);
		}

		return new ChannelWriteFutureImpl(future, buf.flip());
	}
	
	protected void putHeaderExtend(ProtobaseReadFuture future,ByteBuf buf){
		
	}
	
	private byte getFirstByte(){
		return ProtobaseProtocolDecoder.PROTOCOL_PACKET;
	}
	
	private byte getBinaryFirstByte(){
		return ProtobaseProtocolDecoder.PROTOCOL_HAS_BINARY | ProtobaseProtocolDecoder.PROTOCOL_PACKET;
	}
	
	protected int getHeaderLengthNoBinary(){
		return ProtobaseProtocolDecoder.PROTOCOL_HEADER_NO_BINARY;
	}
	
	protected int getHeaderLengthWithBinary(){
		return ProtobaseProtocolDecoder.PROTOCOL_HEADER_WITH_BINARY;
	}

	private ChannelWriteFuture encode(ByteBufAllocator allocator, ProtobaseReadFuture f,
			byte[] future_name_array, byte[] text_array, ByteArrayBuffer binary)
			throws IOException {

		byte future_name_length = (byte) future_name_array.length;
		int text_length = text_array.length;
		int header_length = getHeaderLengthWithBinary();
		int binary_length = binary.size();
		byte byte0 = getBinaryFirstByte();

		int all_length = header_length + future_name_length + text_length + binary_length;

		ByteBuf buf = allocator.allocate(all_length);

		buf.putByte(byte0);
		buf.putByte((byte) (future_name_length));
		buf.putInt(f.getFutureId());
		putHeaderExtend(f,buf);
		buf.putUnsignedShort(text_length);
		buf.putInt(binary_length);

		buf.put(future_name_array);

		if (text_length > 0) {
			buf.put(text_array, 0, text_length);
		}

		buf.put(binary.array(), 0, binary_length);

		return new ChannelWriteFutureImpl(f, buf.flip());
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy