Mercurial > hg > RemoteEditor > Eclipse
changeset 67:2e5bd7bffa4b
JavaPathFinderpÌeXgNX
author | kent |
---|---|
date | Thu, 08 Nov 2007 12:21:25 +0900 |
parents | 4ef0f4f47240 |
children | 735e70091564 |
files | src/pathfinder/ChannelSimulator.java src/pathfinder/NetworkSimulator.java src/pathfinder/SeMaEmulator.java src/pathfinder/Test.java |
diffstat | 4 files changed, 136 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/pathfinder/ChannelSimulator.java Thu Nov 08 12:21:25 2007 +0900 @@ -0,0 +1,41 @@ +package pathfinder; + +import java.util.LinkedList; +import java.util.Queue; + +public class ChannelSimulator<P> { + public NetworkSimulator<P> ns; + public Queue<P>[] q; + + public ChannelSimulator() { + } +/* + public ChannelSimulator(NetworkSimulator<P> ns){ + q = new Queue<P>[2]; + q[0] = new LinkedList<P>(); + q[1] = new LinkedList<P>(); + this.ns = ns; + } +*/ + public ChannelSimulator(NetworkSimulator<P> _ns){ + this(new LinkedList<P>(), new LinkedList<P>(), _ns); + } + public ChannelSimulator(Queue<P> _a, Queue<P> _b, NetworkSimulator<P> ns){ + q = new Queue[2]; + q[0] = _a; + q[1] = _b; + this.ns = ns; + } + + public P read(){ + return ns.read(q[0]); + } + + public boolean write(P p){ + return ns.write(q[1],p); + } + + public ChannelSimulator<P> getServerChannel() { + return new ChannelSimulator<P>(q[1], q[0],ns); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/pathfinder/NetworkSimulator.java Thu Nov 08 12:21:25 2007 +0900 @@ -0,0 +1,34 @@ +package pathfinder; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.Queue; + +public class NetworkSimulator<P> { + + public LinkedList<ChannelSimulator<P>> acceptList; + public ArrayList<ChannelSimulator<P>> connectList; + + public NetworkSimulator(){ + acceptList = new LinkedList<ChannelSimulator<P>>(); + connectList = new ArrayList<ChannelSimulator<P>>(); + } + + public ChannelSimulator<P> accept(){ + ChannelSimulator<P> cs = acceptList.poll(); + connectList.add(cs); + + return cs.getServerChannel(); + } + public void connect(ChannelSimulator<P> cs){ + acceptList.add(cs); + } + + public synchronized P read(Queue<P>q) { + return q.poll(); + } + + public synchronized boolean write(Queue<P>q,P p) { + return q.add(p); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/pathfinder/SeMaEmulator.java Thu Nov 08 12:21:25 2007 +0900 @@ -0,0 +1,32 @@ +package pathfinder; + +import java.util.ArrayList; +import java.util.List; + +public class SeMaEmulator<P> implements Runnable { + private List<ChannelSimulator<P>> csList; + + public SeMaEmulator(){ + csList = new ArrayList<ChannelSimulator<P>>(); + } + + public ChannelSimulator<P> addChannelSimulator(ChannelSimulator<P> cs){ + csList.add(cs); + return cs; + } + + public void run(){ + int i=0; + P packet; + ChannelSimulator<P> cs = csList.get(1); + + /* Main Loop */ + while(true){ + packet=cs.read(); + i = (i+1)%csList.size(); + cs = csList.get(i+1); + if (packet!=null) cs.write(packet); + } + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/pathfinder/Test.java Thu Nov 08 12:21:25 2007 +0900 @@ -0,0 +1,29 @@ +package pathfinder; + +public class Test { + NetworkSimulator<String> ns; + + ChannelSimulator<String> client; + ChannelSimulator<String> server; + + public static void main(String [] arg) { + Test test = new Test(); + test.test(); + } + + void test() { + ns = new NetworkSimulator<String>(); + client = new ChannelSimulator<String>(ns); + ns.connect(client); + server = ns.accept(); + + client.write("client write some thing"); + + System.out.println(server.read()); + + } + + void thread() { + + } +}