changeset 9:ff7e7833cded

pack/ unpack with putInt/getInt and ByteBuffer
author pin
date Sun, 22 Oct 2006 20:36:30 +0900
parents bf97ccde466d
children e40c010c703b
files src/remoteeditor/network/REP.java
diffstat 1 files changed, 44 insertions(+), 84 deletions(-) [+]
line wrap: on
line diff
--- a/src/remoteeditor/network/REP.java	Sun Oct 22 19:06:59 2006 +0900
+++ b/src/remoteeditor/network/REP.java	Sun Oct 22 20:36:30 2006 +0900
@@ -21,19 +21,19 @@
 	int seqid;
 	int textsiz;
 	
-	byte REP_INSERT_CMD = 6;
-	byte REP_INSERT_ACK_CMD = 7;
-	byte REP_JOIN_CMD = 41;
-	byte REP_JOIN_ACK_CMD = 42;
-	byte REP_PUT_CMD = 45;
-	byte REP_PUT_ACK_CMD = 46;
-	byte REP_SELECT_CMD = 47;
-	byte REP_SELECT_ACK_CMD = 48;
-	byte REP_QUIT_CMD = 53;
-	byte REP_QUIT_ACK_CMD = 54;
+	static final int REP_INSERT_CMD = 6;
+	static final int REP_INSERT_ACK_CMD = 7;
+	static final int REP_JOIN_CMD = 41;
+	static final int REP_JOIN_ACK_CMD = 42;
+	static final int REP_PUT_CMD = 45;
+	static final int REP_PUT_ACK_CMD = 46;
+	static final int REP_SELECT_CMD = 47;
+	static final int REP_SELECT_ACK_CMD = 48;
+	static final int REP_QUIT_CMD = 53;
+	static final int REP_QUIT_ACK_CMD = 54;
 	
 	ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
-	byte[] readbyte = new byte[1024];
+	ByteBuffer read_buffer = ByteBuffer.allocateDirect(1024);
 	
 	String string;
 	private RSocketListener socketListener;
@@ -84,19 +84,19 @@
 	
 	public void join() throws IOException{
 		sc.write(pack(buffer, REP_JOIN_CMD, sid, eid, seqid, lineno, "afro"));
-		unpack(buffer);
+		unpack(read_buffer);
 		System.out.println("read packet:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +", "+ textsiz +", "+ string);
 	}
 	
 	public void put() throws Exception {
 		sc.write(pack(buffer, REP_PUT_CMD, sid, eid, seqid, lineno, "filename"));
-		unpack(buffer);
+		unpack(read_buffer);
 		System.out.println("read packet:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +", "+ textsiz +", "+ string);
 	}
 	
 	public void select() throws Exception {
 		sc.write(pack(buffer, REP_SELECT_CMD, sid, eid, seqid, lineno, "afro"));
-		unpack(buffer);
+		unpack(read_buffer);
 		System.out.println("read packet:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +", "+ textsiz +", "+ string);
 	}
 	
@@ -111,96 +111,56 @@
 
 
     	//System.out.println("pack:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno);
-    
+    	
     	//ByteBuffer buffer = ByteBuffer.allocateDirect(24+textsiz);
-    	
+    	buffer.clear();  // position = 0 
     	buffer.putInt(cmd); buffer.putInt(sid); buffer.putInt(eid);
     	buffer.putInt(seqid); buffer.putInt(lineno); 
-    	buffer.putInt(text.length()); 
+    	buffer.putInt(text.length()*2); 
     	//buffer.putString(text);
+    	System.out.println("pack:" + cmd +", "+ "length="+ text.length() +" "+ sid +", "+ eid +", "+ seqid +", "+ lineno);
     	
     	for(int i=0;i<text.length();i++) {
 			buffer.putChar(text.charAt(i));
 		}	
-    	System.out.println("pack:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno);
-    	
+    	buffer.flip();    // limit = current position, position = 0
     	return buffer;
     }
 
-
-/*    public void unpack() {
-    	int cmd,  sid,  eid,  seqid,  lineno; String text = "";
-    	int textsiz = 0;
-
-    	buffer.rewind();
-    	
-    	cmd = buffer.getInt(); sid = buffer.getInt(); eid = buffer.getInt();
-    	seqid = buffer.getInt(); lineno = buffer.getInt(); textsiz = buffer.getInt();
-    	
-
-    	for(int i=0;i<textsiz;i++) {
-			text +=buffer.getChar();
-		}
-
-    	System.out.println("unpack:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +"," + text);
-    	//byte[] readbyte = new byte[textsiz];
-    	//for(int i = 0; i < textsiz; i++){
-			//readbyte[i] = buffer.get();
-			//System.out.println(readbyte[i]);
-		//}
-    	//text = new String(readbyte, 0, textsiz);
-    	
-    }
-
 	
-*/	/*public ByteBuffer pack(byte REP_CMD, byte sid, byte eid, byte seqid, byte lineno, String text) {
-		byte[] REP_PACK = new byte[24];
-		REP_PACK = new byte[24+text.length()];
-		REP_PACK[3] = REP_CMD;
-		REP_PACK[23] = (byte)(text.length());
-		byte[] textToByte = text.getBytes();
-		for(int i = 0; i < textToByte.length; i++){
-			REP_PACK[i+24] = textToByte[i];
+    static final int HEADER_SIZE = 24;
+    ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE);
+    
+	public void unpack(ByteBuffer buffer) throws IOException{
+		long len;
+		header.clear();
+		len = sc.read(header);  // limit = read length
+		if (len !=HEADER_SIZE) {
+			// this can't happen
 		}
-		REP_PACK[7] = sid;
-		REP_PACK[11] = eid;
-		REP_PACK[15] = seqid;
-		REP_PACK[19] = lineno;
-		ByteBuffer buffer = ByteBuffer.wrap(REP_PACK);
-		
-		/*
-		 * ByteBuffer buffer = ByteBuffer.allocateDirect(1024);
-		 * buffer.putInt(REP_CMD);
-		 * buffer.putInt(sid);
-		 * buffer.putInt(eid);
-		 * buffer.putInt(seqid);
-		 * buffer.putInt(lineno);
-		 * 
-		 * 
-		 */
-		
-	/*	return buffer;
-	}*/
-	
-	public void unpack(ByteBuffer buffer) throws IOException{
-		buffer.clear();
-		sc.read(buffer);
-		buffer.rewind();
+		header.rewind();  // position = 0
 		/*for(int i = 0; i < 24; i++){
-			readbyte[i] = buffer.get();
+			readbyte[i] = header.get();
 			//System.out.println(readbyte[i]);
 		}*/
 		String text = "";
-		cmd = buffer.getInt();
-		sid = buffer.getInt();
-		eid = buffer.getInt();
-		seqid = buffer.getInt();
-		lineno = buffer.getInt();
-		textsiz = buffer.getInt();
+		cmd = header.getInt();
+		sid = header.getInt();
+		eid = header.getInt();
+		seqid = header.getInt();
+		lineno = header.getInt();
+		textsiz = header.getInt()/2;
+		
+		buffer.limit(textsiz*2);
+		buffer.rewind();
+		len = sc.read(buffer);  // limit = read length
+		if (len !=HEADER_SIZE) {
+			// this can't happen
+		}
+		buffer.rewind();
 		for(int i=0;i<textsiz;i++) {
 			text +=buffer.getChar();
 		}
-
 		/*for(int i = 0; i < textsiz; i++){
 			readbyte[i] = buffer.get();
 		}