Click here to Skip to main content
15,881,852 members
Articles / Hosted Services / Serverless
Article

Peer Graph - Importing and Exporting a Database

Rate me:
Please Sign up or sign in to vote.
4.64/5 (4 votes)
24 Jan 20064 min read 34K   649   12  
Importing and exporting a Peer Graph database using Microsoft's Peer-to-Peer technology.

Image 1

Background

Microsoft's Peer-to-Peer Graphing technology provides a stable, reliable, and robust infrastructure for Windows peer-to-peer applications to communicate. Peers use Peer Name Resolution Protocol (PNRP - a serverless DNS) to register and discover other peers within the graph. Graphs are the foundation for connecting peers, services, and resources within a peer network. A peer can be a user-interactive application, service, or resource. Graphing allows data to be passed between peers efficiently and reliably.

Microsoft's entire Peer-to-Peer technology is exposed through the latest Platform SDK as C/C++ API calls. However, the code in this article shows these APIs being used from .NET managed code using C#.

Introduction

This article completes the series on Peer Graphing by demonstrating the remaining two APIs which deal with importing and exporting a Peer Graph database to a file. I can think of several situations where exporting the current state of a peer graph is useful:

  • The creator of the application wants to deploy a blank database. This database is automatically imported during installation and allows the application to run even if the network is not available.
  • The creator of the application wants to include some default data in the database.
  • Exporting is used by the application to backup the current state of the graph for disaster recovery purposes.
  • The communication link between two peers is slow. Synchronizing a large database over this link is time consuming or not cost effective. Instead, the database file is copied to a CD or DVD and transported to the other location. The database is then imported and the initial synchronization is quick.

Exporting

When a database is exported, what's really happening is that any public records which are published to the graph are saved to a file format which can later be imported. In the situation where the creator of the application wants to deploy initial records, care should be taken to set their expiration to infinite (or far in the future) to avoid them expiring on import.

The PeerGraph ExportDatabase method is a simple wrapper around the PeerGraphExportDatabase API method. ExportDatabase must be called when the graph is open.

C#
public void ExportDatabase(string Path)
{
  uint hr = 
    PeerGraphNative.PeerGraphExportDatabase(hGraph, Path);
  if (hr != 0) throw new PeerGraphException(hr);
}

Two important bits of information are included in the exported file: the graph ID and the peer identity (in this case 0.TestGraph).

Importing

Before a database is imported, the underlying API compares the current graph ID and identity against those stored in the file. If they are different, a PEER_E_INVALID_DATABASE exception is thrown. Comparing the graph ID prevents someone else from using PeerGraphCreate to create a graph with the same identity and attempt to disrupt the graph. Comparing the peer identity prevents someone accidentally importing a file exporting from some other peer-to-peer graphing application.

The PeerGraph ImportDatabase method is static (Shared) since importing cannot occur when the graph is connected. The ImportDatabase method is a simple wrapper around the PeerGraphImportDatabase API method.

C#
public static void ImportDatabase(string GraphName, 
       string Identity, string DatabaseName, string Path)
{
  PeerGraph graph = new PeerGraph(GraphName);
  graph.Open(DatabaseName, Identity, false);

  uint hr = 
    PeerGraphNative.PeerGraphImportDatabase(graph.hGraph, Path);
  if (hr != 0)
      throw new PeerGraphException(hr);

  graph.Dispose();
}

The method starts by opening a graph, but not connecting. The database is imported and the graph is closed.

Using the Sample Application

The sample application includes a database exported by the author (test.p2pgraph). This database includes a single record published by the author. You must import this file before the graph can be opened, otherwise, you will receive a PEER_E_GRAPH_NOT_READY exception. Click the Import button to locate the file and import it. Once imported, click the Open button to open the graph. You should see a record containing a message from the author. An example of using the ImportDatabase method is shown below:

C#
private void button7_Click(object sender, System.EventArgs e)
{
  DialogResult result = openFileDialog1.ShowDialog();
  if (result == DialogResult.OK)
  {
    try
    {
      PeerGraph.ImportDatabase(GraphName, textBox2.Text, 
                DatabaseName, openFileDialog1.FileName);
      LogMessage(@"Import", @"Completed");
    }
    catch (PeerGraphException ex)
    {
      LogMessage(@"Import", ex.Message);
    }
  }
}

Once opened, you can use the text box provided to enter your own messages and add records to the graph. These records will expire after 10 minutes. Click the Export button to save the database. Run a second instance of the application locally or on another computer. Enter a different identity and import the database you just exported. You should see the messages you entered. Click the Open button to open the graph and synchronize with the first instance of the application. An example of using the Export database is shown below:

C#
private void button5_Click(object sender, System.EventArgs e)
{
  DialogResult result = saveFileDialog1.ShowDialog();
  if (result == DialogResult.OK)
  {
    try
    {
      graph.ExportDatabase(saveFileDialog1.FileName);
      LogMessage(@"Export", @"Completed");
    }
    catch (PeerGraphException ex)
    {
      LogMessage(@"Export", ex.Message);
    }
  }
}

Points of Interest

Sometimes the documentation is wrong. While implementing the ImportDatabase function, I saw that the documentation in Microsoft's MSDN library had a little note at the end of the PeerGraphImportDatabase method that concerned me. After contacting one of the developers at Microsoft about this, they agreed it was wrong. As a result, this did not complicate the implementation. Hopefully, by mid 2006, the documentation will have been corrected.

Links to Resources

I have found the following resources to be very useful in understanding peer graphs:

Conclusion

I hope you have found this article to be useful. The next article will introduce Microsoft's secure graphing technology called Peer Groups.

  1. Peer Groups - Introduction
  2. Peer Groups - Identity
  3. Peer Groups - Invitations
  1. Peer Collaboration - People Near Me
  2. Peer Collaboration - EndPoints
  3. Peer Collaboration - Capabilities
  4. Peer Collaboration - Presence
  5. Peer Collaboration - Invitations
  1. Peer Name Resolution - Windows Vista Enhancements

If you have suggestions for other topics, please leave a comment. Finally, a big thanks to all those who have been voting and for your encouraging feedback.

History

Initial revision.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Canada Canada
Adrian Moore is the Development Manager for the SCADA Vision system developed by ABB Inc in Calgary, Alberta.

He has been interested in compilers, parsers, real-time database systems and peer-to-peer solutions since the early 90's. In his spare time, he is currently working on a SQL parser for querying .NET DataSets (http://www.queryadataset.com).

Adrian is a Microsoft MVP for Windows Networking.

Comments and Discussions

 
-- There are no messages in this forum --