changeset 40:046feb56a196 metacomm-worked **INVALID**

message logging
author kono
date Mon, 25 Aug 2008 14:38:18 +0900
parents 81abceebc869
children cb13a91175ad
files src/fdl/AcceptHandler.java src/fdl/ByteBufferReader.java src/fdl/FDLindaServ.java src/fdl/FederatedLinda.java src/fdl/IOHandler.java src/fdl/PSXLindaImpl.java src/fdl/PSXReply.java src/fdl/TupleSpace.java src/fdl/test/ComDebug_Client.java
diffstat 9 files changed, 63 insertions(+), 89 deletions(-) [+]
line wrap: on
line diff
--- a/src/fdl/AcceptHandler.java	Mon Aug 25 14:01:19 2008 +0900
+++ b/src/fdl/AcceptHandler.java	Mon Aug 25 14:38:18 2008 +0900
@@ -5,30 +5,33 @@
 import java.nio.channels.SelectionKey;
 import java.nio.channels.ServerSocketChannel;
 import java.nio.channels.SocketChannel;
+import java.util.logging.Level;
 
 public class AcceptHandler implements TupleHandler {
 	
 	public TupleSpace tupleSpace;
 	public ServerSocketChannel ss;
+	private FDLindaServ fds;
 	
-	public AcceptHandler(ServerSocketChannel ss, TupleSpace tupleSpace) {
+	public AcceptHandler(FDLindaServ fds,ServerSocketChannel ss, TupleSpace tupleSpace) {
     	// 読みこんだデータを格納するためのリストの初期化
         this.tupleSpace = tupleSpace;
         this.ss = ss;
+        this.fds = fds;
     }
 	
 	public void handle(SelectionKey key) 
                 throws ClosedChannelException, IOException {
         ServerSocketChannel serverChannel
-            = (ServerSocketChannel)key.channel();
+        	= (ServerSocketChannel)key.channel();
 
         if (ss!=serverChannel) {
-        	System.err.println("Wrong server socket channel.");
+		fds.log(Level.SEVERE,"Wrong server socket channel.");
         }
         // アクセプト処理
         SocketChannel channel = serverChannel.accept();
         channel.configureBlocking(false);
-        System.out.println("Server: accepted "+channel.socket());
+        fds.log(Level.INFO,"Server: accepted "+channel.socket());
 
         tupleSpace.newUser();
 
@@ -36,7 +39,7 @@
         // 監視する操作は読み込みのみ        
         channel.register(key.selector(),
                          SelectionKey.OP_READ,
-                         new IOHandler(tupleSpace,channel));
+                         new IOHandler(fds, tupleSpace,channel));
 	}
 
 }
--- a/src/fdl/ByteBufferReader.java	Mon Aug 25 14:01:19 2008 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,34 +0,0 @@
-
-package fdl;
-
-import java.io.*;
-import java.io.IOException;
-import java.nio.*;
-
-public class ByteBufferReader extends Reader {
-	public ByteBuffer _buf;
-	
-	public ByteBufferReader(ByteBuffer buf) {
-		_buf = buf;
-		_buf.reset();
-	}
-	
-	@Override
-	public int read(char[] cbuf, int off, int len) throws IOException {
-		for(int i=0;i<len;i++) {
-			cbuf[i]=_buf.getChar();
-		}
-		return len;
-	}
-	
-	@Override
-	public void reset() {
-		_buf.reset();
-	}
-	
-	@Override
-	public void close() throws IOException {
-
-	}
-
-}
--- a/src/fdl/FDLindaServ.java	Mon Aug 25 14:01:19 2008 +0900
+++ b/src/fdl/FDLindaServ.java	Mon Aug 25 14:38:18 2008 +0900
@@ -10,6 +10,7 @@
 import java.nio.channels.spi.AbstractSelector;
 import java.nio.channels.spi.SelectorProvider;
 import java.util.Iterator;
+import java.util.logging.Level;
 
 /**
  * @author kono
@@ -67,10 +68,10 @@
 		ssChannel.socket().setReuseAddress(true);
 		ssChannel.socket().bind(address);
 		ssChannel.configureBlocking(false);
-		System.out.println("Server: litening at "+ssChannel);
+		this.log(Level.INFO,"Server: litening at "+ssChannel);
 		//セレクタにチャンネルを登録
-        tupleSpace = new TupleSpace();
-		ssChannel.register(selector, SelectionKey.OP_ACCEPT, new AcceptHandler(ssChannel,tupleSpace));
+        tupleSpace = new TupleSpace(this);
+		ssChannel.register(selector, SelectionKey.OP_ACCEPT, new AcceptHandler(this, ssChannel,tupleSpace));
 
 
 	}
@@ -100,4 +101,11 @@
 		} catch (IOException e) {
 		}
 	}
+	
+
+	public void log(Level level,String msg) {
+		System.err.println(msg);
+		if (level==Level.SEVERE)
+			new IOException().setStackTrace(null);
+	}
 }
--- a/src/fdl/FederatedLinda.java	Mon Aug 25 14:01:19 2008 +0900
+++ b/src/fdl/FederatedLinda.java	Mon Aug 25 14:38:18 2008 +0900
@@ -14,13 +14,12 @@
 
 import java.io.IOException;
 import java.nio.ByteBuffer;
-import java.nio.ByteOrder;
 import java.nio.channels.ClosedSelectorException;
 import java.nio.channels.SelectionKey;
 import java.nio.channels.Selector;
-import java.nio.channels.SocketChannel;
 import java.util.Hashtable;
 import java.util.Iterator;
+import java.util.logging.Level;
 
 /**
  FederatedLinda 
@@ -118,8 +117,7 @@
 			s = new Integer(seq);
 		} while (seqHash.containsKey(s));
 		if (debug) {
-			System.out.print("hash value = ");
-			System.out.println(s.hashCode());
+			log(Level.INFO,"hash value = "+s.hashCode());
 		}
 		seqHash.put(s,reply);
 		seq++;
@@ -151,7 +149,7 @@
 			        	handle.handle(s);
 			        } catch (IOException e) {
 			        	s.cancel();
-			        	System.err.println(""+s.channel()+" is closed.");
+					log(Level.INFO,""+s.channel()+" is closed.");
 			        }
 		      }
 			}
@@ -181,6 +179,11 @@
 		return r;
 	}
 
+	public void log(Level level,String msg) {
+		System.err.println(msg);
+		if (level==Level.SEVERE)
+			new IOException().setStackTrace(null);
+	}
 }
 
 /* end */
--- a/src/fdl/IOHandler.java	Mon Aug 25 14:01:19 2008 +0900
+++ b/src/fdl/IOHandler.java	Mon Aug 25 14:38:18 2008 +0900
@@ -6,22 +6,25 @@
 import java.nio.channels.ClosedChannelException;
 import java.nio.channels.SelectionKey;
 import java.nio.channels.SocketChannel;
+import java.util.logging.Level;
 
 public class IOHandler implements TupleHandler {
     static final boolean debug = true;
     public TupleSpace tupleSpace;
     public SocketChannel ch;
+    public FDLindaServ fds;
 
 	String remoteString;
 	String localString;
 	public int cnt = 0;
     
-    public IOHandler(TupleSpace tupleSpace,SocketChannel ch) {
+    public IOHandler(FDLindaServ fds, TupleSpace tupleSpace,SocketChannel ch) {
     	this.tupleSpace = tupleSpace;
 
 		remoteString = PSX.getRemoteHostAndPort(ch);
 		localString =  PSX.getLocalHostAndPort(ch);
 		this.ch = ch;
+		this.fds = fds;
     }
 
     public void handle(SelectionKey key) {
@@ -29,8 +32,8 @@
         if (key.isReadable()) {
             try {
             	SocketChannel channel = (SocketChannel)key.channel();
-                if (ch!=channel) {
-                	System.err.println("Wrong socket on IOHandler");
+            	if (ch!=channel) {
+                	fds.log(Level.INFO,"Wrong socket on IOHandler");
                 }
                 // 読み込み用のバッファの生成
                 ByteBuffer command = ByteBuffer.allocate(PSX.LINDA_HEADER_SIZE);
@@ -53,7 +56,7 @@
 	    command.rewind();
 
     	if (debug) {
-        	System.out.println("data from : "+key.channel());
+		fds.log(Level.INFO,"data from : "+key.channel());
     	}
 		if(mode == '!') {
 			tupleSpace.hook.closeHook(key); 
@@ -67,14 +70,13 @@
 	    	tupleSpace.Out(key, command, data);		
 	    } else {
 	    	tupleSpace.hook.closeHook(key);
-    		System.err.println("Incorrect tuple operation");
-    		System.exit(1);
+		fds.log(Level.SEVERE,"Incorrect tuple operation");
 	    }
     	
     }
 
 	void Connection_Close(SelectionKey key) throws IOException {
-		System.out.println("Connection closed by "+key.channel());
+		fds.log(Level.INFO,"Connection closed by "+key.channel());
 		SocketChannel channel = (SocketChannel)key.channel();
 		key.cancel();
 		channel.close();
--- a/src/fdl/PSXLindaImpl.java	Mon Aug 25 14:01:19 2008 +0900
+++ b/src/fdl/PSXLindaImpl.java	Mon Aug 25 14:38:18 2008 +0900
@@ -22,6 +22,7 @@
 import java.nio.channels.SelectionKey;
 import java.nio.channels.Selector;
 import java.nio.channels.SocketChannel;
+import java.util.logging.Level;
 
 
 /**
@@ -63,7 +64,7 @@
 		socketChannel.connect(new InetSocketAddress(_host, _port));
 		while (! socketChannel.finishConnect()) {
 			if (debug) {
-				System.out.println("waiting for connect");
+			    fdl.log(Level.INFO,"waiting for connect");
 			}
 			if (false) {
 				try {
@@ -72,7 +73,7 @@
 				}
 			}
 		}
-		System.err.println("Linda client connect to "+socketChannel);
+		fdl.log(Level.INFO,"Linda client connect to "+socketChannel);
         socketChannel.register(selector,SelectionKey.OP_READ,this);
 
 		checkConnect("PSXLinda");
@@ -83,7 +84,7 @@
 			IOException {
 		SocketChannel sock = (SocketChannel)key.channel();
 		if (sock!=socketChannel) {
-			System.err.println("wrong socket on PSXLindaImple.");
+		    fdl.log(Level.SEVERE,"wrong socket on PSXLindaImple.");
 		}
 		ByteBuffer command = ByteBuffer.allocate(PSX.LINDA_HEADER_SIZE);
 		command.order(ByteOrder.BIG_ENDIAN);
@@ -97,7 +98,7 @@
 		int mode = command.get(PSX.LINDA_MODE_OFFSET);
 		PSXReply r = fdl.getReply(rseq);
 		if (r==null) {
-			System.err.println("Illegal answer sequence.");
+			fdl.log(Level.SEVERE,"Illegal answer sequence.");
 			return;
 		}
 		r.setAnswer(mode,command,data);
@@ -118,10 +119,8 @@
 	}
 
 	private void checkConnect(String s) {
-		System.out.print("Connected:");
-		System.out.print(s);
-		System.out.print(": ");
-		System.out.println(socketChannel.isConnected());
+		fdl.log(Level.INFO, "Connected:"+ s +": "
+		    +socketChannel.isConnected());
 	}
 
 	public PSXReply in(int id) {
--- a/src/fdl/PSXReply.java	Mon Aug 25 14:01:19 2008 +0900
+++ b/src/fdl/PSXReply.java	Mon Aug 25 14:38:18 2008 +0900
@@ -23,7 +23,6 @@
 	public PSXReply next;
 	public int mode;
 	public PSXCallback callback;
-	static final boolean debug = false;
 
 	public PSXReply(int _mode,PSXCallback _callback) {
 		mode = _mode;
@@ -37,12 +36,6 @@
 		mode = _mode;
 		data = _data;
 		command = _command;
-		if (debug) {
-			System.out.print("setAnswer mode:");
-			System.out.println(mode);
-			System.out.print("setAnswer bool:");
-			System.out.println(mode==PSX.PSX_ANSWER);
-		}
 	}
 
 	public int getMode() {
--- a/src/fdl/TupleSpace.java	Mon Aug 25 14:01:19 2008 +0900
+++ b/src/fdl/TupleSpace.java	Mon Aug 25 14:38:18 2008 +0900
@@ -4,6 +4,7 @@
 import java.nio.ByteBuffer;
 import java.nio.channels.SelectionKey;
 import java.nio.channels.SocketChannel;
+import java.util.logging.Level;
 
 public class TupleSpace {
     static final boolean debug = true;
@@ -11,19 +12,21 @@
 	public byte userchar[] = new byte[2];
     public Tuple[] tuple_space;
     public IOHandlerHook hook = new NullIOHandlerHook();
+	private FDLindaServ fds;
 	public static final int MAX_TUPLE_ID = 65536;
     
-	public TupleSpace() {
+	public TupleSpace(FDLindaServ fds) {
 		// 読みこんだデータを格納するためのリストの初期化
-        tuple_space = new Tuple[TupleSpace.MAX_TUPLE_ID];      
+        tuple_space = new Tuple[MAX_TUPLE_ID];      
+        this.fds = fds;
 	}
     
 
 	public void newUser() {
 		Tuple tuple;
 		//初期生成        
-        if((tuple = tuple_space[TupleSpace.MAX_TUPLE_ID-1]) == null) {
-        	tuple = tuple_space[TupleSpace.MAX_TUPLE_ID-1] = new Tuple();
+        if((tuple = tuple_space[MAX_TUPLE_ID-1]) == null) {
+        	tuple = tuple_space[MAX_TUPLE_ID-1] = new Tuple();
         	tuple.next = null;
         } else {
         	while(tuple.next != null) tuple = tuple.next;
@@ -44,7 +47,7 @@
         int id = TupleSpace.MAX_TUPLE_ID-1;
         int seq = 0;
         tuple.setTuple('o', id, seq, data);
-        System.out.println("Server: assign id "+user);
+        fds.log(Level.INFO,"Server: assign id "+user);
 	}
 	
 	protected void Out(SelectionKey key,ByteBuffer command, ByteBuffer data) {
@@ -58,7 +61,7 @@
 		datasize = command.getInt(PSX.LINDA_DATA_LENGTH_OFFSET);
 		command.rewind();
 		
-		if (debug) System.out.println("*** out command : id = "+id +" ***");
+		if (debug) fds.log(Level.INFO,"*** out command : id = "+id +" ***");
 		int seq = command.getInt(PSX.LINDA_SEQ_OFFSET);
 		command.rewind();
 		hook.outHook(key,id,seq,'o',data);    
@@ -75,7 +78,7 @@
 			PSX.setAnserCommand(command, tuple_space[id].getSeq());
 			
 			if(debug){
-				System.out.println("send size "+datasize+" : mode = "+(char)'a');
+				if (debug) fds.log(Level.INFO,"send size "+datasize+" : mode = "+(char)'a');
 			}
 			PSX.send(tuple_space[id].ch, command, data);
 			removeTuple(id);
@@ -97,13 +100,10 @@
 			tuple.setSeq(seq);
 			tuple.setData(data);
 			if(debug){
-				System.out.println("data inserted len = "+tuple.getdataLength()+" : id = "+id);
+			    fds.log(Level.INFO, "data inserted len = "+tuple.getdataLength()+" : id = "+id);
 			}
 		} else {
-			System.out.println("Incorrect mode :"+(char)tuple_space[id].getMode());
-			command.clear();
-			data.clear();
-			System.exit(1);
+			fds.log(Level.SEVERE, "Incorrect mode :"+(char)tuple_space[id].getMode());
 		}
 	}
 
@@ -123,7 +123,7 @@
 		id = (int)idc;
 		
 		if (debug)
-			System.out.println("*** "+(char)mode+" command : id = "+ id +" ***\n");
+			fds.log(Level.INFO, "*** "+(char)mode+" command : id = "+ id +" ***\n");
 		
 		tuple = new Tuple();
 		tuple.setMode(mode);
@@ -139,7 +139,7 @@
 		tuple.next = tuple_space[id];
 		tuple_space[id] = tuple;
 		if(debug){
-			System.out.println("data inserted insert seq = "+seq +", id = "+id);
+			fds.log(Level.INFO, "data inserted insert seq = "+seq +", id = "+id);
 		}
 	}
 
@@ -167,7 +167,7 @@
 		int seq = command.getInt(PSX.LINDA_SEQ_OFFSET);
 		command.rewind();
 
-		if (debug) System.out.println("*** "+(char)mode+" command : id = "+ id +" ***\n");    		
+		if (debug) fds.log(Level.INFO, "*** "+(char)mode+" command : id = "+ id +" ***\n");    		
 		hook.inHook(key,id,seq,(char)mode);
 
 		tuple = tuple_space[id];
@@ -215,7 +215,7 @@
 
 		if(debug){
 			int sendsize = tuple.getdataLength()+PSX.LINDA_HEADER_SIZE;
-			System.out.println("send size "+sendsize+" : mode = "+(char)tuple.getMode());
+			fds.log(Level.INFO, "send size "+sendsize+" : mode = "+(char)tuple.getMode());
 		}
 		//INの場合はremoveする
 		if(mode == PSX.PSX_IN) {
@@ -249,7 +249,7 @@
 		tuple = null;
 			
 		if(debug){
-			System.out.println("wait inserted seq = "+seq +", id = "+id);
+			fds.log(Level.INFO, "wait inserted seq = "+seq +", id = "+id);
 		}
 		return tuple;
 	}
@@ -288,4 +288,4 @@
 	}
 
 
-}
\ No newline at end of file
+}
--- a/src/fdl/test/ComDebug_Client.java	Mon Aug 25 14:01:19 2008 +0900
+++ b/src/fdl/test/ComDebug_Client.java	Mon Aug 25 14:38:18 2008 +0900
@@ -76,12 +76,12 @@
 				};
 				psx.in(65535, checkid);
 
-				System.err.println("COM_DEBUG Connected.["+host+":"+port+"]");
+				System.out.println("COM_DEBUG Connected.["+host+":"+port+"]");
 				psx.out(PSX.META_MONITOR, null);
 				debugCallback = 
 					new PSXCallback() {
 					public void callback(ByteBuffer reply) {
-						System.err.println("COM_DEBUG: "+PSX.getdataString(reply));
+						System.out.println("COM_DEBUG: "+PSX.getdataString(reply));
 						psx.out(PSX.META_MONITOR, null);
 						psx.in(PSX.META_MONITOR_DATA,debugCallback); 
 					}