Click on the request button and add the following code
C#
private void btnRequest_Click(object sender,
System.EventArgs e)
{
Tunnel.postRequest(peerName,tbFilename.Text,tbRequestFrom.Text);
tmrCheckResponses.Enabled
= true;
}
VB.NET
Private
Sub btnRequest_Click(ByVal sender As Object, ByVal e As _
System.EventArgs)
Tunnel.postRequest(peerName,tbFilename.Text,tbRequestFrom.Text)
tmrCheckResponses.Enabled
= True
End Sub
The response-checking algorithm is quite simple. A remote
call is made to checkResponse, with the details of the file
expected. If there is no response, this call returns null, otherwise the file
contents are displayed on-screen. Once the task is complete, the timer
disables itself.
Click on the response timer, and add the following code.
C#
private void tmrCheckResponses_Tick(object
sender, System.EventArgs e)
{
string
fileContents = null;
fileContents
= Tunnel.checkResponse(tbRequestFrom.Text,tbFilename.Text);
if
(fileContents == null) return;
tbStatus.Text
= fileContents;
tmrCheckResponses.Enabled
= false;
}
VB.NET
Private
Sub tmrCheckResponses_Tick(ByVal sender As Object, ByVal e As
System.EventArgs)
Dim
fileContents As String = Nothing
fileContents
= Tunnel.checkResponse(tbRequestFrom.Text,tbFilename.Text)
If
fileContents Is Nothing Then
Return
End
If
tbStatus.Text
= fileContents
tmrCheckResponses.Enabled
= False
End Sub
By issuing requests, we provoke responses from peers,
however, requests from other peers can arrive at any moment, for this reason,
a free-running timer tmrCheckRequests polls the server for new requests
by calling the remote procedure checkRequests. If there are no requests,
null is returned otherwise, the requested file will be read from disk and a
response will be posted, with the file contents and file name.
Click on the request timer, and add the following code
C#
private void tmrCheckRequests_Tick(object
sender, System.EventArgs e)
{
string
requestedFile = null;
string
fileContents = "";
requestedFile
= Tunnel.checkRequests(peerName);
if
(requestedFile==null) return;
fileContents
= (new StreamReader(requestedFile)).ReadToEnd();
Tunnel.postResponse(peerName,requestedFile,fileContents,"");
}
VB.NET
Private
Sub tmrCheckRequests_Tick(ByVal sender As Object, ByVal e As _
System.EventArgs)
Dim
requestedFile As String = Nothing
Dim
fileContents As String = ""
requestedFile
= Tunnel.checkRequests(peerName)
if
requestedFile=Nothing then return
fileContents
= (New StreamReader(requestedFile)).ReadToEnd()
Tunnel.postResponse(peerName,requestedFile,fileContents,"")
End Sub
The tmrCheckRequests_Tick event occurs periodically, and
serves to execute code that makes a query against the remote object to
determine if there are any new rows in the request table that correspond to
this peer’s name. If there were requests made, then the name of the file
requested is returned from the checkRequests method on the remote object.
Should another peer request a file, it is read from the
local disk by passing the filename as a parameter to a StreamReader, and then
reading to the end of the stream. The contents of the file are then posted
back to the remote object.
Page 5
Page 7