changeset 22:56e015e8f5dc

Testing TestLindaServer
author kono
date Tue, 19 Aug 2008 16:02:48 +0900
parents fac6e0073b1a
children b4fd7fb9135a
files .settings/org.eclipse.jdt.core.prefs src/fdl/AcceptHandler.java src/fdl/ComDebug_Client.java src/fdl/CommDebugHook.java src/fdl/FDLindaServ.java src/fdl/FederatedLinda.java src/fdl/IOHandler.java src/fdl/MetaLogEngine.java src/fdl/NullIOHandlerHook.java src/fdl/NullMetaEngine.java src/fdl/PSX.java src/fdl/PSXQueue.java src/fdl/Tuple.java src/fdl/test/TestLindaServer.java src/fdl/test/TestPSXLinda.java
diffstat 15 files changed, 105 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.settings/org.eclipse.jdt.core.prefs	Tue Aug 19 16:02:48 2008 +0900
@@ -0,0 +1,12 @@
+#Tue Aug 19 14:22:01 JST 2008
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.5
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.5
--- a/src/fdl/AcceptHandler.java	Tue Aug 19 10:48:22 2008 +0900
+++ b/src/fdl/AcceptHandler.java	Tue Aug 19 16:02:48 2008 +0900
@@ -31,7 +31,7 @@
         // 監視する操作は読み込みのみ        
         channel.register(key.selector(),
                          SelectionKey.OP_READ,
-                         new IOHandler(tupleSpace,key));
+                         new IOHandler(tupleSpace,channel));
 	}
 
 }
--- a/src/fdl/ComDebug_Client.java	Tue Aug 19 10:48:22 2008 +0900
+++ b/src/fdl/ComDebug_Client.java	Tue Aug 19 16:02:48 2008 +0900
@@ -72,7 +72,6 @@
 					public MyCallBack(PSXLindaInterface psx) {
 						this.psx = psx;
 					}
-					@Override
 					public void callback(ByteBuffer reply) {
 						int p = psxs.indexOf(psx);
 						System.out.println("PSXReply("+p+") =>"+reply);
@@ -84,7 +83,6 @@
 				psx.out(PSX.META_MONITOR, nullBuffer, 0);
 				debugCallback = 
 					new PSXCallback() {
-					@Override
 					public void callback(ByteBuffer reply) {
 						System.out.println(PSX.getdataString(reply));
 						psx.out(PSX.META_MONITOR, nullBuffer, 0);
--- a/src/fdl/CommDebugHook.java	Tue Aug 19 10:48:22 2008 +0900
+++ b/src/fdl/CommDebugHook.java	Tue Aug 19 16:02:48 2008 +0900
@@ -13,19 +13,16 @@
 	}
 
 
-	@Override
 	public void checkHook(SelectionKey key, int id, int seq, char mode) {
 		if (key!=null) logs.add(log(key, id, seq, mode, null));
 	}
 
 
-	@Override
 	public void inHook(SelectionKey key, int id, int seq, char mode) {
 		if (key!=null) logs.add(log(key, id, seq, mode, null));
 	}
 
 
-	@Override
 	public void outHook(SelectionKey key, int id, int seq, char mode, ByteBuffer data) {
 		if (key==null) return;
 		String sendtext = PSX.getdataString(data);
@@ -33,7 +30,6 @@
 	}
 
 
-	@Override
 	public void waitReadHook(SelectionKey key, int id, int seq, char mode) {
 		if (key!=null) logs.add(log(key, id, seq, mode, null));
 	}
--- a/src/fdl/FDLindaServ.java	Tue Aug 19 10:48:22 2008 +0900
+++ b/src/fdl/FDLindaServ.java	Tue Aug 19 16:02:48 2008 +0900
@@ -9,6 +9,7 @@
 import java.nio.channels.ServerSocketChannel;
 import java.nio.channels.spi.AbstractSelector;
 import java.nio.channels.spi.SelectorProvider;
+import java.util.Iterator;
 
 public class FDLindaServ  {
 	static final int MAX_REQ = 1;
@@ -61,10 +62,11 @@
 		selector = SelectorProvider.provider().openSelector();		
 		//ソケット・チャネルを生成・設定
 		ssChannel = SelectorProvider.provider().openServerSocketChannel();
+		// getAllByName で、すべて取って、その上のすべてでselectする必要がある。
 		InetSocketAddress address = new InetSocketAddress(InetAddress.getLocalHost(), port);
+		ssChannel.socket().setReuseAddress(true);
 		ssChannel.socket().bind(address);
 		ssChannel.configureBlocking(false);
-		//ssChannel.socket().setReuseAddress(true);
 		System.out.println("Server: litening at "+ssChannel);
 		//セレクタにチャンネルを登録
         tupleSpace = new TupleSpace();
@@ -74,25 +76,16 @@
 	}
 
 	public void checkTuple() {
-		// セレクタによる監視    
-		try {
-			if (selector.select()>0) {
-				for(SelectionKey s:selector.selectedKeys()) {
-					TupleHandler handler = (TupleHandler)s.attachment();
-					handler.handle(s);
-				}
-			}
-		} catch (ClosedChannelException e) {
-			// we have to do something...
-		} catch (IOException e) {
-		}
+		checkTuple(0);
 	}
 	
 	public void checkTuple(long timeout) {
 		// セレクタによる監視    
 		try {
 			if (selector.select(timeout)>0) {
-				for(SelectionKey s:selector.selectedKeys()) {
+			      for (Iterator<SelectionKey> it = selector.selectedKeys().iterator();it.hasNext(); ) {
+			        SelectionKey s = it.next();
+			        it.remove();
 					TupleHandler handler = (TupleHandler)s.attachment();
 					handler.handle(s);
 				}
--- a/src/fdl/FederatedLinda.java	Tue Aug 19 10:48:22 2008 +0900
+++ b/src/fdl/FederatedLinda.java	Tue Aug 19 16:02:48 2008 +0900
@@ -38,7 +38,7 @@
 
 public class FederatedLinda  {
 
-	static FederatedLinda fdl;
+	static FederatedLinda fdl = new FederatedLinda();
 	static int MAX_SEQUENCE = 2048;
 	static boolean debug = true;
 
@@ -53,17 +53,16 @@
 	public PSXReply r_top,r_end;
 	public Hashtable<Integer,PSXReply> seqHash;
 
-	public static FederatedLinda init() 
-	throws IOException {
-		if (fdl==null) {
-			fdl = new FederatedLinda();
-		}
+	public static FederatedLinda init() {
 		return fdl;
 	}
 
-	private FederatedLinda() 
-	throws IOException {
-		selector = Selector.open();
+	private FederatedLinda() {
+		try {
+			selector = Selector.open();
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
 		seqHash =  new Hashtable<Integer, PSXReply>();
 	}
 
--- a/src/fdl/IOHandler.java	Tue Aug 19 10:48:22 2008 +0900
+++ b/src/fdl/IOHandler.java	Tue Aug 19 16:02:48 2008 +0900
@@ -10,7 +10,7 @@
 import java.nio.channels.SelectionKey;
 import java.nio.channels.SocketChannel;
 
-public class IOHandler {
+public class IOHandler implements TupleHandler {
     static final boolean debug = false;
     public TupleSpace tupleSpace;
 
@@ -18,10 +18,9 @@
 	String localString;
 	public int cnt = 0;
     
-    public IOHandler(TupleSpace tupleSpace,SelectionKey key) {
+    public IOHandler(TupleSpace tupleSpace,SocketChannel ch) {
     	this.tupleSpace = tupleSpace;
 
-		SocketChannel ch = (SocketChannel) key.channel();
 		remoteString = getRemoteHostAndPort(ch);
 		localString =  getLocalHostAndPort(ch);
     }
@@ -125,8 +124,8 @@
 		channel.close();
 	}
 
-	private static String getRemoteHostAndPort(SocketChannel channel) {
-		String socketString = channel.socket().getRemoteSocketAddress().toString();
+	private static String getRemoteHostAndPort(SocketChannel ch) {
+		String socketString = ch.socket().getRemoteSocketAddress().toString();
 		String[] split = socketString.split("/");
 		int length = split.length;
 		String hostAndPort = split[length-1];
@@ -144,8 +143,8 @@
         }		
 	}
 	
-	private static String getLocalHostAndPort(SocketChannel channel) {
-		String socketString = channel.socket().getLocalSocketAddress().toString();
+	private static String getLocalHostAndPort(SocketChannel ch) {
+		String socketString = ch.socket().getLocalSocketAddress().toString();
 		String[] split = socketString.split("/");
 		int length = split.length;
 		String hostAndPort = split[length-1];
--- a/src/fdl/MetaLogEngine.java	Tue Aug 19 10:48:22 2008 +0900
+++ b/src/fdl/MetaLogEngine.java	Tue Aug 19 16:02:48 2008 +0900
@@ -35,7 +35,7 @@
 			meta.in(PSX.META_MONITOR,monitor_callback);
 		}};
 	
-	void mainLoop() {
+	public void mainLoop() {
 		meta.in(PSX.META_MONITOR,monitor_callback_start);
 		meta.in(PSX.META_STOP, new PSXCallback() {public void callback(ByteBuffer reply) { running = false;}});
 		while(running) 
--- a/src/fdl/NullIOHandlerHook.java	Tue Aug 19 10:48:22 2008 +0900
+++ b/src/fdl/NullIOHandlerHook.java	Tue Aug 19 16:02:48 2008 +0900
@@ -5,24 +5,19 @@
 
 public class NullIOHandlerHook implements IOHandlerHook {
 
-	@Override
 	public void checkHook(SelectionKey key, int id, int seq, char mode) {
 	}
 
-	@Override
 	public void closeHook(SelectionKey key) {
 	}
 
-	@Override
 	public void inHook(SelectionKey key, int id, int seq, char mode) {
 	}
 
-	@Override
 	public void outHook(SelectionKey key, int id, int seq, char mode,
 			ByteBuffer data) {
 	}
 
-	@Override
 	public void waitReadHook(SelectionKey key, int id, int seq, char mode) {
 	}
 
--- a/src/fdl/NullMetaEngine.java	Tue Aug 19 10:48:22 2008 +0900
+++ b/src/fdl/NullMetaEngine.java	Tue Aug 19 16:02:48 2008 +0900
@@ -1,6 +1,6 @@
 package fdl;
 
-public class NullMetaEngine implements MetaLogEngine, MetaEngine {
+public class NullMetaEngine implements MetaEngine {
 	public MetaLinda meta;
 	public boolean running=true;
 
@@ -8,8 +8,7 @@
 		this.meta = meta;
 	}
 	
-	@Override
-	void mainLoop() {
+	public void mainLoop() {
 		while(running) 
 			meta.fds.checkTuple();
 	}
--- a/src/fdl/PSX.java	Tue Aug 19 10:48:22 2008 +0900
+++ b/src/fdl/PSX.java	Tue Aug 19 16:02:48 2008 +0900
@@ -88,8 +88,8 @@
 	}
 	
 	
-	static void setCommand(ByteBuffer command, int _mode, int _id, int _seq, int _datalen) {
-		command = ByteBuffer.allocate(LINDA_HEADER_SIZE);
+	static ByteBuffer setCommand(int _mode, int _id, int _seq, int _datalen) {
+		ByteBuffer command = ByteBuffer.allocate(LINDA_HEADER_SIZE);
 		command.order(ByteOrder.BIG_ENDIAN);
 	
 		command.putInt(LINDA_PACKET_LENGTH_OFFSET,
@@ -99,6 +99,7 @@
 		command.putInt(LINDA_SEQ_OFFSET,_seq);
 		command.putInt(LINDA_DATA_LENGTH_OFFSET,_datalen);
 		command.rewind();
+		return command;
 	}
 
 	static void setAnserCommand(ByteBuffer command, int seq) {
--- a/src/fdl/PSXQueue.java	Tue Aug 19 10:48:22 2008 +0900
+++ b/src/fdl/PSXQueue.java	Tue Aug 19 16:02:48 2008 +0900
@@ -44,7 +44,7 @@
 	}
 
 	private void setCommand() {
-		PSX.setCommand(command,mode,id,size,seq);
+		command = PSX.setCommand(mode,id,size,seq);
 	}
 
 	public void setSeq(int _seq) {
--- a/src/fdl/Tuple.java	Tue Aug 19 10:48:22 2008 +0900
+++ b/src/fdl/Tuple.java	Tue Aug 19 16:02:48 2008 +0900
@@ -25,11 +25,11 @@
     }
 
     public void setCommand(int _mode, int _seq) {
-    	PSX.setCommand(command, _mode, id, _seq,datalen);
+    	setCommand( _mode, id, _seq,datalen);
     }
 
 	public void setCommand(int _mode, int _id, int _seq, int _datalen) {
-    	PSX.setCommand(command, _mode, _id, _seq, _datalen);
+    	command = PSX.setCommand(_mode, _id, _seq, _datalen);
     }
 
 	public void setTuple(int _mode,int _id, int _seq, int _datalen, ByteBuffer _data) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/fdl/test/TestLindaServer.java	Tue Aug 19 16:02:48 2008 +0900
@@ -0,0 +1,48 @@
+package fdl.test;
+
+import fdl.FDLindaServ;
+import fdl.FederatedLinda;
+
+public class TestLindaServer {
+	public FederatedLinda fdl;
+	public FDLindaServ fds;
+	public static final int PORT = 10000;
+
+	class Server implements Runnable {
+		public void run() {
+			String[] args = {"-p",Integer.toString(PORT)};
+			FDLindaServ.main(args);
+		}
+	}
+
+	class Client implements Runnable {
+		public void run() {
+			String[] args = {};
+			sleep(2000);
+			TestPSXLinda.main(args);
+		}
+		public synchronized void sleep(int time) {
+			try {
+				wait(time);
+			} catch (InterruptedException e) {
+				e.printStackTrace();
+			}
+		}
+	}
+	
+	public static void main(String[] arg) {
+		TestLindaServer me = new TestLindaServer();
+		me.test1();
+
+	}
+	
+	public void test1() {
+		Server s = new Server();
+		Client c = new Client();
+		new Thread(s).start();
+		new Thread(c).start();
+		
+	}
+	
+
+}
--- a/src/fdl/test/TestPSXLinda.java	Tue Aug 19 10:48:22 2008 +0900
+++ b/src/fdl/test/TestPSXLinda.java	Tue Aug 19 16:02:48 2008 +0900
@@ -12,6 +12,9 @@
 package fdl.test;
 
 import java.io.IOException;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.UnknownHostException;
 import java.nio.ByteBuffer;
 
 import fdl.FederatedLinda;
@@ -36,9 +39,19 @@
 
 	FederatedLinda fdl;
 	PSXLindaInterface psx;
-	String host  = "localhost";
+	String host;
 	int port = 10000;
 	PSXReply r;
+	InetSocketAddress localAddress;
+	
+
+	try {
+		localAddress = new InetSocketAddress(InetAddress.getLocalHost(), port);
+		host = localAddress.getHostName();
+	} catch (UnknownHostException e) {
+		// TODO Auto-generated catch block
+		host = "localhost";
+	}
 	
 
 	// try {