Imports System.Reflection Imports Microsoft.VisualBasic.ComponentModel Imports Microsoft.VisualBasic.Net.Tcp Imports Microsoft.VisualBasic.Parallel Imports Microsoft.VisualBasic.Serialization.JSON Public Class TaskBuilder : Implements ITaskDriver ReadOnly masterPort As Integer Sub New(port As Integer) masterPort = port End Sub Public Function Run() As Integer Implements ITaskDriver.Run Dim task As IDelegate = GetMethod() Dim n As Integer = GetArgumentValueNumber() Dim args As New List(Of Object) For i As Integer = 0 To n - 1 args.Add(GetArgumentValue(i)) Next Dim api As MethodInfo = task.GetMethod Dim params As ParameterInfo() = api.GetParameters For i As Integer = n To params.Length - 1 If Not params(i).IsOptional Then Return PostError(New Exception($"missing parameter value for [{i}]{params(i).Name}!")) Else args.Add(params(i).DefaultValue) End If Next Call PostFinished(api.Invoke(Nothing, args.ToArray)) Return 0 End Function Private Function GetArgumentValueNumber() As Integer Dim resp = New TcpRequest(masterPort).SendMessage(New RequestStream(IPCSocket.Protocol, Protocols.GetArgumentNumber)) Dim n As Integer = BitConverter.ToInt32(resp.ChunkBuffer, Scan0) Return n End Function Private Function GetMethod() As IDelegate Dim resp = New TcpRequest(masterPort).SendMessage(New RequestStream(IPCSocket.Protocol, Protocols.GetTask)) Dim json As String = resp.GetUTF8String Dim target As IDelegate = json.LoadJSON(Of IDelegate) Return target End Function Private Function GetArgumentValue(i As Integer) As Object Dim resp = New TcpRequest(masterPort).SendMessage(New RequestStream(IPCSocket.Protocol, Protocols.GetArgumentByIndex, BitConverter.GetBytes(i))) Dim stream As New ObjectStream(resp.ChunkBuffer) If stream.method = StreamMethods.BSON Then Else Throw New NotImplementedException End If End Function Private Function PostError(err As Exception) As Integer Return 500 End Function Private Sub PostFinished(result As Object) End Sub End Class