Skip to content

Commit

Permalink
Merge pull request #115 from Sonderwoods/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Sonderwoods committed Nov 6, 2022
2 parents 5989f7e + 1d5ccc0 commit ed9590a
Show file tree
Hide file tree
Showing 29 changed files with 824 additions and 328 deletions.
Binary file modified Examples/TestRenders.gh
Binary file not shown.
9 changes: 9 additions & 0 deletions MantaRay.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ VisualStudioVersion = 17.1.32228.430
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MantaRay", "MantaRay\MantaRay.csproj", "{24A544EF-FACA-4732-9B5E-2B5DCC5C817C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "UnitTests\UnitTests.csproj", "{B6FFEA6A-E7C3-4447-84A0-88BD20F7F25A}"
ProjectSection(ProjectDependencies) = postProject
{24A544EF-FACA-4732-9B5E-2B5DCC5C817C} = {24A544EF-FACA-4732-9B5E-2B5DCC5C817C}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +20,10 @@ Global
{24A544EF-FACA-4732-9B5E-2B5DCC5C817C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{24A544EF-FACA-4732-9B5E-2B5DCC5C817C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{24A544EF-FACA-4732-9B5E-2B5DCC5C817C}.Release|Any CPU.Build.0 = Release|Any CPU
{B6FFEA6A-E7C3-4447-84A0-88BD20F7F25A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B6FFEA6A-E7C3-4447-84A0-88BD20F7F25A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B6FFEA6A-E7C3-4447-84A0-88BD20F7F25A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B6FFEA6A-E7C3-4447-84A0-88BD20F7F25A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
81 changes: 45 additions & 36 deletions MantaRay/Components/GH_Connect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
using MantaRay.Components;
using Rhino.Geometry;
using MantaRay.Setup;
using System.Runtime.InteropServices;

namespace MantaRay.Components
{
public class GH_Connect : GH_Template
public class GH_Connect : GH_Template, ISetConnection
{
/// <summary>
/// Initializes a new instance of the GH_Connect class.
Expand All @@ -29,6 +30,8 @@ public GH_Connect()

private string _pw;
int connectID = 0;
SSH_Helper sshHelper;
SSH_Helper ISetConnection.SshHelper => sshHelper;


/// <summary>
Expand Down Expand Up @@ -75,10 +78,12 @@ protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager
/// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
protected override void SolveInstance(IGH_DataAccess DA)
{

sshHelper = new SSH_Helper();
ManPageHelper.Initiate();
bool run = DA.Fetch<bool>(this, "connect");




// Moving to back will make sure this expires/runs before other objects when you load the file
Grasshopper.Instances.ActiveCanvas.Document.ArrangeObject(this, GH_Arrange.MoveToBack);
Expand Down Expand Up @@ -170,45 +175,45 @@ protected override void SolveInstance(IGH_DataAccess DA)

);

SSH_Helper.ExportPrefixes = string.IsNullOrEmpty(prefixes) ? SSH_Helper.ExportPrefixesDefault : prefixes;
sshHelper.ExportPrefixes = string.IsNullOrEmpty(prefixes) ? sshHelper.ExportPrefixesDefault : prefixes;


Stopwatch stopwatch = new Stopwatch();
//Connect SSH
SSH_Helper.SshClient = new SshClient(ConnNfo);
sshHelper.SshClient = new SshClient(ConnNfo);

if (!string.IsNullOrEmpty(winDir))
{
SSH_Helper.WindowsParentPath = winDir;
sshHelper.WindowsParentPath = winDir;
}
else
{
SSH_Helper.WindowsParentPath = SSH_Helper.DefaultWindowsParentPath;
sshHelper.WindowsParentPath = SSH_Helper.DefaultWindowsParentPath;
}



if (!string.IsNullOrEmpty(linDir))
{
SSH_Helper.LinuxParentPath = linDir;
sshHelper.LinuxParentPath = linDir;
}
else
{
SSH_Helper.LinuxParentPath = SSH_Helper.DefaultLinuxParentPath;
sshHelper.LinuxParentPath = SSH_Helper.DefaultLinuxParentPath;
}






if (!string.IsNullOrEmpty(subfolder))
{
SSH_Helper.ProjectSubPath = subfolder;
sshHelper.ProjectSubPath = subfolder;
}
else
{
SSH_Helper.ProjectSubPath = SSH_Helper.DefaultProjectSubFolder;
sshHelper.ProjectSubPath = SSH_Helper.DefaultProjectSubFolder;
}


Expand All @@ -219,8 +224,8 @@ protected override void SolveInstance(IGH_DataAccess DA)

try
{
SSH_Helper.HomeDirectory = null;
SSH_Helper.SshClient.Connect();
sshHelper.HomeDirectory = null;
sshHelper.SshClient.Connect();

sb.AppendFormat("SSH: Connected in {0} ms\n", stopwatch.ElapsedMilliseconds);

Expand Down Expand Up @@ -282,11 +287,11 @@ protected override void SolveInstance(IGH_DataAccess DA)
stopwatch.Restart();

//Connect Sftp
SSH_Helper.SftpClient = new SftpClient(ConnNfo);
sshHelper.SftpClient = new SftpClient(ConnNfo);
try
{
SSH_Helper.HomeDirectory = null;
SSH_Helper.SftpClient.Connect();
sshHelper.HomeDirectory = null;
sshHelper.SftpClient.Connect();

sb.AppendFormat("Sftp: Connected in {0} ms\n", stopwatch.ElapsedMilliseconds);

Expand All @@ -309,11 +314,11 @@ protected override void SolveInstance(IGH_DataAccess DA)

if (!string.IsNullOrEmpty(sftpDir))
{
SSH_Helper.SftpPath = linDir;
sshHelper.SftpHome = sftpDir + (sftpDir.Contains("/") ? "/" : "\\") + sshHelper.ProjectSubPath;
}
else
{
SSH_Helper.SftpPath = SSH_Helper.SftpClient.WorkingDirectory;
sshHelper.SftpHome = sshHelper.SftpClient.WorkingDirectory;
}
}
else
Expand All @@ -323,40 +328,42 @@ protected override void SolveInstance(IGH_DataAccess DA)
sb.Append("Sftp + SSH: Disconnected\n");
}

//SSH_Helper.Execute($"mkdir -p {SSH_Helper.LinuxFullpath}");
//sshHelper.Execute($"mkdir -p {sshHelper.LinuxFullpath}");


sb.AppendFormat("SSH: Created directory {0}\n\n", SSH_Helper.LinuxFullpath);
sb.AppendFormat("SSH: Created server directory {0}\n\n", sshHelper.LinuxHome);

int pad = 45;

sb.AppendFormat("SSH: Setup <WinHome> to {0}\n", SSH_Helper.WindowsFullpath);
sb.AppendFormat("SSH: Setup <LinuxHome> to {0}\n", SSH_Helper.LinuxFullpath);
sb.AppendFormat("SSH: Setup <Project> to {0}\n", SSH_Helper.ProjectSubPath);
sb.AppendFormat("SSH: Setup <SftpHome> to {0}\n", SSH_Helper.SftpPath);

GlobalsHelper.GlobalsFromConnectComponent["WinHome"] = SSH_Helper.WindowsFullpath;
GlobalsHelper.GlobalsFromConnectComponent["LinuxHome"] = SSH_Helper.LinuxFullpath;
GlobalsHelper.GlobalsFromConnectComponent["Project"] = SSH_Helper.ProjectSubPath;
GlobalsHelper.GlobalsFromConnectComponent["SftpHome"] = SSH_Helper.SftpPath;
sb.AppendFormat("SSH: Set <WinHome> to {0}\n", sshHelper.WinHome);
sb.AppendFormat("SSH: Set <LinuxHome> to {0}\n", sshHelper.LinuxHome);
sb.AppendFormat("SSH: Set <Project> to {0}\n", sshHelper.ProjectSubPath);
sb.AppendFormat("SSH: Set <SftpHome> to {0} This is used in the upload components\n", sshHelper.SftpHome.PadRight(pad, '.'));

if (SSH_Helper.CheckConnection() == SSH_Helper.ConnectionDetails.Connected)
GlobalsHelper.GlobalsFromConnectComponent["WinHome"] = sshHelper.WinHome;
GlobalsHelper.GlobalsFromConnectComponent["LinuxHome"] = sshHelper.LinuxHome;
GlobalsHelper.GlobalsFromConnectComponent["Project"] = sshHelper.ProjectSubPath;
GlobalsHelper.GlobalsFromConnectComponent["SftpHome"] = sshHelper.SftpHome;

if (sshHelper.CheckConnection() == SSH_Helper.ConnectionDetails.Connected)
{
string cpuSB = (int.Parse(SSH_Helper.Execute("nproc --all")) - 1).ToString();
string cpuSB = (int.Parse(sshHelper.Execute("nproc --all")) - 1).ToString();
GlobalsHelper.GlobalsFromConnectComponent["cpus"] = cpuSB;
sb.AppendFormat("SSH: Setup <cpus> to {0} (locally you would have used {1})\n", cpuSB, (Environment.ProcessorCount - 1).ToString());
sb.AppendFormat("SSH: Set <cpus> to {0} Locally you would have used {1}\n", cpuSB.PadRight(pad, '.'), (Environment.ProcessorCount - 1).ToString());

}

DA.SetData("status", sb.ToString());

//the run output
var runTree = new GH_Structure<GH_Boolean>();
runTree.Append(new GH_Boolean(SSH_Helper.CheckConnection() == SSH_Helper.ConnectionDetails.Connected));
DA.SetData(1, SSH_Helper.ExportPrefixes);
runTree.Append(new GH_Boolean(sshHelper.CheckConnection() == SSH_Helper.ConnectionDetails.Connected));
DA.SetData(1, sshHelper.ExportPrefixes);
Params.Output[Params.Output.Count - 1].ClearData();
DA.SetDataTree(Params.Output.Count - 1, runTree);

if (SSH_Helper.CheckConnection() != SSH_Helper.ConnectionDetails.Connected)
if (sshHelper.CheckConnection() != SSH_Helper.ConnectionDetails.Connected)
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "Not Connected.\n\nTry restarting SSH in your bash with:\nsudo service ssh start");


Expand All @@ -368,7 +375,7 @@ protected override void SolveInstance(IGH_DataAccess DA)

public void TryDisconnect()
{
SSH_Helper.Disconnect();
sshHelper.Disconnect();
}

public override void RemovedFromDocument(GH_Document document)
Expand Down Expand Up @@ -419,7 +426,7 @@ public override void DocumentContextChanged(GH_Document document, GH_DocumentCon

this.Params.Input[connectID].ClearData();

if (isRunSet && SSH_Helper.CheckConnection() != SSH_Helper.ConnectionDetails.Connected)
if (isRunSet && sshHelper.CheckConnection() != SSH_Helper.ConnectionDetails.Connected)
{

document.ScheduleSolution(100, (e) => this.ExpireSolution(true));
Expand Down Expand Up @@ -463,6 +470,8 @@ public override Guid ComponentGuid
get { return new Guid("1B57442F-E5FE-4462-9EB0-564497CB076E"); }
}



private bool GetCredentials(string username, string ip, out string password)
{
bool localIp = string.Equals(ip, "127.0.0.1") || string.Equals(ip, "localhost");
Expand Down
63 changes: 59 additions & 4 deletions MantaRay/Components/GH_ConvertPaths.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
using System.Collections.Generic;

using System.Drawing;
using ClipperLib;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Parameters;
using MantaRay.Components;
using Rhino.Geometry;
using MantaRay.Helpers;

namespace MantaRay.Components
{
public class GH_ConvertPaths : GH_Template
public class GH_ConvertPaths : GH_Template, IGH_VariableParameterComponent
{
/// <summary>
/// Initializes a new instance of the GH_ToLinux class.
Expand All @@ -35,7 +38,7 @@ protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager
{
pManager.AddTextParameter("Linux", "L", "Linux", GH_ParamAccess.item);
pManager.AddTextParameter("Windows", "W", "Windows", GH_ParamAccess.item);
pManager.AddTextParameter("Sftp", "F", "Sftp", GH_ParamAccess.item);
//pManager.AddTextParameter("Sftp", "F", "Sftp", GH_ParamAccess.item);
}

/// <summary>
Expand All @@ -46,14 +49,66 @@ protected override void SolveInstance(IGH_DataAccess DA)
{
string path = DA.Fetch<string>(this, 0);

if (SSH_Helper.CheckConnection() != SSH_Helper.ConnectionDetails.Connected)
SSH_Helper sshHelper = SSH_Helper.CurrentFromDocument(OnPingDocument());
if (sshHelper == null)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "No connection");
return;
}

if (sshHelper.CheckConnection() != SSH_Helper.ConnectionDetails.Connected)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Error, "No Connection");
return;
}
DA.SetData(0, path.ToLinuxPath());
DA.SetData(1, path.ToWindowsPath());
DA.SetData(2, path.ToSftpPath());

if (Params.Output.Count == 3)
{
DA.SetData(2, path.ToSftpPath());
}

}

bool IGH_VariableParameterComponent.CanInsertParameter(GH_ParameterSide side, int index)
{
return side == GH_ParameterSide.Output && index == 2 && Params.Output.Count == 2;
}

bool IGH_VariableParameterComponent.CanRemoveParameter(GH_ParameterSide side, int index)
{
return side == GH_ParameterSide.Output && index == 2;
}

IGH_Param IGH_VariableParameterComponent.CreateParameter(GH_ParameterSide side, int index)
{

var param = new Param_String { NickName = "-" };
return param;


}

bool IGH_VariableParameterComponent.DestroyParameter(GH_ParameterSide side, int index)
{
return true;
}

void IGH_VariableParameterComponent.VariableParameterMaintenance()
{

if (Params.Output.Count == 3)
{
var param = Params.Output[2];
if (param.NickName == "-")
{
param.NickName = "F";
param.Name = "Sftp";
param.Access = GH_ParamAccess.item;
}
}

}

protected override Bitmap Icon => Resources.Resources.Ra_Paths_Icon;
Expand Down
14 changes: 11 additions & 3 deletions MantaRay/Components/GH_Download.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,19 @@ protected override void SolveInstance(IGH_DataAccess DA)

if (!CheckIfRunOrUseOldResults(DA, 0)) return; //template

SSH_Helper sshHelper = SSH_Helper.CurrentFromDocument(OnPingDocument());
if (sshHelper == null)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "No connection");
return;
}


string targetFolder = DA.Fetch<string>(this, "Target local folder");

if (targetFolder == null || String.IsNullOrEmpty(targetFolder))
{
targetFolder = SSH_Helper.WindowsFullpath;
targetFolder = sshHelper.WinHome;
}

string localTargetFolder = targetFolder; // Path.GetDirectoryName(targetFolder);
Expand All @@ -74,8 +81,9 @@ protected override void SolveInstance(IGH_DataAccess DA)

foreach (var file in allFilePaths)
{
SSH_Helper.Download(file, localTargetFolder, sb);
localFilePaths.Add(localTargetFolder + "\\" + Path.GetFileName(file));
var file2 = file.Trim('\r', '\n');
sshHelper.Download(file2, localTargetFolder, sb);
localFilePaths.Add(localTargetFolder + "\\" + Path.GetFileName(file2));
}

OldResults = localFilePaths.ToArray();
Expand Down
6 changes: 3 additions & 3 deletions MantaRay/Components/GH_ExecuteAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -441,11 +441,11 @@ public override void DoWork(Action<string, double> ReportProgress, Action Done)

string command = String.Join(";echo _JOIN_;", commands).Replace("\r\n", "\n").ApplyGlobals();


SSH_Helper sshHelper = SSH_Helper.CurrentFromDocument(Parent.OnPingDocument());


Renci.SshNet.SshCommand cmd = null;
(asyncResult, cmd, pid) = SSH_Helper.ExecuteAsync(command, prependPrefix: ((GH_ExecuteAsync)Parent).addPrefix, ((GH_ExecuteAsync)Parent).addSuffix, HasZeroAreaPolygons);
(asyncResult, cmd, pid) = sshHelper.ExecuteAsync(command, prependPrefix: ((GH_ExecuteAsync)Parent).addPrefix, ((GH_ExecuteAsync)Parent).addSuffix, HasZeroAreaPolygons);


// TODO Need to get pid through "beginexecute" instead of "execute" of SSH.
Expand Down Expand Up @@ -493,7 +493,7 @@ public override void DoWork(Action<string, double> ReportProgress, Action Done)
break;
}

if (SSH_Helper.CheckConnection() != SSH_Helper.ConnectionDetails.Connected && waitingForNewConnection-- <= 0)
if (sshHelper.CheckConnection() != SSH_Helper.ConnectionDetails.Connected && waitingForNewConnection-- <= 0)
{
((GH_ExecuteAsync)Parent).RequestCancellation();
//ran = false;
Expand Down
Loading

0 comments on commit ed9590a

Please sign in to comment.