# HG changeset patch # User riono # Date 1602431372 -32400 # Node ID e6f5b7d14dd11b6f410199fc2fb619fcc18fe444 # Parent f48a2df9da65a737ee203e187b8cc0bcbcf7bf2a update DataGears diff -r f48a2df9da65 -r e6f5b7d14dd1 Program.cs --- a/Program.cs Fri Oct 09 00:24:06 2020 +0900 +++ b/Program.cs Mon Oct 12 00:49:32 2020 +0900 @@ -1,15 +1,11 @@ using System; -using System.Threading; -using Christie_net.annotation; namespace Christie_net { - class Program { - //[Peek] Test testindex; +internal class Program { + //[Peek] Test testindex; - static void Main(string[] args) { - Console.WriteLine("Hello World!"); - - - } + private static void Main(string[] args) { + Console.WriteLine("Hello World!"); } +} } \ No newline at end of file diff -r f48a2df9da65 -r e6f5b7d14dd1 annotation/Peek.cs --- a/annotation/Peek.cs Fri Oct 09 00:24:06 2020 +0900 +++ b/annotation/Peek.cs Mon Oct 12 00:49:32 2020 +0900 @@ -1,7 +1,6 @@ using System; namespace Christie_net.annotation { - [AttributeUsage(AttributeTargets.Field)] - public class Peek : Attribute { - } +[AttributeUsage(AttributeTargets.Field)] +public class Peek : Attribute { } } \ No newline at end of file diff -r f48a2df9da65 -r e6f5b7d14dd1 annotation/PeekFrom.cs --- a/annotation/PeekFrom.cs Fri Oct 09 00:24:06 2020 +0900 +++ b/annotation/PeekFrom.cs Mon Oct 12 00:49:32 2020 +0900 @@ -1,12 +1,12 @@ using System; namespace Christie_net.annotation { - [AttributeUsage(AttributeTargets.Field)] - public class PeekFrom : Attribute { - private string _name; +[AttributeUsage(AttributeTargets.Field)] +public class PeekFrom : Attribute { + private string _name; - public String Value() { - return _name; - } + public string Value() { + return _name; } +} } \ No newline at end of file diff -r f48a2df9da65 -r e6f5b7d14dd1 annotation/Take.cs --- a/annotation/Take.cs Fri Oct 09 00:24:06 2020 +0900 +++ b/annotation/Take.cs Mon Oct 12 00:49:32 2020 +0900 @@ -1,8 +1,6 @@ using System; namespace Christie_net.annotation { - [AttributeUsage(AttributeTargets.Field)] - public class Take : Attribute { - - } +[AttributeUsage(AttributeTargets.Field)] +public class Take : Attribute { } } \ No newline at end of file diff -r f48a2df9da65 -r e6f5b7d14dd1 annotation/TakeFrom.cs --- a/annotation/TakeFrom.cs Fri Oct 09 00:24:06 2020 +0900 +++ b/annotation/TakeFrom.cs Mon Oct 12 00:49:32 2020 +0900 @@ -1,12 +1,12 @@ using System; namespace Christie_net.annotation { - [AttributeUsage(AttributeTargets.Field)] - public class TakeFrom : Attribute { - private string _name; +[AttributeUsage(AttributeTargets.Field)] +public class TakeFrom : Attribute { + private string _name; - public String Value() { - return _name; - } + public string Value() { + return _name; } +} } \ No newline at end of file diff -r f48a2df9da65 -r e6f5b7d14dd1 codegear/CodeGear.cs --- a/codegear/CodeGear.cs Fri Oct 09 00:24:06 2020 +0900 +++ b/codegear/CodeGear.cs Mon Oct 12 00:49:32 2020 +0900 @@ -1,5 +1,5 @@ namespace Christie_net.codegear { - public abstract class CodeGear { - private InputDataGear idg; - } +public abstract class CodeGear { + private InputDataGear idg; +} } \ No newline at end of file diff -r f48a2df9da65 -r e6f5b7d14dd1 codegear/InputDataGear.cs --- a/codegear/InputDataGear.cs Fri Oct 09 00:24:06 2020 +0900 +++ b/codegear/InputDataGear.cs Mon Oct 12 00:49:32 2020 +0900 @@ -4,31 +4,27 @@ using Christie_net.datagear.dg; namespace Christie_net.codegear { - public class InputDataGear { - public ConcurrentDictionary> inputValue = new ConcurrentDictionary>(); - public CodeGearManager cgm; - public CodeGear cg; - private int count = 0; +public class InputDataGear { + public CodeGear cg; + public CodeGearManager cgm; + private int count; + public ConcurrentDictionary> inputValue = new ConcurrentDictionary>(); - public InputDataGear(CodeGear cg) { - this.cg = cg; - } - - private void FinishInput(CodeGearManager cgm, List commandList) { - this.cgm = cgm; - count = commandList.Count; + public InputDataGear(CodeGear cg) { + this.cg = cg; + } - if (count == 0) { - SubmitCG(); - } + private void FinishInput(CodeGearManager cgm, List commandList) { + this.cgm = cgm; + count = commandList.Count; + + if (count == 0) SubmitCG(); - foreach (Command cm in commandList) { - cgm.GetDGM(cm.toDgmName).RunCommand(cm); - } - } + foreach (Command cm in commandList) cgm.GetDGM(cm.toDgmName).RunCommand(cm); + } - public void SetInputs(string key, DataGear dg) { - inputValue.AddOrUpdate(key, dg); - } + public void SetInputs(string key, DataGear dg) { + inputValue.AddOrUpdate(key, dg); } +} } \ No newline at end of file diff -r f48a2df9da65 -r e6f5b7d14dd1 datagear/DataGearManager.cs --- a/datagear/DataGearManager.cs Fri Oct 09 00:24:06 2020 +0900 +++ b/datagear/DataGearManager.cs Mon Oct 12 00:49:32 2020 +0900 @@ -3,14 +3,14 @@ using Christie_net.datagear.dg; namespace Christie_net.datagear { - 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 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(); +} } \ No newline at end of file diff -r f48a2df9da65 -r e6f5b7d14dd1 datagear/DataGears.cs --- a/datagear/DataGears.cs Fri Oct 09 00:24:06 2020 +0900 +++ b/datagear/DataGears.cs Mon Oct 12 00:49:32 2020 +0900 @@ -1,39 +1,107 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; -using System.Linq; using Christie_net.datagear.command; namespace Christie_net.datagear.dg { - public class DataGears { - protected SortedDictionary>> dataGears = - new SortedDictionary>>(); - private object syncObject = new object(); +public class DataGears { + protected SortedDictionary>> dataGears = + new SortedDictionary>>(); + + private readonly object syncObject = new object(); - public void Put(string key, DataGear dg) { - lock (syncObject) { - + /// + /// 新しいdataGearの追加 + /// + /// + /// + public void Put(string key, DataGear dg) { + lock (syncObject) { + if (dataGears.ContainsKey(key)) { + dataGears[key].Enqueue(dg); + } else { + var queue = new ConcurrentQueue>(); + queue.Enqueue(dg); + dataGears.Add(key, queue); } } + } + + /// + /// DataGeraの取得と削除 + /// + /// + /// + public object Take(string key) { + object data = null; + // Queueからの取得は参照渡し + DataGear dataGear; + if (dataGears[key].TryDequeue(out dataGear)) { + data = dataGear.GetData(); + } + + // DataGeraを削除取り出ししたのでkeyも削除 + if (dataGears[key].IsEmpty) { + dataGears.Remove(key); + } - public object Take(string key) { - DataGear data; - if (dataGears[key].TryDequeue(out data)) { - data.GetData(); - } - return data; + return data; + } + + /// + /// DataGeraを削除せずに取得 + /// + /// + /// + public object Peek(string key) { + object data = null; + DataGear dataGear; + if (dataGears[key].TryPeek(out dataGear)) { + data = dataGear.GetData(); } - public object Peek(string key) { - return dataGears. - } + return data; + } - public object GetData(Command cm) { + /// + /// commandによりDataの取得 + /// + /// + /// + public object GetData(Command cm) { + lock (syncObject) { switch (cm.type) { - + case CommandType.TAKE: + case CommandType.REMOTETAKE: + return Take(cm.key); + case CommandType.PEEK: + case CommandType.REMOTEPEEK: + return Peek(cm.key); } + + return null; } - - public void SetData() + } + + /// + /// Dataを挿入する + /// + /// + public void SetData(Command cm) { + lock (syncObject) { + Put(cm.key, cm.dg); + } } + + /// + /// 既にkeyが含まれているか返す + /// + /// + /// + public bool ContainsKey(string key) { + lock (syncObject) { + return dataGears.ContainsKey(key); + } + } +} } \ No newline at end of file diff -r f48a2df9da65 -r e6f5b7d14dd1 datagear/command/Command.cs --- a/datagear/command/Command.cs Fri Oct 09 00:24:06 2020 +0900 +++ b/datagear/command/Command.cs Mon Oct 12 00:49:32 2020 +0900 @@ -1,39 +1,36 @@ using System; -using System.Data.Common; using Christie_net.codegear; using Christie_net.datagear.dg; namespace Christie_net.datagear.command { - public abstract class Command { - public CommandType type; - public string key = null; - public string toDgmName = null; // for take - public string fromDgmName = "local"; // for remotetake/reply - public int cgmID = -1; // for localtake - public CodeGear cg = null; // for localtake - public DataGear dg = null; // for put/localtake/reply - public Type clazz = null; // for remote - public Connection connection = null; // for reply +public abstract class Command { + public CodeGear cg; // for localtake + public int cgmID = -1; // for localtake + public Type clazz; // for remote + public Connection connection = null; // for reply + public DataGear dg; // for put/localtake/reply + public string fromDgmName = "local"; // for remotetake/reply + public string key; + public string toDgmName; // for take + public CommandType type; - public Command(CommandBuilder cb) { - this.type = type; - this.key = null; - this.toDgmName = null; - this.fromDgmName = "local"; - this.cgmID = null; - this.cg = null; - this.dg = null; - this.clazz = null; - this.connection = null; - } + public Command(CommandBuilder cb) { + type = type; + key = null; + toDgmName = null; + fromDgmName = "local"; + cgmID = null; + cg = null; + dg = null; + clazz = null; + connection = null; + } - // instead of any Constoractor args - protected void CheckNeedParam(ComanndBuilder cb) { - - } + // instead of any Constoractor args + protected void CheckNeedParam(ComanndBuilder cb) { } - public abstract void Execute(); + public abstract void Execute(); - public abstract - } + public abstract +} } \ No newline at end of file diff -r f48a2df9da65 -r e6f5b7d14dd1 datagear/command/CommandBuilder.cs --- a/datagear/command/CommandBuilder.cs Fri Oct 09 00:24:06 2020 +0900 +++ b/datagear/command/CommandBuilder.cs Mon Oct 12 00:49:32 2020 +0900 @@ -1,19 +1,15 @@ using System; + namespace Christie_net.datagear.command { - public class CommandBuilder { - protected CommandType type; // need - protected string kye = null; - protected string toDgmName = null; // for take - protected string fromDgmname = "local"; // for remotetake/reply - protected int? cgmID = null; // for local meta - protected CodeGear cg = null; // for localtake - protected DataGear dg = null; // for put/localtake/reply - protected Type clazz = null; // for remote - protected Connection connection = null; // for reply - - - - public CommandBuilder() { - } - } +public class CommandBuilder { + protected CodeGear cg = null; // for localtake + protected int? cgmID = null; // for local meta + protected Type clazz = null; // for remote + protected Connection connection = null; // for reply + protected DataGear dg = null; // for put/localtake/reply + protected string fromDgmname = "local"; // for remotetake/reply + protected string kye = null; + protected string toDgmName = null; // for take + protected CommandType type; // need } +} \ No newline at end of file diff -r f48a2df9da65 -r e6f5b7d14dd1 datagear/command/CommandType.cs --- a/datagear/command/CommandType.cs Fri Oct 09 00:24:06 2020 +0900 +++ b/datagear/command/CommandType.cs Mon Oct 12 00:49:32 2020 +0900 @@ -2,39 +2,37 @@ using System.Collections.Generic; namespace Christie_net.datagear.command { - public enum CommandType { - PUT, - TAKE, - PEEK, - REMOTETAKE, - REMOTEPEEK, - REPLY, - CLOSE, - FINISH - }; +public enum CommandType { + PUT, + TAKE, + PEEK, + REMOTETAKE, + REMOTEPEEK, + REPLY, + CLOSE, + FINISH +} - public static class CommandTypeEtx { - public static int id; // command ID - public static Dictionary hash = new Dictionary(); - private static int lastID = 0; // total command number +public static class CommandTypeEtx { + public static int id; // command ID + public static Dictionary hash = new Dictionary(); + private static int lastID; // total command number - static CommandTypeEtx() { - StoreValues(); - id = IncrementLastID(); - } + static CommandTypeEtx() { + StoreValues(); + id = IncrementLastID(); + } - private static int IncrementLastID() { - return ++lastID; - } + private static int IncrementLastID() { + return ++lastID; + } - public static CommandType GetCommandTypeFromID(int id) { - return hash[id]; - } + public static CommandType GetCommandTypeFromID(int id) { + return hash[id]; + } - public static void StoreValues() { - foreach (CommandType value in Enum.GetValues (typeof (CommandType))) { - hash.Add(Convert.ToInt32(value), value); - } - } + public static void StoreValues() { + foreach (CommandType value in Enum.GetValues(typeof(CommandType))) hash.Add(Convert.ToInt32(value), value); } +} } \ No newline at end of file diff -r f48a2df9da65 -r e6f5b7d14dd1 datagear/dg/DataGear.cs --- a/datagear/dg/DataGear.cs Fri Oct 09 00:24:06 2020 +0900 +++ b/datagear/dg/DataGear.cs Mon Oct 12 00:49:32 2020 +0900 @@ -1,61 +1,62 @@ using System; namespace Christie_net.datagear.dg { - public class DataGear { - protected T data; - protected Type clazz = null; +public class DataGear { + protected Type clazz; + protected T data; + + public DataGear(T data) { + SetClazz(data.GetType()); + SetData(data); + } - public DataGear(T data) { - SetClazz(data.GetType()); - SetData(data); + public DataGear(Type clazz) { + SetClazz(clazz); + } + + public void SetData(T data) { + var dataClazz = data.GetType(); + + if (dataClazz == clazz) { + this.data = data; + return; } - public DataGear(Type clazz) { - SetClazz(clazz); - } - - public void SetData(T data) { - Type dataClazz = data.GetType(); - - if (dataClazz == this.clazz) { + var dataSuperClazz = dataClazz.BaseType; + while (dataSuperClazz != null) { + if (dataClazz == clazz) { this.data = data; return; } - Type dataSuperClazz = dataClazz.BaseType; - while (dataSuperClazz != null) { - if (dataClazz == this.clazz) { - this.data = data; - return; - } - dataSuperClazz = dataSuperClazz.BaseType; - } - - Type[] interfaces = dataClazz.GetInterfaces(); - foreach (Type interfaze in interfaces) { - if (interfaze == this.clazz) { - this.data = data; - return; - } - } - - throw new InvalidCastException("datagear cannot set class from" + dataClazz.GetType().Name + " to " + data.GetType().Name); + dataSuperClazz = dataSuperClazz.BaseType; } - public void SetClazz(Type clazz) { - this.clazz = clazz; - } + var interfaces = dataClazz.GetInterfaces(); + foreach (var interfaze in interfaces) + if (interfaze == clazz) { + this.data = data; + return; + } - public Type GetClazz() { - return clazz; - } + throw new InvalidCastException("datagear cannot set class from" + dataClazz.GetType().Name + " to " + + data.GetType().Name); + } - public T GetData() { - return data; - } + public void SetClazz(Type clazz) { + this.clazz = clazz; + } + + public Type GetClazz() { + return clazz; + } - public override string ToString() { - return "DataGear : classname " + clazz; - } + public T GetData() { + return data; } + + public override string ToString() { + return "DataGear : classname " + clazz; + } +} } \ No newline at end of file diff -r f48a2df9da65 -r e6f5b7d14dd1 datagear/dg/MessagePackDataGear.cs --- a/datagear/dg/MessagePackDataGear.cs Fri Oct 09 00:24:06 2020 +0900 +++ b/datagear/dg/MessagePackDataGear.cs Mon Oct 12 00:49:32 2020 +0900 @@ -2,50 +2,45 @@ using MessagePack; namespace Christie_net.datagear.dg { - public class MessagePackDataGear : DataGear { - private byte[] messagePack = null; - private int dataSize; - private object syncObject = new object(); +public class MessagePackDataGear : DataGear { + private int dataSize; + private byte[] messagePack; + private readonly object syncObject = new object(); + + public MessagePackDataGear(T data) : base(data) { + GetMessagePack(); + } - public MessagePackDataGear(T data) : base(data){ - GetMessagePack(); + public MessagePackDataGear(Type clazz) : base(clazz) { } + + public byte[] GetMessagePack() { + if (messagePack != null) + return messagePack; + try { + messagePack = MessagePackSerializer.Serialize(data); + SetDataSize(messagePack.Length); + } catch (Exception e) { + Console.WriteLine(e.StackTrace); } - public MessagePackDataGear(Type clazz) : base(clazz) { - } + return messagePack; + } - public byte[] GetMessagePack() { - if (messagePack != null) { - return messagePack; - } - else { + public T GetData() { + lock (syncObject) { + if (data == null) try { - messagePack = MessagePackSerializer.Serialize(data); - SetDataSize(messagePack.Length); - } - catch (Exception e) { + SetData(MessagePackSerializer.Deserialize(messagePack)); + } catch (Exception e) { Console.WriteLine(e.StackTrace); } - } - return messagePack; - } - public T GetData() { - lock (syncObject) { - if (data == null) { - try { - SetData(MessagePackSerializer.Deserialize(messagePack)); - } - catch (Exception e) { - Console.WriteLine(e.StackTrace); - } - } - return base.GetData(); - } - } - - public void SetDataSize(int dataSize) { - this.dataSize = dataSize; + return base.GetData(); } } + + public void SetDataSize(int dataSize) { + this.dataSize = dataSize; + } +} } \ No newline at end of file