Click here to Skip to main content
15,896,473 members
Articles / Hosted Services / Serverless

An introduction to Microsoft's Peer-to-Peer Graphing technology

Rate me:
Please Sign up or sign in to vote.
4.80/5 (18 votes)
9 Nov 20059 min read 94.7K   1K   68  
An introduction to Microsoft's Peer-to-Peer Graphing technology.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using Peer.Graph;

namespace article_src
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		internal System.Windows.Forms.GroupBox GroupBox2;
		internal System.Windows.Forms.Button Button3;
		internal System.Windows.Forms.Button button1;
		internal System.Windows.Forms.StatusBar StatusBar1;
		internal System.Windows.Forms.StatusBarPanel Listening;
		internal System.Windows.Forms.StatusBarPanel Synchronized;
		internal System.Windows.Forms.StatusBarPanel HasConnections;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.TextBox textBox1;
		internal System.Windows.Forms.Button button4;
		internal System.Windows.Forms.Button Button2;
		private System.Windows.Forms.ListBox listBox1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.GroupBox2 = new System.Windows.Forms.GroupBox();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.Button3 = new System.Windows.Forms.Button();
			this.StatusBar1 = new System.Windows.Forms.StatusBar();
			this.Listening = new System.Windows.Forms.StatusBarPanel();
			this.Synchronized = new System.Windows.Forms.StatusBarPanel();
			this.HasConnections = new System.Windows.Forms.StatusBarPanel();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.button4 = new System.Windows.Forms.Button();
			this.Button2 = new System.Windows.Forms.Button();
			this.GroupBox2.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.Listening)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.Synchronized)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.HasConnections)).BeginInit();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// GroupBox2
			// 
			this.GroupBox2.Controls.Add(this.listBox1);
			this.GroupBox2.Controls.Add(this.textBox2);
			this.GroupBox2.Controls.Add(this.button1);
			this.GroupBox2.Controls.Add(this.Button3);
			this.GroupBox2.Location = new System.Drawing.Point(8, 8);
			this.GroupBox2.Name = "GroupBox2";
			this.GroupBox2.Size = new System.Drawing.Size(368, 208);
			this.GroupBox2.TabIndex = 14;
			this.GroupBox2.TabStop = false;
			this.GroupBox2.Text = "Open Graph";
			// 
			// listBox1
			// 
			this.listBox1.Location = new System.Drawing.Point(8, 64);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(344, 134);
			this.listBox1.TabIndex = 17;
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(96, 24);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(176, 20);
			this.textBox2.TabIndex = 14;
			this.textBox2.Text = "User1";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(280, 24);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(72, 24);
			this.button1.TabIndex = 11;
			this.button1.Text = "Close";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// Button3
			// 
			this.Button3.Location = new System.Drawing.Point(16, 24);
			this.Button3.Name = "Button3";
			this.Button3.Size = new System.Drawing.Size(72, 24);
			this.Button3.TabIndex = 10;
			this.Button3.Text = "Open";
			this.Button3.Click += new System.EventHandler(this.Button3_Click);
			// 
			// StatusBar1
			// 
			this.StatusBar1.Location = new System.Drawing.Point(0, 290);
			this.StatusBar1.Name = "StatusBar1";
			this.StatusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
																						  this.Listening,
																						  this.Synchronized,
																						  this.HasConnections});
			this.StatusBar1.ShowPanels = true;
			this.StatusBar1.Size = new System.Drawing.Size(386, 22);
			this.StatusBar1.TabIndex = 15;
			this.StatusBar1.Text = "StatusBar1";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.textBox1);
			this.groupBox1.Controls.Add(this.button4);
			this.groupBox1.Controls.Add(this.Button2);
			this.groupBox1.Location = new System.Drawing.Point(8, 224);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(368, 64);
			this.groupBox1.TabIndex = 17;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Create or Delete \"Test Graph\"";
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(96, 24);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(176, 20);
			this.textBox1.TabIndex = 16;
			this.textBox1.Text = "User1";
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(280, 24);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(72, 24);
			this.button4.TabIndex = 15;
			this.button4.Text = "Delete";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// Button2
			// 
			this.Button2.Location = new System.Drawing.Point(16, 24);
			this.Button2.Name = "Button2";
			this.Button2.Size = new System.Drawing.Size(72, 24);
			this.Button2.TabIndex = 14;
			this.Button2.Text = "Create";
			this.Button2.Click += new System.EventHandler(this.Button2_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(386, 312);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.StatusBar1);
			this.Controls.Add(this.GroupBox2);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Form1";
			this.Text = "Peer Graph - Create / Open Demo";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.GroupBox2.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.Listening)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.Synchronized)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.HasConnections)).EndInit();
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}
		
		private PeerGraph graph;
		string DatabaseName = @"Database";
		string GraphName = @"Test Graph";

		private void Form1_Load(object sender, System.EventArgs e)
		{
			graph = new PeerGraph(GraphName);
			graph.StatusChanged += new PeerGraph.StatusChangedHandler(OnStatusChanged);
		}

		private void LogMessage(string Member, string Message)
		{
			listBox1.Items.Add(String.Format("[{0}]: {1}", Member, Message));
		}

		private void OpenClose(bool state)
		{
			Button3.Enabled = state;
			textBox2.Enabled = state;
		}

		private void Button3_Click(object sender, System.EventArgs e)
		{
			try
			{
				graph.Open(DatabaseName, textBox2.Text);
				OpenClose(false);
				LogMessage(@"Open", @"Completed");
			}
			catch (Exception ex)
			{
				LogMessage(@"Open", ex.Message);
			}
		}


		private void button1_Click(object sender, System.EventArgs e)
		{
			try
			{
				graph.Close();
				OpenClose(true);
				LogMessage(@"Close", @"Completed");
			}
			catch (Exception ex)
			{
				LogMessage(@"Close", ex.Message);
			}
		}

		private void Button2_Click(object sender, System.EventArgs e)
		{
			try
			{
				PeerGraph.Create(GraphName, textBox1.Text, DatabaseName);
				LogMessage(@"Create", @"Completed");
			}
			catch (Exception ex)
			{
				LogMessage(@"Create", ex.Message);
			}
		}

		private void button4_Click(object sender, System.EventArgs e)
		{
			try
			{
				PeerGraph.Delete(GraphName, textBox1.Text, DatabaseName);
				LogMessage(@"Delete", @"Completed");
			}
			catch (Exception ex)
			{
				LogMessage(@"Delete", ex.Message);
			}
		}

		private void OnStatusChanged(object sender, PeerGraphStatusChangedEventArgs e)
		{
			string status = string.Empty;
			if (e.IsListening)
				 status = @"Listening";
			StatusBar1.Panels[0].Text = status;
			status = string.Empty;
			if (e.IsSynchronized)
				status = @"Synchronized";
			StatusBar1.Panels[1].Text = status;
			status = string.Empty;
			if (e.HasConnections)
				status = @"Has Connections";
			StatusBar1.Panels[2].Text = status;
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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