Skip to content

Commit

Permalink
Result is now correctly given, does break the interface again.
Browse files Browse the repository at this point in the history
  • Loading branch information
gieldid committed Feb 14, 2022
1 parent 481e63b commit 029c20c
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 29 deletions.
8 changes: 8 additions & 0 deletions Network/NetworkComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ public NetworkComponent(string name, string nickname, string description)
{
}
}

abstract public class TaskCapableNetworkComponent<T> : GH_TaskCapableComponent<T>
{
public TaskCapableNetworkComponent(string name, string nickname, string description)
: base(name, nickname, description, "Datawood", "Network")
{
}
}
}
108 changes: 80 additions & 28 deletions Network/SocketConnection/SocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

namespace DatawoodGH.Network.SocketConnection
{
public class SocketClient : NetworkComponent
public class SocketClient : TaskCapableNetworkComponent<SolveResults>
{
//Timeout for connecting to socket server in ms
public const int TimeOut = 5000;
/// <summary>
/// Initializes a new instance of the WebSocketComponent class.
Expand All @@ -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);

Expand All @@ -41,49 +41,101 @@ protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager
{
pManager.AddBooleanParameter("Finished","f","finished",GH_ParamAccess.item);
}


/// <summary>
/// This is the method that actually does the work.
/// </summary>
/// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
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<string> targets = new List<string>();
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<SolveResults> 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;
}

/// <summary>
Expand Down
14 changes: 14 additions & 0 deletions Network/SocketConnection/SolveResults.cs
Original file line number Diff line number Diff line change
@@ -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; }

}
}
2 changes: 1 addition & 1 deletion Network/SocketConnection/WaitCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 029c20c

Please sign in to comment.