changeset 116:cf4df4f73605

*** empty log message ***
author pin
date Sun, 23 Dec 2007 16:47:01 +0900
parents 418e2433bd04
children a441426fe48a
files src/remoteeditor/network/REPPacketReceive.java src/remoteeditor/network/REPPacketSend.java
diffstat 2 files changed, 130 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/remoteeditor/network/REPPacketReceive.java	Sun Dec 23 16:25:00 2007 +0900
+++ b/src/remoteeditor/network/REPPacketReceive.java	Sun Dec 23 16:47:01 2007 +0900
@@ -2,7 +2,12 @@
 
 import java.io.IOException;
 import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.channels.SelectionKey;
 import java.nio.channels.SocketChannel;
+import java.nio.charset.CharacterCodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetDecoder;
 
 import remoteeditor.command.REPCommand;
 import remoteeditor.command.REPCommandEvent;
@@ -19,7 +24,7 @@
 	}
 
 	
-	public REPCommand unpack() {
+	private REPCommand unpack() {
 		//System.out.println("test1");
 		ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE);
 		long len = 0;
@@ -65,6 +70,85 @@
 		return repcommand;
 	}
 	
+	public REPCommand unpackUConv() {
+		ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE);
+		long len = 0;
+		header.clear();
+		try {
+			len = socketchannel.read(header);
+			if(len == -1){			
+				socketchannel.close();
+				return null;
+			}else if(len == 0){
+				return null;
+			}
+		} catch (IOException e1) {
+			e1.printStackTrace();
+		}  // limit = read length
+		if (len !=HEADER_SIZE) {
+			System.out.println("てす");
+			// this can't happen
+		}
+		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();
+		//int tmptextsiz = header.getInt();
+		//int textsiz = (tmptextsiz /5) + (tmptextsiz % 5);
+		
+		ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz);
+		
+		try {
+			len = socketchannel.read(textBuffer);
+			if(len == -1){				
+				socketchannel.close();
+				return null;
+			}else if(len == 0){
+				return null;
+			}
+		} catch (IOException e1) {
+			e1.printStackTrace();
+		}  // limit = read length
+		if (len != textsiz) {
+			// this can't happen
+			System.out.println("あと");
+		}
+		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();
+		if(textsiz > 2){
+			System.out.println(string);
+		}
+		//System.out.println("CharBuffer size: " +cb.length());
+		
+		//System.out.println("textsize: " +textsiz);			
+		
+		//System.out.println(cb.toString());
+
+		REPCommand repcommand = new REPCommand(cmd, sid, eid, seqid, lineno, textsiz, string);
+		System.out.println("UnPack Packet: => cmd:"+cmd+" sid:"+sid+" eid:"+eid+"seqid:"+seqid+" lineno:"+lineno+" textsiz:" +textsiz+" text: "+string);
+		System.out.println("received command: " + repcommand.toString());
+		
+		return repcommand;		
+	}
+	
 	public void addCommandListener(REPCommandListener listener){
 		commandlistener = listener;
 		Thread th = new Thread(this);
@@ -74,7 +158,8 @@
 	public void run() {
 		while(socketchannel.isConnected()){
 			//unpack();
-			commandlistener.CommandReceived(new REPCommandEvent(unpack()));
+//			commandlistener.CommandReceived(new REPCommandEvent(unpack()));
+			commandlistener.CommandReceived(new REPCommandEvent(unpackUConv()));
 		}
 	}
 	
--- a/src/remoteeditor/network/REPPacketSend.java	Sun Dec 23 16:25:00 2007 +0900
+++ b/src/remoteeditor/network/REPPacketSend.java	Sun Dec 23 16:47:01 2007 +0900
@@ -2,18 +2,22 @@
 
 import java.io.IOException;
 import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
 import java.nio.channels.SocketChannel;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetEncoder;
 
 import remoteeditor.command.REPCommand;
 
 public class REPPacketSend {
 	SocketChannel socketchannel;
+	private int CHAR_ORDER = 5;
 	
 	public REPPacketSend(SocketChannel sc){
 		socketchannel = sc;
 	}
 	
-	public ByteBuffer pack(REPCommand command){
+	private ByteBuffer pack(REPCommand command){
 		//command.setString(command.string + ":temp:123456");	
     	System.out.println("send command: " + command.toString());
     	ByteBuffer buffer = ByteBuffer.allocateDirect(24+(command.string).length()*2);
@@ -28,9 +32,46 @@
 		return buffer;
 	}
 	
+	private ByteBuffer packUConv(REPCommand command){		
+    	System.out.println("send command byUTF8: " + command.toString());
+    	if(command.string == null){
+    		command.setString("test");
+    	}
+    	ByteBuffer buffer = ByteBuffer.allocateDirect(24+(command.string.length()*CHAR_ORDER ));
+    	buffer.clear();  // position = 0 
+    	buffer.putInt(command.cmd); 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(24+length);
+		buffer.rewind();
+		
+		return buffer;    	
+	}
+	
 	public void send(REPCommand command){
 		try {
-			socketchannel.write(pack(command));
+			socketchannel.write(packUConv(command));
 		} catch (IOException e) {
 			e.printStackTrace();
 		}