Mercurial > hg > Database > Christie
changeset 15:4e867538adb7
change DataGearManager to abstract class for delete static dataGears
author | Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 14 Jan 2018 23:33:46 +0900 |
parents | f2b30b47ef67 |
children | 186a86dc4c8a |
files | src/main/java/christie/codegear/CodeGear.java src/main/java/christie/codegear/CodeGearManager.java src/main/java/christie/codegear/StartCodeGear.java src/main/java/christie/daemon/ChristieDaemon.java src/main/java/christie/daemon/Config.java src/main/java/christie/datagear/DataGearManager.java src/main/java/christie/datagear/LocalDataGearManager.java src/main/java/christie/datagear/RemoteDataGearManager.java src/main/java/christie/test/Remote/CreateRemotePutTest.java src/main/java/christie/test/Remote/RemotePutTest.java src/main/java/christie/test/Remote/RemoteTestConfig.java src/main/java/christie/test/Remote/StartRemoteTest.java src/main/java/christie/test/TestLocal/StartTest.java |
diffstat | 13 files changed, 126 insertions(+), 65 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/christie/codegear/CodeGear.java Thu Jan 11 18:02:59 2018 +0900 +++ b/src/main/java/christie/codegear/CodeGear.java Sun Jan 14 23:33:46 2018 +0900 @@ -64,7 +64,6 @@ throw new NullPointerException("please initialize DataGear"); } - //TODO:cgmName - commandList.add(new Command(this, dg, "first","local", name, CommandType.TAKE)); + commandList.add(new Command(this, dg, cgm.cgmName,"local", name, CommandType.TAKE)); } }
--- a/src/main/java/christie/codegear/CodeGearManager.java Thu Jan 11 18:02:59 2018 +0900 +++ b/src/main/java/christie/codegear/CodeGearManager.java Sun Jan 14 23:33:46 2018 +0900 @@ -20,14 +20,15 @@ private ThreadPoolExecutor threadPoolExecutor; private LocalDataGearManager localDGM = new LocalDataGearManager(); private ConcurrentHashMap<String, IncomingTcpConnection> acceptHash = new ConcurrentHashMap<String, IncomingTcpConnection>(); + public String cgmName; public Config conf; - public CodeGearManager(ThreadPoolExecutor exe, ConcurrentHashMap<String, CodeGearManager> cgms, Config conf) { + public CodeGearManager(String cgmName, ThreadPoolExecutor exe, ConcurrentHashMap<String, CodeGearManager> cgms, int localPort) { dataGearManagers.put("local", localDGM); this.cgms = cgms; threadPoolExecutor = exe; - this.conf = conf; - new ChristieDaemon(conf, this).listen();//run only first time + this.cgmName = cgmName; + new ChristieDaemon(localPort, this).listen(); } public LocalDataGearManager getLocalDGM(){ @@ -38,12 +39,10 @@ return dataGearManagers.get(dsmName); } - public void createRemoteDGM(String dsmName, String address, int port){ - dataGearManagers.put(dsmName, new RemoteDataGearManager(dsmName, address, port, this)); - } - - public void createRemoteDGM(String dsmName){ - dataGearManagers.put(dsmName, new RemoteDataGearManager(dsmName, conf.address, conf.connectPort, this)); + public RemoteDataGearManager createRemoteDGM(String dsmName, String address, int port){ + RemoteDataGearManager remote = new RemoteDataGearManager(dsmName, address, port, this); + dataGearManagers.put(dsmName, remote); + return remote; } public void submit(CodeGear cg){
--- a/src/main/java/christie/codegear/StartCodeGear.java Thu Jan 11 18:02:59 2018 +0900 +++ b/src/main/java/christie/codegear/StartCodeGear.java Sun Jan 14 23:33:46 2018 +0900 @@ -9,28 +9,38 @@ import java.util.concurrent.TimeUnit; public abstract class StartCodeGear extends CodeGear{ - ConcurrentHashMap<String, CodeGearManager> cgms = new ConcurrentHashMap<>(); - LinkedBlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<Runnable>(); - ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), // initial number of threads + static ConcurrentHashMap<String, CodeGearManager> cgms = new ConcurrentHashMap<>(); + static LinkedBlockingQueue<Runnable> taskQueue = new LinkedBlockingQueue<Runnable>(); + static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), // initial number of threads Runtime.getRuntime().availableProcessors(), Integer.MAX_VALUE, // keepAliveTime TimeUnit.SECONDS, taskQueue); - Config conf; - public StartCodeGear(Config conf){ - this.conf = conf; + public StartCodeGear(CodeGearManager cgm){ + cgm.setup(this); } - public CodeGearManager createCGM(String name){ - CodeGearManager cgm = new CodeGearManager(threadPoolExecutor, cgms, conf); + public static CodeGearManager createCGM(String name, int localPort){ + if (cgms.containsKey(name)){ + throw new IllegalArgumentException("CGM name '" + name +"' is already used"); + } + + CodeGearManager cgm = new CodeGearManager(name, threadPoolExecutor, cgms, localPort); cgms.put(name, cgm); return cgm; } - public CodeGearManager getCGM(String name){ + public static CodeGearManager createCGM(String name){ + return createCGM(name, 10000); + } + + public static CodeGearManager getCGM(String name){ return cgms.get(name); } + @Override + protected void run(CodeGearManager cgm) {} + }
--- a/src/main/java/christie/daemon/ChristieDaemon.java Thu Jan 11 18:02:59 2018 +0900 +++ b/src/main/java/christie/daemon/ChristieDaemon.java Sun Jan 14 23:33:46 2018 +0900 @@ -10,12 +10,17 @@ public class ChristieDaemon { - private Config conf; + private int localPort; private AcceptThread acceptThread; public CodeGearManager cgm; public ChristieDaemon(Config conf, CodeGearManager cgm) { - this.conf = conf; + this.localPort = conf.localPort; + this.cgm = cgm; + } + + public ChristieDaemon(int localPort, CodeGearManager cgm) { + this.localPort = localPort; this.cgm = cgm; } @@ -26,11 +31,11 @@ ss.setReuseAddress(true); // listen on any address ipv4/ipv6 - InetSocketAddress a = new InetSocketAddress("::", conf.localPort); + InetSocketAddress a = new InetSocketAddress("::", localPort); System.out.println("AliceDaemon.listen: bind to " + a); ss.bind(a); - acceptThread = new AcceptThread(ss, "ACCEPT" + conf.localPort, cgm); + acceptThread = new AcceptThread(ss, "ACCEPT" + localPort, cgm); acceptThread.start(); } catch (IOException e) { e.printStackTrace();
--- a/src/main/java/christie/daemon/Config.java Thu Jan 11 18:02:59 2018 +0900 +++ b/src/main/java/christie/daemon/Config.java Sun Jan 14 23:33:46 2018 +0900 @@ -3,17 +3,11 @@ public class Config { public int localPort = 10000; - public String address = "127.0.0.1"; - public int connectPort = 10001; public Config(String[] args) { for (int i = 0; i< args.length; i++) { if ("-localPort".equals(args[i])) { localPort = Integer.parseInt(args[++i]); - }if ("-address".equals(args[i])) { - address = args[++i]; - } else if ("-connectPort".equals(args[i])) { - connectPort = Integer.parseInt(args[++i]); } } }
--- a/src/main/java/christie/datagear/DataGearManager.java Thu Jan 11 18:02:59 2018 +0900 +++ b/src/main/java/christie/datagear/DataGearManager.java Sun Jan 14 23:33:46 2018 +0900 @@ -14,11 +14,12 @@ * Created by e125769 on 12/7/17. * PUT/TAKEなどDataGearManagerに対するComandの実行。 */ -public interface DataGearManager { +public abstract class DataGearManager { TreeMap<String, LinkedBlockingQueue<DataGear>> dataGears = new TreeMap<String, LinkedBlockingQueue<DataGear>>(); HashMap<String, Command> waitList = new HashMap<String, Command>(); - void take(Command cm); - void put(String key, Object data); - void runCommand(Command cm); - void addWaitList(Command command); + public abstract void take(Command cm); + public abstract void put(String key, Object data); + public abstract void put(String dsmName, String key, Object data); + public abstract void runCommand(Command cm); + public abstract void addWaitList(Command command); }
--- a/src/main/java/christie/datagear/LocalDataGearManager.java Thu Jan 11 18:02:59 2018 +0900 +++ b/src/main/java/christie/datagear/LocalDataGearManager.java Sun Jan 14 23:33:46 2018 +0900 @@ -2,27 +2,32 @@ import christie.codegear.Command; +import java.util.TreeMap; import java.util.concurrent.LinkedBlockingQueue; /** * Created by e125769 on 12/7/17. */ -public class LocalDataGearManager implements DataGearManager { +public class LocalDataGearManager extends DataGearManager { public void take(Command cm) { if (dataGears.containsKey(cm.key)) { - System.out.println(dataGears); runCommand(cm); } else { addWaitList(cm); } } - public void put(String key, Object data) { + public void put(String key, Object data) {//main use DataGear dg = new DataGear(data, data.getClass()); put(key, dg); } + @Override + public void put(String dsmName, String key, Object data) { + put(key, data); + } + public void put(String key, DataGear dg){ if(dataGears.containsKey(key)){ dataGears.get(key).add(dg); @@ -43,6 +48,7 @@ switch (cm.type){ case TAKE: cm.cg.idg.setInputs(cm.key, cm.dg); + TreeMap<String, LinkedBlockingQueue<DataGear>> hoge = dataGears; dataGears.get(cm.key).poll(); if (dataGears.get(cm.key).isEmpty()){ dataGears.remove(cm.key); @@ -59,4 +65,5 @@ public void addWaitList(Command command) { waitList.put(command.key, command); } + }
--- a/src/main/java/christie/datagear/RemoteDataGearManager.java Thu Jan 11 18:02:59 2018 +0900 +++ b/src/main/java/christie/datagear/RemoteDataGearManager.java Sun Jan 14 23:33:46 2018 +0900 @@ -12,7 +12,7 @@ import java.net.InetSocketAddress; import java.nio.channels.SocketChannel; -public class RemoteDataGearManager implements DataGearManager{ +public class RemoteDataGearManager extends DataGearManager{ Connection connection; @@ -53,9 +53,13 @@ } + public void put(String cgmName, String key, Object data){//main use + Command cmd = new Command(new DataGear(data, data.getClass()), cgmName,"local", key, CommandType.PUT, data.getClass()); + connection.write(cmd); + } + @Override public void put(String key, Object data) { - //cgmNameを受け取れるようにする Command cmd = new Command(new DataGear(data, data.getClass()), "first","local", key, CommandType.PUT, data.getClass()); connection.write(cmd); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/test/Remote/CreateRemotePutTest.java Sun Jan 14 23:33:46 2018 +0900 @@ -0,0 +1,19 @@ +package christie.test.Remote; + +import christie.codegear.CodeGear; +import christie.codegear.CodeGearManager; + +public class CreateRemotePutTest extends CodeGear { + + String cgmName; + + public CreateRemotePutTest(String cgmName) { + this.cgmName = cgmName; + } + + @Override + protected void run(CodeGearManager cgm) { + cgm.setup(new RemotePutTest(cgmName)); + dgm("remote").put(cgmName,"hoge", 1); + } +}
--- a/src/main/java/christie/test/Remote/RemotePutTest.java Thu Jan 11 18:02:59 2018 +0900 +++ b/src/main/java/christie/test/Remote/RemotePutTest.java Sun Jan 14 23:33:46 2018 +0900 @@ -11,13 +11,18 @@ @Take("hoge") public DataGear<Integer> hoge = new DataGear<>(); + public String cgmName; + + public RemotePutTest(String cgmName){ + this.cgmName = cgmName; + } + @Override protected void run(CodeGearManager cgm) { - System.out.println(hoge.getData()); - if (hoge.getData() != 10){ - cgm.setup(new RemotePutTest()); - dgm("remote").put("hoge", hoge.getData() + 1); + System.out.println(hoge.getData()); + cgm.setup(new RemotePutTest(cgmName)); + dgm("remote").put(cgmName,"hoge", hoge.getData() + 1); } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/christie/test/Remote/RemoteTestConfig.java Sun Jan 14 23:33:46 2018 +0900 @@ -0,0 +1,23 @@ +package christie.test.Remote; + +import christie.daemon.Config; + +public class RemoteTestConfig extends Config{ + + public String address = "127.0.0.1"; + public int connectPort = 10001; + + public RemoteTestConfig(String[] args) { + super(args); + for (int i = 0; i< args.length; i++) { + if ("-localPort".equals(args[i])) { + localPort = Integer.parseInt(args[++i]); + }if ("-address".equals(args[i])) { + address = args[++i]; + } else if ("-connectPort".equals(args[i])) { + connectPort = Integer.parseInt(args[++i]); + } + } + } + +}
--- a/src/main/java/christie/test/Remote/StartRemoteTest.java Thu Jan 11 18:02:59 2018 +0900 +++ b/src/main/java/christie/test/Remote/StartRemoteTest.java Sun Jan 14 23:33:46 2018 +0900 @@ -2,25 +2,23 @@ import christie.codegear.CodeGearManager; import christie.codegear.StartCodeGear; -import christie.daemon.Config; + +public class StartRemoteTest extends StartCodeGear{ -public class StartRemoteTest extends StartCodeGear { - - public StartRemoteTest(Config conf) { - super(conf); + public StartRemoteTest(CodeGearManager cgm) { + super(cgm); } public static void main(String args[]){ - StartRemoteTest start = new StartRemoteTest(new Config(args)); - CodeGearManager cgm = start.createCGM("first"); - cgm.setup(start); - //もう一つスタートする - } + CodeGearManager cgm = createCGM("first", 10000); + new StartRemoteTest(cgm); - @Override - protected void run(CodeGearManager cgm) { - cgm.setup(new RemotePutTest()); - cgm.createRemoteDGM("remote"); - dgm("remote").put("hoge", 1); + cgm.createRemoteDGM("remote", "localhost", 10001); + cgm.setup(new CreateRemotePutTest( "second")); + + CodeGearManager cgm2 = createCGM("second", 10001); + cgm2.createRemoteDGM("remote", "localhost", 10000); + cgm2.setup(new CreateRemotePutTest("first")); + } }
--- a/src/main/java/christie/test/TestLocal/StartTest.java Thu Jan 11 18:02:59 2018 +0900 +++ b/src/main/java/christie/test/TestLocal/StartTest.java Sun Jan 14 23:33:46 2018 +0900 @@ -6,15 +6,12 @@ public class StartTest extends StartCodeGear{ - - public StartTest(Config conf) { - super(conf); + public StartTest(CodeGearManager cgm) { + super(cgm); } public static void main(String args[]){ - StartTest start = new StartTest(new Config(args)); - CodeGearManager cgm = start.createCGM("first"); - cgm.setup(start); + StartTest start = new StartTest(createCGM("first")); } @Override