Mercurial > hg > RemoteEditor > Eclipse
changeset 204:aaab17635d0c
source code cleanup
author | one |
---|---|
date | Sat, 18 Dec 2010 17:33:47 +0900 |
parents | fce2776071d4 |
children | 540d7a8a9e33 |
files | src/remoteeditor/command/REPCommand.java src/remoteeditor/network/REP.java src/remoteeditor/ui/REPSelectWindow.java src/rep/REP.java src/rep/REPCommand.java src/rep/REPCommandPacker.java src/rep/channel/ChannelSimulator.java src/rep/channel/NetworkSimulator.java src/rep/channel/NullSelector.java src/rep/channel/REPLogger.java src/rep/channel/REPPack.java src/rep/channel/REPSelectionKey.java src/rep/channel/REPSelector.java src/rep/channel/REPServerSocket.java src/rep/channel/REPServerSocketChannel.java src/rep/channel/REPSocketChannel.java src/rep/channel/SelectionKeySimulator.java src/rep/channel/SelectorSimulator.java src/rep/channel/ServerChannelSimulator.java src/sample/merge/Merge.java src/sample/merge/MergeTest.java src/sample/merge/Merger.java src/sample/merge/PermEnum.java src/sample/merge/Rep_Cmd.java src/sample/merge/SMRoutingTable.java src/sample/merge/TestEditor.java src/sample/merge/TestLoop.java src/sample/merge/TestSessionManager.java src/sample/merge/TestTranslate.java src/sample/merge/TestTranslate2.java src/sample/merge/TestTranslate3.java src/sample/merge/Translate.java src/sample/merge/Translater.java src/sample/merge/TranslaterImp0.java src/sample/merge/TranslaterImp1.java src/sample/pack/PackTest.java src/sample/pack/PackTest3.java |
diffstat | 37 files changed, 1 insertions(+), 3431 deletions(-) [+] |
line wrap: on
line diff
--- a/src/remoteeditor/command/REPCommand.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,81 +0,0 @@ -package remoteeditor.command; - -import remoteeditor.network.REP; - -public class REPCommand { - public static REPCommand SMCMD_SESSION_JOIN = new REPCommand(REP.SMCMD_SM_JOIN, 0, 0, 0, 0, 0, ""); - public int cmd; - public int sid; - public int eid; - public int seq; - public int len; - public int lineno; - public boolean stat; - public boolean throughMaster; - - public String string; - private int textsiz; - String host; - String port; - - public REPCommand(int cmd,int sid,int eid, int seq, int lineno, int textsiz, String string) { - this.cmd = cmd; - this.sid = sid; - this.eid = eid; - this.seq = seq; - this.textsiz = textsiz; - this.lineno = lineno; - this.string = string; - this.throughMaster = false; - } - public REPCommand(REPCommand cmd){ - this(cmd.cmd, cmd.sid, cmd.eid, cmd.seq, cmd.lineno, cmd.string.length(), new String(cmd.string)); - } - - public REPCommand() { - // TODO Auto-generated constructor stub - } - public String toString(){ - String repCmdString = new String(cmd + "," + sid + "," + eid + "," + seq + "," + lineno + "," + textsiz + "," + string); - return repCmdString; - } - - public void setEID(int eid2) { - // TODO Auto-generated method stub - this.eid = eid2; - } - - public void setCMD(int cmd2) { - // TODO Auto-generated method stub - this.cmd = cmd2; - } - - public void setSID(int sessionID) { - // TODO Auto-generated method stub - this.sid = sessionID; - } - - public void setString(String string2) { - // TODO Auto-generated method stub - string = string2; - } - - public void setHost(String host) { - // TODO Auto-generated method stub - this.host = host; - } - - public void setPort(String port) { - // TODO Auto-generated method stub - this.port = port; - } - - public void setThroughMaster(boolean f){ - this.throughMaster=f; - } - public void setSEQID(int i) { - // TODO Auto-generated method stub - seq = i; - } - -}
--- a/src/remoteeditor/network/REP.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,53 +0,0 @@ -package remoteeditor.network; - -public class REP { - public static final int REPCMD_OPEN = 1; - public static final int REPCMD_OPEN_ACK = 2; - public static final int REPCMD_READ = 3; - public static final int REPCMD_READ_ACK = 4; - public static final int REPCMD_INSERT = 6; - public static final int REPCMD_INSERT_ACK = 7; - public static final int REPCMD_DELETE = 9; - public static final int REPCMD_DELETE_ACK = 10; - public static final int REPCMD_CLOSE = 11; - public static final int REPCMD_CLOSE_ACK = 12; - public static final int REPCMD_REPLACE = 13; - public static final int REPCMD_REPLACE_ACK = 14; - public static final int REPCMD_NOP = 15; - public static final int SMCMD_JOIN = 41; - public static final int SMCMD_JOIN_ACK = 42; - public static final int SMCMD_GET = 43; - public static final int SMCMD_GET_ACK = 44; - public static final int SMCMD_PUT = 45; - public static final int SMCMD_PUT_ACK = 46; - public static final int SMCMD_SELECT = 47; - public static final int SMCMD_SELECT_ACK = 48; - public static final int SMCMD_REGISTER = 49; - public static final int SMCMD_REGISTER_ACK = 50; - public static final int SMCMD_DEREGISTER = 51; - public static final int SMCMD_DEREGISTER_ACK= 52; - public static final int SMCMD_QUIT = 53; - public static final int SMCMD_QUIT_ACK = 54; - public static final int SMCMD_SESSION = 60; - public static final int SMCMD_SESSION_ACK = 61; - public static final int SMCMD_SM_JOIN = 62; - public static final int SMCMD_SM_JOIN_ACK = 63; - public static final int SMCMD_UPDATE = 65; - public static final int SMCMD_UPDATE_ACK = 66; - public static final int SMCMD_GET_UNDO = 71; - public static final int SMCMD_GET_UNDO_ACK = 72; - - public static final int SMCMD_UNDO_REPLACE = 73; - public static final int SMCMD_START_MERGE = 75; - public static final int SMCMD_START_MERGE_ACK = 76; - public static final int SMCMD_END_MERGE = 77; - public static final int SMCMD_QUIT_2 = 78; - - - public static final int MERGE_EID = -2; - public static final int SMCMD_SYNC = 83; - - //public static final int SMCMD_START_MERGE = 73; - //public static final int SMCMD_START_MERGE_ACK = 74; - -}
--- a/src/remoteeditor/ui/REPSelectWindow.java Sat Dec 18 13:18:54 2010 +0900 +++ b/src/remoteeditor/ui/REPSelectWindow.java Sat Dec 18 17:33:47 2010 +0900 @@ -17,7 +17,7 @@ import org.eclipse.swt.widgets.Shell; import remoteeditor.command.REPCommand; -import remoteeditor.network.REP; +import rep.REP; import remoteeditor.network.REPPacketReceive; import remoteeditor.network.REPPacketSend;
--- a/src/rep/REP.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,68 +0,0 @@ -package rep; - -public enum REP { - REPCMD_INSERT_USER ( 5), - REPCMD_INSERT ( 6), - REPCMD_INSERT_ACK ( 7), - REPCMD_DELETE_USER ( 8), - REPCMD_DELETE ( 9), - REPCMD_DELETE_ACK ( 10), - REPCMD_CLOSE ( 11), - REPCMD_CLOSE_2 ( 12), - REPCMD_MERGE_MARK(13), - REPCMD_NOP ( 15), - SMCMD_JOIN ( 41), - SMCMD_JOIN_ACK ( 42), - SMCMD_PUT ( 45), - SMCMD_PUT_ACK ( 46), - SMCMD_SELECT ( 47), - SMCMD_SELECT_ACK ( 48), - SMCMD_SELECT0(49), - SMCMD_QUIT ( 53), - SMCMD_QUIT_ACK ( 54), - SMCMD_SM_JOIN ( 62), - SMCMD_SM_JOIN_ACK ( 63), - SMCMD_UPDATE ( 65), - SMCMD_UPDATE_ACK ( 66), - SMCMD_START_MERGE ( 75), - SMCMD_START_MERGE_ACK ( 76), - SMCMD_END_MERGE ( 77), - SMCMD_QUIT_2 ( 67), - SMCMD_QUIT_2_ACK ( 68), - - - SM_EID ( -1), - MERGE_EID ( -2), - - SMCMD_SYNC ( 83), - SMCMD_SYNC_ACK ( 84), - SMCMD_SLEEP(85); - - public final int id; - - REP(int id) { - this.id = id; - } - - static int max = 0; - static int min = 100; - - static REP rep[] ; - static { - // Certainly this is ridiculous... - for (REP r : REP.values()) { - if (max<r.id) max = r.id; - if (min>r.id) min = r.id; - } - rep = new REP[max-min+1]; - for (REP r : REP.values()) { - rep[r.id-min] = r; - } - } - - public static REP newREP(int id) { - // return new REP(id); this does not work... - return rep[id-min]; - } - -}
--- a/src/rep/REPCommand.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,76 +0,0 @@ -package rep; - -import rep.REP; - -public class REPCommand { - public REP cmd; - public int sid; - public int eid; - public int seq; - public int len; - public int lineno; - - public String string; - // if this command is an undo command, it has it's original - public REPCommand original; - - public REPCommand(REP cmd,int sid,int eid, int seq, int lineno, String string) { - this.cmd = cmd; - this.sid = sid; - this.eid = eid; - this.seq = seq; - this.lineno = lineno; - this.string = string; - this.original = null; - } - - public REPCommand(REPCommand cmd){ - this(cmd.cmd, cmd.sid, cmd.eid, cmd.seq, cmd.lineno, cmd.string); - } - - public REPCommand() { - } - - public REPCommand(int cmd, int sid, int eid, int seq, int lineno, - int textsiz, String string) { - this.cmd = REP.newREP(cmd); - this.sid = sid; - this.eid = eid; - this.seq = seq; - this.lineno = lineno; - this.string = string; - this.original = null; - } - - public String toString(){ - String repCmdString = cmd + ",sid=" + sid + ",eid=" + eid + ",seq=" + seq + - ",lineno=" + lineno ; - if (string!=null) repCmdString += ",sz=" + string.length() +"," + string; - return repCmdString; - } - - public void setEID(int eid2) { - this.eid = eid2; - } - - public void setCMD(REP cmd2) { - this.cmd = cmd2; - } - - public void setSID(int sessionID) { - this.sid = sessionID; - } - - public void setString(String string2) { - string = string2; - } - - public void setSEQID(int i) { - seq = i; - } - - public boolean isSameSeq(REPCommand commit) { - return seq==commit.seq && sid==commit.sid && eid==commit.eid; - } - -}
--- a/src/rep/REPCommandPacker.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,127 +0,0 @@ -package rep; - -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.CharBuffer; -import java.nio.channels.SocketChannel; -import java.nio.charset.CharacterCodingException; -import java.nio.charset.Charset; -import java.nio.charset.CharsetDecoder; -import java.nio.charset.CharsetEncoder; - -import rep.channel.REPPack; - -/* -//+-------+--------+--------+-------+--------+---------+------+ -//| cmd | session| editor | seqid | lineno | textsiz | text | -//| | id | id | | | | | -//+-------+--------+--------+-------+--------+---------+------+ -//o-------header section (network order)-------------o -int cmd; // command -int sid; // session ID : uniqu to editing file -int eid; // editor ID : owner editor ID = 1。Session に対して unique -int seqno; // Sequence number : sequence number はエディタごとに管理 -int lineno; // line number -int textsize; // textsize : bytesize -byte[] text; -*/ - - -public class REPCommandPacker implements REPPack<REPCommand> { - private static final int TEXTSIZELIMIT = 0x7000000; - private static final int HEADER_SIZE = 24; - // JIS/S-JIS = 2, UTF-8 = 3, UTF-?? = 5 - private static final int CHARSIZE = 5; - - Charset charset = Charset.forName("UTF-8"); - CharsetEncoder encoder = charset.newEncoder(); - CharsetDecoder decoder = charset.newDecoder(); - - /* (non-Javadoc) - * @see rep.REPPack#packUConv(rep.REPCommand) - */ - public ByteBuffer packUConv(REPCommand command){ - int size = 0; - if (command.string!=null) size = command.string.length()*CHARSIZE; - ByteBuffer buffer = ByteBuffer.allocateDirect(HEADER_SIZE+size); - buffer.clear(); // position = 0 - buffer.putInt(command.cmd.id); - buffer.putInt(command.sid); - buffer.putInt(command.eid); - buffer.putInt(command.seq); - buffer.putInt(command.lineno); - - int pos = buffer.position(); - buffer.putInt(0); - int pos1 = buffer.position(); - - if (command.string!=null) { - //Encode to UTF8 - CharBuffer cb = CharBuffer.wrap(command.string); - try { - encoder.encode(cb, buffer, true); - } catch (IllegalStateException e) { - buffer.position(pos1); - } - } - - //Encoded string length set - int length = buffer.position() -pos1 ; - buffer.putInt(pos, length); - buffer.limit(HEADER_SIZE+length); - buffer.rewind(); - - return buffer; - } - - - public REPCommand unpackUConv(SocketChannel sc) throws IOException { - ByteBuffer header = ByteBuffer.allocateDirect(HEADER_SIZE); - header.clear(); - - while(header.remaining()>0){ - if (sc.read(header)<0) throw new IOException(); - } - - header.rewind(); // position = 0 - - int cmd = header.getInt(); - int sid = header.getInt(); - int eid = header.getInt(); - int seqid = header.getInt(); - int lineno = header.getInt(); - int textsiz = header.getInt(); - - /** - * We should avoid large reading here. Large command should be - * broke in smaller one. It should be easy. - */ - if (textsiz>TEXTSIZELIMIT||textsiz<0) { - // corrupted packet - throw new IOException(); - } - ByteBuffer textBuffer = ByteBuffer.allocateDirect(textsiz); - - while(textBuffer.remaining()>0){ - if (sc.read(textBuffer)<0) throw new IOException(); - } - textBuffer.rewind(); - - //Decode UTF-8 to System Encoding(UTF-16) - String string; - try { - CharBuffer cb; - cb = decoder.decode(textBuffer); - cb.rewind(); - string = cb.toString(); - } catch (CharacterCodingException e) { - string = ""; - } - textsiz = string.length(); - REPCommand repcommand = new REPCommand(cmd, sid, eid, seqid, lineno, textsiz, string); - //if (isLogging) - //System.out.println("Reading: "+repcommand); - return repcommand; - } - -}
--- a/src/rep/channel/ChannelSimulator.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,188 +0,0 @@ -package rep.channel; - -import java.io.IOException; -import java.net.InetSocketAddress; -import java.net.Socket; -import java.net.SocketAddress; -import java.nio.channels.ClosedChannelException; -import java.nio.channels.NotYetConnectedException; -import java.nio.channels.SelectableChannel; -import java.nio.channels.SelectionKey; -import java.nio.channels.Selector; -import java.util.LinkedList; -import java.util.Queue; - -public class ChannelSimulator<P> extends REPSocketChannel<P>{ - protected NetworkSimulator ns; - protected Queue<P> readQ; - protected Selector selector = new NullSelector(); - protected boolean isBlocking; - protected ChannelSimulator<P> otherEnd; - protected SelectionKeySimulator<P> key; - private String ownerName; - - /** Constructors. */ - public ChannelSimulator(){ - super(null, null); - ns = NetworkSimulator.singleton(); - readQ = new LinkedList<P>(); - ownerName = Thread.currentThread().getName(); - } - - /** read from Queue. - * @throws IOException */ - public synchronized P read() throws IOException { - // We may lock selector instead of this, but it reduces - // concurrency, but be careful of dead lock. - P tmp; - while ( (tmp=readQ.poll())==null && isBlocking ) { - try { - wait(); // queue is empty - } catch (InterruptedException e) { - throw new IOException(); - } - } - // for write wait (we don't need this) - //otherEnd.wakeup(); selector.wakeup(); - return tmp; - } - - //private synchronized void wakeup() { - // notify(); - //} - - /** write packet to other end. */ - public boolean write(P p){ - if (otherEnd==null) throw new NotYetConnectedException(); - return otherEnd.enQ(p); - } - - /** otherEnd Channel enqueue p to own queue using this method. */ - - boolean enQ(P p) { - if (enQ1(p)) { - // don't lock this channel while calling selector.wakeup(). - // selector may lock this channel, which may cause dead lock. - selector.wakeup(); - return true; - } - return false; - } - - protected synchronized boolean enQ1(P p){ - while(true) { - if (readQ.offer(p)) { - notify(); // other end my wait() - break; - } else { // this can't happen - assert(false); - try { - wait(); // queue is full, we have to wait here - } catch (InterruptedException e) { - } - } - } - return true; - } - - /** Connecting methods */ - // for clients. - public boolean connect(InetSocketAddress ip){ - return ns.connect(ip, this); // ns.connectはotherEndを返した方がよいか? - } - - public boolean connect(SocketAddress ip){ - return ns.connect((InetSocketAddress)ip, this); - } - - void setOtherEnd(ChannelSimulator<P> other){ - otherEnd = other; - } - - public ChannelSimulator<P> accept(){ - return null; - } - - public boolean isAcceptable() { - return false; - } - public synchronized boolean isReadable() { - return !readQ.isEmpty(); - } - - public boolean isWritable() { - return true; - } - - @Override - public SelectableChannel configureBlocking(boolean block) throws IOException { - isBlocking = block; - return this; - } - - - @SuppressWarnings("unchecked") - @Override - public SelectionKey keyFor(Selector selector2) { - return ((SelectorSimulator) selector2).getKey(this); - } - - @SuppressWarnings("unchecked") - @Override - public SelectionKey keyFor(REPSelector<?> sel) { - return (SelectionKey)((SelectorSimulator) sel).getKey(this); - } - - @SuppressWarnings("unchecked") - @Override - public REPSelectionKey<P> keyFor1(REPSelector<P> sel) { - return (REPSelectionKey<P>)((SelectorSimulator) sel).getKey(this); - } - - @SuppressWarnings("unchecked") - @Override - public SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException { - SelectorSimulator<P> sel1 = (SelectorSimulator<P>) sel; - selector = sel1; - key = sel1.register(this, ops, att); - return key; - } - - @SuppressWarnings("unchecked") - public SelectionKey register(REPSelector<P> sel, int ops, Object att) throws ClosedChannelException { - selector = sel; - key = (SelectionKeySimulator<P>) sel.register(this, ops, att); - return key; - } - - - public boolean finishConnect() throws IOException { - return otherEnd!=null; - } - - public Socket socket() { - assert(false); - return null; - } - - @Override - public String getLocalHostName() { - return "localhost"; // always... - } - - public String toString(){ - return "ChSim("+ownerName+")"; - } - - public ChannelSimulator<P> newChannel() { - return new ChannelSimulator<P>(); - } - - @SuppressWarnings("unchecked") - public void setOtherEnd1(ChannelSimulator<?> other) { - otherEnd = (ChannelSimulator<P>) other; - } - - public void close1() throws IOException { - } -}
--- a/src/rep/channel/NetworkSimulator.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,113 +0,0 @@ -package rep.channel; - -import java.net.InetSocketAddress; -import java.net.SocketAddress; -import java.util.HashMap; -import java.util.LinkedList; - -public class NetworkSimulator { - // we don't use <P> because we need singleton. - public static NetworkSimulator ns = new NetworkSimulator(); - - public HashMap<SocketAddress,Integer>namedb = new HashMap<SocketAddress,Integer>(); - public int ipcount = 1; - public REPLogger logger; - - public static NetworkSimulator singleton() { - // singleton pattern may used here, but it has a little cost. - return ns; - } - - int logLevel=5; - /** Listening Servers. */ - private LinkedList<ServerData> serverList; - - /** Constructor. */ - public NetworkSimulator(){ - serverList = new LinkedList<ServerData>(); - logger = REPLogger.singleton(); - logger.writeLog("construct Networksimulator", 1); - // printAllState(); - } - - /* */ - synchronized public void listen(InetSocketAddress ip, ServerChannelSimulator<?> scs) { - serverList.add(new ServerData(ip, scs)); - logger.writeLog("listen", 1); - printAllState(); - } - - synchronized public boolean connect(InetSocketAddress ip, ChannelSimulator<?> CHclient) { - logger.writeLog("connecting..", 1); - for (ServerData sd0: serverList){ - // ANY address (0.0.0.0/0.0.0.0) should be considered. - if (sd0.IP.getAddress().isAnyLocalAddress()) { - if (sd0.IP.getPort() != ip.getPort()) continue; - // we have to check, ip is really reachable to sd0 server, - // but this simulator has no notion of host. To distinguish, - // use different port address. - } else if (!sd0.IP.equals(ip)) continue; - - //ChannelSimulator<?> CHserver = new ChannelSimulator<?>(); - ChannelSimulator<?> CHserver = CHclient.newChannel(); - CHserver.setOtherEnd1(CHclient); - CHclient.setOtherEnd1(CHserver); - - sd0.connectedListS.add(CHserver); - sd0.connectedListC.add(CHclient); - sd0.scs.enQ(CHserver); - - logger.writeLog("connected", 1); - //printAllState(); - return true; - } - return false; - } - - /** for DEBUG methods. */ - void printAllState(){ - String log = "NetworkSimulator State:"; - for (ServerData sd: serverList){ - log += "\tSessionManager(ip="+sd.IP.toString()+"): "; - log += channelList(sd.connectedListC); - } - logger.writeLog(log); - } - - private String channelList(LinkedList<ChannelSimulator<?>> list){ - String tmp = ""; - for (ChannelSimulator<?> ch: list){ - tmp += ch.toString()+" "; - } - return "\t"+tmp; - } - - - - public synchronized int nslookup(SocketAddress semaIP) { - Integer ip; - if ((ip=namedb.get(semaIP))==null) { - namedb.put(semaIP, (ip=ipcount++)); - } - return ip; - } - - -} - -class ServerData { - //int virtualIP; - InetSocketAddress IP; - //SelectorSimulator<P> selector; - ServerChannelSimulator<?> scs; - LinkedList<ChannelSimulator<?>> connectedListS; - LinkedList<ChannelSimulator<?>> connectedListC; - - ServerData(InetSocketAddress ip, ServerChannelSimulator<?> _scs){ - IP = ip; - //selector = _selector; - scs = _scs; - connectedListS = new LinkedList<ChannelSimulator<?>>(); - connectedListC = new LinkedList<ChannelSimulator<?>>(); - } -} \ No newline at end of file
--- a/src/rep/channel/NullSelector.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,56 +0,0 @@ -package rep.channel; - -import java.io.IOException; -import java.nio.channels.SelectionKey; -import java.nio.channels.Selector; -import java.nio.channels.spi.SelectorProvider; -import java.util.Set; - -public class NullSelector extends Selector { - - @Override - public void close() throws IOException { - - } - - @Override - public boolean isOpen() { - return false; - } - - @Override - public Set<SelectionKey> keys() { - return null; - } - - @Override - public SelectorProvider provider() { - return null; - } - - @Override - public int select() throws IOException { - return 0; - } - - @Override - public int select(long timeout) throws IOException { - return 0; - } - - @Override - public int selectNow() throws IOException { - return 0; - } - - @Override - public Set<SelectionKey> selectedKeys() { - return null; - } - - @Override - public Selector wakeup() { - return this; - } - -}
--- a/src/rep/channel/REPLogger.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,33 +0,0 @@ -package rep.channel; - -public class REPLogger { - static REPLogger single = new REPLogger(); - - public static REPLogger singleton(){ -// if(single==null){ -// synchronized(REPLogger.class){ -// if(single==null) -// single = new REPLogger(); -// } -// } - return single; - } - protected REPLogger(){ - } - - private int logLevel; - /** simulation log command */ - public void writeLog(String log, int level){ - if ( level<=logLevel ) - System.out.println(Thread.currentThread().getName()+": "+log); - //System.out.flush(); - } - public void writeLog(String log){ - writeLog(log, 0); - } - - public void setLogLevel(int logLevel) { - this.logLevel = logLevel; - } - -}
--- a/src/rep/channel/REPPack.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,14 +0,0 @@ -package rep.channel; - -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.channels.SocketChannel; - -public interface REPPack<P> { - - public ByteBuffer packUConv(P command); - - public P unpackUConv(SocketChannel sc) throws IOException ; - - -} \ No newline at end of file
--- a/src/rep/channel/REPSelectionKey.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,92 +0,0 @@ -package rep.channel; - -import java.io.IOException; -import java.nio.channels.SelectableChannel; -import java.nio.channels.SelectionKey; -import java.nio.channels.Selector; -import java.nio.channels.ServerSocketChannel; -import java.nio.channels.SocketChannel; - - -public class REPSelectionKey<P> extends SelectionKey { - SelectionKey key; - private REPSelector<P> selector; - - public REPSelectionKey() { - - } - - public REPSelectionKey(SelectionKey key,REPSelector<P>s) { - this.key = key; - this.selector = s; - attach(key.attachment()); - } - - @Override - public void cancel() { - key.cancel(); - } - - @Override - public SelectableChannel channel() { - if (REPServerSocketChannel.isSimulation) return key.channel(); - SelectableChannel sc = key.channel(); - SelectableChannel rsc = REPSocketChannel.channels.get(sc); - return rsc; - } - - @SuppressWarnings("unchecked") - public REPSocketChannel<P> channel1() { - assert (!REPServerSocketChannel.isSimulation); - SelectableChannel sc = key.channel(); - REPSocketChannel<P> rsc = (REPSocketChannel<P>) REPSocketChannel.channels.get(sc); - return rsc; - } - - @SuppressWarnings("unchecked") - public REPServerSocketChannel<P> serverSocketChannel() { - assert (!REPServerSocketChannel.isSimulation); - SelectableChannel sc = key.channel(); - REPServerSocketChannel<P> rsc = (REPServerSocketChannel<P>) REPSocketChannel.channels.get(sc); - return rsc; - } - - @Override - public int interestOps() { - return key.interestOps(); - } - - @Override - public SelectionKey interestOps(int ops) { - return key.interestOps(ops); - } - - @Override - public boolean isValid() { - return key.isValid(); - } - - @Override - public int readyOps() { - return key.readyOps(); - } - - @Override - public Selector selector() { - return selector; - } - - @SuppressWarnings("unchecked") - public REPSocketChannel<P> accept(REPPack<P> pack) throws IOException { - assert(!REPServerSocketChannel.isSimulation); - if (!key.isAcceptable()) throw new IOException(); - ServerSocketChannel ssc = (ServerSocketChannel)key.channel(); - if (ssc==null) return null; - SocketChannel ss = (SocketChannel)ssc.accept(); - //System.err.println("Accept in SelectionKey "+ss); - if (ss==null) return null; - return new REPSocketChannel(ss, pack); - } - - -}
--- a/src/rep/channel/REPSelector.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,94 +0,0 @@ -package rep.channel; - -import java.io.IOException; -import java.nio.channels.ClosedChannelException; -import java.nio.channels.SelectableChannel; -import java.nio.channels.SelectionKey; -import java.nio.channels.Selector; -import java.nio.channels.spi.AbstractSelector; -import java.nio.channels.spi.SelectorProvider; -import java.util.HashSet; -import java.util.Iterator; -import java.util.Set; - - -public class REPSelector<P> extends Selector{ - - Selector selector; - - public REPSelector(AbstractSelector selector) { - this.selector = selector; - } - - public static <T>REPSelector<T> create() throws IOException{ - if(REPServerSocketChannel.isSimulation){ - return new SelectorSimulator<T>(); - } - return new REPSelector<T>(SelectorProvider.provider().openSelector()); - } - - @Override - public void close() throws IOException { - selector.close(); - } - - @Override - public boolean isOpen() { - return selector.isOpen(); - } - - @Override - public Set<SelectionKey> keys() { - return selector.keys(); - } - - @Override - public SelectorProvider provider() { - return selector.provider(); - } - - @Override - public int select() throws IOException { - return selector.select(); - } - - @Override - public int select(long timeout) throws IOException { - return selector.select(timeout); - } - - @Override - public int selectNow() throws IOException { - return selector.selectNow(); - } - - @Override - public Set<SelectionKey> selectedKeys() { - return selector.selectedKeys(); - } - - @Override - public Selector wakeup() { - return selector.wakeup(); - } - - public SelectionKey register(SelectableChannel ch, int ops, Object att) throws ClosedChannelException{ - return ch.register(selector, ops, att); - } - - public Set<REPSelectionKey<P>> selectedKeys1() { - Set<SelectionKey> keys = selector.selectedKeys(); - //System.err.println("Selected keys = "+keys); - HashSet<REPSelectionKey<P>> newKeys = new HashSet<REPSelectionKey<P>>(); - - for (Iterator<SelectionKey> it = keys.iterator();it.hasNext(); ) { - SelectionKey k = it.next(); - newKeys.add(new REPSelectionKey<P>(k,this)); - it.remove(); - } - return newKeys; - } - - - -}
--- a/src/rep/channel/REPServerSocket.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,62 +0,0 @@ -package rep.channel; - -import java.io.IOException; -import java.net.InetAddress; -import java.net.InetSocketAddress; -import java.net.ServerSocket; -import java.net.SocketAddress; -import java.nio.channels.ServerSocketChannel; - - -/* こいつはシミュレーションの時しか生成され無いゾ */ -public class REPServerSocket extends ServerSocket{ - ServerChannelSimulator<?> scs; - - public REPServerSocket(ServerChannelSimulator<?> channel) throws IOException { - scs = channel; - } - - public void bind(SocketAddress ip){ - scs.bind((InetSocketAddress)ip); - } - - @Override - public void close(){ - } - @Override - public ServerSocketChannel getChannel(){ - return null; - } - @Override - public InetAddress getInetAddress(){ - return null; - } - @Override - public int getLocalPort(){ - return 0; - } - @Override - public SocketAddress getLocalSocketAddress(){ - return null; - } - @Override - public int getReceiveBufferSize(){ - return 0; - } - @Override - public boolean getReuseAddress(){ - return false; - } - @Override - public int getSoTimeout(){ - return 0; - } - /* - @Override - public (){ - } - @Override - public (){ - }*/ - -}
--- a/src/rep/channel/REPServerSocketChannel.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,134 +0,0 @@ -package rep.channel; - -import java.io.IOException; -import java.net.ServerSocket; -import java.nio.channels.ClosedChannelException; -import java.nio.channels.SelectableChannel; -import java.nio.channels.SelectionKey; -import java.nio.channels.Selector; -import java.nio.channels.ServerSocketChannel; -import java.nio.channels.SocketChannel; -import java.nio.channels.spi.SelectorProvider; - -/* - * シミュレーションでは inheritance のServerChannelSimulator を生成、 - * リアルコミュニケーションでは 自身を生成、内部にもつ ServerSocketChannelを扱う - */ -public class REPServerSocketChannel<P> extends SelectableChannel { - - public static boolean isSimulation=false; - private ServerSocketChannel ssc; - private REPPack<P> packer; - - public REPServerSocketChannel() { - - } - - public static <T> REPServerSocketChannel<T> open(REPPack<T> packer) throws IOException{ - if(isSimulation){ - return new ServerChannelSimulator<T>(); - }else{ - return new REPServerSocketChannel<T>(ServerSocketChannel.open(), packer); - } - } - - public static <T> REPServerSocketChannel<T> open(SelectableChannel c,REPPack<T> packer) throws IOException{ - assert(!isSimulation); - return new REPServerSocketChannel<T>((ServerSocketChannel)c, packer); - } - - public REPServerSocketChannel(ServerSocketChannel open, REPPack<P> packer) { - ssc = open; - this.packer = packer; - REPSocketChannel.addChannel(ssc,this); - } - - public void close1() throws IOException { - REPSocketChannel.removeChannel(ssc); - ssc.close(); - } - - public REPServerSocketChannel(SelectableChannel channel, REPPack<P> packer) { - this.ssc = (ServerSocketChannel)channel; - this.packer = packer; - REPSocketChannel.addChannel(ssc,this); - } - - public REPSocketChannel<P> accept1() throws IOException { - return new REPSocketChannel<P>(ssc.accept(), packer); - } - - public SelectionKey register(REPSelector<P> sel, int ops, Object att) throws ClosedChannelException { - assert(!isSimulation); - if(sel!=null) - return sel.register(ssc, ops, att); - else - return null; - } - - public SocketChannel accept() throws IOException { - return accept1().sc; - } - - - public ServerSocket socket() { - return ssc.socket(); - } - - public SelectableChannel configureBlocking(boolean block) throws IOException - { - ssc.configureBlocking(block); - return this; - } - - @Override - public Object blockingLock() { - // TODO Auto-generated method stub - return null; - } - - @Override - public boolean isBlocking() { - // TODO Auto-generated method stub - return false; - } - - @Override - public boolean isRegistered() { - // TODO Auto-generated method stub - return false; - } - - @Override - public SelectionKey keyFor(Selector sel) { - // TODO Auto-generated method stub - return null; - } - - @Override - public SelectorProvider provider() { - // TODO Auto-generated method stub - return null; - } - - @Override - public SelectionKey register(Selector sel, int ops, Object att) - throws ClosedChannelException { - // TODO Auto-generated method stub - return null; - } - - @Override - public int validOps() { - // TODO Auto-generated method stub - return 0; - } - - @Override - protected void implCloseChannel() throws IOException { - // TODO Auto-generated method stub - - } - - -}
--- a/src/rep/channel/REPSocketChannel.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,181 +0,0 @@ -package rep.channel; - -import java.io.IOException; -import java.net.Socket; -import java.net.SocketAddress; -import java.nio.ByteBuffer; -import java.nio.channels.ClosedChannelException; -import java.nio.channels.SelectableChannel; -import java.nio.channels.SelectionKey; -import java.nio.channels.Selector; -import java.nio.channels.SocketChannel; -import java.nio.channels.spi.AbstractSelector; -import java.nio.channels.spi.SelectorProvider; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -public class REPSocketChannel<P> extends SelectableChannel{ - - public SocketChannel sc; - private REPPack<P> pack; - static public Map<SelectableChannel,SelectableChannel> channels = - Collections.synchronizedMap(new HashMap<SelectableChannel,SelectableChannel>()); - - public REPSocketChannel(SocketChannel channel, REPPack<P> packer) { - sc = channel; - pack = packer; - addChannel(sc,this); - } - - public REPSocketChannel(SelectableChannel channel, REPPack<P> packer) { - sc = (SocketChannel)channel; - pack = packer; - addChannel(sc,this); - } - - public static void addChannel(SelectableChannel sc,SelectableChannel rc) { - channels.put(sc, rc); - } - - public void close1() throws IOException { - removeChannel(sc); - sc.close(); - } - - public static void removeChannel(SelectableChannel sc) throws IOException { - if(channels.containsKey(sc)) channels.remove(sc); - } - - @Override - public Object blockingLock() { - return sc.blockingLock(); - } - - @Override - public SelectableChannel configureBlocking(boolean block) throws IOException { - return sc.configureBlocking(block); - } - - @Override - public boolean isBlocking() { - return sc.isBlocking(); - } - - @Override - public boolean isRegistered() { - return sc.isRegistered(); - } - - @Override - public SelectionKey keyFor(Selector sel) { - return sc.keyFor(sel); - } - - public SelectionKey keyFor(REPSelector<?> sel) { - return sc.keyFor(sel.selector); - } - - public REPSelectionKey<P> keyFor1(REPSelector<P> sel) { - return new REPSelectionKey<P>(sc.keyFor(sel.selector), - new REPSelector<P>((AbstractSelector) sel.selector)); - } - - @Override - public SelectorProvider provider() { - return sc.provider(); - } - - - @Override - public int validOps() { - return sc.validOps(); - } - - @Override - protected void implCloseChannel() throws IOException { - close1(); - } - - - public int read(ByteBuffer header) throws IOException { - return sc.read(header); - } - - public void write(ByteBuffer buffer) throws IOException { - sc.write(buffer); - - } - - public boolean finishConnect() throws IOException { - return sc.finishConnect(); - } - - public Socket socket() { - return sc.socket(); - } - - public P read() throws IOException{ - return pack.unpackUConv(sc); - } - - public boolean write(P p){ - ByteBuffer bb = pack.packUConv(p); - if (bb==null) return true; - try { - while (bb.remaining() > 0 ){ - sc.write(bb); - } - return true; - } catch (IOException e) { - return false; - } - } - - public static <T> REPSocketChannel<T> create(REPPack<T> packer) throws IOException { - if (REPServerSocketChannel.isSimulation) { - return new ChannelSimulator<T>(); - } else { - REPSocketChannel<T> rsc = new REPSocketChannel<T>(SocketChannel.open(), packer); - return rsc; - } - } - - - public boolean connect(SocketAddress semaIP) throws IOException { - return sc.connect(semaIP); - } - - public SelectionKey register(REPSelector<P> sel, int ops, Object att) throws ClosedChannelException { - return sc.register(sel.selector, ops, att); - } - - - - public SelectionKey register1(REPSelector<P> sel, int ops, Object att) - throws ClosedChannelException { - if(sel instanceof REPSelector) { - REPSelector<P> s = (REPSelector<P>)sel; - return sc.register(s.selector, ops,att); - } - return sc.register(sel, ops,att); - } - - @SuppressWarnings("unchecked") - @Override - public SelectionKey register(Selector sel, int ops, Object att) - throws ClosedChannelException { - if(sel instanceof REPSelector) { - REPSelector<P> s = (REPSelector<P>)sel; - return sc.register(s.selector, ops,att); - } - return sc.register(sel, ops,att); - } - - public String getLocalHostName() { - return sc.socket().getLocalAddress().getHostName(); - - } - - -} \ No newline at end of file
--- a/src/rep/channel/SelectionKeySimulator.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,120 +0,0 @@ -package rep.channel; - -import java.io.IOException; -import java.nio.channels.SelectableChannel; -import java.nio.channels.SelectionKey; -import java.nio.channels.Selector; - -public class SelectionKeySimulator<P> extends REPSelectionKey<P>{ - - private int interestOpt; - private SelectableChannel channel; - private int ready; - public Selector selector; - public boolean canceled = false; - - public SelectionKeySimulator(SelectableChannel cs, int opt, Selector _selector) { - channel = cs; - interestOpt = opt; - selector = _selector; - } - public SelectionKeySimulator(SelectionKey k) { - channel = k.channel(); - interestOpt = k.interestOps(); - selector = k.selector(); - attach(k.attachment()); - } - - - public boolean isAble() { - if ( (interestOpt&OP_READ)!=0 && isReadable() ) - return true; - else if( (interestOpt&OP_ACCEPT)!=0 && isAcceptable() ) - return true; - else if( (interestOpt&OP_WRITE)!=0 && isWritable() ) - return true; - else - return false; - } - - public void setFlag() { - ChannelSimulator<?> scs = (ChannelSimulator<?>) channel; - ready = 0; - if(scs.isAcceptable()) ready |= OP_ACCEPT; - if(scs.isReadable()) ready |= OP_READ; - if(scs.isWritable()) ready |= OP_WRITE; - } - - public SelectableChannel channel() { - return channel; - } - - @SuppressWarnings("unchecked") - @Override - public REPSocketChannel<P> channel1() { - return (REPSocketChannel<P>)channel; - } - - @SuppressWarnings("unchecked") - @Override - public REPServerSocketChannel<P> serverSocketChannel() { - return (REPServerSocketChannel<P>)channel; - } - - public SelectableChannel channel(REPPack<P> packer) { - return channel; - } - - @SuppressWarnings("unchecked") - public REPSocketChannel<P> accept(REPPack<P> pack) throws IOException { - assert(channel instanceof ServerChannelSimulator); - ServerChannelSimulator<P> scs = (ServerChannelSimulator<P>) channel; - return scs.accept1(); - } - - @SuppressWarnings("unchecked") - @Override - public void cancel() { - canceled = true; - SelectorSimulator s = (SelectorSimulator)selector; - s.deregister(channel); - } - - @Override - public int interestOps() { - return interestOpt; - } - - @Override - public SelectionKey interestOps(int ops) { - interestOpt = ops; - return this; - } - - @Override - public boolean isValid() { - return (!canceled) && channel.isOpen() && selector.isOpen(); - } - - - @Override - public Selector selector() { - return selector; - } - - @Override - public int readyOps() { - int ops=0; - if ( channel instanceof ServerChannelSimulator ){ - ServerChannelSimulator<?> scs = (ServerChannelSimulator<?>) channel; - ops = ( OP_ACCEPT * (scs.isAcceptable()? 1:0) ); - } else if ( channel instanceof ChannelSimulator ){ - ChannelSimulator<?> scs = (ChannelSimulator<?>) channel; - ops = ( OP_READ * (scs.isReadable()? 1:0) ) - | ( OP_WRITE * (scs.isWritable()? 1:0) ); - // (OP_READ & true) がつかえないらしい. - } - return ops; - } - -}
--- a/src/rep/channel/SelectorSimulator.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,162 +0,0 @@ -package rep.channel; - -import java.io.IOException; -import java.nio.channels.SelectableChannel; -import java.nio.channels.SelectionKey; -import java.nio.channels.Selector; -import java.nio.channels.spi.SelectorProvider; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -public class SelectorSimulator<P> extends REPSelector<P>{ - - // This selector cannot be shared among threads. - - private Map<SelectableChannel, SelectionKeySimulator<P>> keyList; - private Set<SelectionKeySimulator<P>> selectedKeys; - private boolean isOpen=true; - - public SelectorSimulator() { - super(null); - keyList = new HashMap<SelectableChannel,SelectionKeySimulator<P>>(); - } - - public int select() throws IOException { - while(true) { - getSelectedKeys(); - if(selectedKeys.isEmpty()) { - try { - synchronized(this) { - wait(); - } - } catch (InterruptedException e) { - throw new IOException(); - } - } else - break; - } - return selectedKeys.size(); - } - - @Override - public int select(long timeout) throws IOException { - getSelectedKeys(); - if(selectedKeys.isEmpty()) { - try { - synchronized(this) { - wait(timeout); - } - // we cannot know if we time outed or not - getSelectedKeys(); - } catch (InterruptedException e) { - throw new IOException(); - } - } - return selectedKeys.size(); - } - - private void getSelectedKeys() { - selectedKeys = new HashSet<SelectionKeySimulator<P>>(); - for(SelectionKeySimulator<P> key : keyList.values()){ - if(key.isAble()) - selectedKeys.add(new SelectionKeySimulator<P>(key)); - } - } - - @Override - public int selectNow() throws IOException { - getSelectedKeys(); - return selectedKeys.size(); - } - - public SelectionKeySimulator<P> register(SelectableChannel cs, int opt){ - return register(cs, opt, null); - } - public SelectionKeySimulator<P> register(SelectableChannel cs, int opt, Object handler){ - SelectionKeySimulator<P> key = keyList.get(cs); - if (key!=null) { - key.attach(handler); - key.interestOps(opt); - return key; - } - key = new SelectionKeySimulator<P>(cs, opt, this); - key.attach(handler); - keyList.put(cs,key); - return key; - } - - public SelectionKeySimulator<P> deregister(SelectableChannel channel) { - SelectionKeySimulator<P> key = keyList.remove(channel); - return key; - } - - - public SelectionKey getKey(SelectableChannel channel){ - return keyList.get(channel); - } - - @Override - public void close() throws IOException { - isOpen = false; - } - - @Override - public boolean isOpen() { - return isOpen; - } - - @Override - public Set<SelectionKey> keys() { - Set<SelectionKey> newKeys = new HashSet<SelectionKey>(); - for(SelectionKey k: keyList.values()) { - newKeys.add(k); - } - return newKeys; - } - - public Set<REPSelectionKey<P>> keys1() { - // we cannot solve cast, we need the same method again with different types - Set<REPSelectionKey<P>> newKeys = new HashSet<REPSelectionKey<P>>(); - for(SelectionKeySimulator<P> k: keyList.values()) { - newKeys.add(k); - } - return newKeys; - } - - @Override - public SelectorProvider provider() { - // should return NetworkSimulator? - return null; - } - - - @Override - public synchronized Selector wakeup() { - notifyAll(); - return this; - } - - - public Set<REPSelectionKey<P>> selectedKeys1() { - Set<REPSelectionKey<P>> newKeys = new HashSet<REPSelectionKey<P>>(); - for(SelectionKeySimulator<P> k: selectedKeys) { - newKeys.add(k); - } - return newKeys; - } - - /* - * type safe copy of selectedKeys1() - */ - @Override - public Set<SelectionKey> selectedKeys() { - Set<SelectionKey> newKeys = new HashSet<SelectionKey>(); - for(SelectionKeySimulator<P> k: selectedKeys) { - newKeys.add(k); - } - return newKeys; - } - -}
--- a/src/rep/channel/ServerChannelSimulator.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,148 +0,0 @@ -package rep.channel; - -import java.io.IOException; -import java.net.InetSocketAddress; -import java.net.ServerSocket; -import java.nio.channels.ClosedChannelException; -import java.nio.channels.SelectableChannel; -import java.nio.channels.SelectionKey; -import java.nio.channels.Selector; -import java.nio.channels.spi.SelectorProvider; -import java.util.LinkedList; -import java.util.Queue; - -/* シミュレーションの際にコンストラクトされる REPServerSocketChannel の実装 */ -public class ServerChannelSimulator<P>extends REPServerSocketChannel<P> { - protected NetworkSimulator ns; - //public REPServerSocket<REPSocketChannel<P>> socket; - protected InetSocketAddress IP; - protected Queue<ChannelSimulator<P>> acceptQ; - protected Selector selector; - protected boolean isBlocking; - private SelectionKeySimulator<P> key; - - /** Constructors. - * @throws IOException */ - public ServerChannelSimulator() throws IOException { - ns = NetworkSimulator.singleton(); - selector = new NullSelector(); // new Object(); - acceptQ = new LinkedList<ChannelSimulator<P>>(); - } - - public void bind(InetSocketAddress ip){ - IP = ip; - } - - public synchronized REPSocketChannel<P> accept1() throws IOException { - ChannelSimulator<P> channel; - while ( (channel=acceptQ.poll())==null && isBlocking ) { - try { - wait(); - } catch (InterruptedException e) { - throw new IOException(); - } - } - return channel; - } - - protected boolean enQ(ChannelSimulator<?> ch){ - // Don't lock a selector from a locked channel, the selector may - // use channel.isAble() which locks the channel. - @SuppressWarnings("unchecked") - ChannelSimulator<P>ch1 = (ChannelSimulator<P>)ch; - synchronized(this) { - acceptQ.offer(ch1); - notify(); - } - selector.wakeup(); - return true; - } -// -// @SuppressWarnings("unchecked") -// public void enQ(ChannelSimulator<?> hserver) { -// // NetworkSimulator doesn't know P -// ChannelSimulator<P>ch = (ChannelSimulator<P>) hserver; -// enQ(ch); -// } - - public ServerSocket socket() { - try { - return new REPServerSocket(this); - } catch (IOException e) { - e.printStackTrace(); - return null; - } - } - - - - @SuppressWarnings("unchecked") - public SelectionKey register(REPSelector<P> sel, int ops, Object att) throws ClosedChannelException { - selector = sel; - REPSelector<P> selector1 = sel; - ns.listen(IP, this); - key = (SelectionKeySimulator<P>) selector1.register(this, ops, att); - return key; - } - @SuppressWarnings("unchecked") - @Override - public SelectionKey register(Selector sel, int ops, Object att) throws ClosedChannelException { - selector = sel; - REPSelector<P> selector1 = (REPSelector<P>)sel; - ns.listen(IP, this); // bindに移動してもいいよ - key = (SelectionKeySimulator<P>) selector1.register(this, ops, att); - return key; - } - - public synchronized boolean isAcceptable() { - return !acceptQ.isEmpty(); - } - - @Override - public Object blockingLock() { - return selector; - } - - public SelectableChannel configureBlocking(boolean block) throws IOException { - isBlocking = block; - return this; - } - - @Override - public boolean isBlocking() { - return isBlocking; - } - - @Override - public boolean isRegistered() { - // TODO Auto-generated method stub - return false; - } - - @Override - public SelectionKey keyFor(Selector sel) { - // TODO Auto-generated method stub - return null; - } - - @Override - public SelectorProvider provider() { - // TODO Auto-generated method stub - return null; - } - - - @Override - public int validOps() { - // TODO Auto-generated method stub - return 0; - } - - @Override - protected void implCloseChannel() throws IOException { - // TODO Auto-generated method stub - - } - - -}
--- a/src/sample/merge/Merge.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,90 +0,0 @@ -//package sample.merge; -// -//import java.util.LinkedList; -//import java.util.List; -// -//import remoteeditor.network.REP; -// -// -//public class Merge { -// -// public static void main(String[] args) throws Exception{ -// List <Rep_Cmd> userCmdList = new LinkedList<Rep_Cmd>(); -// List <Rep_Cmd> tokenCmdList = new LinkedList<Rep_Cmd>(); -// -// System.out.println("---------- p^[2 ----------"); -// userCmdList.add(new Rep_Cmd(REP.REP_INSERT_CMD, 1, 1, 0, 10, 4, "test")); -// userCmdList.add(new Rep_Cmd(REP.REP_INSERT_CMD, 1, 1, 0, 11, 5, "test2")); -// System.out.println("UserList : " + userCmdList.toString()); -// tokenCmdList.add(new Rep_Cmd(REP.REP_INSERT_CMD, 1, 2, 0, 12, 5, "test3")); -// tokenCmdList.add(new Rep_Cmd(REP.REP_INSERT_CMD, 1, 2, 0, 15, 5, "test4")); -// System.out.println("TokenList : " + tokenCmdList.toString()); -// //new Translate(userCmdList, tokenCmdList); -// System.out.println(); -// System.out.println("NewUserList : " + userCmdList.toString()); -// System.out.println("NewTokenList : " + tokenCmdList.toString()); -// -// System.out.println(""); -// -// System.out.println("---------- p^[2 ----------"); -// userCmdList = new LinkedList<Rep_Cmd>(); -// tokenCmdList = new LinkedList<Rep_Cmd>(); -// userCmdList.add(new Rep_Cmd(REP.REP_INSERT_CMD, 1, 1, 0, 10, 4, "test")); -// System.out.println("UserList : " + userCmdList.toString()); -// tokenCmdList.add(new Rep_Cmd(REP.REP_INSERT_CMD, 1, 2, 0, 5, 5, "test3")); -// tokenCmdList.add(new Rep_Cmd(REP.REP_INSERT_CMD, 1, 2, 0, 15, 5, "test4")); -// System.out.println("TokenList : " + tokenCmdList.toString()); -// //new Translate(userCmdList, tokenCmdList); -// System.out.println(); -// System.out.println("NewUserList : " + userCmdList.toString()); -// System.out.println("NewTokenList : " + tokenCmdList.toString()); -// -// System.out.println(""); -// -// System.out.println("---------- p^[3 ----------"); -// userCmdList = new LinkedList<Rep_Cmd>(); -// tokenCmdList = new LinkedList<Rep_Cmd>(); -// userCmdList.add(new Rep_Cmd(REP.REP_INSERT_CMD, 1, 1, 0, 10, 4, "test")); -// System.out.println("UserList : " + userCmdList.toString()); -// tokenCmdList.add(new Rep_Cmd(REP.REP_INSERT_CMD, 1, 2, 0, 10, 5, "test3")); -// System.out.println("TokenList : " + tokenCmdList.toString()); -// //new Translate(userCmdList, tokenCmdList); -// System.out.println(); -// System.out.println("NewUserList : " + userCmdList.toString()); -// System.out.println("NewTokenList : " + tokenCmdList.toString()); -// -// System.out.println(""); -// -// System.out.println("---------- p^[4 ----------"); -// userCmdList = new LinkedList<Rep_Cmd>(); -// tokenCmdList = new LinkedList<Rep_Cmd>(); -// userCmdList.add(new Rep_Cmd(REP.REP_DELETE_CMD, 1, 1, 0, 10, 4, "test")); -// System.out.println("UserList : " + userCmdList.toString()); -// tokenCmdList.add(new Rep_Cmd(REP.REP_INSERT_CMD, 1, 2, 0, 10, 5, "test3")); -// System.out.println("TokenList : " + tokenCmdList.toString()); -// //new Translate(userCmdList, tokenCmdList); -// System.out.println(); -// System.out.println("NewUserList : " + userCmdList.toString()); -// System.out.println("NewTokenList : " + tokenCmdList.toString()); -// -// System.out.println(""); -// -// System.out.println("---------- p^[5 ----------"); -// userCmdList = new LinkedList<Rep_Cmd>(); -// tokenCmdList = new LinkedList<Rep_Cmd>(); -// userCmdList.add(new Rep_Cmd(REP.REP_DELETE_CMD, 1, 1, 0, 10, 4, "test")); -// System.out.println("UserList : " + userCmdList.toString()); -// tokenCmdList.add(new Rep_Cmd(REP.REP_REPLACE_CMD, 1, 2, 0, 10, 5, "test3")); -// System.out.println("TokenList : " + tokenCmdList.toString()); -// //new Translate(userCmdList, tokenCmdList); -// System.out.println(); -// System.out.println("NewUserList : " + userCmdList.toString()); -// System.out.println("NewTokenList : " + tokenCmdList.toString()); -// -// } -// -// public Merge(){ -// -// } -// -//}
--- a/src/sample/merge/MergeTest.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,18 +0,0 @@ -package sample.merge; -/* -import junit.framework.TestCase; - -public class MergeTest extends TestCase { - - private Merge merge; - - protected void setUp() throws Exception { - merge = new Merge(); - super.setUp(); - } - - public void testAdd() { - assertEquals("hoge", merge.add(1, 1), 2); - } -} -*/ \ No newline at end of file
--- a/src/sample/merge/Merger.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,108 +0,0 @@ -package sample.merge; - -import remoteeditor.command.REPCommand; -import remoteeditor.command.REPCommandEvent; -import remoteeditor.network.*; -import remoteeditor.command.*; - -public class Merger implements REPCommandListener { - //private REPPacketReceive receiver; - private REPPacketSend sender; - private int eid; - private REPCommandListener editor; - -// private RemoteEditor; - private Translater translater; - - public Merger(int _eid, REPCommandListener _editor, REPPacketSend _sender){ - editor = _editor; - sender = _sender; - eid = _eid; - translater = new TranslaterImp1(_eid); - } - - public void CommandReceived(REPCommandEvent event){ - REPCommand cmd = event.getCommand(); - REPCommand[] cmds; - - if ( checkMergingRequirement(cmd) ) { - editor.CommandReceived(new REPCommandEvent(cmd)); - return ; - } - - - if (cmd.eid==eid){ - cmds = translater.catchOwnCommand(cmd); - for (int i=0; i<cmds.length; i++){ - editor.CommandReceived(new REPCommandEvent(cmds[i])); - } - } else { - cmds = translater.transReceiveCmd(cmd); - for (int i=0; i<cmds.length; i++){ - editor.CommandReceived(new REPCommandEvent(cmds[i])); - sender.send(cmd); - } - } - return ; - } - - public void CommandSend(REPCommand cmd){ - REPCommand cmds; - cmd.eid = eid; - cmds = translater.transSendCmd(cmd); - -// for (int i=0; i<cmds.length; i++){ -// sender.send(cmds[i]); -// } - } - - /* */ - private boolean checkMergingRequirement(REPCommand cmd){ - if (cmd.cmd>REP.SMCMD_JOIN) return false; - else if (cmd.cmd==REP.REPCMD_INSERT - || cmd.cmd==REP.REPCMD_DELETE - || cmd.cmd==REP.REPCMD_REPLACE ) - return true; - else - return false; - } - -/* - public void pushRemoteCmd(REPCommand cmd){ - REPCommand[] cmds; - cmds = translater.transReceiveCmd(cmd); - if (cmd==null) return; - - for (int i=0; i<cmds.length; i++){ - sender.send(cmds[i]); - edit(cmds[i]); - } - } - - public void pushEditorCmd(REPCommand cmd){ - cmd = translater.transSendCmd(cmd); - sender.send(cmd); - } -*/ - -} - -/* - - +-----------------+ - pushEditor------> | Merger | <------ pushRemoteCmd - | | -Editor.changeText<------ | +-------+| ------> REPPacketSend - +--+-----+Trans- ++ - ^ | later| - +---->| | - +-------+ - - ------------- - pushEditorCmd --> | | <-- pushRemoteCmd - | Merger | - editor. <-- | | --> REPPacketSend.send - ------------- - - -*/ \ No newline at end of file
--- a/src/sample/merge/PermEnum.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,52 +0,0 @@ -package sample.merge; - -import java.util.Enumeration; - -public class PermEnum implements Enumeration { - private int N; - private int c[], k; - private Object[] objs; - - public PermEnum(Object[] items){ - N = items.length; - c = new int [N+1]; - for(int i = 0; i<=N; i++) c[i] = i; - objs = items; - k = 1; - } - - public boolean hasMoreElements(){ - return (k < N); - } - - public Object nextElement(){ - int i = 0; - if((k & 1) != 0) i = c[k]; - - Object tmp = objs[k]; - objs[k] = objs[i]; - objs[i] = tmp; - - k = 1; - while(c[k] == 0) c[k] = k++; - c[k]--; - return objs; - } - - public static void main(String[] args){ - String[] strs = {"1", "2", "3", "4"}; - //String[] strs = {"a"}; - System.out.println("N="+strs.length); - Enumeration e = new PermEnum(strs); - int count = 0; - while(e.hasMoreElements()) { - String[] a = (String[])e.nextElement(); - System.out.print("{" + a[0]); - for(int i = 1; i<a.length; i++) System.out.print(", "+a[i]); - System.out.println("}"); - count++; - } - System.out. - println("count="+count); - } -}
--- a/src/sample/merge/Rep_Cmd.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,35 +0,0 @@ -package sample.merge; - -public class Rep_Cmd { - public int cmd; - public int sid; - public int eid; - public int seq; - public int len; - public int lineno; - public boolean stat; - - public String string; - private int textsiz; - - public Rep_Cmd(int cmd,int sid,int eid, int seq, int lineno, int textsiz, String string) { - this.cmd = cmd; - this.sid = sid; - this.eid = eid; - this.seq = seq; - this.textsiz = textsiz; - this.lineno = lineno; - this.string = string; - } - - public String toString(){ - /*String[] cmdString = new String[14]; - cmdString[6] = "REP_INSERT_CMD"; - cmdString[9] = "REP_DELETE_CMD"; - cmdString[13] = "REP_REPLACE_CMD";*/ - String repCmdString = new String(cmd + "," + sid + "," + eid + "," + seq + "," + lineno + "," + textsiz + ", stat:" + stat + "," + string); - return repCmdString; - - } - -}
--- a/src/sample/merge/SMRoutingTable.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,34 +0,0 @@ -package sample.merge; - -import java.util.LinkedList; -import java.util.List; - -public class SMRoutingTable { - - private List<TestEditor> editorList = new LinkedList<TestEditor>(); - - public TestEditor getNextEditor(int eid) { - return null ; - } - - public void setNext(int eid) { - - } - - public void addEditor(TestEditor editor) { - editorList.add(editor); - } - - public TestEditor getNextEditor(TestEditor editor) { - int size = editorList.size(); - TestEditor lastEditor = ((LinkedList<TestEditor>) editorList).getLast(); - if(size == 0){ - return null; - }else if(editor == lastEditor){ - return editorList.get(0); - }else{ - return editorList.get(editorList.indexOf(editor)+1); - } - } - -}
--- a/src/sample/merge/TestEditor.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,220 +0,0 @@ -package sample.merge; - -import java.io.IOException; -import java.net.InetSocketAddress; -import java.nio.channels.SocketChannel; -import java.util.Enumeration; -import java.util.LinkedList; -import java.util.List; - -import remoteeditor.command.REPCommand; -import remoteeditor.command.REPCommandEvent; -import remoteeditor.command.REPCommandListener; -import remoteeditor.network.REP; -import remoteeditor.network.REPPacketReceive; -import remoteeditor.network.REPPacketSend; - -public class TestEditor extends Thread implements REPCommandListener, Runnable{ - private SocketChannel sc; - private REPPacketReceive repreceive; - private REPPacketSend repsend; - private String myEditorName; - private int myseq; - private int mysid; - private int myeid; - List <REPCommand> userCmdList = new LinkedList<REPCommand>(); - List <REPCommand> tokenCmdList = new LinkedList<REPCommand>(); - List <REPCommand> userQueue = new LinkedList<REPCommand>(); - List <String> myTextList = new LinkedList<String>(); - Translate trans = new Translate(userCmdList, tokenCmdList); - private String myString = " "; - - List <String> myText = new LinkedList<String>(); - private TestSessionManager sessionManager; - - - public static void main(String[] args){ - TestEditor editorA = new TestEditor("EditorA"); - editorA.init(); - editorA.rep_join(); - editorA.put(); - editorA.select(); - - TestEditor editorB = new TestEditor("EditorB"); - editorB.init(); - editorB.rep_join(); - editorB.select(); - - //editorA.send(REP.REP_INSERT_CMD); - //editorB.send(REP.REP_INSERT_CMD); - //editorA.send(REP.REP_INSERT_CMD); - //editorB.send(REP.REP_INSERT_CMD); - //editorA.send(REP.REP_INSERT_CMD); - //editorB.send(REP.REP_INSERT_CMD); - - Integer[] cmdkind = {REP.REPCMD_INSERT, REP.REPCMD_DELETE, REP.REPCMD_REPLACE}; - Enumeration e = new PermEnum(cmdkind); - while(e.hasMoreElements()){ - Integer[] a = (Integer[])e.nextElement(); -// System.out.print("{" + a[0].intValue()); -// for(int i=1; i<a.length; i++) System.out.print(", "+a[i]); -// System.out.println("}"); - for(int i = 0; i<a.length; i++){ - editorA.send(a[i].intValue()); - } - System.out.println(""); - } - } - - public TestEditor(String str){ - myEditorName = str; - } - - public TestEditor(int sid, int eid, String string) { - mysid = sid; myeid = eid; myEditorName = string; - for(int i = 0; i < 20; i++){ - myTextList.add(" "); - } - } - - public void init(){ - int port = 8765; - String host = "localhost"; - InetSocketAddress addr = new InetSocketAddress(host, port); - try { - sc = SocketChannel.open(); - sc.configureBlocking(true); - sc.connect(addr); - while(!sc.finishConnect()){ - System.out.println("afro"); - } - }catch (IOException e) { - e.printStackTrace(); - } - repreceive = new REPPacketReceive(sc); - repsend = new REPPacketSend(sc); - - } - - public void rep_join(){ - repsend.send(new REPCommand(REP.SMCMD_JOIN, 0, 0, myseq, 0, 0, "")); myseq++; - REPCommand command = repreceive.unpackUConv(); - myeid = command.eid; - trans.setMyEID(myeid); - } - - public void put(){ - repsend.send(new REPCommand(REP.SMCMD_PUT, 0, myeid, myseq, 0, 4, "afro")); myseq++; - REPCommand command = repreceive.unpackUConv(); - mysid = command.sid; - } - - public void select(){ - mysid = 1; - repsend.send(new REPCommand(REP.SMCMD_SELECT, mysid, myeid, myseq, 0, 0, "")); myseq++; - REPCommand command = repreceive.unpackUConv(); - repreceive.addCommandListener(this); - } - - public void send(int cmdKind){ - REPCommand sendCommand = new REPCommand(cmdKind, mysid, myeid, myseq, 10, myEditorName.length(), myEditorName); - myString = sendCommand.string + myString; - //System.out.println(myEditorName + " : " + myString); - trans.addUserList(sendCommand); - repsend.send(sendCommand); - myseq++; - } - - public void send(REPCommand command){ - System.out.println(command.toString()); - //sessionManager.send(command, myeid); - sessionManager.send(command, this); - } - - public void CommandReceived(REPCommandEvent event) { - trans.addTokenList(event.getCommand()); - //System.out.println(myEditorName + " : userCmdList : " + userCmdList.toString()); - //System.out.println(myEditorName + " : tokenCmdList : " + tokenCmdList.toString()); - trans.merge(); - REPCommand tokenCmd = null; - if (tokenCmdList.size() != 0) { - tokenCmd = tokenCmdList.get(0); - repsend.send(tokenCmd); - if(tokenCmd.lineno == 10){ - myString = tokenCmd.string + myString; - }else if(tokenCmd.lineno == 11){ - myString = myString + tokenCmd.string; - } - tokenCmdList.remove(0); - } - System.out.println(myEditorName + " : " + myString); - } - -/* - public void run() { - Integer[] cmdkind = {REP.REP_INSERT_CMD, REP.REP_DELETE_CMD, REP.REP_REPLACE_CMD}; - //Integer[] cmdkind = {REP.REP_INSERT_CMD, REP.REP_DELETE_CMD}; - Enumeration e = new PermEnum(cmdkind); - //TestEditor editorA = new TestEditor("EditorA"); - while(e.hasMoreElements()){ - Integer[] a = (Integer[])e.nextElement(); - for(int i = 0; i<a.length; i++){ - REPCommand command = new REPCommand(a[i].intValue(), 1, 1, myseq, 10, 1, myEditorName + myseq); myseq++; - send(command); - } - } - } -*/ - - public void run() { - while(true){ - if(userQueue.size() > 0){ - REPCommand command = userQueue.get(0); - myTextList.add(command.lineno, command.string); - if(command.string == "exit") { - System.out.println(myTextList.toString()); - break; - } - send(command); - userCmdList.add(command); - userQueue.remove(0); - } - } - } - - public int getEID() { - return myeid; - } - - public void changeText(REPCommand command){ - switch(command.cmd){ - case REP.REPCMD_INSERT: - myText.add(command.lineno, command.string); - break; - } - } - - public void setSessionManager(TestSessionManager sessionManager) { - this.sessionManager = sessionManager; - } - - public String getEditorName() { - return myEditorName; - } - - public synchronized void changeText(int kind, int lineno, String string) { - userQueue.add(new REPCommand(REP.REPCMD_INSERT, mysid, myeid, getMyseq(), lineno, string.length(), string)); - } - - private int getMyseq() { - myseq++; - return myseq; - } - - public void CommandReceived(REPCommand command) { - if(command.eid != myeid){ - myTextList.add(command.string); - } - } - -}
--- a/src/sample/merge/TestLoop.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,9 +0,0 @@ -package sample.merge; - -public class TestLoop { - public static void main(String[] args){ - while(true){ - System.out.println("test"); - } - } -}
--- a/src/sample/merge/TestSessionManager.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,96 +0,0 @@ -package sample.merge; - -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; - -import remoteeditor.command.REPCommand; - -public class TestSessionManager extends Thread{ - List <REPCommandPlus> list = new LinkedList<REPCommandPlus>(); - List<REPCommandPlus> commandList = Collections.synchronizedList(list); - List <TestEditor> editorList = new LinkedList<TestEditor>(); - SMRoutingTable table = new SMRoutingTable(); - private boolean go = true; - //private final int LIST_GET = 1; - //private final int LIST_ADD = 2; - //private final int LIST_REMOVE =3; - - public void send(REPCommand command, TestEditor editor){ - REPCommandPlus commandp = new REPCommandPlus(command, editor); - commandList.add(commandp); - //listOperation(LIST_ADD, commandp); - } - - public void run() { - while(go){ - if(commandList.size() > 0){ - //int eid = commandList.get(0).getEID(); - TestEditor editor = commandList.get(0).getEditor(); - //TestEditor editor = listOperation(LIST_GET, null).getEditor(); - TestEditor nextEditor = table.getNextEditor(editor); - if (nextEditor != null) nextEditor.CommandReceived(commandList.get(0).getCommand()); - //if(nextEditor !=null) nextEditor.CommandReceived(listOperation(LIST_GET, null).getCommand()); - commandList.remove(0); - //listOperation(LIST_REMOVE, null); - } - } - } - - public void addEditor(TestEditor editor) { - editorList.add(editor); - table.addEditor(editor); - } - - public void setRun(boolean b) { - go = b; - } - -// public synchronized REPCommandPlus listOperation(int operation, REPCommandPlus commandp2){ -// REPCommandPlus editor2 = null; -// switch(operation){ -// case LIST_GET: -// editor2 = commandList.get(0); -// break; -// case LIST_ADD: -// commandList.add(commandp2); -// break; -// case LIST_REMOVE: -// commandList.remove(0); -// break; -// default: -// System.out.println("Select GET, ADD, REMOVE"); -// } -// return editor2; -// } -} - -class REPCommandPlus{ - private REPCommand command; - private int EID; - private TestEditor editor; - - public REPCommandPlus(REPCommand command, int eid) { - this.command = command; - this.EID = eid; - } - - public REPCommandPlus(REPCommand command2, TestEditor editor) { - this.command = command2; - this.EID = editor.getEID(); - this.editor = editor; - } - - public int getEID(){ - return EID; - } - - public REPCommand getCommand(){ - return command; - } - - public TestEditor getEditor(){ - return editor; - } -} -
--- a/src/sample/merge/TestTranslate.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,52 +0,0 @@ -package sample.merge; - -import java.util.LinkedList; -import java.util.List; - -import remoteeditor.command.REPCommand; -import remoteeditor.network.REP; - -public class TestTranslate { - public static void main(String[] args){ - - List <REPCommand> userCmdListA = new LinkedList<REPCommand>(); - List <REPCommand> tokenCmdListA = new LinkedList<REPCommand>(); - //tokenCmdList.add(new REPCommand(0, 0, 0, 0, 0, 0, null)); - //userCmdList.add(new REPCommand(0, 0, 0, 0, 0, 0, null)); - Translate trans = new Translate(userCmdListA, tokenCmdListA); - trans.myeid = 1; - int eidA = 1; - int lineno = 10; - int sid = 1; - REPCommand repcmdA = new REPCommand(REP.REPCMD_INSERT, sid , eidA, 1, lineno, 3, "AAA"); - int eidB = 2; - REPCommand repcmdB = new REPCommand(REP.REPCMD_INSERT, sid, eidB, 1, lineno, 3, "BBB"); - - trans.addUserList(repcmdA); - trans.addUserList(repcmdA); - trans.addTokenList(repcmdB); - - //trans.merge(); - - System.out.println("UserListA" + userCmdListA.toString()); - System.out.println("TokenListA" + tokenCmdListA.toString()); - - System.out.println("---------- merge ----------"); - trans.merge(); - System.out.println("UserListA" + userCmdListA.toString()); - System.out.println("TokenListA" + tokenCmdListA.toString() + "\n"); - - System.out.println("---------- return UserList ----------"); - trans.addTokenList(repcmdA); - System.out.println("UserListA" + userCmdListA.toString()); - System.out.println("TokenListA" + tokenCmdListA.toString()); - - System.out.println("---------- merge ----------"); - trans.merge(); - System.out.println("UserListA" + userCmdListA.toString()); - System.out.println("TokenListA" + tokenCmdListA.toString()); - - - - } -}
--- a/src/sample/merge/TestTranslate2.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,44 +0,0 @@ -package sample.merge; - -import java.util.Enumeration; - -import remoteeditor.command.REPCommand; -import remoteeditor.network.REP; - -public class TestTranslate2 { - public static void main(String[] args){ - TestEditor editorA = new TestEditor(1, 1, "EditorA"); - TestEditor editorB = new TestEditor(1, 2, "EditorB"); - TestSessionManager sessionManager = new TestSessionManager(); - - sessionManager.addEditor(editorA); - sessionManager.addEditor(editorB); - sessionManager.start(); - - editorA.setSessionManager(sessionManager); - editorB.setSessionManager(sessionManager); - - editorA.start(); - editorB.start(); - - /* test */ - int lineno = 10; - editorA.changeText(REP.REPCMD_INSERT, lineno, "testA"); - editorA.changeText(REP.REPCMD_INSERT, lineno, "testA2"); - - editorB.changeText(REP.REPCMD_INSERT, lineno, "testB"); - editorB.changeText(REP.REPCMD_INSERT, lineno, "testB2"); - - editorA.changeText(REP.REPCMD_INSERT, lineno, "exit"); - editorB.changeText(REP.REPCMD_INSERT, lineno, "exit"); - - try { - editorA.join(); // Thread.join() : ̃XbhÎҋ@܂B(REPjoinƂ͕) - editorB.join(); // ̃XbhÎҋ@܂B - } catch (InterruptedException e) { - e.printStackTrace(); - } - sessionManager.setRun(false); - - } -}
--- a/src/sample/merge/TestTranslate3.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -package sample.merge; - -public class TestTranslate3 { - public static void main(String[] args){ - EditorTmp editorA = new EditorTmp("editorA"); - Thread thA = new Thread(editorA); - thA.start(); - - EditorTmp editorB = new EditorTmp("editorB"); - Thread thB = new Thread(editorB); - thB.start(); - } -} - -class EditorTmp implements Runnable{ - private String editorName; - - public EditorTmp(String string) { - editorName = string; - } - - public void run() { - System.out.println(editorName); - } -} \ No newline at end of file
--- a/src/sample/merge/Translate.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,150 +0,0 @@ -package sample.merge; - -import java.util.Iterator; -import java.util.List; - -import remoteeditor.command.REPCommand; -import remoteeditor.network.REP; - -public class Translate { - List <REPCommand> userList; - List <REPCommand> tokenList; - private boolean REP_IGNORE = true; - public int myeid; - - public Translate(){ - - } - - //public Translate(List<REPCommand> userList, List<REPCommand> tokenList){ - // this.userList = userList; - // this.tokenList = tokenList; - //merge(); - //} - - - - public Translate(List<REPCommand> userCmdList, List<REPCommand> tokenCmdList) { - this.userList = userCmdList; - this.tokenList = tokenCmdList; - } - - public void addUserList(REPCommand usercmd){ - userList.add(usercmd); - } - public void addTokenList(REPCommand tokencmd){ - if(tokencmd.eid == myeid){ - tokenList.clear(); - userList.remove(0); - }else{ - tokenList.add(tokencmd); - } - } - - void merge(){ - REPCommand h_pricmd; - REPCommand l_pricmd; -// Iterator userListIterator = userList.iterator(); -// while(userListIterator.hasNext()){ - for(REPCommand userCmd:userList){ - //Iterator tokenListIterator = tokenList.iterator(); - //Rep_Cmd userCmd = (Rep_Cmd)userListIterator.next(); - if(userCmd.stat) continue; /* 폜(\)REPR}h̔r͖ */ - //while(tokenListIterator.hasNext()){ - for(REPCommand tokenCmd:tokenList){ -// Rep_Cmd tokenCmd =(Rep_Cmd)tokenListIterator.next(); - if(tokenCmd.eid == myeid) { - if(tokenCmd.seq == userCmd.seq){ - tokenCmd.stat = REP_IGNORE; - userCmd.stat = REP_IGNORE; - } - } - if(tokenCmd.stat == REP_IGNORE) continue; /* 폜(\)REPR}h̔r͖ */ - if(userCmd.stat == REP_IGNORE) break; - if(userCmd.lineno < tokenCmd.lineno) { /* UsersLineNumber < TokensLineNumber */ - if(userCmd.cmd == REP.REPCMD_INSERT){ - tokenCmd.lineno++; - }else if(userCmd.cmd == REP.REPCMD_DELETE){ - tokenCmd.lineno--; - } - }else if(userCmd.lineno > tokenCmd.lineno){ /* UsersLineNumber > TokensLineNumber */ - if(tokenCmd.cmd == REP.REPCMD_INSERT){ - userCmd.lineno++; - }else if(tokenCmd.cmd == REP.REPCMD_DELETE){ - userCmd.lineno--; - } - }else if(userCmd.lineno == tokenCmd.lineno){ /* UsersLineNumber == TokensLineNumber */ - /* - * sԍdȂREPR}h̋N̂ŁA - * ǂ炩ȂƂȂB - * uid D(h_pricmd)A - * uid 傫(l_pricmd)ύXB - */ - if(userCmd.eid < tokenCmd.eid){ - h_pricmd = userCmd; - l_pricmd = tokenCmd; - }else { - h_pricmd = tokenCmd; - l_pricmd = userCmd; - } - if(h_pricmd.cmd == REP.REPCMD_INSERT){ - l_pricmd.lineno++; - }else if(l_pricmd.cmd == REP.REPCMD_REPLACE){ - if(l_pricmd.cmd == REP.REPCMD_INSERT){ - /* h_pricmd D悳,l_pricmd ͍폜() */ - l_pricmd.stat = REP_IGNORE; - }else if(l_pricmd.cmd == REP.REPCMD_DELETE){ - /* - * l_pricmd ł͂łdeleteĂ̂ŁA - * h_pricmd REP_REPLACE_CMD -> REP_INSERT_CMD ֕ύXB - */ - h_pricmd.cmd = REP.REPCMD_INSERT; - l_pricmd.stat = REP_IGNORE; - } - }else if(h_pricmd.cmd == REP.REPCMD_DELETE){ - if (l_pricmd.cmd == REP.REPCMD_INSERT) { - h_pricmd.lineno++; - } else if(l_pricmd.cmd == REP.REPCMD_REPLACE){ - /* - * h_pricmd ł͂łdeleteĂ̂ŁA - * l_pricmd REP_REPLACE_CMD -> REP_INSERT_CMD ֕ύXB - */ - l_pricmd.cmd = REP.REPCMD_INSERT; - h_pricmd.stat= REP_IGNORE; - } else { /* l_pricmd->cmd == REP_DELETE_LINE_CMD */ - /* - * ƍ폜sdȂ̂ŁA - * ̃R}hɂB - * ł͂łɂ̍s͍폜ĂB - */ - h_pricmd.stat = REP_IGNORE; - l_pricmd.stat = REP_IGNORE; - break; - } - } - } - } - } -// Iterator userListIterator = userList.iterator(); -// while(userListIterator.hasNext()){ -// //Iterator tokenListIterator = tokenList.iterator(); -// REPCommand userCmd = (REPCommand)userListIterator.next(); -// if(userCmd.stat == REP_IGNORE){ -// userListIterator.remove(); -// } -// } -// -// Iterator tokenListIterator = tokenList.iterator(); -// while(tokenListIterator.hasNext()){ -// REPCommand tokenCmd = (REPCommand)tokenListIterator.next(); -// if(tokenCmd.stat == REP_IGNORE){ -// tokenListIterator.remove(); -// } -// } - } - - public void setMyEID(int myeid2) { - // TODO Auto-generated method stub - myeid = myeid2; - } -}
--- a/src/sample/merge/Translater.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,33 +0,0 @@ -package sample.merge; - -import remoteeditor.command.REPCommand; - -public interface Translater { - - /** - * Translate command When the editor send REPCommand to remote editor. - * @param command which the editor want to send. - * @return translated command which should be sent by the editor. - */ - abstract public REPCommand transSendCmd(REPCommand cmd); - - /** - * Inform translater about that the editor receive own command which it sent. - * but in this case, you can use also transReceiveCmd() - * @param command which the editor sent. - */ - abstract public REPCommand[] catchOwnCommand(REPCommand cmd); - - /** - * Translate Command cmd that was received from SeMa. - * @param cmd the command to be translated. - * @return translated command. - */ - abstract public REPCommand[] transReceiveCmd(REPCommand cmd); - - /** - * set the editor's id. - * @param editor's id. - */ - abstract public void setEid(int _eid); -}
--- a/src/sample/merge/TranslaterImp0.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,173 +0,0 @@ -package sample.merge; - -import java.util.LinkedList; - -import remoteeditor.command.REPCommand; -import remoteeditor.network.REP; - -/* 最初に書いたマージャ */ -public class TranslaterImp0{ - //List <REPCommand> userList; - //List <REPCommand> tokenList; - private LinkedList<REPCommand> cmdList; - public int eid; - - public TranslaterImp0(int _eid){ - eid = _eid; - cmdList = new LinkedList<REPCommand>(); - } - - /** - * Translate cmd When the editor send REPCommand. - * but now, Only adding cmd to the queue is available. - * @param cmd - * @return translated command. - */ - public REPCommand transSendCmd(REPCommand cmd){ - cmdList.add(cmd); - return cmd; - } - /** - * Dequeue command cmd that was returned. - * @param cmd - */ - public REPCommand[] catchOwnCommand(REPCommand cmd){ - // ringである以上、戻ってきたコマンドは確実にキューの先頭にある事を期待している - REPCommand cmd0 = cmdList.poll(); - assert cmd.eid==eid; - assert cmd0.seq!=cmd.seq; - // とりあえず今のところは何もしない事になっている - return null; - } - /** - * Translate Command cmd that was received from SeMa. - * @param cmd the command to be translated. - * @return translated commannd. - */ - public REPCommand[] transReceiveCmd(REPCommand cmd){ - REPCommand cmds[]; - if (cmd.eid==eid){ - return catchOwnCommand(cmd); - } - // 拡張for構文、順序は保証されるの? 保証されないとダメ。 - for(REPCommand cmd0: cmdList){ - if (cmd0.lineno>cmd.lineno) continue; - else if (cmd0.lineno==cmd.lineno){ - // 既に送ったCommandと行番号が重なる限り、繰り返し変換する - cmd = translate(cmd, cmd0); - } - // ずれたlinenoを合わせる - else if (cmd0.cmd==REP.REPCMD_INSERT) cmd.lineno++; - else if (cmd0.cmd==REP.REPCMD_DELETE) cmd.lineno--; - } - cmds = new REPCommand[1]; - cmds[0] = cmd; - return cmds; - } - - /* - * - * 比較時に行番号が重なったときの処理は以下の表。 - * なるべくテキストが残るようにしている。 - * 0 -- なにもしない - * +1 -- 行番号を +1 - * INS -- コマンド id を 'REPCMD_INSERT' にする。 - * NOP -- コマンドをNOP(何もしない)にする。 - * ?* -- TOKENがMasterを通っていなければ - * REMOTE - * | i | r | d - * ---|------------------ - * U i | +1* | +1 | +1 - * S ---|------------------ - * E r | 0 | NOP*| NOP - * R ---|------------------ - * d | 0 | INS | NOP - * - * - */ - private REPCommand translate(REPCommand Rcmd, REPCommand Lcmd){ - assert Rcmd.lineno==Lcmd.lineno; - int Lird = Lcmd.cmd; - int Rird = Rcmd.cmd; - - if (Lird==REP.REPCMD_INSERT){ - if (Rird==REP.REPCMD_INSERT){ - if (!Rcmd.throughMaster) Rcmd.lineno++; - }else if (Rird==REP.REPCMD_REPLACE){ - Rcmd.lineno++; - }else if (Rird==REP.REPCMD_DELETE){ - Rcmd.lineno++; - } - }else if (Lird==REP.REPCMD_REPLACE){ - if (Rird==REP.REPCMD_INSERT){ - }else if (Rird==REP.REPCMD_REPLACE){ - if (!Rcmd.throughMaster) Rcmd.cmd=REP.REPCMD_NOP; - }else if (Rird==REP.REPCMD_DELETE){ - Rcmd.cmd = REP.REPCMD_NOP; - } - }else if (Lird==REP.REPCMD_DELETE){ - if (Rird==REP.REPCMD_INSERT){ - }else if (Rird==REP.REPCMD_REPLACE){ - Rcmd.cmd = REP.REPCMD_INSERT; - }else if (Rird==REP.REPCMD_DELETE){ - Rcmd.cmd = REP.REPCMD_NOP; - } - } - - return Rcmd; - -/* - // あとで整形しよう - if (Lcmd.cmd==REP.REPCMD_INSERT && Rcmd.cmd==REP.REPCMD_INSERT){ - if (!Rcmd.throughMaster) Rcmd.lineno++; - } - if (Lcmd.cmd==REP.REPCMD_DELETE && Rcmd.cmd==REP.REPCMD_DELETE){ - Rcmd.cmd = REP.REPCMD_NOP; - } - if (Lcmd.cmd==REP.REPCMD_REPLACE && Rcmd.cmd==REP.REPCMD_REPLACE){ - if (!Rcmd.throughMaster) Rcmd.cmd=REP.REPCMD_NOP; - } - - if (Lcmd.cmd==REP.REPCMD_INSERT && Rcmd.cmd==REP.REPCMD_DELETE){ - Rcmd.lineno++; - } - if (Lcmd.cmd==REP.REPCMD_DELETE && Rcmd.cmd==REP.REPCMD_INSERT){ - } - - if (Lcmd.cmd==REP.REPCMD_REPLACE && Rcmd.cmd==REP.REPCMD_DELETE){ - Rcmd.cmd = REP.REPCMD_NOP; - } - if (Lcmd.cmd==REP.REPCMD_DELETE && Rcmd.cmd==REP.REPCMD_REPLACE){ - Rcmd.cmd = REP.REPCMD_INSERT; - } - - if (Lcmd.cmd==REP.REPCMD_INSERT && Rcmd.cmd==REP.REPCMD_REPLACE){ - Rcmd.lineno++; - } - if (Lcmd.cmd==REP.REPCMD_REPLACE && Rcmd.cmd==REP.REPCMD_INSERT){ - } - return Rcmd; -*/ - } - - public void setEid(int _eid){ - eid = _eid; - } - - /* - old version ( not ring type) - 0 -- なにもしない - +1 -- 行番号を +1 - i -- コマンド id を 'i' にする。 - X -- コマンドを削除(無視に)する。 - - USER - | i | r | d - ---|-------------------- - T i | 0¥+1 | 0¥+1 | 0¥+1 - O ---|-------------------- - K r | +1¥0 | 0¥X | i¥X - E ---|-------------------- - N d | +1¥0 | X¥i | X¥X - */ -}
--- a/src/sample/merge/TranslaterImp1.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,295 +0,0 @@ -package sample.merge; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Comparator; -import java.util.LinkedList; -import java.util.List; -import java.util.Stack; -import java.util.TreeSet; - -import remoteeditor.command.REPCommand; -import remoteeditor.network.REP; - -public class TranslaterImp1 implements Translater{ - //List <REPCommand> userList; - //List <REPCommand> tokenList; - private LinkedList<REPCommand> sentCmds; - //private LinkedList<REPCommand> unMergedCmds; - private Stack<REPCommand> unMergedCmds; - public int eid; - private int seq; - private LinkedList<REPCommand> undoReplaceList; - private LinkedList<REPCommand> sentMergedList; - private int interruptNumber; - private LinkedList<REPCommand> mergeAgainList; - - public TranslaterImp1(int _eid){ - eid = _eid; - sentCmds = new LinkedList<REPCommand>(); - unMergedCmds = new Stack<REPCommand>(); - undoReplaceList = new LinkedList<REPCommand>(); - mergeAgainList = new LinkedList<REPCommand>(); - sentMergedList = new LinkedList<REPCommand>(); - } - - /** - * Translate cmd When the editor send REPCommand. - * but now, Only adding cmd to the queue is available. - * @param cmd - * @return translated command. - */ - public REPCommand transSendCmd(REPCommand cmd){ - setCmdState(cmd); - sentCmds.add(cmd); - unMergedCmds.push(cmd); - - //マージ中にユーザから割り込みがあった場合 - if(sentMergedList.size() > 0){ - mergeAgainList.add(cmd); - } - - return cmd; - } - /** - * Dequeue command cmd that was returned. - * @param cmd - */ - synchronized public REPCommand[] catchOwnCommand(REPCommand cmd){ - ArrayList<REPCommand> returnCmds = new ArrayList<REPCommand>(); - ArrayList<REPCommand> cmds = new ArrayList<REPCommand>(); - // ringである以上、戻ってきたコマンドは確実にキューsentCmdsの先頭にある事を期待している - REPCommand tmp = sentCmds.poll(); - assert tmp.seq==cmd.seq; - assert cmd.eid==eid; - - //スタック上にあるコマンドを全部undoコマンドにする - while ( !unMergedCmds.isEmpty() ){ - REPCommand cmd0 = unMergedCmds.pop(); - returnCmds.add( createUndo(cmd0) ); - cmds.add(cmd0); - } - - /* 必要な分だけソートして返却用のリストに追加 */ - //if (cmds.size()==0) return null; - returnCmds.addAll( sortCmds(cmds) ); - - /* 残ったコマンドも再び実行させるが、まだマージされてないのでunMergedにも入れる */ - for(int i=0; i<cmds.size(); i++){ - returnCmds.add( cmds.get(i)); - unMergedCmds.push( cmds.get(i)); - } - - //マージ中のエディタからの割り込み検知に使う - sentMergedList.addAll(returnCmds); - - return returnCmds.toArray(new REPCommand[0]); - } - - private REPCommand createUndo(REPCommand cmd){ - //REPCommand retCmd = cmd.clone(); - String str = new String(cmd.string); - REPCommand retCmd = new REPCommand(cmd.cmd, cmd.sid, cmd.eid, cmd.seq, cmd.lineno, str.length(), str); - - retCmd.eid = REP.MERGE_EID; - - if (cmd.cmd==REP.REPCMD_INSERT) retCmd.cmd=REP.REPCMD_DELETE; - else if (cmd.cmd==REP.REPCMD_DELETE) retCmd.cmd=REP.REPCMD_INSERT; - else if (cmd.cmd == REP.REPCMD_REPLACE) retCmd = createUndoReplace(retCmd); - return retCmd; - } - - private REPCommand createUndoReplace(REPCommand cmd) { - for(REPCommand command : undoReplaceList){ - if(command.eid == cmd.eid && command.seq == cmd.seq){ - REPCommand tmp = new REPCommand(command); - tmp.setCMD(REP.REPCMD_REPLACE); - return tmp; - } - } - System.out.println(undoReplaceList); - System.out.println(cmd); - System.out.println(); - return null; - } - - class REPCommandComparator implements Comparator<REPCommand>{ - - public int compare(REPCommand o1, REPCommand o2) { - - if ( o2.lineno > o1.lineno ) return 1; - else if ( o2.lineno < o1.lineno - || o2.eid > o1.eid ) - /* deleteとinsertの場合などはeidによらず、順序を強制する必要があるかも */ - return -1; - - return 1; - } - - } - - private Collection<REPCommand> sortCmds(ArrayList<REPCommand> cmds) { - TreeSet<REPCommand> sortedCmds1 = new TreeSet<REPCommand>(new REPCommandComparator()); - int top; - int prevEid=-1; - int i,j; - - while ( -1 != (top=getPrecedence(cmds, prevEid+1)) ){ - //while ( -1 != (top=getPrecedence(cmds, prevEid)) ){ - REPCommand tmp = cmds.remove(top); - sortedCmds1.add(tmp); - prevEid = tmp.eid; - } - - if(false){ - ArrayList<REPCommand> sortedCmds = new ArrayList<REPCommand>(); - /* lineno の大きい順にソート */ - for (i=0; i<sortedCmds.size(); i++){ - int k=i; - for (j=i+1; j<sortedCmds.size(); j++){ - if ( sortedCmds.get(k).lineno > sortedCmds.get(j).lineno ) continue; - else if ( sortedCmds.get(k).lineno < sortedCmds.get(j).lineno - || sortedCmds.get(k).eid > sortedCmds.get(j).eid ) - /* deleteとinsertの場合などはeidによらず、順序を強制する必要があるかも */ - k=j; - } - REPCommand tmp = sortedCmds.get(i); - sortedCmds.set(i, sortedCmds.get(k)); - sortedCmds.set(k, tmp); - } - } - return sortedCmds1; - } - - /* search cmd. ordering by min EID that is lower lowEid and min SEQ. */ - private int getPrecedence(ArrayList<REPCommand> cmds, int lowEid) { - int cEid, cSeq; - cEid=cSeq=Integer.MAX_VALUE; - int ret=-1; - for (int i=0; i<cmds.size(); i++){ - REPCommand tmp = cmds.get(i); - if ( tmp.eid<lowEid ) continue; - else if ( tmp.eid>cEid ) continue; - else if ( tmp.eid==cEid ) { - if ( tmp.seq>cSeq ) continue; - cSeq=tmp.seq; - ret = i; - } else { /* tmp.eid<cEid */ - cEid = tmp.eid; - cSeq = tmp.seq; - ret = i; - } - //if ( cEid>cmds.get(i) && cmds.get(i)>lowEid ) - } - return ret; - } - - /** - * Translate Command cmd that was received from SeMa. - * @param cmd the command to be translated. - * @return translated commannd. - */ - synchronized public REPCommand[] transReceiveCmd(REPCommand cmd){ - int i=0; - REPCommand cmds[]; - assert (cmd.eid != eid); -// if (cmd.eid==eid){ -// return catchOwnCommand(cmd); -// } - - for (REPCommand cmd0 : unMergedCmds){ - if (cmd0.eid==cmd.eid) i++; - } - - if ( sentCmds.size()<i ){ - //自分のエディタでされた編集コマンドより他のエディタでの編集コマンドのほうが多かった場合NOPを挿入 - //自分のコマンドがないとマージできないのでNOPを挿入 - cmds = new REPCommand[2]; - String str = "NO OPERATION"; - cmds[0] = setCmdState( new REPCommand(REP.REPCMD_NOP, 0, eid, 0, 0, str.length(), str) ); - cmds[1] = cmd; - //unMergedCmds.push(cmds[0]); - unMergedCmds.push(cmd); - sentCmds.add(cmds[0]); - return cmds; - } - - unMergedCmds.push(cmd); /* but.. */ - cmds = new REPCommand[1]; - cmds[0] = cmd; - return cmds; - } - - private REPCommand setCmdState(REPCommand cmd){ - cmd.seq = seq++; - cmd.eid = eid; - return cmd; - } - public void setEid(int _eid){ - eid = _eid; - } - - public Stack<REPCommand> getList() { - // TODO Auto-generated method stub - return unMergedCmds; - } - - public LinkedList<REPCommand> getSentCmds() { - // TODO Auto-generated method stub - return sentCmds; - } - - public void setUndoCommand(REPCommand command) { - // TODO Auto-generated method stub - undoReplaceList.add(command); - } - - public boolean checkMergeConflict(REPCommand command) { - sentMergedList.remove(); - - if(mergeAgainList.size() > 0){ - mergeAgainList.add(command); - if(sentMergedList.size() == 0){ - return true; - } - } - - return false; - } - - public LinkedList<REPCommand> getMergeAgain() { - LinkedList<REPCommand> returnCommand = new LinkedList<REPCommand>(); - for(int i = 0; i < mergeAgainList.size(); i++){ - //eid = REP.MEGE_EID - returnCommand.add(createUndo(mergeAgainList.get(mergeAgainList.size() - i -1))); - } - for(REPCommand command : mergeAgainList){ - if(command.eid == REP.MERGE_EID){ - returnCommand.add(command); - } - } - for(REPCommand command : mergeAgainList){ - if(command.eid == eid){ - command.eid = REP.MERGE_EID; - returnCommand.add(command); - } - } - mergeAgainList.clear(); - sentMergedList = returnCommand; - return returnCommand; - } - - public boolean isFinished() { - if(unMergedCmds.size() > 0) return false; - if(sentMergedList.size() > 0) return false; - if(sentCmds.size() > 0) return false; - return true; - } - - public boolean isMerging() { - // TODO Auto-generated method stub - if(sentMergedList.size() > 0) return true; - return false; - } - -}
--- a/src/sample/pack/PackTest.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,92 +0,0 @@ -package sample.pack; - - -import java.nio.*; - -public class PackTest { - - static int cmd = 47; - static int eid = 1; - static int lineno = 100; - static int sid = 1; - static int seqid = 32767; - static int textsiz = 5; - - /*private static int cmd2; - private static int eid2; - private static int lineno2; - private static int sid2; - private static int seqid2; - private static int textsiz2; - */ - - static byte[] chgBytes( int i ){ - byte[] b = new byte[4] ; - - b[0] = (byte)((i >>> 24 ) & 0xFF); - b[1] = (byte)((i >>> 16 ) & 0xFF); - b[2] = (byte)((i >>> 8 ) & 0xFF); - b[3] = (byte)((i >>> 0 ) & 0xFF); - - return b; - } - - public static ByteBuffer pack(ByteBuffer buffer,int cmd, int sid, int eid, int seqid, int lineno, String text ) { - - - //System.out.println("pack:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno); - - //ByteBuffer buffer = ByteBuffer.allocateDirect(24+textsiz); - - buffer.putInt(cmd); buffer.putInt(sid); buffer.putInt(eid); - buffer.putInt(seqid); buffer.putInt(lineno); - buffer.putInt(text.length()); - //buffer.putString(text); - - for(int i=0;i<text.length();i++) { - buffer.putChar(text.charAt(i)); - } - System.out.println("pack:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno); - - return buffer; - } - - public static void unpack(ByteBuffer buffer) { - 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 static void main(String args[]) { - ByteBuffer testbuf = ByteBuffer.allocateDirect(1024); - - System.out.println("pack unpack test."); - pack(testbuf, cmd, sid, eid, seqid, lineno, "test"); - - //System.out.println("check:" + testbuf.get(0)+ ":" +testbuf.get(1)+ ":" +testbuf.get(2)+ ":" +testbuf.get(3)); - //System.out.println("check:" + testbuf.get(4)+ ":" +testbuf.get(5)+ ":" +testbuf.get(6)+ ":" +testbuf.get(7)); - - //String string = ""; - unpack(testbuf); - - //System.out.println("unpack:" + cmd2 +", "+ sid2 +", "+ eid2 +", "+ seqid2 +", "+ lineno2 +", "+ string); - } -}
--- a/src/sample/pack/PackTest3.java Sat Dec 18 13:18:54 2010 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,102 +0,0 @@ -package sample.pack; - - -import java.nio.*; - -public class PackTest3 { - - private int cmd = 47; - private int eid = 1; - private int lineno = 100; - private int sid = 1; - private int seqid = 32767; - private int textsiz = 5; - - 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; - - public byte[] chgBytes( int i ){ - byte[] b = new byte[4] ; - - b[0] = (byte)((i >>> 24 ) & 0xFF); - b[1] = (byte)((i >>> 16 ) & 0xFF); - b[2] = (byte)((i >>> 8 ) & 0xFF); - b[3] = (byte)((i >>> 0 ) & 0xFF); - - return b; - } - - public ByteBuffer pack(int cmd, int sid, int eid, int seqid, int lineno, String text ) { - byte[] b_cmd = new byte[3]; - byte[] b_sid = new byte[3]; - byte[] b_eid = new byte[3]; - byte[] b_seqid = new byte[3]; - byte[] b_lineno = new byte[3]; - byte[] b_textsiz = new byte[3]; - byte[] textToByte = text.getBytes(); - textsiz = text.length(); - - System.out.println(cmd); - - b_cmd = chgBytes(cmd); - - System.out.println(b_cmd); - - b_sid = chgBytes(sid); - b_eid = chgBytes(eid); - b_seqid = chgBytes(seqid); - b_lineno = chgBytes(lineno); - b_textsiz = chgBytes(textsiz); - - ByteBuffer buffer = ByteBuffer.allocateDirect(24+textsiz); - - buffer.put(b_cmd); buffer.put(b_sid); buffer.put(b_eid); - buffer.put(b_seqid); buffer.put(b_lineno); buffer.put(b_textsiz); - buffer.put(textToByte); - - return buffer; - } - - public void unpack(ByteBuffer buffer, int cmd, int sid, int eid, int seqid, int lineno, String text) { - int textsiz = 0; - - buffer.flip(); - buffer.getInt(cmd); buffer.getInt(sid); buffer.getInt(eid); - buffer.getInt(seqid); buffer.getInt(lineno); buffer.getInt(textsiz); - - 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); - - System.out.println("unpack:" + cmd +", "+ sid +", "+ eid +", "+ seqid +", "+ lineno +", "+ text); - - } - - public void job() { - ByteBuffer testbuf = ByteBuffer.allocateDirect(1024); - - System.out.println("pack unpack test."); - testbuf = pack(cmd, sid, eid, seqid, lineno, "test"); - - int cmd2=0,sid2=0,eid2=0,seqid2=0,lineno2=0; - String string = ""; - unpack(testbuf,cmd2,sid2,eid2,seqid2,lineno2,string); - - System.out.println("unpack:" + cmd2 +", "+ sid2 +", "+ eid2 +", "+ seqid2 +", "+ lineno2 +", "+ string); - } - - public static void main(String args[]) { - new PackTest3().job(); - } -}