1 #Region "Microsoft.VisualBasic::d39cd878fc06a992f1453c699e3e6462, Microsoft.VisualBasic.Core\Net\Protocol\Reflection\AppMgr.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 AppMgr
35     
36     '         Properties: ProtocolApps, ProtocolEntry
37     
38     '         Constructor: (+1 OverloadsSub New
39     '         Function: HandleRequest, Register, RegisterApp
40     
41     
42     ' /********************************************************************************/
43
44 #End Region
45
46 Imports Microsoft.VisualBasic.Net.Abstract
47 Imports Microsoft.VisualBasic.Net.Http
48 Imports TcpEndPoint = System.Net.IPEndPoint
49
50 Namespace Net.Protocols.Reflection
51
52     ''' <summary>
53     ''' 能够处理多种协议数据
54     ''' </summary>
55     Public Class AppMgr : Inherits IProtocolHandler
56
57         Public ReadOnly Property ProtocolApps As New Dictionary(Of Long, ProtocolHandler)
58
59         Public Overrides ReadOnly Property ProtocolEntry As Long
60             Get
61                 Return -1
62             End Get
63         End Property
64
65         Sub New()
66
67         End Sub
68
69         Public Function Register(app As Object, [overrides] As BooleanAs Boolean
70             Dim protocol = ProtocolHandler.SafelyCreateObject(app)
71
72             If protocol Is Nothing Then
73                 Return False
74             End If
75             If ProtocolApps.ContainsKey(protocol.ProtocolEntry) Then
76                 If [overrides] Then
77                     ' 覆盖掉原有的协议数据
78                     Call ProtocolApps.Remove(protocol.ProtocolEntry)
79                 Else
80                     ' 没有被注册
81                     Return False
82                 End If
83             End If
84
85             Call ProtocolApps.Add(protocol.ProtocolEntry, protocol)
86
87             Return True
88         End Function
89
90         ''' <summary>
91         ''' 有点多此一举??
92         ''' </summary>
93         ''' <typeparam name="T"></typeparam>
94         ''' <param name="App"></param>
95         ''' <param name="[overrides]"></param>
96         ''' <returns></returns>
97         Public Function RegisterApp(Of T As Class)(App As T, [overrides] As BooleanAs Boolean
98             Return Register(DirectCast(App, Object), [overrides])
99         End Function
100
101         Public Overrides Function HandleRequest(request As RequestStream, remoteDevcie As TcpEndPoint) As RequestStream
102             If Not ProtocolApps.ContainsKey(request.ProtocolCategory) Then
103                 Return NetResponse.RFC_NOT_FOUND
104             End If
105
106             Dim protocol As ProtocolHandler = ProtocolApps(request.ProtocolCategory)
107             Return protocol.HandleRequest(request, remoteDevcie)
108         End Function
109     End Class
110 End Namespace