# HG changeset patch # User riono # Date 1606821789 -32400 # Node ID 3aaa77e124934469972411d23126e0c662178167 # Parent c9d1a5a7925425d36e02409139eeeefa03b8d19a update diff -r c9d1a5a79254 -r 3aaa77e12493 codegear/CodeGearManager.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/codegear/CodeGearManager.cs Tue Dec 01 20:23:09 2020 +0900 @@ -0,0 +1,16 @@ +using System.Collections.Concurrent; +using System.Threading; +using Christie_net.datagear; + +namespace Christie_net.codegear { +public class CodeGearManager { + private ConcurrentDictionary dgmList = new ConcurrentDictionary(); + private ConcurrentDictionary cgmList = new ConcurrentDictionary(); + //private ThreadPool threadPoolExecutor; + private LocalDataGearManager localDgm = new LocalDataGearManager(); + //private ConcurrentDictionary + public int cgmID; + //public + public int localPort; +} +} \ No newline at end of file diff -r c9d1a5a79254 -r 3aaa77e12493 codegear/InputDataGear.cs --- a/codegear/InputDataGear.cs Tue Dec 01 17:48:03 2020 +0900 +++ b/codegear/InputDataGear.cs Tue Dec 01 20:23:09 2020 +0900 @@ -5,11 +5,13 @@ using Christie_net.datagear.dg; namespace Christie_net.codegear { +// InputDataGearの待ち合わせの管理 public class InputDataGear { - public CodeGear cg; + public ConcurrentDictionary> inputValue = new ConcurrentDictionary>(); //public CodeGearManager cgm; + public CodeGear cg; private int count; - public ConcurrentDictionary> inputValue = new ConcurrentDictionary>(); + public InputDataGear(CodeGear cg) { this.cg = cg; diff -r c9d1a5a79254 -r 3aaa77e12493 daemon/ChristieDaemon.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/daemon/ChristieDaemon.cs Tue Dec 01 20:23:09 2020 +0900 @@ -0,0 +1,7 @@ +namespace Christie_net.daemon { +public class ChristieDaemon { + private int localPort; + //private + +} +} \ No newline at end of file diff -r c9d1a5a79254 -r 3aaa77e12493 datagear/DataGearManager.cs --- a/datagear/DataGearManager.cs Tue Dec 01 17:48:03 2020 +0900 +++ b/datagear/DataGearManager.cs Tue Dec 01 20:23:09 2020 +0900 @@ -3,14 +3,17 @@ using Christie_net.datagear.dg; namespace Christie_net.datagear { +/// +/// PUT/TAKEなどDataGearManagerに対するcommandの実行 +/// public abstract class DataGearManager { protected DataGears dataGears = new DataGears(); - //public WaitList WaitList = new WaitList(); - public abstract void put(string key, object data); - public abstract void runCommand(Command cm); - public abstract void resolveWaitCommand(string key, DataGear dg); - public abstract void finish(); - public abstract void close(); - public abstract void shutdown(); + public WaitList waitList = new WaitList(); + public abstract void Put(string key, object data); + public abstract void RunCommand(Command cm); + public abstract void ResolveWaitCommand(string key, DataGear dg); + public abstract void Finish(); + public abstract void Close(); + public abstract void Shutdown(); } } \ No newline at end of file diff -r c9d1a5a79254 -r 3aaa77e12493 datagear/DataGears.cs --- a/datagear/DataGears.cs Tue Dec 01 17:48:03 2020 +0900 +++ b/datagear/DataGears.cs Tue Dec 01 20:23:09 2020 +0900 @@ -61,6 +61,7 @@ lock (syncObject) { object data = null; DataGear dataGear; + // BlockingQueueではpeekできないので、中のConcurrentQueueを取り出して操作 ConcurrentQueue> queue = new ConcurrentQueue>(dataGears[key]); if (queue.TryPeek(out dataGear)) { data = dataGear.GetData(); diff -r c9d1a5a79254 -r 3aaa77e12493 datagear/LocalDataGearManager.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/datagear/LocalDataGearManager.cs Tue Dec 01 20:23:09 2020 +0900 @@ -0,0 +1,58 @@ +using System; +using Christie_net.datagear.command; +using Christie_net.datagear.dg; + +namespace Christie_net.datagear { +public class LocalDataGearManager : DataGearManager { + // 通常はこちらを使用する + public override void Put(string key, object data) { + DataGear dg = new DataGear(data); + PutRun(key, dg); + } + + public void PutRun(string key, DataGear dg) { + Command cm = new CommandBuilder().Init(CommandType.PUT).CgmID(1) + .ToDgmName("local").Key(key).Dg(dg).Build(); + RunCommand(cm); + } + + public override void RunCommand(Command cm) { + switch (cm.type) { + case CommandType.PUT: + dataGears.SetData(cm); + if (waitList.ContainsKey(cm.key)) { + RunCommand(waitList.GetAndRemoveCommand(cm.key)); + } + + break; + case CommandType.TAKE: + case CommandType.REMOTETAKE: + case CommandType.PEEK: + case CommandType.REMOTEPEEK: + if (dataGears.ContainsKey(cm.key)) { + SetData(cm); + cm.Execute(); + } else { + waitList.Add(cm); + } + + break; + case CommandType.REPLY: + cm.Execute(); + break; + } + } + + private void SetData(Command cm) { + cm.SetDg(dataGears.GetData(cm)); + } + + public override void ResolveWaitCommand(string key, DataGear dg) { } + + public override void Finish() { } + + public override void Close() { } + + public override void Shutdown() { } +} +} \ No newline at end of file diff -r c9d1a5a79254 -r 3aaa77e12493 datagear/WaitList.cs --- a/datagear/WaitList.cs Tue Dec 01 17:48:03 2020 +0900 +++ b/datagear/WaitList.cs Tue Dec 01 20:23:09 2020 +0900 @@ -8,7 +8,7 @@ private Dictionary> waitlist = new Dictionary>(); private object syncObject = new object(); - public void add(Command cm) { + public void Add(Command cm) { lock (syncObject) { if (waitlist.ContainsKey(cm.key)) { waitlist[cm.key].TryAdd(cm); @@ -20,6 +20,19 @@ } } + public Command GetAndRemoveCommand(string key) { + lock (syncObject) { + Command cm = null; + if (waitlist.ContainsKey(key)) { + waitlist[key].TryTake(out cm); + if (waitlist[key].IsCompleted) { + waitlist[key].Dispose(); + waitlist.Remove(key); + } + } + return cm; + } + } public bool ContainsKey(string key) { lock (syncObject) { diff -r c9d1a5a79254 -r 3aaa77e12493 datagear/command/CloseCommand.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/datagear/command/CloseCommand.cs Tue Dec 01 20:23:09 2020 +0900 @@ -0,0 +1,16 @@ +using System.IO; +using Christie_net.daemon; + +namespace Christie_net.datagear.command { +public class CloseCommand : Command { + public CloseCommand(CommandBuilder cb) : base(cb) { } + + public override void Execute() { + connection.Close(); + } + + public override MemoryStream Convert() { + return null; + } +} +} \ No newline at end of file diff -r c9d1a5a79254 -r 3aaa77e12493 datagear/command/Command.cs --- a/datagear/command/Command.cs Tue Dec 01 17:48:03 2020 +0900 +++ b/datagear/command/Command.cs Tue Dec 01 20:23:09 2020 +0900 @@ -20,7 +20,7 @@ this.type = cb.type; this.key = cb.key; this.toDgmName = cb.toDgmName; - this.fromDgmName = cb.fromDgmname; + this.fromDgmName = cb.fromDgmName; this.cgmID = cb.cgmID; this.cg = cb.cg; this.dg = cb.dg; diff -r c9d1a5a79254 -r 3aaa77e12493 datagear/command/CommandBuilder.cs --- a/datagear/command/CommandBuilder.cs Tue Dec 01 17:48:03 2020 +0900 +++ b/datagear/command/CommandBuilder.cs Tue Dec 01 20:23:09 2020 +0900 @@ -8,20 +8,20 @@ protected internal CommandType type; protected internal string key = null; protected internal string toDgmName = null; // for take - protected internal string fromDgmname = "local"; // for remotetake/reply + protected internal string fromDgmName = "local"; // for remotetake/reply protected internal int? cgmID = null; // for local meta protected internal CodeGear cg = null; // for localtake protected internal DataGear dg = null; // for put/localtake/reply protected internal Type clazz = null; // for remote protected internal Connection connection = null; // for reply - //private CommandFactory factory = new CommandFactory(); + private CommandFactory factory = new CommandFactory(); - public CommandBuilder init(CommandType type) { + public CommandBuilder Init(CommandType type) { this.type = type; this.key = null; this.toDgmName = null; - this.fromDgmname = "local"; + this.fromDgmName = "local"; this.cgmID = null; this.cg = null; this.dg = null; @@ -29,5 +29,98 @@ this.connection = null; return this; } + + public CommandBuilder Key(string key) { + this.key = key; + return this; + } + + public CommandBuilder ToDgmName(string toDgmName) { + this.toDgmName = toDgmName; + return this; + } + + public CommandBuilder FromDgmName(string fromDgmName) { + this.fromDgmName = fromDgmName; + return this; + } + + public CommandBuilder CgmID(int cgmID) { + this.cgmID = cgmID; + return this; + } + + public CommandBuilder Cg(CodeGear codeGear) { + this.cg = codeGear; + return this; + } + + public CommandBuilder Dg(DataGear dg) { + this.dg = dg; + return this; + } + + public CommandBuilder Clazz(Type clazz) { + this.clazz = clazz; + return this; + } + + public CommandBuilder Connection(Connection connection) { + this.connection = connection; + return this; + } + + public Command Build() { + if (type == null) { + throw new NullReferenceException(); + } + return factory.GetCommand(type, this); + } + + private class CommandFactory { + public Command GetCommand(CommandType type, CommandBuilder cb) { + switch (type) { + case CommandType.PUT: + CheckNull(cb.key, cb.dg); + return new PutCommand(cb); + case CommandType.TAKE: + CheckNull(cb.cgmID, cb.cg, cb.toDgmName, cb.key, cb.dg); + return new TakeCommand(cb); + case CommandType.PEEK: + CheckNull(cb.cgmID, cb.cg, cb.toDgmName, cb.key, cb.dg); + return new PeekCommand(cb); + case CommandType.REMOTETAKE: + if (cb.fromDgmName.Equals("local")) { + throw new NullReferenceException(); + } + CheckNull(cb.key, cb.connection, cb.clazz); + return new RemoteTakeCommand(cb); + case CommandType.REMOTEPEEK: + if (cb.fromDgmName.Equals("local")) { + throw new NullReferenceException(); + } + CheckNull(cb.key, cb.connection, cb.clazz); + return new RemotePeedCommand(cb); + case CommandType.REPLY: + CheckNull(cb.key, cb.connection, cb.dg); + return new ReplyCommand(cb); + case CommandType.CLOSE: + CheckNull(cb.connection); + return new CloseCommand(cb); + case CommandType.FINISH: + return new FinishCommand(cb); + } + return null; + } + + public void CheckNull(params object[] param) { + foreach (var variable in param) { + if (variable == null) { + throw new NullReferenceException(); + } + } + } + } + } } \ No newline at end of file diff -r c9d1a5a79254 -r 3aaa77e12493 datagear/command/PutCommand.cs --- a/datagear/command/PutCommand.cs Tue Dec 01 17:48:03 2020 +0900 +++ b/datagear/command/PutCommand.cs Tue Dec 01 20:23:09 2020 +0900 @@ -24,6 +24,7 @@ stream.Write(command, 0, command.Length); stream.Write(data, 0, data.Length); stream.Write(dataSize, 0, dataSize.Length); + stream.Position = 0; } catch (IOException e) { Console.WriteLine(e.StackTrace); } diff -r c9d1a5a79254 -r 3aaa77e12493 datagear/command/RemoteDataGearManager.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/datagear/command/RemoteDataGearManager.cs Tue Dec 01 20:23:09 2020 +0900 @@ -0,0 +1,36 @@ +using System; +using Christie_net.daemon; +using Christie_net.datagear.dg; + +namespace Christie_net.datagear.command { +public class RemoteDataGearManager : DataGearManager { + private Connection connection; + //private CodegearManager cgm; + private bool connect = false; + private object lockObj = new object(); + + public override void Put(string key, object data) { + throw new NotImplementedException(); + } + + public override void RunCommand(Command cm) { + throw new NotImplementedException(); + } + + public override void ResolveWaitCommand(string key, DataGear dg) { + throw new NotImplementedException(); + } + + public override void Finish() { + throw new NotImplementedException(); + } + + public override void Close() { + throw new NotImplementedException(); + } + + public override void Shutdown() { + throw new NotImplementedException(); + } +} +} \ No newline at end of file diff -r c9d1a5a79254 -r 3aaa77e12493 datagear/command/RemotePeedCommand.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/datagear/command/RemotePeedCommand.cs Tue Dec 01 20:23:09 2020 +0900 @@ -0,0 +1,14 @@ +using System.IO; + +namespace Christie_net.datagear.command { +public class RemotePeedCommand : Command{ + public RemotePeedCommand(CommandBuilder cb) : base(cb) { } + + public override void Execute() { + } + + public override MemoryStream Convert() { + return null; + } +} +} \ No newline at end of file diff -r c9d1a5a79254 -r 3aaa77e12493 datagear/command/RemoteTakeCommand.cs --- a/datagear/command/RemoteTakeCommand.cs Tue Dec 01 17:48:03 2020 +0900 +++ b/datagear/command/RemoteTakeCommand.cs Tue Dec 01 20:23:09 2020 +0900 @@ -8,7 +8,7 @@ } public override void Execute() { - //new CommandBuilder().init(CommandType.REPLY). + new CommandBuilder().Init(CommandType.REPLY).Connection(connection).Key(key).Dg(dg).Build().Execute(); } public override MemoryStream Convert() {