Mercurial > hg > FederatedLinda
changeset 24:35375016b2f0 simple-test-passed
cleanup.
author | kono |
---|---|
date | Wed, 20 Aug 2008 10:18:05 +0900 |
parents | b4fd7fb9135a |
children | 330fa49bc4fd |
files | src/fdl/ByteBufferReader.java src/fdl/ComDebug_Client.java src/fdl/FDLindaServ.java src/fdl/FederatedLinda.java src/fdl/IOHandler.java src/fdl/PSX.java src/fdl/PSXLinda.java src/fdl/PSXQueue.java src/fdl/PSXReply.java src/fdl/Tuple.java src/fdl/TupleHandler.java src/fdl/TupleSpace.java src/fdl/URLKicker.java src/fdl/test/TestLindaServer.java |
diffstat | 14 files changed, 120 insertions(+), 200 deletions(-) [+] |
line wrap: on
line diff
--- a/src/fdl/ByteBufferReader.java Wed Aug 20 03:28:45 2008 +0900 +++ b/src/fdl/ByteBufferReader.java Wed Aug 20 10:18:05 2008 +0900 @@ -15,7 +15,6 @@ @Override public int read(char[] cbuf, int off, int len) throws IOException { - // TODO Auto-generated method stub for(int i=0;i<len;i++) { cbuf[i]=_buf.getChar(); } @@ -29,8 +28,7 @@ @Override public void close() throws IOException { - // TODO Auto-generated method stub - + } }
--- a/src/fdl/ComDebug_Client.java Wed Aug 20 03:28:45 2008 +0900 +++ b/src/fdl/ComDebug_Client.java Wed Aug 20 10:18:05 2008 +0900 @@ -41,17 +41,17 @@ hosts.add(args[++i]); System.err.println("host = "+hosts.getLast()); } else { - //System.err.println(usages); + System.err.println(usages); return; } if("-p".equals(args[i])) { ports.add(Integer.parseInt(args[++i])); System.err.println("port = "+ports.getLast()); } else { - //System.err.println(usages); + System.err.println(usages); return; } } if (hosts.size()!=ports.size()) { - System.err.println("-h and -p pair is always necessary."); + System.err.println("-h and -p pairs are always necessary."); System.exit(1); } } catch (NumberFormatException e) {
--- a/src/fdl/FDLindaServ.java Wed Aug 20 03:28:45 2008 +0900 +++ b/src/fdl/FDLindaServ.java Wed Aug 20 10:18:05 2008 +0900 @@ -15,7 +15,6 @@ static final int MAX_REQ = 1; static final int FAIL = (-1); static final int DEF_PORT = 10000; - //public static final int TIMEOUT = 5*1000; public int port = DEF_PORT; private AbstractSelector selector; private ServerSocketChannel ssChannel; @@ -23,8 +22,6 @@ public static void main(final String[] args) { final String usages = "usage: FDLindaServ [-p port]"; - //バイトオーダー確認 - //System.out.println(ByteOrder.nativeOrder().toString()); int port = DEF_PORT; //引数判定
--- a/src/fdl/FederatedLinda.java Wed Aug 20 03:28:45 2008 +0900 +++ b/src/fdl/FederatedLinda.java Wed Aug 20 10:18:05 2008 +0900 @@ -69,19 +69,11 @@ public PSXLindaInterface open(String _host,int _port) throws IOException { tid++; - // System.out.print("Tid = "); - // System.out.println(tid); PSXLindaInterface newlinda = new PSXLinda(this,tid,_host,_port); linda = newlinda.add(linda); return linda; } - /** - psx_queue (unsigned int tspace_id, unsigned int id, - unsigned int size, unsigned char *data, char mode, - void(*callback)(char*,void*), void * obj): - */ - public PSXReply psx_queue(PSXLinda linda, int id, ByteBuffer s, int mode, PSXCallback callback) { PSXQueue c = new PSXQueue(linda,id,mode,s,callback); @@ -97,12 +89,6 @@ PSXReply p = new PSXReply(PSX.PSX_REPLY,callback); p.seq = seq(p); c.setSeq(p.seq); - if (debug) { - System.out.print("Integer compare="); - System.out.println((new Integer(2)).equals(new Integer(2))); - System.out.print("Sedning seq="); - System.out.println(p.seq); - } if (r_top == null){ r_end = r_top = p; } else { @@ -149,8 +135,6 @@ PSXQueue c = q_top; c.send(); q_top = c.next; - // psx_free(c); - // q_top = c = t; qsize--; } @@ -158,7 +142,6 @@ key_num = selector.select(mtimeout); keys = selector.selectedKeys(); for (SelectionKey key : keys) { - // System.out.println("selecting"); SocketChannel sock = (SocketChannel)key.channel(); chkServe(sock); } @@ -197,50 +180,28 @@ if (debug) { PSX.printCommand(command, data); } - /***if (debug) { - System.out.print("header:"); - for(int i=0;i<LINDA_HEADER_SIZE;i++) { - System.out.println(command.get(i)); - } - System.out.print("data:"); - for(int i=0;i<length;i++) { - System.out.println(data.get(i)); - } - data.rewind(); - }***/ int rseq = command.getInt(PSX.LINDA_SEQ_OFFSET); int mode = command.get(PSX.LINDA_MODE_OFFSET); - Integer a; - /*** - if (debug) { - System.out.print("mode = "); - System.out.println(mode); - System.out.print("seq = "); - System.out.println(rseq); - }***/ - try { - PSXReply r = seqHash.get((a = new Integer(rseq))); - seqHash.remove(a); - if (debug) { - System.out.print("hash value = "); - System.out.println(a.hashCode()); - } + PSXReply r = getReply(rseq); + r.setAnswer(mode,command,data); - r.setAnswer(mode,command,data); - - if (r.callback != null ) { - r.callback.callback(data); - } - } catch (NullPointerException e ) { - if (debug) { - System.out.println("hashed reply not found"); - } - // can't happen - return ; + if (r.callback != null ) { + r.callback.callback(data); } } } + + private PSXReply getReply(int rseq) { + 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; + } } /* end */
--- a/src/fdl/IOHandler.java Wed Aug 20 03:28:45 2008 +0900 +++ b/src/fdl/IOHandler.java Wed Aug 20 10:18:05 2008 +0900 @@ -1,9 +1,6 @@ package fdl; import java.io.IOException; -import java.net.InetAddress; -import java.net.InetSocketAddress; -import java.net.UnknownHostException; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.channels.ClosedChannelException; @@ -21,8 +18,8 @@ public IOHandler(TupleSpace tupleSpace,SocketChannel ch) { this.tupleSpace = tupleSpace; - remoteString = getRemoteHostAndPort(ch); - localString = getLocalHostAndPort(ch); + remoteString = PSX.getRemoteHostAndPort(ch); + localString = PSX.getLocalHostAndPort(ch); } public void handle(SelectionKey key) { @@ -75,7 +72,6 @@ data.order(ByteOrder.BIG_ENDIAN); data.clear(); while(read>0) { - //System.out.print("reading2..."); read -= channel.read(data); } data.rewind(); @@ -87,9 +83,7 @@ PSX.printData(command); } manager_run(key, command, data); - // I believe we don't need this - //key.interestOps(key.interestOps() | SelectionKey.OP_READ ); - assert((key.interestOps()& SelectionKey.OP_READ) !=0); + // assert((key.interestOps()& SelectionKey.OP_READ) !=0); } public void manager_run(SelectionKey key, ByteBuffer command, ByteBuffer data) throws IOException { @@ -125,42 +119,5 @@ key.cancel(); channel.close(); } - - 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]; - split = hostAndPort.split(":"); - String host = split[0]; - String port = split[1]; - int portnum = Integer.parseInt(port); - try { - InetSocketAddress address = new InetSocketAddress(InetAddress.getByName(host), portnum); - host = address.getHostName().toString(); - return (host +":"+port); - } - catch( UnknownHostException e ){ - return hostAndPort; - } - } - - 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]; - split = hostAndPort.split(":"); - String host = split[0]; - String port = split[1]; - try { - InetAddress localhost = InetAddress.getLocalHost(); - host = localhost.getHostName(); - return (host +":"+port); - } - catch( UnknownHostException e ){ - return (host +":"+port); - } - } }
--- a/src/fdl/PSX.java Wed Aug 20 03:28:45 2008 +0900 +++ b/src/fdl/PSX.java Wed Aug 20 10:18:05 2008 +0900 @@ -13,6 +13,9 @@ package fdl; import java.io.IOException; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.net.UnknownHostException; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.CharBuffer; @@ -27,7 +30,8 @@ /** PSX Tuple Command Protocol Format - All PSX offseted command operation should be here. + All PSX offset command operation should be here. + Some public library */ public class PSX { @@ -58,16 +62,6 @@ static final int META_MONITOR = PRIVILEGED_ID_START+1; static final int META_MONITOR_DATA = PRIVILEGED_ID_START+2; - // this method should be removed - static void setReportCommand(ByteBuffer command, String report_txt) { - command.order(ByteOrder.BIG_ENDIAN); - command.putInt(LINDA_PACKET_LENGTH_OFFSET,(report_txt).length()*2+LINDA_HEADER_SIZE-INT_SIZE); - command.put(LINDA_MODE_OFFSET,(byte)'D'); - command.putShort(LINDA_ID_OFFSET,(short) 0); - command.putInt(LINDA_SEQ_OFFSET,0); - command.putInt(LINDA_DATA_LENGTH_OFFSET,(report_txt).length()*2); - command.rewind(); - } static void printCommand(ByteBuffer command, ByteBuffer data) { char id = (char)command.getShort(LINDA_ID_OFFSET); @@ -168,6 +162,43 @@ SocketChannel ch = (SocketChannel)key.channel(); send(ch,command,data); } + + static String getRemoteHostAndPort(SocketChannel ch) { + String socketString = ch.socket().getRemoteSocketAddress().toString(); + String[] split = socketString.split("/"); + int length = split.length; + String hostAndPort = split[length-1]; + split = hostAndPort.split(":"); + String host = split[0]; + String port = split[1]; + int portnum = Integer.parseInt(port); + try { + InetSocketAddress address = new InetSocketAddress(InetAddress.getByName(host), portnum); + host = address.getHostName().toString(); + return (host +":"+port); + } + catch( UnknownHostException e ){ + return hostAndPort; + } + } + + static String getLocalHostAndPort(SocketChannel ch) { + String socketString = ch.socket().getLocalSocketAddress().toString(); + String[] split = socketString.split("/"); + int length = split.length; + String hostAndPort = split[length-1]; + split = hostAndPort.split(":"); + String host = split[0]; + String port = split[1]; + try { + InetAddress localhost = InetAddress.getLocalHost(); + host = localhost.getHostName(); + return (host +":"+port); + } + catch( UnknownHostException e ){ + return (host +":"+port); + } + } }
--- a/src/fdl/PSXLinda.java Wed Aug 20 03:28:45 2008 +0900 +++ b/src/fdl/PSXLinda.java Wed Aug 20 10:18:05 2008 +0900 @@ -55,11 +55,9 @@ socketChannel.configureBlocking(false); socket = socketChannel.socket(); - // socket.setReuseAddress(true); + // socket.setReuseAddress(true); Client side don't need this. socket.setTcpNoDelay(true); - // can be blocked (thread required?) - //socketChannel.connect(new InetSocketAddress(InetAddress.getLocalHost(), _port)); socketChannel.connect(new InetSocketAddress(_host, _port)); while (! socketChannel.finishConnect()) { if (debug) {
--- a/src/fdl/PSXQueue.java Wed Aug 20 03:28:45 2008 +0900 +++ b/src/fdl/PSXQueue.java Wed Aug 20 10:18:05 2008 +0900 @@ -12,7 +12,6 @@ package fdl; -import java.io.IOException; import java.nio.ByteBuffer; /**
--- a/src/fdl/PSXReply.java Wed Aug 20 03:28:45 2008 +0900 +++ b/src/fdl/PSXReply.java Wed Aug 20 10:18:05 2008 +0900 @@ -17,58 +17,58 @@ import java.nio.ByteBuffer; public class PSXReply { - public ByteBuffer command; - public ByteBuffer data; - public int seq; - public PSXReply next; - public int mode; - public PSXCallback callback; - static final boolean debug = false; + public ByteBuffer command; + public ByteBuffer data; + public int seq; + public PSXReply next; + public int mode; + public PSXCallback callback; + static final boolean debug = false; - public PSXReply(int _mode,PSXCallback _callback) { - mode = _mode; - callback = _callback; - } + public PSXReply(int _mode,PSXCallback _callback) { + mode = _mode; + callback = _callback; + } - public PSXReply() { - } + public PSXReply() { + } - public void setAnswer(int _mode, ByteBuffer _command,ByteBuffer _data) { - 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 void setAnswer(int _mode, ByteBuffer _command,ByteBuffer _data) { + 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() { - return command.get(PSX.LINDA_MODE_OFFSET); - } + public int getMode() { + return command.get(PSX.LINDA_MODE_OFFSET); + } - public int getId() { - return command.getShort(PSX.LINDA_ID_OFFSET); - } + public int getId() { + return command.getShort(PSX.LINDA_ID_OFFSET); + } - public int getSeq() { - return command.getInt(PSX.LINDA_SEQ_OFFSET); - } + public int getSeq() { + return command.getInt(PSX.LINDA_SEQ_OFFSET); + } - public int getLength() { - return command.getInt(PSX.LINDA_DATA_LENGTH_OFFSET); - } + public int getLength() { + return command.getInt(PSX.LINDA_DATA_LENGTH_OFFSET); + } - public ByteBuffer getData() { - data.rewind(); - return data; - } + public ByteBuffer getData() { + data.rewind(); + return data; + } - public boolean ready() { - return mode==PSX.PSX_ANSWER; - } + public boolean ready() { + return mode==PSX.PSX_ANSWER; + } }
--- a/src/fdl/Tuple.java Wed Aug 20 03:28:45 2008 +0900 +++ b/src/fdl/Tuple.java Wed Aug 20 10:18:05 2008 +0900 @@ -10,6 +10,7 @@ public int seq; public ByteBuffer command; public ByteBuffer data; + // a Tuple muse remember the answer destination public SocketChannel ch; public Tuple next; @@ -31,17 +32,11 @@ } public void setTuple(int _mode,int _id, int _seq, ByteBuffer _data) { - mode = _mode; - id = _id; - seq = _seq; - data = _data; - - if (debug) { - System.out.print("setTuple mode:"); - System.out.println(mode); - } - //setCommand(); - } + mode = _mode; + id = _id; + seq = _seq; + data = _data; + } public void setSeq(int _seq) { seq = _seq; @@ -74,15 +69,4 @@ public ByteBuffer getCommand() { return this.command; } - /*public ByteBuffer getCommand() { - command = ByteBuffer.allocate(LINDA_HEADER_SIZE); - command.order(ByteOrder.BIG_ENDIAN); - command.putInt(LINDA_PACKET_LENGTH_OFFSET, LINDA_HEADER_SIZE+datalen-INT_SIZE); - command.put(LINDA_MODE_OFFSET, (byte)mode); - command.putShort(LINDA_ID_OFFSET,(short)id); - command.putInt(LINDA_SEQ_OFFSET, seq); - command.putInt(LINDA_DATA_LENGTH_OFFSET, datalen); - command.rewind(); - return command; - }*/ }
--- a/src/fdl/TupleHandler.java Wed Aug 20 03:28:45 2008 +0900 +++ b/src/fdl/TupleHandler.java Wed Aug 20 10:18:05 2008 +0900 @@ -5,9 +5,7 @@ import java.nio.channels.SelectionKey; public interface TupleHandler { - static final int MAX_TUPLE = 65536; - static final int MAX_USER = 4; - static final int BUFSIZE = 65535; + static final int MAX_USER = 99; public int user = 0; public void handle(SelectionKey key) throws ClosedChannelException, IOException;
--- a/src/fdl/TupleSpace.java Wed Aug 20 03:28:45 2008 +0900 +++ b/src/fdl/TupleSpace.java Wed Aug 20 10:18:05 2008 +0900 @@ -7,23 +7,23 @@ public class TupleSpace { static final boolean debug = true; - static final int CAPSIZE = 4194304; public static int user = 0; public byte userchar[] = new byte[2]; public Tuple[] tuple_space; public IOHandlerHook hook = new NullIOHandlerHook(); + public static final int MAX_TUPLE_ID = 65536; public TupleSpace() { // 読みこんだデータを格納するためのリストの初期化 - tuple_space = new Tuple[TupleHandler.MAX_TUPLE]; + tuple_space = new Tuple[TupleSpace.MAX_TUPLE_ID]; } public void newUser() { Tuple tmpTuple; //初期生成 - if((tmpTuple = tuple_space[TupleHandler.MAX_TUPLE-1]) == null) { - tmpTuple = tuple_space[TupleHandler.MAX_TUPLE-1] = new Tuple(); + if((tmpTuple = tuple_space[TupleSpace.MAX_TUPLE_ID-1]) == null) { + tmpTuple = tuple_space[TupleSpace.MAX_TUPLE_ID-1] = new Tuple(); tmpTuple.next = null; } else { while(tmpTuple.next != null) tmpTuple = tmpTuple.next; @@ -43,7 +43,7 @@ tmpTuple.setData(data); //Tuple - int id = TupleHandler.MAX_TUPLE-1; + int id = TupleSpace.MAX_TUPLE_ID-1; int seq = 0; tmpTuple.setTuple('o', id, seq, data); System.out.println("Server: assign id "+user);
--- a/src/fdl/URLKicker.java Wed Aug 20 03:28:45 2008 +0900 +++ b/src/fdl/URLKicker.java Wed Aug 20 10:18:05 2008 +0900 @@ -13,7 +13,6 @@ import java.net.*; import java.io.*; -// mport java.nio.*; /**
--- a/src/fdl/test/TestLindaServer.java Wed Aug 20 03:28:45 2008 +0900 +++ b/src/fdl/test/TestLindaServer.java Wed Aug 20 10:18:05 2008 +0900 @@ -33,7 +33,6 @@ public static void main(String[] arg) { TestLindaServer me = new TestLindaServer(); me.test1(); - } public void test1() { @@ -41,7 +40,6 @@ Client c = new Client(); new Thread(s).start(); new Thread(c).start(); - }