Allow listing devices from remote.

This commit is contained in:
2019-10-12 23:12:39 +01:00
parent 58b2b84f0a
commit 580b8e0981
3 changed files with 60 additions and 27 deletions

View File

@@ -33,6 +33,7 @@
using System; using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using DiscImageChef.CommonTypes.Interop; using DiscImageChef.CommonTypes.Interop;
using DiscImageChef.Console;
using PlatformID = DiscImageChef.CommonTypes.Interop.PlatformID; using PlatformID = DiscImageChef.CommonTypes.Interop.PlatformID;
namespace DiscImageChef.Devices namespace DiscImageChef.Devices
@@ -60,15 +61,30 @@ namespace DiscImageChef.Devices
public partial class Device public partial class Device
{ {
public static DeviceInfo[] ListDevices() public static DeviceInfo[] ListDevices(string dicRemote = null)
{ {
if (dicRemote is null)
switch (DetectOS.GetRealPlatformID()) switch (DetectOS.GetRealPlatformID())
{ {
case PlatformID.Win32NT: return Windows.ListDevices.GetList(); case PlatformID.Win32NT: return Windows.ListDevices.GetList();
case PlatformID.Linux: return Linux.ListDevices.GetList(); case PlatformID.Linux: return Linux.ListDevices.GetList();
case PlatformID.FreeBSD: return FreeBSD.ListDevices.GetList(); case PlatformID.FreeBSD: return FreeBSD.ListDevices.GetList();
default: default:
throw new InvalidOperationException($"Platform {DetectOS.GetRealPlatformID()} not yet supported."); throw new InvalidOperationException(
$"Platform {DetectOS.GetRealPlatformID()} not yet supported.");
}
try
{
using (var remote = new Remote.Remote(dicRemote))
{
return remote.ListDevices();
}
}
catch (Exception)
{
DicConsole.ErrorWriteLine("Error connecting to host.");
return new DeviceInfo[0];
} }
} }
} }

View File

@@ -10,7 +10,7 @@ using Version = DiscImageChef.CommonTypes.Interop.Version;
namespace DiscImageChef.Devices.Remote namespace DiscImageChef.Devices.Remote
{ {
public class Remote public class Remote : IDisposable
{ {
private readonly Socket _socket; private readonly Socket _socket;
@@ -115,10 +115,20 @@ namespace DiscImageChef.Devices.Remote
public string ServerArchitecture { get; } public string ServerArchitecture { get; }
public int ServerProtocolVersion { get; } public int ServerProtocolVersion { get; }
public void Dispose()
{
Disconnect();
}
public void Disconnect() public void Disconnect()
{ {
_socket.Shutdown(SocketShutdown.Both); _socket.Shutdown(SocketShutdown.Both);
_socket.Close(); _socket.Close();
} }
public DeviceInfo[] ListDevices()
{
return new DeviceInfo[0];
}
} }
} }

View File

@@ -40,9 +40,9 @@ using Mono.Options;
namespace DiscImageChef.Commands namespace DiscImageChef.Commands
{ {
class ListDevicesCommand : Command internal class ListDevicesCommand : Command
{ {
bool showHelp; private bool showHelp;
public ListDevicesCommand() : base("list-devices", "Lists all connected devices.") public ListDevicesCommand() : base("list-devices", "Lists all connected devices.")
{ {
@@ -51,7 +51,7 @@ namespace DiscImageChef.Commands
$"{MainClass.AssemblyTitle} {MainClass.AssemblyVersion?.InformationalVersion}", $"{MainClass.AssemblyTitle} {MainClass.AssemblyVersion?.InformationalVersion}",
$"{MainClass.AssemblyCopyright}", $"{MainClass.AssemblyCopyright}",
"", "",
$"usage: DiscImageChef {Name}", $"usage: DiscImageChef {Name} [dic-remote-host]",
"", "",
Help, Help,
{"help|h|?", "Show this message and exit.", v => showHelp = v != null} {"help|h|?", "Show this message and exit.", v => showHelp = v != null}
@@ -60,31 +60,38 @@ namespace DiscImageChef.Commands
public override int Invoke(IEnumerable<string> arguments) public override int Invoke(IEnumerable<string> arguments)
{ {
List<string> extra = Options.Parse(arguments); var extra = Options.Parse(arguments);
if(showHelp) if (showHelp)
{ {
Options.WriteOptionDescriptions(CommandSet.Out); Options.WriteOptionDescriptions(CommandSet.Out);
return (int)ErrorNumber.HelpRequested; return (int) ErrorNumber.HelpRequested;
} }
MainClass.PrintCopyright(); MainClass.PrintCopyright();
if(MainClass.Debug) DicConsole.DebugWriteLineEvent += System.Console.Error.WriteLine; if (MainClass.Debug) DicConsole.DebugWriteLineEvent += System.Console.Error.WriteLine;
if(MainClass.Verbose) DicConsole.VerboseWriteLineEvent += System.Console.WriteLine; if (MainClass.Verbose) DicConsole.VerboseWriteLineEvent += System.Console.WriteLine;
Statistics.AddCommand("list-devices"); Statistics.AddCommand("list-devices");
if(extra.Count > 0) string dicRemote = null;
if (extra.Count > 1)
{ {
DicConsole.ErrorWriteLine("Too many arguments."); DicConsole.ErrorWriteLine("Too many arguments.");
return (int)ErrorNumber.UnexpectedArgumentCount; return (int) ErrorNumber.UnexpectedArgumentCount;
} }
if (extra.Count == 1) dicRemote = extra[0];
DicConsole.DebugWriteLine("List-Devices command", "--debug={0}", MainClass.Debug); DicConsole.DebugWriteLine("List-Devices command", "--debug={0}", MainClass.Debug);
DicConsole.DebugWriteLine("List-Devices command", "--verbose={0}", MainClass.Verbose); DicConsole.DebugWriteLine("List-Devices command", "--verbose={0}", MainClass.Verbose);
DeviceInfo[] devices = Device.ListDevices(); var devices = Device.ListDevices(dicRemote);
if(devices == null || devices.Length == 0) DicConsole.WriteLine("No known devices attached."); if (devices == null || devices.Length == 0)
{
DicConsole.WriteLine("No known devices attached.");
}
else else
{ {
devices = devices.OrderBy(d => d.Path).ToArray(); devices = devices.OrderBy(d => d.Path).ToArray();
@@ -94,12 +101,12 @@ namespace DiscImageChef.Commands
DicConsole.WriteLine("{0,-22}+{1,-16}+{2,-24}+{3,-24}+{4,-10}+{5,-10}", "----------------------", DicConsole.WriteLine("{0,-22}+{1,-16}+{2,-24}+{3,-24}+{4,-10}+{5,-10}", "----------------------",
"----------------", "------------------------", "------------------------", "----------------", "------------------------", "------------------------",
"----------", "----------"); "----------", "----------");
foreach(DeviceInfo dev in devices) foreach (var dev in devices)
DicConsole.WriteLine("{0,-22}|{1,-16}|{2,-24}|{3,-24}|{4,-10}|{5,-10}", dev.Path, dev.Vendor, DicConsole.WriteLine("{0,-22}|{1,-16}|{2,-24}|{3,-24}|{4,-10}|{5,-10}", dev.Path, dev.Vendor,
dev.Model, dev.Serial, dev.Bus, dev.Supported); dev.Model, dev.Serial, dev.Bus, dev.Supported);
} }
return (int)ErrorNumber.NoError; return (int) ErrorNumber.NoError;
} }
} }
} }