changeset 17:4a3115ba746d

fix CommandType enum
author riono <e165729@ie.u-ryukyu.ac.jp>
date Tue, 24 Nov 2020 03:11:50 +0900
parents 7352793b5dbe
children 8fe565f8acb8
files Christie_net.csproj Test/RewritingTest/EnumInit.cs datagear/DataGears.cs datagear/command/Command.cs datagear/command/CommandBuilder.cs datagear/command/CommandType.cs datagear/command/RemoteTakeCommand.cs
diffstat 7 files changed, 60 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/Christie_net.csproj	Sun Nov 22 01:52:22 2020 +0900
+++ b/Christie_net.csproj	Tue Nov 24 03:11:50 2020 +0900
@@ -3,7 +3,7 @@
     <PropertyGroup>
         <OutputType>Exe</OutputType>
         <TargetFramework>netcoreapp3.1</TargetFramework>
-        <StartupObject>SocketIPPortCheck</StartupObject>
+        <StartupObject>EnumInit</StartupObject>
     </PropertyGroup>
 
     <ItemGroup>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Test/RewritingTest/EnumInit.cs	Tue Nov 24 03:11:50 2020 +0900
@@ -0,0 +1,27 @@
+using System;
+using System.Collections.Generic;
+using Christie_net.datagear.command;
+
+
+public class EnumInit {
+    private static int nextId = 0;
+    public readonly int id = ++nextId;
+    public static readonly Dictionary<int, EnumInit> hash = new Dictionary<int, EnumInit>();
+
+    private EnumInit() {
+        hash.Add(id, this);
+    }
+
+    public static EnumInit GetEnumInit(int nid) {
+        return hash[nid];
+    }
+    
+    public static readonly EnumInit Val1 = new EnumInit();
+    public static readonly EnumInit Val2 = new EnumInit();
+
+    public static void Main() {
+        Console.WriteLine(Val1.id);
+        Console.WriteLine(GetEnumInit(1).id);
+        
+    }
+}
--- a/datagear/DataGears.cs	Sun Nov 22 01:52:22 2020 +0900
+++ b/datagear/DataGears.cs	Tue Nov 24 03:11:50 2020 +0900
@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Concurrent;
 using System.Collections.Generic;
+using Christie_net.annotation;
 using Christie_net.datagear.command;
 using Christie_net.datagear.dg;
 
@@ -71,15 +72,12 @@
     /// <returns></returns>
     public object GetData(Command cm) {
         lock (syncObject) {
-            switch (cm.type.commandType) {
-                case CommandType.TAKE:
-                case CommandType.REMOTETAKE:
-                    return Take(cm.key);
-                case CommandType.PEEK:
-                case CommandType.REMOTEPEEK:
-                    return Peek(cm.key);
+            CommandType type = cm.type;
+            if (type == CommandType.TAKE || type == CommandType.REMOTETAKE) {
+                return Take(cm.key);
+            } else if (type == CommandType.PEEK || type == CommandType.REMOTEPEEK){
+                return Peek(cm.key);
             }
-
             return null;
         }
     }
--- a/datagear/command/Command.cs	Sun Nov 22 01:52:22 2020 +0900
+++ b/datagear/command/Command.cs	Tue Nov 24 03:11:50 2020 +0900
@@ -6,8 +6,7 @@
 
 namespace Christie_net.datagear.command {
 public abstract class Command {
-    private CommandType _type;
-    public CommandTypeEtx type;
+    public CommandType type;
     public string key;
     public string toDgmName; // for take
     public string fromDgmName = "local"; // for remotetake/reply
@@ -18,8 +17,7 @@
     public Connection connection = null; // for reply
 
     public Command(CommandBuilder cb) {
-        this._type =  cb.type.commandType;
-        this.type = new CommandTypeEtx(_type);
+        this.type =  cb.type;
         this.key = cb.key;
         this.toDgmName = cb.toDgmName;
         this.fromDgmName = cb.fromDgmname;
@@ -47,7 +45,7 @@
     }
 
     public override string ToString() {
-        return "Command : type = " + type.commandType + ", key = " + key + "toDgmName = " + toDgmName +
+        return "Command : type = " + type + ", key = " + key + "toDgmName = " + toDgmName +
                " fromDgmName = " + fromDgmName + " cgmID = " + cgmID + " cg = " + cg + " dg = " + dg + " clazz = " +
                clazz + "connection = " + connection;
     }
--- a/datagear/command/CommandBuilder.cs	Sun Nov 22 01:52:22 2020 +0900
+++ b/datagear/command/CommandBuilder.cs	Tue Nov 24 03:11:50 2020 +0900
@@ -5,8 +5,7 @@
 
 namespace Christie_net.datagear.command {
 public class CommandBuilder {
-    private CommandType _type;
-    protected internal CommandTypeEtx type;
+    protected internal CommandType type;
     protected internal string key = null;
     protected internal string toDgmName = null; // for take
     protected internal string fromDgmname = "local"; // for remotetake/reply
@@ -19,8 +18,7 @@
     //private CommandFactory factory = new CommandFactory();
 
     public CommandBuilder init(CommandType type) {
-        this._type = type;
-        this.type = new CommandTypeEtx(_type);
+        this.type = type;
         this.key = null;
         this.toDgmName = null;
         this.fromDgmname = "local";
--- a/datagear/command/CommandType.cs	Sun Nov 22 01:52:22 2020 +0900
+++ b/datagear/command/CommandType.cs	Tue Nov 24 03:11:50 2020 +0900
@@ -2,43 +2,31 @@
 using System.Collections.Generic;
 
 namespace Christie_net.datagear.command {
-public enum CommandType {
-    PUT,
-    TAKE,
-    PEEK,
-    REMOTETAKE,
-    REMOTEPEEK,
-    REPLY,
-    CLOSE,
-    FINISH
-}
+public class CommandType {
+    private static int lastId = 0; // コマンドの総数
+    public readonly int id = ++lastId; // コマンドのid コンストラクタが呼ばれるたびにlastIdが++される
+    public static readonly Dictionary<int, CommandType> hash = new Dictionary<int, CommandType>();
 
-// C#ではenumに関数は生やせないためヘルパークラスを実装
-public class CommandTypeEtx {
-    public CommandType commandType;
-    public int id; // command ID
-    public static Dictionary<int, CommandType> hash = new Dictionary<int, CommandType>();  // コマンド対応表
-    private static int lastID = 0; // total command number
-
-    // コマンドごとにコンストラクタ内の処理をする必要がある
-    public CommandTypeEtx(CommandType type) {
-        commandType = type;
-        StoreValues();
-        id = IncrementLastID();
+    private CommandType() {
+        hash.Add(this.id, this);
     }
 
-    private static int IncrementLastID() {
-        return ++lastID;
-    }
-
-    public static CommandType GetCommandTypeFromID(int id) {
+    /// <summary>
+    /// idよりCommandTypeを返す
+    /// </summary>
+    /// <param name="id"></param>
+    /// <returns></returns>
+    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 readonly CommandType PUT = new CommandType();
+    public static readonly CommandType TAKE = new CommandType();
+    public static readonly CommandType PEEK = new CommandType();
+    public static readonly CommandType REMOTETAKE = new CommandType();
+    public static readonly CommandType REMOTEPEEK = new CommandType();
+    public static readonly CommandType REPLY = new CommandType();
+    public static readonly CommandType CLOSE = new CommandType();
+    public static readonly CommandType FINISH = new CommandType();
 }
 }
\ No newline at end of file
--- a/datagear/command/RemoteTakeCommand.cs	Sun Nov 22 01:52:22 2020 +0900
+++ b/datagear/command/RemoteTakeCommand.cs	Tue Nov 24 03:11:50 2020 +0900
@@ -8,7 +8,7 @@
     }
     
     public override void Execute() {
-        new CommandBuilder().init(CommandType.REPLY).    
+        //new CommandBuilder().init(CommandType.REPLY).    
     }
 
     public override MemoryStream Convert() {