view rep/REPCommandPacker.java @ 271:5b7abc22e61a

enum
author kono
date Thu, 11 Sep 2008 17:23:27 +0900
parents c513cf1ce9cc
children 6deb6de8d0eb
line wrap: on
line source

package rep;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;

import rep.channel.REPPack;

/*
//+-------+--------+--------+-------+--------+---------+------+
//| cmd   | session| editor | seqid | lineno | textsiz | text |
//|       | id     | id     |       |        |         |      |
//+-------+--------+--------+-------+--------+---------+------+
//o-------header section (network order)-------------o
int cmd;	// command
int sid;	// session ID : uniqu to editing file
int eid;	// editor ID : owner editor ID = 1。Session に対して unique
int seqno;	// Sequence number : sequence number はエディタごとに管理
int lineno;	// line number
int textsize;   // textsize : bytesize
byte[] text;
*/


public class REPCommandPacker implements REPPack<REPCommand> {
	// JIS/S-JIS = 2, UTF-8 = 3, UTF-?? = 5
	private final int HEADER_SIZE = 24;
	final int CHAR_ORDER = 5;


	/* (non-Javadoc)
	 * @see rep.REPPack#packUConv(rep.REPCommand)
	 */
	public ByteBuffer packUConv(REPCommand command){		
    	System.out.println("send command byUTF8: " + command.toString());
    	if(command.string == null){
    		command.setString("test");
    	}
    	ByteBuffer buffer = ByteBuffer.allocateDirect(HEADER_SIZE+(command.string.length()*CHAR_ORDER));
    	buffer.clear();  // position = 0 
    	buffer.putInt(command.cmd.id);
    	buffer.putInt(command.sid);
    	buffer.putInt(command.eid);
    	buffer.putInt(command.seq);
    	buffer.putInt(command.lineno);     	
    	
    	int pos = buffer.position();
    	buffer.putInt(0);
    	
    	//Encode to UTF8
    	CharBuffer cb = CharBuffer.wrap(command.string);
   		Charset charset = Charset.forName("UTF-8");
		CharsetEncoder encoder = charset.newEncoder();
		try {
			encoder.encode(cb, buffer, true);
		} catch (IllegalStateException e) {
			e.printStackTrace();
		}
		
		//Encoded string length set
		int length = (buffer.position() -pos) -4;
		System.out.println("UTF-8: Set REPComand textlen(Byte) : " + (buffer.position() - pos-4));  
		if(length < 0) {
			length = 0;
		}
		buffer.putInt(pos, length);

		buffer.limit(HEADER_SIZE+length);
		buffer.rewind();
		
		return buffer;    	
	}


	public REPCommand unpackUConv(SocketChannel sc) throws IOException {
		ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE);
		header.clear();
		/* 
		len = sc.read(header);
		if(len <= 0){
			return null;
		}
		if (len !=HEADER_SIZE) {
			throw new IOException();
		}  下のwhileループで OK ?  */
		while(header.remaining()>0){
			if (sc.read(header)<0) throw new IOException();
		} // 壊れたパケットに対してはブロックする可能性あり まぁTCPだしいいか?
		
		header.rewind();  // position = 0

		int cmd = header.getInt();
		int sid = header.getInt();
		int eid = header.getInt();
		int seqid = header.getInt();
		int lineno = header.getInt();
		int textsiz = header.getInt();
		
		ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz);

		/*
		len = sc.read(textBuffer);
		if(len <= 0){
			return null;
		}
		if (len != textsiz) {
			throw new IOException();
		}*/
		while(textBuffer.remaining()>0){
			if (sc.read(textBuffer)<0) throw new IOException();
		}
		textBuffer.rewind();

		//Decode UTF-8 to System Encoding(UTF-16) 
		Charset charset = Charset.forName("UTF-8");
		CharsetDecoder decoder = charset.newDecoder();
		CharBuffer cb = null;
		try {
			cb = decoder.decode(textBuffer);
		} catch (CharacterCodingException e) {
			e.printStackTrace();
		}
		cb.rewind();
		
		String string = cb.toString();
		
		textsiz = string.length();

		REPCommand repcommand = new REPCommand(cmd, sid, eid, seqid, lineno, textsiz, string);

		return repcommand;		
	}

}