Skip to content

Commit

Permalink
Implemented file server
Browse files Browse the repository at this point in the history
  • Loading branch information
fahminlb33 committed Jun 10, 2020
1 parent 780a4f5 commit b60a55a
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/KFlearning.Core/KFlearning.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@
<Compile Include="API\UpdateCheckClient.cs" />
<Compile Include="CLIS\StudentRecord.cs" />
<Compile Include="Services\IUsesPersistance.cs" />
<Compile Include="Services\KFserverService.cs" />
<Compile Include="Services\ProcessManager.cs" />
<Compile Include="Services\ProcessWatcher.cs" />
<Compile Include="Services\RemoteShutdownServer.cs" />
<Compile Include="Services\ServerInfo.cs" />
<Compile Include="Services\ShutdownRequestedEventArgs.cs" />
<Compile Include="Services\SystemInfoService.cs" />
<Compile Include="Services\SystemTweaker.cs" />
Expand Down
105 changes: 105 additions & 0 deletions src/KFlearning.Core/Services/KFserverService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;

namespace KFlearning.Core.Services
{
public class KFserverService
{
private const int ServerPort = 21002;
private const string KFserverProcessName = "kfserver";

private readonly IPathManager _pathManager;

public bool IsRunning => Process.GetProcessesByName(KFserverProcessName).Length > 0;


public KFserverService(IPathManager pathManager)
{
_pathManager = pathManager;
}

public void Start(string servePath)
{
var startInfo = new ProcessStartInfo
{
FileName = _pathManager.GetPath(PathKind.KFserverExecutable),
Arguments = servePath,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden
};
Process.Start(startInfo);
}

public void Stop()
{
try
{
foreach (var process in Process.GetProcessesByName(KFserverProcessName))
{
process.Kill();
}
}
catch (Exception)
{
// ignore
}
}

public ServerInfo GetInfo()
{
if (!NetworkInterface.GetIsNetworkAvailable())
throw new KFlearningException("Komputer ini tidak terhubung ke jaringan.");

var hosts = Dns.GetHostEntry(Dns.GetHostName());
var address = hosts.AddressList.FirstOrDefault(x => x.AddressFamily == AddressFamily.InterNetwork);
if (address == null)
throw new KFlearningException("Tidak dapat menemukan IP komputer.");

return new ServerInfo
{
IP = address.ToString(),
Port = ServerPort,
Link = $"http://{address}:{ServerPort}"
};
}

#region IDisposable Support

private bool _disposedValue;

protected virtual void Dispose(bool disposing)
{
if (!_disposedValue)
{
if (disposing)
{
// TODO: dispose managed state (managed objects)
}

Stop();
_disposedValue = true;
}
}

// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
// ~KFserverService()
// {
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
// Dispose(disposing: false);
// }

public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

#endregion

}
}
1 change: 1 addition & 0 deletions src/KFlearning.Core/Services/PathKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ public enum PathKind
MingwInclude2Directory,
MingwGXXExecutable,
MingwGDBExecutable,
KFserverExecutable
}
}
5 changes: 5 additions & 0 deletions src/KFlearning.Core/Services/PathManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System;
using System.IO;
using System.Linq;
using System.Reflection;

#endregion

Expand All @@ -30,6 +31,7 @@ public class PathManager : IPathManager
{
private static readonly string SystemRoot = Path.GetPathRoot(Environment.SystemDirectory);
private static readonly string DocumentRoot = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
private static readonly string AppRoot = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
private static readonly char[] InvalidFileNameChars = Path.GetInvalidFileNameChars();
private string _cachedVscodePath;
private string _cachedKfMingwPath;
Expand Down Expand Up @@ -63,6 +65,9 @@ public string GetPath(PathKind kind, bool forwardSlash = false)
case PathKind.MingwGDBExecutable:
path = Path.Combine(FindKfMingw(), @"bin\gdb.exe");
break;
case PathKind.KFserverExecutable:
path = Path.Combine(AppRoot, @"kfserver.exe");
break;
default:
throw new ArgumentOutOfRangeException(nameof(kind), kind, null);
}
Expand Down
9 changes: 9 additions & 0 deletions src/KFlearning.Core/Services/ServerInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace KFlearning.Core.Services
{
public class ServerInfo
{
public string IP { get; set; }
public int Port { get; set; }
public string Link { get; set; }
}
}

0 comments on commit b60a55a

Please sign in to comment.