diff src/fdl/FederatedLinda.java @ 31:846c6c14cf04

worked?
author kono
date Fri, 22 Aug 2008 14:48:41 +0900
parents 330fa49bc4fd
children 7e0f6f00763e
line wrap: on
line diff
--- a/src/fdl/FederatedLinda.java	Thu Aug 21 18:46:40 2008 +0900
+++ b/src/fdl/FederatedLinda.java	Fri Aug 22 14:48:41 2008 +0900
@@ -20,6 +20,7 @@
 import java.nio.channels.Selector;
 import java.nio.channels.SocketChannel;
 import java.util.Hashtable;
+import java.util.Iterator;
 import java.util.Set;
 
 
@@ -129,8 +130,6 @@
 	public int sync(long mtimeout) 
 	throws IOException {
 		int key_num = 0;
-		Set<SelectionKey> keys;
-
 		while (q_top != null){
 			PSXQueue c = q_top;
 			c.send();
@@ -139,11 +138,12 @@
 		}
 
 		try {
-			key_num = selector.select(mtimeout);
-			keys = selector.selectedKeys();
-			for (SelectionKey key : keys) {
-				SocketChannel sock = (SocketChannel)key.channel();
-				chkServe(sock);
+			if (selector.select(mtimeout)>0) {
+		      for (Iterator<SelectionKey> it = selector.selectedKeys().iterator();it.hasNext(); ) {
+			        SelectionKey s = it.next();
+			        it.remove();
+					chkServe((SocketChannel)s.channel());
+				}
 			}
 		} catch (IOException e) {
 			e.printStackTrace();
@@ -159,36 +159,37 @@
 		int length;
 		ByteBuffer command = ByteBuffer.allocate(PSX.LINDA_HEADER_SIZE);
 		command.order(ByteOrder.BIG_ENDIAN);
+		PSX.receive(sock, command, PSX.LINDA_HEADER_SIZE);
 
-		sock.read(command);
-		command.rewind();
 		length =  command.getInt(PSX.LINDA_DATA_LENGTH_OFFSET);
-		if (length>0) {
-			ByteBuffer data = ByteBuffer.allocate(length);
-			int read = length;
-			if (debug) {
-				System.out.print("client reading:");
-				System.out.println(length);
-			}
+		ByteBuffer data = ByteBuffer.allocate(length);
+		int read = length;
+		if (debug) {
+			System.out.print("client reading:");
+			System.out.println(length);
+		}
+
+		data.order(ByteOrder.BIG_ENDIAN);
+		while(read>0) {
+			read -= sock.read(data);
+		}
+		data.rewind();
 
-			data.order(ByteOrder.BIG_ENDIAN);
-			while(read>0) {
-				read -= sock.read(data);
-			}
-			data.rewind();
+		if (debug) {
+			PSX.printCommand("chkServe:",command, data);
+		}
 
-			if (debug) {
-				PSX.printCommand("chkServe:",command, data);
-			}
+		int rseq = command.getInt(PSX.LINDA_SEQ_OFFSET);
+		int mode = command.get(PSX.LINDA_MODE_OFFSET);
+		PSXReply r = getReply(rseq);
+		if (r==null) {
+			System.err.println("Illegal answer sequence.");
+			return;
+		}
+		r.setAnswer(mode,command,data);
 
-			int rseq = command.getInt(PSX.LINDA_SEQ_OFFSET);
-			int mode = command.get(PSX.LINDA_MODE_OFFSET);
-			PSXReply r = getReply(rseq);
-			r.setAnswer(mode,command,data);
-
-			if (r.callback != null ) {
-				r.callback.callback(data);
-			}
+		if (r.callback != null ) {
+			r.callback.callback(data);
 		}
 	}
 
@@ -196,9 +197,6 @@
 		Integer a;
 
 		PSXReply r = seqHash.get((a = new Integer(rseq)));
-		if (r==null) {
-			System.out.println("hashed reply not found");
-		}
 		seqHash.remove(a);
 		return r;
 	}