Imports System.Text
Public Class ServiceGroupManager
''' 历史通讯数据服务集合
Private _groupService As Dictionary(Of Integer, OtherService)
Sub New()
_groupService = New Dictionary(Of Integer, OtherService)
End Sub
'''
''' 添加网上邻居,已存在则更新存活时间与状态
'''
'''
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
'''
''' 校验其他服务的当前发送包索引是否与上一包索引重复,相同返回True,不相同返回False
'''
'''
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
'''
''' 校验服务是否存活
'''
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
'''
''' 获取存活状态的所有网上邻居的索引字符串,以逗号分割
'''
'''
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
'''
''' 最后通讯时间
'''
Public Property LastTime As DateTime
'''
''' 最后通讯包ID
'''
Public Property LastPacketIndex As Integer
'''
''' 服务索引
'''
Public Property ServiceIndex As Integer
'''
''' 是否判定为在线
'''
Public Property IsAlive As Boolean
End Class