diff --git a/Network/NetworkComponent.cs b/Network/NetworkComponent.cs index e103449..1d15c48 100644 --- a/Network/NetworkComponent.cs +++ b/Network/NetworkComponent.cs @@ -14,4 +14,12 @@ public NetworkComponent(string name, string nickname, string description) { } } + + abstract public class TaskCapableNetworkComponent : GH_TaskCapableComponent + { + public TaskCapableNetworkComponent(string name, string nickname, string description) + : base(name, nickname, description, "Datawood", "Network") + { + } + } } diff --git a/Network/SocketConnection/SocketClient.cs b/Network/SocketConnection/SocketClient.cs index 057f42b..a3528f2 100644 --- a/Network/SocketConnection/SocketClient.cs +++ b/Network/SocketConnection/SocketClient.cs @@ -9,8 +9,9 @@ namespace DatawoodGH.Network.SocketConnection { - public class SocketClient : NetworkComponent + public class SocketClient : TaskCapableNetworkComponent { + //Timeout for connecting to socket server in ms public const int TimeOut = 5000; /// /// Initializes a new instance of the WebSocketComponent class. @@ -28,7 +29,6 @@ protected override void RegisterInputParams(GH_Component.GH_InputParamManager pM { pManager.AddTextParameter("Ip", "IP", "IP to make a web socket connection to", GH_ParamAccess.item); pManager.AddIntegerParameter("Port","P","Port for the socket connection",GH_ParamAccess.item); - //pManager.AddTextParameter("Targets", "T", "Robottargets", GH_ParamAccess.list); pManager.AddTextParameter("Mod file", "m", "Mod file to read", GH_ParamAccess.item); pManager.AddBooleanParameter("Run", "R", "When to run", GH_ParamAccess.item, true); @@ -41,49 +41,101 @@ protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager { pManager.AddBooleanParameter("Finished","f","finished",GH_ParamAccess.item); } + /// /// This is the method that actually does the work. /// /// The DA object is used to retrieve from inputs and store in outputs. - protected override async void SolveInstance(IGH_DataAccess DA) + protected override void SolveInstance(IGH_DataAccess DA) { - string ip = null; - int port = 0; - bool run = true; - string path = null; - //List targets = new List(); + if (InPreSolve) + { + string ip = null; + int port = 0; + bool run = true; + string path = null; + + if (!DA.GetData("Ip", ref ip)) + { + return; + } - if (!DA.GetData("Ip", ref ip)) { - return; - } + if (!DA.GetData("Port", ref port)) + { + return; + } - if (!DA.GetData("Port", ref port)) { + if (!DA.GetData("Mod file", ref path)) + { + return; + } + DA.GetData("Run", ref run); + if (!run) { + return; + } + Task task = Task.Run(() => ComputeSockets(path, ip, port), CancelToken); + TaskList.Add(task); return; } - //if (!DA.GetDataList("Targets", targets)) { - // return; - //} + if(!GetSolveResults(DA, out SolveResults result)) { - if (!DA.GetData("Mod file", ref path)) { - return; - } + string ip = null; + int port = 0; + bool run = true; + string path = null; - DA.GetData("Run", ref run); - if (run) { - ModFileObject mod = new ModFileObject(path); - try + if (!DA.GetData("Ip", ref ip)) { - Socket client = SocketConnection(ip, port); - await SendCommands(client, mod.Commands); - CloseConnection(client); - DA.SetData("Finished", true); + return; } - catch (Exception ex) { - this.Message = ex.Message; + + if (!DA.GetData("Port", ref port)) + { + return; } + + if (!DA.GetData("Mod file", ref path)) + { + return; + } + DA.GetData("Run", ref run); + if (!run) + { + return; + } + + result = ComputeSockets(path, ip, port); + } + + + if (result != null) { + DA.SetData("Finished", result.Value); + } + } + + private SolveResults ComputeSockets(string path, string ip, int port) { + SolveResults result = new SolveResults + { + Value = false + }; + ModFileObject mod = new ModFileObject(path); + try + { + Socket client = SocketConnection(ip, port); + Task task = SendCommands(client, mod.Commands); + task.Wait(); + CloseConnection(client); + result.Value = true; + + } + catch (Exception ex) + { + result.Value = false; + Message = ex.Message; } + return result; } /// diff --git a/Network/SocketConnection/SolveResults.cs b/Network/SocketConnection/SolveResults.cs new file mode 100644 index 0000000..db2b79c --- /dev/null +++ b/Network/SocketConnection/SolveResults.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DatawoodGH.Network.SocketConnection +{ + public class SolveResults + { + public bool Value { get; set; } + + } +} diff --git a/Network/SocketConnection/WaitCommand.cs b/Network/SocketConnection/WaitCommand.cs index 6712f64..be75157 100644 --- a/Network/SocketConnection/WaitCommand.cs +++ b/Network/SocketConnection/WaitCommand.cs @@ -37,7 +37,7 @@ public override async Task SendOverSocket(Socket client) private int ConvertSecToMs(float waitValue) { - int waitTimeMS = (int)(WaitTime * 1000); + int waitTimeMS = (int)(waitValue * 1000); return waitTimeMS; } }