Skip to content

Commit

Permalink
StarClient继承自ClientBase,后者的大部分功能来自前者。架构升级后,StarClient中仅剩下业务逻辑代码,不包含通信…
Browse files Browse the repository at this point in the history
…和基础登录心跳等接口代码。
  • Loading branch information
nnhy committed Jun 27, 2024
1 parent 9862527 commit 3d5f421
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 687 deletions.
114 changes: 57 additions & 57 deletions ClientTest/StarClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,77 +3,77 @@
using NewLife;
using NewLife.Security;
using Stardust;
using Stardust.Models;
using Xunit;

namespace ClientTest
namespace ClientTest;

public class StarClientTests
{
public class StarClientTests
{
public String Server { get; set; } = "http://localhost:6600";
//public StarClient Client { get; }
public String Server { get; set; } = "http://localhost:6600";
//public StarClient Client { get; }

public StarClientTests()
{
//Client = new StarClient(Server);
//Client.Add("default", new Uri(Server));
}
public StarClientTests()
{
//Client = new StarClient(Server);
//Client.Add("default", new Uri(Server));
}

[Fact]
public void GetLoginInfoTest()
[Fact]
public void GetLoginInfoTest()
{
var client = new StarClient
{
var client = new StarClient
{
Code = Rand.NextString(8),
Secret = Rand.NextString(16)
};
Code = Rand.NextString(8),
Secret = Rand.NextString(16)
};

var inf = client.GetLoginInfo();
Assert.NotNull(inf);
Assert.NotNull(inf.Node);
var inf = client.BuildLoginRequest() as LoginInfo;
Assert.NotNull(inf);
Assert.NotNull(inf.Node);

Assert.Equal(client.Code, inf.Code);
Assert.Equal(client.Secret.MD5(), inf.Secret);
Assert.Equal(client.Code, inf.Code);
Assert.Equal(client.Secret.MD5(), inf.Secret);

var node = client.GetNodeInfo();
var mi = MachineInfo.Current;
Assert.Equal(mi.UUID, node.UUID);
Assert.Equal(mi.Guid, node.MachineGuid);
}
var node = client.GetNodeInfo();
var mi = MachineInfo.Current;
Assert.Equal(mi.UUID, node.UUID);
Assert.Equal(mi.Guid, node.MachineGuid);
}

[Theory(DisplayName = "登录测试")]
[InlineData("abcd", "1234")]
[InlineData(null, "1234")]
[InlineData("abcd", null)]
public async Task LoginTest(String code, String secret)
[Theory(DisplayName = "登录测试")]
[InlineData("abcd", "1234")]
[InlineData(null, "1234")]
[InlineData("abcd", null)]
public async Task LoginTest(String code, String secret)
{
var client = new StarClient(Server)
{
var client = new StarClient(Server)
{
Code = code,
Secret = secret
};
Code = code,
Secret = secret
};

var rs = await client.Login();
Assert.NotNull(rs);
Assert.NotNull(client.Info);
Assert.True(client.Logined);
}
var rs = await client.Login();
Assert.NotNull(rs);
//Assert.NotNull(client.Info);
Assert.True(client.Logined);
}

[Fact]
public async Task LogoutTest()
{
var client = new StarClient(Server);
[Fact]
public async Task LogoutTest()
{
var client = new StarClient(Server);

await client.Login();
await client.Logout("test");
}
await client.Login();
await client.Logout("test");
}

[Fact]
public void GetHeartInfoTest()
{
var client = new StarClient();
var inf = client.GetHeartInfo();
Assert.NotNull(inf);
Assert.NotEmpty(inf.Macs);
}
[Fact]
public void GetHeartInfoTest()
{
var client = new StarClient();
var inf = client.GetHeartInfo();
Assert.NotNull(inf);
Assert.NotEmpty(inf.Macs);
}
}
15 changes: 7 additions & 8 deletions StarAgent/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
using NewLife.Reflection;
using NewLife.Remoting;
using NewLife.Remoting.Clients;
using NewLife.Remoting.Models;
using NewLife.Serialization;
using NewLife.Threading;
using Stardust;
using Stardust.Deployment;
using Stardust.Managers;
using Stardust.Models;
using Stardust.Plugins;
using Stardust.Services;
using IHost = NewLife.Agent.IHost;
using Upgrade = Stardust.Web.Upgrade;

namespace StarAgent;

Expand Down Expand Up @@ -387,7 +386,7 @@ public void StartClient()
// 登录后保存证书
client.OnLogined += (s, e) =>
{
var inf = client.Info;
var inf = e.Response;
if (inf != null && !inf.Code.IsNullOrEmpty())
{
set.Code = inf.Code;
Expand Down Expand Up @@ -419,8 +418,8 @@ public void StartClient()

_Manager.Attach(client);

// 使用跟踪
client.UseTrace();
//// 使用跟踪
//client.UseTrace();

_Client = client;
_container.AddSingleton(client);
Expand Down Expand Up @@ -626,7 +625,7 @@ private async Task CheckUpgrade(Object data)
ug.Trim("StarAgent");

// 检查更新
var ur = await client.Upgrade(channel, _lastVersion);
var ur = await client.Upgrade(channel);
if (ur != null && ur.Version != _lastVersion)
{
client.WriteInfoEvent("Upgrade", $"准备从[{_lastVersion}]更新到[{ur.Version}],开始下载 {ur.Source}");
Expand All @@ -650,11 +649,11 @@ private async Task CheckUpgrade(Object data)
}
else
{
if (!ur.Preinstall.IsNullOrEmpty())
if (ur is UpgradeInfo ur2 && !ur2.Preinstall.IsNullOrEmpty())
{
client.WriteInfoEvent("Upgrade", "执行预安装脚本");

ug.Run(ur.Preinstall);
ug.Run(ur2.Preinstall);
}

client.WriteInfoEvent("Upgrade", "解压完成,准备覆盖文件");
Expand Down
7 changes: 6 additions & 1 deletion Stardust/LocalStarClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using NewLife.Log;
using NewLife.Messaging;
using NewLife.Remoting;
using NewLife.Remoting.Clients;
using NewLife.Remoting.Models;
using Stardust.Models;
#if NET45_OR_GREATER || NETCOREAPP || NETSTANDARD
Expand Down Expand Up @@ -240,7 +241,7 @@ public Boolean ProbeAndInstall(String? url = null, String? version = null, Strin

WriteLog("目标:{0}", target);

var ug = new Stardust.Web.Upgrade
var ug = new Upgrade
{
SourceFile = Path.GetFileName(url).GetFullPath(),
DestinationPath = target,
Expand All @@ -253,6 +254,10 @@ public Boolean ProbeAndInstall(String? url = null, String? version = null, Strin
var client = new HttpClient();
client.DownloadFileAsync(url, ug.SourceFile).Wait();

//var file = ug.SourceFile;
//var tmp = Path.GetTempPath().CombinePath(Path.GetFileNameWithoutExtension(file));
//file.AsFile().Extract(tmp, true);
//todo 把tmp赋值给ug.TempPath
ug.Extract();
ug.Update();

Expand Down
2 changes: 1 addition & 1 deletion Stardust/Managers/ServiceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ private async Task DoWork(Object state)

// 应用服务的上报和拉取
DeployInfo[]? deploys = null;
if (_client != null && !_client.Token.IsNullOrEmpty())
if (_client != null && _client.Logined)
{
// 上传失败不应该影响本地拉起服务
try
Expand Down
Loading

0 comments on commit 3d5f421

Please sign in to comment.