116 lines
3.2 KiB
VB.net
116 lines
3.2 KiB
VB.net
Imports System.Text
|
||
|
||
Public Class ServiceGroupManager
|
||
''' <summary>历史通讯数据服务集合</summary>
|
||
Private _groupService As Dictionary(Of Integer, OtherService)
|
||
|
||
Sub New()
|
||
_groupService = New Dictionary(Of Integer, OtherService)
|
||
End Sub
|
||
|
||
''' <summary>
|
||
''' 添加网上邻居,已存在则更新存活时间与状态
|
||
''' </summary>
|
||
''' <param name="other"></param>
|
||
Public Sub Add(other As OtherService)
|
||
If _groupService.ContainsKey(other.ServiceIndex) Then
|
||
_groupService(other.ServiceIndex).LastTime = Now
|
||
_groupService(other.ServiceIndex).IsAlive = True
|
||
Else
|
||
_groupService.Add(other.ServiceIndex, other)
|
||
End If
|
||
End Sub
|
||
|
||
''' <summary>
|
||
''' 校验其他服务的当前发送包索引是否与上一包索引重复,相同返回True,不相同返回False
|
||
''' </summary>
|
||
''' <returns></returns>
|
||
Public Function CheckServiceSendIndex(serviceId As Integer, sendId As Integer) As Boolean
|
||
If _groupService.ContainsKey(serviceId) Then
|
||
If _groupService(serviceId).LastPacketIndex = sendId Then
|
||
Return True
|
||
Else
|
||
Return False
|
||
End If
|
||
Else
|
||
Return False
|
||
End If
|
||
End Function
|
||
|
||
|
||
''' <summary>
|
||
''' 校验服务是否存活
|
||
''' </summary>
|
||
Public Sub CheckAliveService()
|
||
For Each other As KeyValuePair(Of Integer, OtherService) In _groupService
|
||
If (Now - other.Value.LastTime).TotalSeconds > 30 Then
|
||
other.Value.IsAlive = False
|
||
End If
|
||
Next
|
||
End Sub
|
||
|
||
|
||
''' <summary>
|
||
''' 获取存活状态的所有网上邻居的索引字符串,以逗号分割
|
||
''' </summary>
|
||
''' <returns></returns>
|
||
Public Function GetAliveServiceIndexString() As String
|
||
'排序后输出
|
||
Dim sb As New StringBuilder
|
||
Dim bt() As Integer = _groupService.Keys.ToArray()
|
||
Array.Sort(bt)
|
||
For i As Integer = 0 To bt.Length - 1
|
||
If _groupService(bt(i)).IsAlive = False Then Continue For
|
||
If sb.Length = 0 Then
|
||
sb.Append(bt(i))
|
||
Else
|
||
sb.Append("," & bt(i))
|
||
End If
|
||
Next
|
||
|
||
Return sb.ToString
|
||
|
||
|
||
'原来的顺序输出
|
||
'For Each other As KeyValuePair(Of Integer, OtherService) In _groupService
|
||
' If other.Value.IsAlive = False Then Continue For
|
||
' If sb.Length = 0 Then
|
||
' sb.Append(other.Key)
|
||
' Else
|
||
' sb.Append("," & other.Key)
|
||
' End If
|
||
'Next
|
||
End Function
|
||
End Class
|
||
|
||
|
||
Public Class OtherService
|
||
Sub New(index As Integer, packetIndex As Integer)
|
||
ServiceIndex = index
|
||
LastPacketIndex = packetIndex
|
||
LastTime = Now
|
||
IsAlive = True
|
||
End Sub
|
||
|
||
''' <summary>
|
||
''' 最后通讯时间
|
||
''' </summary>
|
||
Public Property LastTime As DateTime
|
||
|
||
''' <summary>
|
||
''' 最后通讯包ID
|
||
''' </summary>
|
||
Public Property LastPacketIndex As Integer
|
||
|
||
''' <summary>
|
||
''' 服务索引
|
||
''' </summary>
|
||
Public Property ServiceIndex As Integer
|
||
|
||
''' <summary>
|
||
''' 是否判定为在线
|
||
''' </summary>
|
||
Public Property IsAlive As Boolean
|
||
|
||
|
||
End Class |