8000 refactor runspace name generation code · PowerShell/PowerShell@d49dea6 · GitHub
[go: up one dir, main page]

Skip to content

Commit d49dea6

Browse files
committed
refactor runspace name generation code
add Transport property
1 parent 5c8432e commit d49dea6

File tree

4 files changed

+39
-60
lines changed

4 files changed

+39
-60
lines changed

src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -999,6 +999,7 @@ private static IEnumerable<FormatViewDefinition> ViewsOf_System_Management_Autom
999999
TableControl.Create()
10001000
.AddHeader(Alignment.Right, label: "Id", width: 3)
10011001
.AddHeader(Alignment.Left, label: "Name", width: 15)
1002+
.AddHeader(Alignment.Left, label: "Transport", width: 9)
10021003
.AddHeader(Alignment.Left, label: "ComputerName", width: 15)
10031004
.AddHeader(Alignment.Left, label: "ComputerType", width: 15)
10041005
.AddHeader(Alignment.Left, label: "State", width: 13)
@@ -1007,6 +1008,7 @@ private static IEnumerable<FormatViewDefinition> ViewsOf_System_Management_Autom
10071008
.StartRowDefinition()
10081009
.AddPropertyColumn("Id")
10091010
.AddPropertyColumn("Name")
1011+
.AddPropertyColumn("Transport")
10101012
.AddPropertyColumn("ComputerName")
10111013
.AddPropertyColumn("ComputerType")
10121014
.AddPropertyColumn("State")

src/System.Management.Automation/engine/remoting/client/remoterunspaceinfo.cs

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,16 @@ public Runspace Runspace
192192
}
193193
}
194194

195+
/// <summary>
196+
/// Name of the transport used.
197+
/// </summary>
198+
public String Transport
199+
{
200+
get
201+
{
202+
return GetTransportName();
203+
}
204+
}
195205
#endregion Public Properties
196206

197207
#region Public Methods
@@ -263,7 +273,7 @@ internal PSSession(RemoteRunspace remoteRunspace)
263273
}
264274
else
265275
{
266-
Name = AutoGenerateRunspaceName(Id);
276+
Name = "Runspace" + Id;
267277
remoteRunspace.PSSessionName = Name;
268278
}
269279

@@ -319,34 +329,35 @@ internal PSSession(RemoteRunspace remoteRunspace)
319329
/// Generates and returns the runspace name
320330
/// </summary>
321< 8000 /code>331
/// <returns>auto generated name</returns>
322-
private string AutoGenerateRunspaceName(int id)
332+
private string GetTransportName()
323333
{
324-
string sessionIdStr = id.ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
325-
326334
if (_remoteRunspace.ConnectionInfo is WSManConnectionInfo)
327335
{
328-
return "WinRM" + sessionIdStr;
336+
return "WSMan";
329337
}
330338
else if (_remoteRunspace.ConnectionInfo is SSHConnectionInfo)
331339
{
332-
return "SSH" + sessionIdStr;
340+
return "SSH";
333341
}
334-
else if ((_remoteRunspace.ConnectionInfo is NamedPipeConnectionInfo) ||
335-
(_remoteRunspace.ConnectionInfo is ContainerConnectionInfo))
342+
else if (_remoteRunspace.ConnectionInfo is NamedPipeConnectionInfo)
336343
{
337-
return "NamedPipe" + sessionIdStr;
344+
return "NamedPipe" 3269 ;
345+
}
346+
else if (_remoteRunspace.ConnectionInfo is ContainerConnectionInfo)
347+
{
348+
return "Container";
338349
}
339350
else if (_remoteRunspace.ConnectionInfo is NewProcessConnectionInfo)
340351
{
341-
return "Process" + sessionIdStr;
352+
return "Process";
342353
}
343354
else if (_remoteRunspace.ConnectionInfo is VMConnectionInfo)
344355
{
345-
return "Socket" + sessionIdStr;
356+
return "VMBus";
346357
}
347358
else
348359
{
349-
return "Session" + sessionIdStr;
360+
return "Unknown";
350361
}
351362
}
352363

@@ -368,26 +379,15 @@ private string GetDisplayShellName(string shell)
368379
#region Static Methods
369380

370381
/// <summary>
371-
/// Generates a unique runspace id and name.
372-
/// </summary>
373-
/// <param name="transport">Transport being used</param>
374-
/// <param name="rtnId">Returned Id</param>
375-
/// <returns>Returned name</returns>
376-
internal static String GenerateRunspaceName(string transport, out int rtnId)
377-
{
378-
int id = System.Threading.Interlocked.Increment(ref s_seed);
379-
rtnId = id;
380-
return ComposeRunspaceName(transport, id);
381-
}
382-
383-
/// <summary>
384-
/// Generates a unique runspace id and name. Assumes WSMan transport.
382+
/// Generates a unique runspace id.
385383
/// </summary>
386384
/// <param name="rtnId">Returned Id</param>
387385
/// <returns>Returned name</returns>
388386
internal static String GenerateRunspaceName(out int rtnId)
389387
{
390-
return GenerateRunspaceName("WSMan", out rtnId);
388+
int id = GenerateRunspaceId();
389+
rtnId = id;
390+
return "Runspace" + id.ToString();
391391
}
392392

393393
/// <summary>
@@ -399,28 +399,6 @@ internal static int GenerateRunspaceId()
399399
return System.Threading.Interlocked.Increment(ref s_seed);
400400
}
401401

402-
/// <summary>
403-
/// Creates a runspace name based on a given Id value.
404-
/// </summary>
405-
/// <param name="transport">Transport being used</param>
406-
/// <param name="id">Integer Id</param>
407-
/// <returns>Runspace name</returns>
408-
internal static string ComposeRunspaceName(string transport, int id)
409-
{
410-
Dbg.Assert(transport != null, "Transport is null");
411-
return transport + id.ToString(System.Globalization.NumberFormatInfo.InvariantInfo);
412-
}
413-
414-
/// <summary>
415-
/// Creates a runspace name based on a given Id value.
416-
/// </summary>
417-
/// <param name="id">Integer Id</param>
418-
/// <returns>Runspace name</returns>
419-
internal static string ComposeRunspaceName(int id)
420-
{
421-
return ComposeRunspaceName("WSMan", id);
422-
}
423-
424402
#endregion
425403
}
426404
}

src/System.Management.Automation/engine/remoting/commands/PSRemotingCmdlet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1308,7 +1308,7 @@ protected virtual void CreateHelpersForSpecifiedComputerNames()
13081308
// it can be easily identified if it becomes disconnected and is queried on the server.
13091309
int rsId = PSSession.GenerateRunspaceId();
13101310
string rsName = (DisconnectedSessionName != null && DisconnectedSessionName.Length > i) ?
1311-
DisconnectedSessionName[i] : PSSession.ComposeRunspaceName(rsId);
1311+
DisconnectedSessionName[i] : PSSession.GenerateRunspaceName(out rsId);
13121312

13131313
remoteRunspace = new RemoteRunspace(Utils.GetTypeTableFromExecutionContextTLS(), connectionInfo,
13141314
this.Host, this.SessionOption.ApplicationArguments, rsName, rsId);

src/System.Management.Automation/engine/remoting/commands/newrunspacecommand.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ private List<RemoteRunspace> CreateRunspacesWhenRunspaceParameterSpecified()
679679

680680
// Create new remote runspace with name and Id.
681681
int rsId;
682-
string rsName = GetRunspaceName("WSMan", rsIndex, out rsId);
682+
string rsName = GetRunspaceName(rsIndex, out rsId);
683683
RemoteRunspace newRemoteRunspace = new RemoteRunspace(
684684
typeTable, newConnectionInfo, this.Host, this.SessionOption.ApplicationArguments,
685685
rsName, rsId);
@@ -740,7 +740,7 @@ private List<RemoteRunspace> CreateRunspacesWhenUriParameterSpecified()
740740

741741
// Create new remote runspace with name and Id.
742742
int rsId;
743-
string rsName = GetRunspaceName("WSMan", i, out rsId);
743+
string rsName = GetRunspaceName(i, out rsId);
744744
RemoteRunspace remoteRunspace = new RemoteRunspace(
745745
Utils.GetTypeTableFromExecutionContextTLS(), connectionInfo, this.Host,
746746
this.SessionOption.ApplicationArguments, rsName, rsId);
@@ -815,7 +815,7 @@ private List<RemoteRunspace> CreateRunspacesWhenComputerNameParameterSpecified()
815815

816816
// Create new remote runspace with name and Id.
817817
int rsId;
818-
string rsName = GetRunspaceName("WSMan", i, out rsId);
818+
string rsName = GetRunspaceName(i, out rsId);
819819
RemoteRunspace runspace = new RemoteRunspace(
820820
Utils.GetTypeTableFromExecutionContextTLS(), connectionInfo, this.Host,
821821
this.SessionOption.ApplicationArguments, rsName, rsId);
@@ -948,7 +948,7 @@ private List<RemoteRunspace> CreateRunspacesWhenVMParameterSpecified()
948948
RemoteRunspace runspace = null;
949949
VMConnectionInfo connectionInfo;
950950
int rsId;
951-
string rsName = GetRunspaceName("VMBus", index, out rsId);
951+
string rsName = GetRunspaceName(index, out rsId);
952952

953953
try
954954
{
@@ -1004,7 +1004,7 @@ private List<RemoteRunspace> CreateRunspacesWhenContainerParameterSpecified()
10041004
RemoteRunspace runspace = null;
10051005
ContainerConnectionInfo connectionInfo = null;
10061006
int rsId;
1007-
string rsName = GetRunspaceName("Container", index, out rsId);
1007+
string rsName = GetRunspaceName(index, out rsId);
10081008
index++;
10091009

10101010
try
@@ -1082,7 +1082,7 @@ private List<RemoteRunspace> CreateRunspacesForSSHHostParameterSet()
10821082
this.KeyFilePath,
10831083
this.Port);
10841084
var typeTable = TypeTable.LoadDefaultTypeFiles();
1085-
string rsName = GetRunspaceName("SSH", index, out int rsIdUnused);
1085+
string rsName = GetRunspaceName(index, out int rsIdUnused);
10861086
index++;
10871087
remoteRunspaces.Add(RunspaceFactory.CreateRunspace( connectionInfo : sshConnectionInfo,
10881088
host : this.Host,
@@ -1107,7 +1107,7 @@ private List<RemoteRunspace> CreateRunspacesForSSHHostHashParameterSet()
11071107
sshConnection.KeyFilePath,
11081108
sshConnection.Port);
11091109
var typeTable = TypeTable.LoadDefaultTypeFiles();
1110-
string rsName = GetRunspaceName("SSH", index, out int rsIdUnused);
1110+
string rsName = GetRunspaceName(index, out int rsIdUnused);
11111111
index++;
11121112
remoteRunspaces.Add(RunspaceFactory.CreateRunspace( connectionInfo : sshConnectionInfo,
11131113
host : this.Host,
@@ -1123,14 +1123,13 @@ private List<RemoteRunspace> CreateRunspacesForSSHHostHashParameterSet()
11231123
/// Helper method to either get a user supplied runspace/session name
11241124
/// or to generate one along with a unique Id.
11251125
/// </summary>
1126-
/// <param name="transport">Transport used.</param>
11271126
/// <param name="rsIndex">Runspace name array index.</param>
11281127
/// <param name="rsId">Runspace Id.</param>
11291128
/// <returns>Runspace name.</returns>
1130-
private string GetRunspaceName(string transport, int rsIndex, out int rsId)
1129+
60F2 private string GetRunspaceName(int rsIndex, out int rsId)
11311130
{
11321131
// Get a unique session/runspace Id and default Name.
1133-
string rsName = PSSession.GenerateRunspaceName(transport, out rsId);
1132+
string rsName = PSSession.GenerateRunspaceName(out rsId);
11341133

11351134
// If there is a friendly name for the runspace, we need to pass it to the
11361135
// runspace pool object, which in turn passes it on to the server during

0 commit comments

Comments
 (0)
0