# HG changeset patch # User kent # Date 1219747597 -32400 # Node ID 66e9cebce3fabb4e02d06e5025d0f5c71050fda7 # Parent e9047957acc28e9d7948460b2c65835701fc1afc move from pathfinder.simulator.channels.* diff -r e9047957acc2 -r 66e9cebce3fa src/pathfinder/mergetest/channels/ChannelSimulator.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/pathfinder/mergetest/channels/ChannelSimulator.java Tue Aug 26 19:46:37 2008 +0900 @@ -0,0 +1,66 @@ +package pathfinder.mergetest.channels; + + +public class ChannelSimulator
extends SelectableChannelSimulator
{ + //private BlockingQueue
qread; + //private BlockingQueue
qwrite; + //private SelectorSimulator
waitingSelector; + protected NetworkSimulator
ns; + + /** Constructors. */ + public ChannelSimulator(NetworkSimulator
_ns){ + this(_ns, null); + } + public ChannelSimulator(NetworkSimulator
_ns, SelectorSimulator
_selector){ + ns = _ns; + //ns = NetworkSimulator.singleton(); //どっちがいい? + } + public ChannelSimulator
createConjugatedChannel() { + ChannelSimulator
ret = new ChannelSimulator
(ns); + ret.qread=qwrite; + ret.qwrite=qread; + ret.readSelector=writeSelector; + ret.writeSelector=readSelector; + return ret; + } + + /** Connecting methods */ + // for clients. + public boolean connect(int ip){ + return ns.connect(ip, this); + } + + public ChannelSimulator
accept(){ + return null; + } + + /* return state of the Queue(debug) */ + /* + public boolean readQisEmpty() { + return qread.isEmpty(); + } + public boolean writeQisEmpty() { + return qwrite.isEmpty(); + } + */ + + @Override + public boolean isAcceptable() { + return false; + } + @Override + public boolean isReadable() { + synchronized (qread){ + return !qread.isEmpty(); + } + } + @Override + public boolean isWritable() { + return true; + } + + public SelectionKeySimulator
keyFor(SelectorSimulator
selector2) { + return selector2.getKey(this); + } + +} diff -r e9047957acc2 -r 66e9cebce3fa src/pathfinder/mergetest/channels/NetworkSimulator.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/pathfinder/mergetest/channels/NetworkSimulator.java Tue Aug 26 19:46:37 2008 +0900 @@ -0,0 +1,126 @@ +package pathfinder.mergetest.channels; + +import java.util.LinkedList; + + +public class NetworkSimulator
{
+ public static NetworkSimulator> ns;
+ synchronized public static selector) {
+ serverList.add(new ServerData (ip, selector));
+ writeLog(Thread.currentThread(), "listen", 1);
+ printAllState();
+ }
+
+ synchronized public ChannelSimulator accept(int ip) {
+ for (ServerData sd: serverList){
+ if (sd.virtualIP!=ip) continue;
+ writeLog(Thread.currentThread(), "accepting..", 1);
+
+ ChannelSimulator serverCH = sd.acceptWaitingList.remove();
+ sd.establishedList.add(serverCH);
+
+ writeLog(Thread.currentThread(), "accepted", 1);
+ printAllState();
+ return serverCH;
+ }
+ return null;
+ }
+ synchronized public boolean canAccept(int ip){
+ for (ServerData sd: serverList){
+ if (sd.virtualIP!=ip) continue;
+ return !sd.acceptWaitingList.isEmpty();
+ }
+ return false;
+ }
+
+ public boolean connect(int ip, ChannelSimulator clientCH) {
+ ServerData sd = null;
+ writeLog(Thread.currentThread(), "connecting..", 1);
+ synchronized (this){
+ for (ServerData sd0: serverList){
+ if (sd0.virtualIP!=ip) continue;
+
+ sd = sd0;
+ }
+ if (sd==null) return false;
+
+ //ChannelSimulator channel = new ChannelSimulator (sd.selector);
+ clientCH.createReadQ();
+ clientCH.createWriteQ();
+ clientCH.setWriteSelector(sd.selector);
+
+ ChannelSimulator serverCH = clientCH.createConjugatedChannel();
+ sd.acceptWaitingList.add(serverCH);
+ }
+
+ synchronized (sd.selector) {
+ sd.selector.notifyAll();
+ }
+ writeLog(Thread.currentThread(), "connected", 1);
+ printAllState();
+ return true;
+ }
+
+ /** for DEBUG methods. */
+ synchronized void printAllState(){
+ writeLog("NetworkSimulator State:");
+ for (ServerData sd: serverList){
+ writeLog("\tSessionManager(ip="+sd.virtualIP+"): ");
+ writeLog("\tacceptWaitingList="+sd.acceptWaitingList.size());
+ writeLog("\testablishedList="+sd.establishedList.size());
+ }
+ }
+
+ /** simulation log command */
+ synchronized public void writeLog(String log, int level){
+ if ( level<=logLevel )
+ System.out.println(log);
+ System.out.flush();
+ }
+ public void writeLog(String log){
+ writeLog(log, 0);
+ }
+ public void writeLog(Thread thr, String log, int level){
+ writeLog(thr.getName()+": "+log, level);
+ }
+ public void setLogLevel(int logLevel) {
+ this.logLevel = logLevel;
+ }
+
+
+}
+
+class ServerData {
+ int virtualIP;
+ SelectorSimulator selector;
+ LinkedList _selector){
+ virtualIP = ip;
+ selector = _selector;
+ acceptWaitingList = new LinkedList {
+ protected BlockingQueue qread;
+ protected BlockingQueue qwrite;
+ protected SelectorSimulator writeSelector;
+ protected SelectorSimulator readSelector;
+
+ /* read from Queue. */
+ public P read(){
+ try {
+ if(readSelector!=null)
+ synchronized (readSelector){
+ return qread.take();
+ }
+ else{
+ return qread.take();
+ }
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+ /* write to Queue. */
+ public boolean write(P p){
+ try {
+ if (writeSelector!=null)
+ synchronized (writeSelector){
+ qwrite.put(p);
+ writeSelector.notifyAll();
+ }
+ else {
+ qwrite.put(p);
+ }
+ return true;
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+ public abstract ChannelSimulator accept();
+
+ /* accessor methods. */
+ public BlockingQueue getReadQ(){
+ return qread;
+ }
+ public BlockingQueue getWriteQ(){
+ return qwrite;
+ }
+ public void createReadQ(){
+ qread = new LinkedBlockingQueue ();
+ }
+ public void createWriteQ(){
+ qwrite = new LinkedBlockingQueue ();
+ }
+ public void setWriteSelector(SelectorSimulator _selector){
+ writeSelector = _selector;
+ }
+
+
+ /* return state of the Queue */
+ abstract public boolean isReadable();
+ abstract public boolean isWritable();
+ abstract public boolean isAcceptable();
+
+}
diff -r e9047957acc2 -r 66e9cebce3fa src/pathfinder/mergetest/channels/SelectionKeySimulator.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pathfinder/mergetest/channels/SelectionKeySimulator.java Tue Aug 26 19:46:37 2008 +0900
@@ -0,0 +1,52 @@
+package pathfinder.mergetest.channels;
+
+public class SelectionKeySimulator {
+
+ public static final int OP_READ = 0x01;
+ public static final int OP_ACCEPT = 0x02;
+ public static final int OP_WRITE = 0x04;
+
+ private int interestOpt;
+ private SelectableChannelSimulator channel;
+ private Object attachment;
+
+ public SelectionKeySimulator(SelectableChannelSimulator cs, int opt) {
+ channel = cs;
+ interestOpt = opt;
+ }
+
+ 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 boolean isAcceptable() {
+ return channel.isAcceptable();
+ }
+
+ public boolean isReadable() {
+ return channel.isReadable();
+ }
+ public boolean isWritable() {
+ return channel.isWritable();
+ }
+
+ public SelectableChannelSimulator channel() {
+ return channel;
+ }
+
+ public Object attachment() {
+ return attachment;
+ }
+
+ public void attach(Object handler) {
+ attachment = handler;
+ }
+
+}
diff -r e9047957acc2 -r 66e9cebce3fa src/pathfinder/mergetest/channels/SelectorSimulator.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pathfinder/mergetest/channels/SelectorSimulator.java Tue Aug 26 19:46:37 2008 +0900
@@ -0,0 +1,67 @@
+package pathfinder.mergetest.channels;
+
+import java.io.IOException;
+import java.util.ArrayList;
+//import java.util.Set; //書き直す?
+
+
+
+public class SelectorSimulator {
+
+ private ArrayList key : keyList){
+ if(key.isAble())
+ selectedKeys.add(key);
+ }
+
+ if(selectedKeys.isEmpty())
+ try {
+ this.wait();
+ } catch (InterruptedException e) {
+ throw new IOException("Error, Selector was interrupted!");
+ }
+ }
+ }
+ return selectedKeys.size();
+ }
+
+ public SelectionKeySimulator register(SelectableChannelSimulator cs, int opt){
+ SelectionKeySimulator key = new SelectionKeySimulator (cs, opt);
+ keyList.add(key);
+ return key;
+ }
+
+ public SelectionKeySimulator register(ChannelSimulator cs, int opt, Object handler){
+ SelectionKeySimulator key = new SelectionKeySimulator (cs, opt);
+ key.attach(handler);
+ keyList.add(key);
+ return key;
+ }
+
+ public ArrayList getKey(ChannelSimulator channel){
+ for(SelectionKeySimulator key : keyList){
+ if(key.channel() == channel)
+ return key;
+ }
+ return null;
+ }
+
+}
diff -r e9047957acc2 -r 66e9cebce3fa src/pathfinder/mergetest/channels/ServerChannelSimulator.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pathfinder/mergetest/channels/ServerChannelSimulator.java Tue Aug 26 19:46:37 2008 +0900
@@ -0,0 +1,42 @@
+package pathfinder.mergetest.channels;
+
+
+
+public class ServerChannelSimulator extends SelectableChannelSimulator {
+ protected NetworkSimulator ns;
+ private int virtualIP;
+
+ /** Constructors. */
+ public ServerChannelSimulator(NetworkSimulator _ns, SelectorSimulator rselector){
+ ns = _ns;
+ readSelector = rselector;
+ writeSelector = null;
+ qread = null;
+ qwrite = null;
+ }
+
+ /** Connecting methods */
+ // for servers.
+ public void bind(int ip){
+ virtualIP = ip;
+ ns.listen(ip, readSelector);
+ }
+
+ public ChannelSimulator accept(){
+ ChannelSimulator channel = ns.accept(virtualIP);
+ return channel;
+ }
+
+
+ /* state check methods for SelectionKeySimulator. */
+ public boolean isReadable() {
+ return false;
+ }
+ public boolean isWritable() {
+ return false;
+ }
+ public boolean isAcceptable() {
+ return ns.canAccept(virtualIP);
+ }
+
+}