1 | #Region "Microsoft.VisualBasic::3cb2b985d6842151b781445429322b32, Microsoft.VisualBasic.Core\ApplicationServices\Parallel\MMFProtocol\Pipeline\API.vb" |
2 | |
3 | ' Author: |
4 | ' |
5 | ' asuka (amethyst.asuka@gcmodeller.org) |
6 | ' xie (genetics@smrucc.org) |
7 | ' xieguigang (xie.guigang@live.com) |
8 | ' |
9 | ' Copyright (c) 2018 GPL3 Licensed |
10 | ' |
11 | ' |
12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
13 | ' |
14 | ' |
15 | ' This program is free software: you can redistribute it and/or modify |
16 | ' it under the terms of the GNU General Public License as published by |
17 | ' the Free Software Foundation, either version 3 of the License, or |
18 | ' (at your option) any later version. |
19 | ' |
20 | ' This program is distributed in the hope that it will be useful, |
21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | ' GNU General Public License for more details. |
24 | ' |
25 | ' You should have received a copy of the GNU General Public License |
26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | |
28 | |
29 | |
30 | ' /********************************************************************************/ |
31 | |
32 | ' Summaries: |
33 | |
34 | ' Module API |
35 | ' |
36 | ' |
37 | ' Enum Protocols |
38 | ' |
39 | ' |
40 | ' |
41 | ' |
42 | ' |
43 | ' |
44 | ' Properties: Protocol |
45 | ' |
46 | ' Function: Delete, IsRef, (+2 Overloads) TryGetValue, WriteData |
47 | ' |
48 | ' |
49 | ' /********************************************************************************/ |
50 | |
51 | #End Region |
52 | |
53 | Imports System.Text.RegularExpressions |
54 | Imports Microsoft.VisualBasic.Net.Http |
55 | Imports Microsoft.VisualBasic.Net.Protocols |
56 | Imports Microsoft.VisualBasic.Net.Protocols.Reflection |
57 | Imports Microsoft.VisualBasic.Net.Tcp |
58 | |
59 | Namespace Parallel.MMFProtocol.Pipeline |
60 | |
61 | Public Module API |
62 | |
63 | Public Const PeplinePort As Integer = 5687 |
64 | |
65 | Enum Protocols As Long |
66 | Allocation = 4556122 |
67 | Destroy = -345639845 |
68 | End Enum |
69 | |
70 | Public ReadOnly Property Protocol As Long = |
71 | New Protocol(GetType(API.Protocols)).EntryPoint |
72 | |
73 | Public Function Delete(var As String, Optional port As Integer = API.PeplinePort) As Boolean |
74 | Dim invoke As New TcpRequest("127.0.0.1", port) |
75 | Dim action As New RequestStream(API.Protocol, Protocols.Destroy, var) |
76 | Dim resp As RequestStream = invoke.SendMessage(action) |
77 | Return resp.Protocol = HTTP_RFC.RFC_OK |
78 | End Function |
79 | |
80 | ''' <summary> |
81 | ''' 生成的映射位置为: <var>:<ChunkSize> |
82 | ''' </summary> |
83 | ''' <param name="var"></param> |
84 | ''' <param name="value"></param> |
85 | ''' <returns></returns> |
86 | Public Function WriteData(var As String, value As RawStream, Optional port As Integer = API.PeplinePort) As Boolean |
87 | Dim buf As Byte() = value.Serialize |
88 | Dim chunkSize As Long = buf.Length |
89 | Dim ref As String = $"{var}:{chunkSize}" |
90 | Dim invoke As New TcpRequest("127.0.0.1", port) |
91 | Dim action As New RequestStream(API.Protocol, Protocols.Allocation, ref) |
92 | Dim resp As RequestStream = invoke.SendMessage(action) |
93 | Dim writer As New MapStream.MSWriter(var, chunkSize) |
94 | |
95 | Call writer.WriteStream(buf) |
96 | |
97 | Return True |
98 | End Function |
99 | |
100 | ''' <summary> |
101 | ''' |
102 | ''' </summary> |
103 | ''' <param name="var">$var or var</param> |
104 | ''' <returns></returns> |
105 | Public Function TryGetValue(var As String) As Byte() |
106 | If var.First = "$"c Then |
107 | var = Mid(var, 2) |
108 | End If |
109 | |
110 | Dim tokens As String() = var.Split(":"c) |
111 | Dim size As Long = Scripting.CTypeDynamic(Of Long)(tokens(1)) |
112 | Dim reader As New MapStream.MSIOReader(tokens(Scan0), Nothing, size) |
113 | Dim buf As Byte() = reader.Read.byteData |
114 | Return buf |
115 | End Function |
116 | |
117 | ''' <summary> |
118 | ''' 不存在的话会返回空值 |
119 | ''' </summary> |
120 | ''' <typeparam name="T"></typeparam> |
121 | ''' <param name="var"></param> |
122 | ''' <returns></returns> |
123 | Public Function TryGetValue(Of T As RawStream)(var As String) As T |
124 | Dim raw As Byte() = API.TryGetValue(var) |
125 | If raw.IsNullOrEmpty Then |
126 | Return Nothing |
127 | End If |
128 | |
129 | Dim obj As Object = Activator.CreateInstance(GetType(T), {raw}) |
130 | Dim x As T = DirectCast(obj, T) |
131 | Return x |
132 | End Function |
133 | |
134 | ''' <summary> |
135 | ''' |
136 | ''' </summary> |
137 | ''' <param name="addr">$var:size</param> |
138 | ''' <returns></returns> |
139 | Public Function IsRef(addr As String) As Boolean |
140 | Dim s As String = Regex.Match(addr, "\$.+?:\d+").Value |
141 | Return String.Equals(addr, s) |
142 | End Function |
143 | End Module |
144 | End Namespace |