changeset 8:e6f5b7d14dd1

update DataGears
author riono <e165729@ie.u-ryukyu.ac.jp>
date Mon, 12 Oct 2020 00:49:32 +0900
parents f48a2df9da65
children ce6906edcbf4
files Program.cs annotation/Peek.cs annotation/PeekFrom.cs annotation/Take.cs annotation/TakeFrom.cs codegear/CodeGear.cs codegear/InputDataGear.cs datagear/DataGearManager.cs datagear/DataGears.cs datagear/command/Command.cs datagear/command/CommandBuilder.cs datagear/command/CommandType.cs datagear/dg/DataGear.cs datagear/dg/MessagePackDataGear.cs
diffstat 14 files changed, 282 insertions(+), 238 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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
--- 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
--- 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
--- 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
--- 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
--- 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<string, DataGear<Type>> inputValue = new ConcurrentDictionary<string, DataGear<Type>>();
-        public CodeGearManager cgm;
-        public CodeGear cg;
-        private int count = 0;
+public class InputDataGear {
+    public CodeGear cg;
+    public CodeGearManager cgm;
+    private int count;
+    public ConcurrentDictionary<string, DataGear<Type>> inputValue = new ConcurrentDictionary<string, DataGear<Type>>();
 
-        public InputDataGear(CodeGear cg) {
-            this.cg = cg;
-        }
-
-        private void FinishInput(CodeGearManager cgm, List<Command> commandList) {
-            this.cgm = cgm;
-            count = commandList.Count;
+    public InputDataGear(CodeGear cg) {
+        this.cg = cg;
+    }
 
-            if (count == 0) {
-                SubmitCG();
-            }
+    private void FinishInput(CodeGearManager cgm, List<Command> 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<Type> dg) {
-            inputValue.AddOrUpdate(key, dg);
-        }
+    public void SetInputs(string key, DataGear<Type> dg) {
+        inputValue.AddOrUpdate(key, dg);
     }
+}
 }
\ No newline at end of file
--- 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<Type> 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<Type> dg);
+    public abstract void finish();
+    public abstract void close();
+    public abstract void shutdown();
+}
 }
\ No newline at end of file
--- 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<string, ConcurrentQueue<DataGear<Type>>> dataGears = 
-            new SortedDictionary<string, ConcurrentQueue<DataGear<Type>>>();
-        private object syncObject = new object();
+public class DataGears {
+    protected SortedDictionary<string, ConcurrentQueue<DataGear<Type>>> dataGears =
+        new SortedDictionary<string, ConcurrentQueue<DataGear<Type>>>();
+
+    private readonly object syncObject = new object();
 
-        public void Put(string key, DataGear<Type> dg) {
-            lock (syncObject) {
-                
+    /// <summary>
+    /// 新しいdataGearの追加
+    /// </summary>
+    /// <param name="key"></param>
+    /// <param name="dg"></param>
+    public void Put(string key, DataGear<Type> dg) {
+        lock (syncObject) {
+            if (dataGears.ContainsKey(key)) {
+                dataGears[key].Enqueue(dg);
+            } else {
+                var queue = new ConcurrentQueue<DataGear<Type>>();
+                queue.Enqueue(dg);
+                dataGears.Add(key, queue);
             }
         }
+    }
+
+    /// <summary>
+    /// DataGeraの取得と削除
+    /// </summary>
+    /// <param name="key"></param>
+    /// <returns></returns>
+    public object Take(string key) {
+        object data = null;
+        // Queueからの取得は参照渡し
+        DataGear<Type> 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<Type> data;
-            if (dataGears[key].TryDequeue(out data)) {
-                data.GetData();
-            }
-            return data;
+        return data;
+    }
+
+    /// <summary>
+    /// DataGeraを削除せずに取得
+    /// </summary>
+    /// <param name="key"></param>
+    /// <returns></returns>
+    public object Peek(string key) {
+        object data = null;
+        DataGear<Type> dataGear;
+        if (dataGears[key].TryPeek(out dataGear)) {
+            data = dataGear.GetData();
         }
 
-        public object Peek(string key) {
-            return dataGears.
-        }
+        return data;
+    }
 
-        public object GetData(Command cm) {
+    /// <summary>
+    /// commandによりDataの取得
+    /// </summary>
+    /// <param name="cm"></param>
+    /// <returns></returns>
+    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()
+    }
+
+    /// <summary>
+    /// Dataを挿入する
+    /// </summary>
+    /// <param name="cm"></param>
+    public void SetData(Command cm) {
+        lock (syncObject) {
+            Put(cm.key, cm.dg);
+        }
     }
+
+    /// <summary>
+    /// 既にkeyが含まれているか返す
+    /// </summary>
+    /// <param name="key"></param>
+    /// <returns></returns>
+    public bool ContainsKey(string key) {
+        lock (syncObject) {
+            return dataGears.ContainsKey(key);
+        }
+    }
+}
 }
\ No newline at end of file
--- 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<Type> 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<Type> 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
--- 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
--- 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<int, CommandType> hash = new Dictionary<int, CommandType>();
-        private static int lastID = 0;  // total command number
+public static class CommandTypeEtx {
+    public static int id; // command ID
+    public static Dictionary<int, CommandType> hash = new Dictionary<int, CommandType>();
+    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
--- 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<T> {
-        protected T data;
-        protected Type clazz = null;
+public class DataGear<T> {
+    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
--- 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<T> : DataGear<T> {
-        private byte[] messagePack = null;
-        private int dataSize;
-        private object syncObject = new object();
+public class MessagePackDataGear<T> : DataGear<T> {
+    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<T>(messagePack));
+                } catch (Exception e) {
                     Console.WriteLine(e.StackTrace);
                 }
-            }
-            return messagePack;
-        }
 
-        public T GetData() {
-            lock (syncObject) {
-                if (data == null) {
-                    try {
-                        SetData(MessagePackSerializer.Deserialize<T>(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