Skip to content

Commit

Permalink
Modify program luanch process via <DynamicComponent>;
Browse files Browse the repository at this point in the history
  • Loading branch information
CuteLeon committed Feb 18, 2021
1 parent 6aaad2e commit 95edcd3
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ public abstract class ProgramComponentBase : ComponentBase, IDisposable
[Inject]
public IPublisher<ProgramCloseMessage> ProgramClosePublisher { get; set; }

private int pID;

[Parameter]
public int PID { get; set; }
public int PID { get => pID; set => pID = value; }

private ProgramEntity programEntity;

Expand Down
6 changes: 5 additions & 1 deletion HackSystem.Web.ProgramSDK/ProgramComponent/ProgramEntity.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
namespace HackSystem.Web.ProgramSDK.ProgramComponent
using System;

namespace HackSystem.Web.ProgramSDK.ProgramComponent
{
public class ProgramEntity
{
public string Name { get; set; }

public Type ProgramComponentType { get; set; }

public int Z_Index { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public ProcessDetail GetProcess(int pID)

public void AddProcess(ProcessDetail process)
{
this.logger.LogInformation($"进程容器:增加进程=> {process.PID} ({process.ProgramComponent?.GetHashCode().ToString("X")})");
this.logger.LogInformation($"进程容器:增加进程=> {process.PID} ({process.DynamicProgramComponent?.GetHashCode().ToString("X")})");
this.Processes.Add(process.PID, process);
this.logger.LogInformation($"进程容器:当前进程集合=> {this.Processes.Count}");
}

public ProcessDetail RemoveProcess(int pID)
{
var process = this.GetProcess(pID);
this.logger.LogInformation($"进程容器:移除进程=> {pID} ({process.ProgramComponent?.GetHashCode().ToString("X")})");
this.logger.LogInformation($"进程容器:移除进程=> {pID} ({process.DynamicProgramComponent?.GetHashCode().ToString("X")})");
this.Processes.Remove(pID);
this.logger.LogInformation($"进程容器:当前进程集合=> {this.Processes.Count}");
return process;
Expand Down
3 changes: 1 addition & 2 deletions HackSystem.Web.Scheduler.Program/Disposer/ProgramDisposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public ProgramDisposer(
private async Task HandleProgramCloseMessage(ProgramCloseMessage message)
{
this.logger.LogInformation($"程序释放器接收到消息并广播消息,结束进程:{message.PID}");
var process = this.processContainer.RemoveProcess(message.PID);
process.ProgramComponent.Dispose();
_ = this.processContainer.RemoveProcess(message.PID);
await this.programClosePublisher.Publish(message);
GC.Collect();
}
Expand Down
31 changes: 10 additions & 21 deletions HackSystem.Web.Scheduler.Program/Launcher/ProgramLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,24 @@ public ProgramLauncher(

public Task<ProcessDetail> LaunchProgram(QueryBasicProgramDTO basicProgram)
{
var process = new ProcessDetail()
var programEntity = new ProgramEntity()
{
PID = this.pIDGenerator.GetAvailablePID(),
Name = basicProgram.Name,
ProgramComponentType = GetProgramComponentType(basicProgram.AssemblyName, basicProgram.TypeName),
};
this.logger.LogInformation($"程序启动器:Name={basicProgram.Name} ({process.PID})");

var programComponentType = GetProgramComponentType(basicProgram.AssemblyName, basicProgram.TypeName);
var programEntity = new ProgramEntity() { Name = basicProgram.Name };
process.ProgramComponentType = programComponentType;

this.logger.LogInformation($"程序启动器:Type={programComponentType.FullName}");
this.logger.LogInformation($"程序启动器:Type={programEntity.ProgramComponentType.FullName}");

process.ProgramRenderFramgment = builder =>
var process = new ProcessDetail()
{
builder.OpenComponent(0, programComponentType);
// Attribute 需要先于其他数据被添加
builder.AddAttribute(1, nameof(ProgramComponentBase.ProgramEntity), programEntity);
builder.AddAttribute(2, nameof(ProgramComponentBase.PID), process.PID);
builder.AddComponentReferenceCapture(3, reference =>
{
this.logger.LogInformation($"进程 {process.ProgramComponentType.Name} PID={process.PID} 的组件引用由 {process.ProgramComponent?.GetHashCode():X} 更新为 {reference.GetHashCode():X}");
process.ProgramComponent = (ProgramComponentBase)reference;
});
builder.CloseComponent();
PID = this.pIDGenerator.GetAvailablePID(),
ProgramEntity = programEntity,
};
this.logger.LogInformation($"程序启动器:Name={basicProgram.Name} ({process.PID})");

this.logger.LogInformation($"程序启动器:添加进程到容器并广播消息...");
this.processContainer.AddProcess(process);
this.publisher.Publish(new ProgramLaunchMessage());
this.logger.LogInformation($"程序启动器:添加进程到容器并广播消息");

return Task.FromResult(process);
}

Expand Down
9 changes: 3 additions & 6 deletions HackSystem.Web.Scheduler.Program/Model/ProcessDetail.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using HackSystem.Web.ProgramSDK.ProgramComponent;
using HackSystem.Web.ProgramSDK.ProgramComponent;
using Microsoft.AspNetCore.Components;

namespace HackSystem.Web.Scheduler.Program.Model
Expand All @@ -8,10 +7,8 @@ public class ProcessDetail
{
public int PID { get; set; }

public Type ProgramComponentType { get; set; }
public DynamicComponent DynamicProgramComponent { get; set; }

public ProgramComponentBase ProgramComponent { get; set; }

public RenderFragment ProgramRenderFramgment { get; set; }
public ProgramEntity ProgramEntity { get; set; }
}
}
13 changes: 9 additions & 4 deletions HackSystem.Web/ProgramLayer/ProgramContainerComponent.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
@inject IProcessContainer processContainer
@inject ISubscriber<ProgramLaunchMessage> programLaunchSubcriber
@inject ISubscriber<ProgramCloseMessage> programCloseSubscriber
@inject IProgramLauncher programLauncher
@inject IProgramDisposer programDisposer
@inject IProgramScheduler programScheduler

Expand All @@ -11,8 +10,14 @@
{
<!-- Fixed a big issue here. More details please navigate to https://github.com/dotnet/aspnetcore/issues/29176 -->
var key = $"Process_{process.PID}";
<span @key=key>
@process.ProgramRenderFramgment
</span>
<DynamicComponent @key="key"
Type="process.ProgramEntity.ProgramComponentType"
@ref="process.DynamicProgramComponent"
Parameters="new Dictionary<string, object>
{
{ nameof(ProgramComponentBase.PID), process.PID },
{ nameof(ProgramComponentBase.ProgramEntity), process.ProgramEntity },
}">
</DynamicComponent>
}
</div>

0 comments on commit 95edcd3

Please sign in to comment.