JavaScript Editor
JavaScript Debugger
The discovery service wraps the P2PDatabase component. As with the discovery service in Chapter 8, it catches all exceptions, logs them, and replaces them with a generic ApplicationException to ensure that no sensitive information will be returned to the client.
Peers interact with the discovery service as follows:
New users call RegisterNewUser() to create a new record in the Peers table.
Users call StartSession() to log in, supply their current connectivity information, and create a new record in the Sessions table.
Users call GetPeers() periodically to retrieve a list of other users. In turn, GetPeers() calls RefreshSession(), ensuring that the record for the requesting peer is kept current.
If a user wants to send a message, it calls GetPeer() to retrieve the connectivity information for a specific user. It can then contact the user directly.
When the user is finished and wants to leave the peer community, it calls EndSession() to remove the session record.
The full DiscoveryService code is shown here:
Public Class DiscoveryService
Inherits System.Web.Services.WebService
Private DB As New P2PDatabase()
<WebMethod()> _
Public Sub RegisterNewUser(ByVal emailAddress As String)
Try
DB.AddPeer(emailAddress)
Catch err As Exception
Trace.Write(err.ToString)
Throw New ApplicationException("Could not register new user.")
End Try
End Sub
<WebMethod()> _
Public Function StartSession(ByVal emailAddress As String, _
objRef() As Byte) As Guid
Try
Return DB.CreateSession(emailAddress, objRef)
Catch err As Exception
Trace.Write(err.ToString)
Throw New ApplicationException("Could not create session.")
End Try
End Function
<WebMethod()> _
Public Sub RefreshSession(ByVal sessionID As Guid)
Try
DB.RefreshSession(sessionID)
Catch err As Exception
Trace.Write(err.ToString)
Throw New ApplicationException("Could not refresh session.")
End Try
End Sub
<WebMethod()> _
Public Sub EndSession(ByVal sessionID As Guid)
Try
DB.DeleteSession(sessionID)
Catch err As Exception
Trace.Write(err.ToString)
Throw New ApplicationException("Could not end session.")
End Try
End Sub
<WebMethod()> _
Public Function GetPeerInfo(ByVal emailAddress As String, _
ByVal sessionID As Guid) As PeerInfo
Try
Return DB.GetPeerInfo(emailAddress)
Catch err As Exception
Trace.Write(err.ToString)
Throw New ApplicationException("Could not find peer.")
End Try
End Function
<WebMethod()> _
Public Function GetPeers() As String()
Try
RefreshSession(sessionID)
Return DB.GetPeers()
Catch err As Exception
Trace.Write(err.ToString)
Throw New ApplicationException("Could not find peers.")
End Try
End Function
End Class
Free JavaScript Editor
JavaScript Editor