1
0
mirror of https://github.com/ZetaKebab/MpcNET.git synced 2024-09-16 05:30:09 +00:00
MpcNET/LibMpcTest/MpdServerTest.cs

118 lines
3.9 KiB
C#
Raw Normal View History

using System;
using System.Diagnostics;
using System.IO;
2016-12-14 09:56:00 +00:00
using System.Runtime.InteropServices;
namespace LibMpcTest
{
2016-12-13 12:12:51 +00:00
public class MpdServerTest : IDisposable
{
public MpdServerTest()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
SendCommand("/usr/bin/pkill mpd");
}
2016-12-14 09:56:00 +00:00
MpdConf.Create(Path.Combine(AppContext.BaseDirectory, "Server"));
2016-12-14 09:56:00 +00:00
var server = GetServer();
2016-12-13 12:12:51 +00:00
Process = new Process
{
StartInfo = new ProcessStartInfo
{
2016-12-14 09:56:00 +00:00
FileName = server.FileName,
WorkingDirectory = server.WorkingDirectory,
Arguments = server.Arguments,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
}
};
2016-12-16 10:58:28 +00:00
Console.Out.WriteLine($"Starting Server: {Process.StartInfo.FileName} {Process.StartInfo.Arguments}");
2016-12-16 10:28:21 +00:00
2016-12-13 12:12:51 +00:00
Process.Start();
Console.Out.WriteLine($"Output: {Process.StandardOutput.ReadToEnd()}");
Console.Out.WriteLine($"Error: {Process.StandardError.ReadToEnd()}");
2016-12-16 11:07:03 +00:00
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
SendCommand("/bin/netstat -ntpl");
2016-12-16 11:07:03 +00:00
}
}
public Process Process { get; }
private Server GetServer()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
return Server.Linux;
}
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return Server.Windows;
}
throw new NotSupportedException("OS not supported");
}
private void SendCommand(string command)
2016-12-16 11:07:03 +00:00
{
var netcat = new Process
{
StartInfo = new ProcessStartInfo
{
2016-12-16 11:20:56 +00:00
FileName = "/bin/bash",
2016-12-16 11:07:03 +00:00
WorkingDirectory = "/bin/",
Arguments = $"-c \"sudo {command}\"",
2016-12-16 11:07:03 +00:00
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
}
};
netcat.Start();
netcat.WaitForExit();
Console.Out.WriteLine(command);
Console.Out.WriteLine($"Output: {netcat.StandardOutput.ReadToEnd()}");
Console.Out.WriteLine($"Error: {netcat.StandardError.ReadToEnd()}");
}
public void Dispose()
{
2016-12-13 12:12:51 +00:00
Process?.Kill();
Process?.Dispose();
}
2016-12-14 09:56:00 +00:00
private class Server
{
public static Server Linux = new Server(
2016-12-16 11:20:56 +00:00
fileName: "/bin/bash",
workingDirectory: "/bin/",
arguments: $"-c \"sudo /usr/bin/mpd {Path.Combine(AppContext.BaseDirectory, "Server", "mpd.conf")} -v\"");
2016-12-14 09:56:00 +00:00
public static Server Windows = new Server(
fileName: Path.Combine(AppContext.BaseDirectory, "Server", "mpd.exe"),
workingDirectory: Path.Combine(AppContext.BaseDirectory, "Server"),
2016-12-16 11:20:56 +00:00
arguments: $"{Path.Combine(AppContext.BaseDirectory, "Server", "mpd.conf")} -v");
2016-12-14 09:56:00 +00:00
private Server(string fileName, string workingDirectory, string arguments)
{
FileName = fileName;
WorkingDirectory = workingDirectory;
Arguments = arguments;
}
public string FileName { get; }
public string WorkingDirectory { get; }
public string Arguments { get; }
}
}
}