changeset 147:4ff68518e9ca

*** empty log message ***
author kent
date Wed, 27 Aug 2008 23:38:21 +0900
parents 20beee6ca31a
children 6a5fe529b192
files rep/REPPacketReceive.java rep/REPPacketSend.java rep/channel/REPPack.java rep/channel/REPPacketSend.java rep/channel/REPSocketChannel.java rep/channel/REPUnpack.java
diffstat 6 files changed, 143 insertions(+), 69 deletions(-) [+]
line wrap: on
line diff
--- a/rep/REPPacketReceive.java	Wed Aug 27 23:14:39 2008 +0900
+++ b/rep/REPPacketReceive.java	Wed Aug 27 23:38:21 2008 +0900
@@ -3,54 +3,36 @@
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.CharBuffer;
-import java.nio.channels.SelectableChannel;
 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 java.util.LinkedList;
-import java.util.StringTokenizer;
+
 
-import rep.channel.ChannelSimulator;
 import rep.channel.REPSocketChannel;
+import rep.channel.REPUnpack;
 
-public class REPPacketReceive {
+public class REPPacketReceive implements REPUnpack<REPCommand> {
 	
 	REPSocketChannel<REPCommand> socketchannel;
 	private final int HEADER_SIZE = 24;
-	SelectionKey key;
+	private boolean debug=false;
 	
 	public REPPacketReceive(REPSocketChannel<REPCommand> sc){
 		socketchannel = sc;
 	}
 	
-	public void setkey(SelectionKey key) {
-		this.key = key;
-	}
-	
 
-	public REPCommand unpackUConv() {
+	public REPCommand unpackUConv(REPSocketChannel<REPCommand> sc) throws IOException {
 		ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE);
 		long len = 0;
 		header.clear();
-		try {
-			len = socketchannel.read(header);
-			if(len == -1){
-				if(key != null){
-					key.cancel();
-				}					
-				socketchannel.close();
-				return null;
-			}else if(len == 0){
-				return null;
-			}
-		} catch (IOException e1) {
-			e1.printStackTrace();
-		}  // limit = read length
+		len = socketchannel.read(header);
+		if(len <= 0){
+			return null;
+		}
 		if (len !=HEADER_SIZE) {
-			System.out.println("error.");
-			// this can't happen
+			throw new IOException();
 		}
 		header.rewind();  // position = 0
 
@@ -63,21 +45,12 @@
 		
 		ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz);
 		
-		try {
-			len = socketchannel.read(textBuffer);
-			if(len == -1){
-				if(key != null){
-					key.cancel();
-				}					
-				socketchannel.close();
-				return null;
-			}
-		} catch (IOException e1) {
-			e1.printStackTrace();
-		}  // limit = read length
+		len = socketchannel.read(textBuffer);
+		if(len <= 0){
+			return null;
+		}
 		if (len != textsiz) {
-			// this can't happen
-			System.out.println("error.");
+			throw new IOException();
 		}
 		textBuffer.rewind();
 
@@ -97,8 +70,11 @@
 		textsiz = string.length();
 
 		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());
+
+		if (debug){
+			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;		
 	}
--- a/rep/REPPacketSend.java	Wed Aug 27 23:14:39 2008 +0900
+++ b/rep/REPPacketSend.java	Wed Aug 27 23:38:21 2008 +0900
@@ -3,14 +3,14 @@
 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 rep.channel.REPPack;
 import rep.channel.REPSocketChannel;
 
 
-public class REPPacketSend {
+public class REPPacketSend implements REPPack<REPCommand> {
 	REPSocketChannel<REPCommand> socketchannel;
 	// JIS/S-JIS = 2, UTF-8 = 3, UTF-?? = 5 
 	final int CHAR_ORDER = 5;
@@ -19,23 +19,9 @@
 		socketchannel = channel;
 	}
 	
-	private ByteBuffer pack(REPCommand command){
-    	System.out.println("send command: " + command.toString());
-    	if(command.string == null){
-    		command.setString("test");
-    	}
-    	ByteBuffer buffer = ByteBuffer.allocateDirect(24+(command.string).length()*2);
-    	buffer.clear();  // position = 0 
-    	buffer.putInt(command.cmd); buffer.putInt(command.sid); buffer.putInt(command.eid);
-    	buffer.putInt(command.seq); buffer.putInt(command.lineno); 
-    	buffer.putInt(command.string.length()*2); 
-    	for(int i=0;i<command.string.length();i++) {
-			buffer.putChar(command.string.charAt(i));
-		}	
-    	buffer.flip();    // limit = current position, position = 0
-		return buffer;
-	}
-	
+	/* (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){
@@ -73,6 +59,9 @@
 		return buffer;    	
 	}
 	
+	/* (non-Javadoc)
+	 * @see rep.REPPack#send(rep.REPCommand)
+	 */
 	public void send(REPCommand command){
 		try {
 			//socketchannel.write(pack(command));
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/channel/REPPack.java	Wed Aug 27 23:38:21 2008 +0900
@@ -0,0 +1,11 @@
+package rep.channel;
+
+import java.nio.ByteBuffer;
+
+public interface REPPack<P> {
+
+	public abstract ByteBuffer packUConv(P command);
+
+	public abstract void send(P command);
+
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/channel/REPPacketSend.java	Wed Aug 27 23:38:21 2008 +0900
@@ -0,0 +1,74 @@
+package rep.channel;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetEncoder;
+
+import rep.REPCommand;
+import rep.channel.REPPack;
+
+
+public class REPPacketSend implements REPPack<REPCommand> {
+	REPSocketChannel<REPCommand> socketchannel;
+	// JIS/S-JIS = 2, UTF-8 = 3, UTF-?? = 5 
+	final int CHAR_ORDER = 5;
+
+	public REPPacketSend(REPSocketChannel<REPCommand> channel){
+		socketchannel = channel;
+	}
+	
+	/* (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(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;    	
+	}
+	
+	/* (non-Javadoc)
+	 * @see rep.REPPack#send(rep.REPCommand)
+	 */
+	public void send(REPCommand command){
+		try {
+			//socketchannel.write(pack(command));
+			socketchannel.write(packUConv(command));
+			//System.out.println(command.toString());
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+}
--- a/rep/channel/REPSocketChannel.java	Wed Aug 27 23:14:39 2008 +0900
+++ b/rep/channel/REPSocketChannel.java	Wed Aug 27 23:38:21 2008 +0900
@@ -14,10 +14,20 @@
 public class REPSocketChannel<P> extends SelectableChannel{
 
 	private SocketChannel sc;
+	private REPUnpack<P> unpack;
+	private REPPack<P> pack;
 
 	public REPSocketChannel(SocketChannel channel) {
 		sc = channel;
 	}
+	
+	public void setUnpack(REPUnpack<P> _unpack){
+		unpack = _unpack;
+	}
+	public void setPack(REPPack<P> _pack){
+		pack = _pack;
+	}
+	
 
 	@Override
 	public Object blockingLock() {
@@ -84,14 +94,19 @@
 		return null;
 	}
 	
-	public P read(){
-		// TODO
-		return null;
-		
+	public P read() throws IOException{
+		return unpack.unpackUConv(this);
 	}
 	public boolean write(P p){
-		return false;
-		//
+		ByteBuffer bb = pack.packUConv(p);
+		try {
+			while (bb.remaining() > 0 ){
+				sc.write(bb);
+			}
+			return true;
+		} catch (IOException e) {
+			return false;
+		}
 	}
 
 	public static <T> REPSocketChannel<T> create() throws IOException {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rep/channel/REPUnpack.java	Wed Aug 27 23:38:21 2008 +0900
@@ -0,0 +1,9 @@
+package rep.channel;
+
+import java.io.IOException;
+
+public interface REPUnpack<P> {
+
+	public P unpackUConv(REPSocketChannel<P> sc) throws IOException;
+	
+}
\ No newline at end of file