changeset 27:efb06874a34e

update RemoteDataGearManager
author riono <e165729@ie.u-ryukyu.ac.jp>
date Tue, 19 Jan 2021 20:18:32 +0900
parents 45ff08d59fda
children 84e0b85af821
files codegear/ThreadPoolExecutors.cs daemon/ThreadPoolExecutors.cs datagear/DataGears.cs datagear/RemoteDataGearManager.cs datagear/WaitList.cs
diffstat 5 files changed, 74 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/codegear/ThreadPoolExecutors.cs	Tue Jan 12 21:23:23 2021 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,19 +0,0 @@
-using System.Threading;
-using System.Threading.Tasks;
-
-namespace Christie_net.codegear {
-public class ThreadPoolExecutors {
-
-    public ThreadPoolExecutors() {
-        
-    }
-    
-    public void CreateThreadPool() {
-        
-    }
-    
-    public void Execute(CodeGearExecutor command) {
-        Task.Factory.StartNew(() => command.Run());
-    }
-}
-}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/daemon/ThreadPoolExecutors.cs	Tue Jan 19 20:18:32 2021 +0900
@@ -0,0 +1,20 @@
+using System.Threading;
+using System.Threading.Tasks;
+using Christie_net.codegear;
+
+namespace Christie_net.daemon {
+public class ThreadPoolExecutors {
+
+    public ThreadPoolExecutors() {
+        
+    }
+    
+    public void CreateThreadPool() {
+        
+    }
+    
+    public void Execute(CodeGearExecutor command) {
+        Task.Factory.StartNew(() => command.Run());
+    }
+}
+}
\ No newline at end of file
--- a/datagear/DataGears.cs	Tue Jan 12 21:23:23 2021 +0900
+++ b/datagear/DataGears.cs	Tue Jan 19 20:18:32 2021 +0900
@@ -44,7 +44,7 @@
             }
 
             // DataGeraを削除取り出ししたのでkeyも削除
-            if (dataGears[key].IsCompleted) {
+            if (dataGears[key].Count == 0) {
                 dataGears[key].Dispose();
                 dataGears.Remove(key);
             }
--- a/datagear/RemoteDataGearManager.cs	Tue Jan 12 21:23:23 2021 +0900
+++ b/datagear/RemoteDataGearManager.cs	Tue Jan 19 20:18:32 2021 +0900
@@ -1,6 +1,8 @@
 using System;
+using System.Collections.Concurrent;
 using System.Net;
 using System.Net.Sockets;
+using System.Threading;
 using System.Threading.Tasks;
 using Christie_net.codegear;
 using Christie_net.daemon;
@@ -12,7 +14,7 @@
     private Connection connection;
     private CodeGearManager cgm;
     private bool connect = false;
-    private object lockObj = new object();
+    private object syncObj = new object();
     
     public RemoteDataGearManager (string address, int port, CodeGearManager cgm) {
         this.cgm = cgm;
@@ -30,6 +32,11 @@
 
                     Socket listener = socket.Accept();
                     connection = new Connection(listener, cgm);
+
+                    lock (syncObj) {
+                        connect = true;
+                        Monitor.Pulse(syncObj);
+                    }
                 } catch { }
             } while (!connect);
 
@@ -46,27 +53,65 @@
     }
     
     public override void Put(string key, object data) {
-        throw new NotImplementedException();
+        Command cm = new CommandBuilder().Init(CommandType.PUT).Key(key)
+            .Dg(new DataGear<object>(data)).Build();
+
+        // TODO: javaの方ではconnectがnullになってしまうときがあるらしい
+        // コンストラクタで呼び出されるThreadをやめて実効すればいいらしい
+        if (!connect) {
+            ConnectWait();
+        }
     }
 
     public override void RunCommand(Command cm) {
-        throw new NotImplementedException();
+        waitList.Add(cm);
+        CommandType type = cm.type;
+        switch (cm.type) {
+            case CommandType.PEEK:
+                type = CommandType.REMOTEPEEK;
+                break;
+            case CommandType.TAKE:
+                type = CommandType.REMOTETAKE;
+                break;
+        }
+
+        Command remoteCmd = new CommandBuilder().Init(type).FromDgmName(connection.name).Key(cm.key)
+            .Clazz(cm.clazz).Connection(connection).Build();
+
     }
 
     public override void ResolveWaitCommand(string key, DataGear<Type> dg) {
-        throw new NotImplementedException();
+        Command cm = waitList.GetAndRemoveCommand(key);
+        cm.SetDg(dg);
+        cm.Execute();
     }
 
     public override void Finish() {
-        throw new NotImplementedException();
+        Command cm = new CommandBuilder().Init(CommandType.FINISH).Build();
+        connection.SendCommand(cm);
     }
 
     public override void Close() {
-        throw new NotImplementedException();
+        Command cm = new CommandBuilder().Init(CommandType.CLOSE).Connection(connection).Build();
+        connection.SendCommand(cm);
     }
 
     public override void Shutdown() {
-        throw new NotImplementedException();
+        connection.Close();
+        BlockingCollection<Command> queue = connection.sendQueue;
+        if (queue.Count == 0) {
+            queue.Dispose();
+        }
+    }
+
+    public void ConnectWait() {
+        lock (syncObj) {
+            while (!connect) {
+                try {
+                    Monitor.Wait(syncObj);
+                } catch {}
+            }
+        }
     }
 }
 }
\ No newline at end of file
--- a/datagear/WaitList.cs	Tue Jan 12 21:23:23 2021 +0900
+++ b/datagear/WaitList.cs	Tue Jan 19 20:18:32 2021 +0900
@@ -25,7 +25,7 @@
             Command cm = null;
             if (waitlist.ContainsKey(key)) {
                 waitlist[key].TryTake(out cm);
-                if (waitlist[key].IsCompleted) {
+                if (waitlist[key].Count == 0) {
                     waitlist[key].Dispose();
                     waitlist.Remove(key);
                 }