1 | #Region "Microsoft.VisualBasic::b0f12490536f283a929877085408362f, Microsoft.VisualBasic.Core\ApplicationServices\Parallel\MMFProtocol\Pipeline\Pipeline.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 | ' Class Pipeline |
35 | ' |
36 | ' Constructor: (+1 Overloads) Sub New |
37 | ' Function: __allocated, __destroy, GetValue |
38 | ' |
39 | ' |
40 | ' /********************************************************************************/ |
41 | |
42 | #End Region |
43 | |
44 | Imports Microsoft.VisualBasic.Net.Http |
45 | Imports Microsoft.VisualBasic.Net.Protocols |
46 | Imports Microsoft.VisualBasic.Net.Protocols.Reflection |
47 | Imports Microsoft.VisualBasic.Net.Tcp |
48 | |
49 | Namespace Parallel.MMFProtocol.Pipeline |
50 | |
51 | ''' <summary> |
52 | ''' exec cmd /var $<piplineName>, this can be using in the CLI programming for passing the variables between the program more efficient |
53 | ''' </summary> |
54 | ''' |
55 | <Protocol(GetType(API.Protocols))> |
56 | Public Class Pipeline |
57 | |
58 | ReadOnly _sockets As SortedDictionary(Of String, MapStream.MSWriter) = |
59 | New SortedDictionary(Of String, MapStream.MSWriter) |
60 | ReadOnly _netSocket As TcpServicesSocket |
61 | ReadOnly _protocols As Net.Abstract.IProtocolHandler |
62 | |
63 | Sub New(Optional port As Integer = API.PeplinePort) |
64 | _protocols = New ProtocolHandler(Me) |
65 | _netSocket = New TcpServicesSocket(port) |
66 | _netSocket.Responsehandler = AddressOf _protocols.HandleRequest |
67 | |
68 | Call Parallel.RunTask(AddressOf _netSocket.Run) |
69 | End Sub |
70 | |
71 | ''' <summary> |
72 | ''' 假若变量不存在,则返回空值 |
73 | ''' </summary> |
74 | ''' <typeparam name="T"></typeparam> |
75 | ''' <param name="var"></param> |
76 | ''' <returns></returns> |
77 | Public Function GetValue(Of T As RawStream)(var As String) As T |
78 | If Not _sockets.ContainsKey(var) Then |
79 | Return Nothing |
80 | End If |
81 | |
82 | Dim data As MapStream.MSWriter = _sockets(var) |
83 | Dim buf As Byte() = data.Read.byteData |
84 | Dim raw As Object = Activator.CreateInstance(GetType(T), {buf}) |
85 | Dim x As T = DirectCast(raw, T) |
86 | Return x |
87 | End Function |
88 | |
89 | ''' <summary> |
90 | ''' 在写数据之前需要先使用这个方法进行内存区块的创建 |
91 | ''' </summary> |
92 | ''' <returns></returns> |
93 | <Protocol(API.Protocols.Allocation)> |
94 | Private Function __allocated(CA As Long, request As RequestStream, remote As System.Net.IPEndPoint) As RequestStream |
95 | Dim s As String = request.GetUTF8String |
96 | If Not API.IsRef(s) Then |
97 | Return NetResponse.RFC_TOKEN_INVALID |
98 | End If |
99 | |
100 | Dim tokens As String() = s.Split(":"c) |
101 | Dim var As String = tokens(Scan0) |
102 | Dim size As Long = Scripting.CTypeDynamic(Of Long)(tokens(1)) |
103 | |
104 | If _sockets.ContainsKey(var) Then |
105 | Call _sockets.Remove(var) |
106 | End If |
107 | Call _sockets.Add(var, New MapStream.MSWriter(var, size)) |
108 | |
109 | Return NetResponse.RFC_OK |
110 | End Function |
111 | |
112 | <Protocol(API.Protocols.Destroy)> |
113 | Private Function __destroy(CA As Long, request As RequestStream, remote As System.Net.IPEndPoint) As RequestStream |
114 | Dim var As String = request.GetUTF8String |
115 | |
116 | If _sockets.ContainsKey(var) Then |
117 | Dim x = _sockets(var) |
118 | |
119 | Call _sockets.Remove(var) |
120 | Call x.Free |
121 | |
122 | Return NetResponse.RFC_OK |
123 | Else |
124 | Return NetResponse.RFC_TOKEN_INVALID |
125 | End If |
126 | End Function |
127 | End Class |
128 | End Namespace |