Create a new Class library project in Visual Studio .NET,
set the namespace to p2pfirewall and the class as tunnel. Since the class
will be remoted, we need to derive the tunnel class from System.MarshallByRef
Object.
The check methods are slightly more complex. checkRequests
for instance, pulls the first pending request from the database that is
marked for the attention of the peer making the call. That request is then
deleted from the database. checkResponses is similar, where the first row from
the responses table that have come from the requested peer, and contains the
correct file is returned.
using System;
using System.Data.OleDb;
namespace p2pfirewall
{
public
class tunnel : System.MarshalByRefObject
{
private
string DSN = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=C:\\filetransfers.mdb";
public
void postRequest(string requestingPeer, string filename,
string respondingPeer)
{
string
SQL;
SQL
= "insert into requests";
SQL
+= "(requester,filename,respondent) values (";
SQL
+= "'" + requestingPeer + "',";
SQL
+= "'" + filename + "',";
SQL
+= "'" + respondingPeer + "')";
nonQuery(SQL,DSN);
}
public
void postResponse(string respondingPeer, string filename,
string fileContents,string requestingPeer)
{
string SQL;
SQL
= "insert into responses ";
SQL
+= " (requester,fileContents,filename,respondent) ";
SQL
+= " values (";
SQL
+= "'" + requestingPeer + "',";
SQL
+= "'" + fileContents + "',";
SQL
+= "'" + filename + "',";
SQL
+= "'" + respondingPeer + "')";
nonQuery(SQL,DSN);
}
public
string checkRequests(string peer)
{
string
SQL;
string
filename;
SQL
= "select top 1 * from requests where ";
SQL
+= " respondent='" + peer + "'";
filename
= Query("filename",SQL,DSN);
//
don't return the same request twice.
SQL
= "delete from requests where ";
SQL
+= " respondent='" + peer + "' and ";
SQL
+= " filename='" + filename + "'";
nonQuery(SQL,DSN);
return
filename;
}
public
string checkResponse(string respondingPeer,
string filename)
{
string
SQL;
SQL
= "select top 1 * from responses where ";
SQL
+= " respondent='" + respondingPeer + "' and ";
SQL
+= " filename='" + filename + "'";
return
Query("fileContents",SQL,DSN);
}
}
}