Skip to content

Commit

Permalink
feat: 节点上线(未经测试)
Browse files Browse the repository at this point in the history
  • Loading branch information
SALTWOOD committed Mar 3, 2024
1 parent ab5d07a commit bce9d28
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 5 deletions.
1 change: 1 addition & 0 deletions CSharp-OpenBMCLAPI/CSharp-OpenBMCLAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<PackageReference Include="log4net" Version="2.0.15" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SaltWood.TeraIO" Version="1.0.2" />
<PackageReference Include="SocketIOClient" Version="3.1.1" />
<PackageReference Include="ZstdSharp.Port" Version="0.7.5" />
</ItemGroup>

Expand Down
8 changes: 8 additions & 0 deletions CSharp-OpenBMCLAPI/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,13 @@ public void LogInfo(params object[] args)
Console.WriteLine(string.Join(" ", args));
}
}

public void LogWarn(params object[] args)
{
lock (this)
{
Console.WriteLine(string.Join(" ", args));
}
}
}
}
71 changes: 66 additions & 5 deletions CSharp-OpenBMCLAPI/Modules/Cluster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
using Avro.Generic;
using Downloader;
using System.Security.Cryptography;
using Microsoft.VisualBasic.FileIO;
using System.IO;
using System.Security.Policy;
using SocketIOClient;
using SocketIO.Core;
using TeraIO.Extension;

namespace CSharpOpenBMCLAPI.Modules
{
Expand All @@ -23,6 +23,8 @@ public class Cluster : RunnableBase
private TokenManager token;
private HttpClient client;
public Guid guid;
private SocketIOClient.SocketIO socket;
public bool IsEnabled { get; private set; }
//List<Task> tasks = new List<Task>();

public Cluster(ClusterInfo info, TokenManager token) : base()
Expand All @@ -34,12 +36,37 @@ public Cluster(ClusterInfo info, TokenManager token) : base()
// Fetch 一下以免出现问题
this.token.FetchToken().Wait();

this.socket = new(HttpRequest.client.BaseAddress, new SocketIOOptions()
{
Transport = SocketIOClient.Transport.TransportProtocol.WebSocket,
Auth = new
{
token = token.Token
}
});

this.socket.ConnectAsync().Wait();

this.socket.On("error", error => HandleError(error));
this.socket.On("message", msg => SharedData.Logger.LogInfo(msg));
this.socket.On("connect", (_) => SharedData.Logger.LogInfo("与主控连接成功"));
this.socket.On("disconnect", (r) =>
{
SharedData.Logger.LogWarn($"与主控断开连接:{r}");
this.IsEnabled = false;
});

client = new HttpClient();
client.BaseAddress = HttpRequest.client.BaseAddress;
client.DefaultRequestHeaders.Add("User-Agent", $"openbmclapi-cluster/{SharedData.Config.clusterVersion}");
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token.Token.token}");
}

private void HandleError(SocketIOResponse resp)
{
return;
}

protected override int Run(string[] args)
{
// 工作进程启动
Expand All @@ -54,11 +81,45 @@ protected async Task<int> AsyncRun()
int returns = 0;

// 检查文件
await CheckFiles();
// await CheckFiles();

await Enable();

return returns;
}

public async Task Enable()
{
socket.Connected.Dump();
await socket.EmitAsync("enable",
(SocketIOResponse resp) =>
{
SharedData.Logger.LogInfo($"启用成功");
},
new
{
host = SharedData.Config.host,
port = SharedData.Config.port,
version = SharedData.Config.clusterVersion,
byoc = SharedData.Config.byoc,
noFastEnable = SharedData.Config.noFastEnable
});
}

public async Task KeepAlive()
{
string time = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
await socket.EmitAsync("keep-alive",
(SocketIOResponse resp) =>
{
SharedData.Logger.LogInfo($"保活成功 at {time}");
},
new
{
time = time
});
}

protected async Task CheckFiles()
{
var resp = await client.GetAsync("openbmclapi/files");
Expand Down Expand Up @@ -115,7 +176,7 @@ protected async Task CheckFiles()
});
}

protected async Task DownloadFile(DownloadService service, string path, string hash)
private async Task DownloadFile(DownloadService service, string path, string hash)
{
if (!File.Exists($"{SharedData.Config.clusterFileDirectory}cache/{hash[0..2]}/{hash}"))
{
Expand Down
10 changes: 10 additions & 0 deletions CSharp-OpenBMCLAPI/Modules/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,22 @@ public class Config
// 指示节点端的版本,不应由用户更改
[Browsable(false)]
public string clusterVersion;
// Host
public string host;
public int port;
public string byoc;
public bool noFastEnable;

public Config()
{
this.refreshTokenTime = 1800000;
this.clusterFileDirectory = "./";
this.clusterVersion = "1.9.7";

this.host = "";
this.port = 4000;
this.byoc = "";
this.noFastEnable = false;
}
}
}

0 comments on commit bce9d28

Please sign in to comment.