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

Associating Attribute Meta-Data in a Peer Graph using Microsoft's Peer-to-Peer technology

Rate me:
Please Sign up or sign in to vote.
4.17/5 (2 votes)
6 Jan 20067 min read 25.6K   177   19  
Associating attribute meta-data in a Peer Graph using Microsoft's Peer-to-Peer technology.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

using Peer.Graph;

namespace article_src
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		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.GroupBox groupBox1;
		private System.Windows.Forms.TextBox textBox1;
		internal System.Windows.Forms.Button button4;
		internal System.Windows.Forms.Button Button2;
		private System.Windows.Forms.ToolTip toolTip1;
		private System.Windows.Forms.TextBox textBox2;
		internal System.Windows.Forms.Button button1;
		internal System.Windows.Forms.Button Button3;
		internal System.Windows.Forms.GroupBox GroupBox2;
		private System.Windows.Forms.GroupBox groupBox3;
		private System.Windows.Forms.ListBox listBox2;
		private System.Windows.Forms.Button Add;
		private System.Windows.Forms.Button Remove;
		private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.ListBox listBox1;
		private System.ComponentModel.IContainer components;

		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.components = new System.ComponentModel.Container();
			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.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.listBox2 = 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.GroupBox2 = new System.Windows.Forms.GroupBox();
			this.groupBox3 = new System.Windows.Forms.GroupBox();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.Remove = new System.Windows.Forms.Button();
			this.Add = new System.Windows.Forms.Button();
			this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
			((System.ComponentModel.ISupportInitialize)(this.Listening)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.Synchronized)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.HasConnections)).BeginInit();
			this.groupBox1.SuspendLayout();
			this.GroupBox2.SuspendLayout();
			this.groupBox3.SuspendLayout();
			this.SuspendLayout();
			// 
			// StatusBar1
			// 
			this.StatusBar1.Location = new System.Drawing.Point(0, 448);
			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(600, 22);
			this.StatusBar1.TabIndex = 15;
			this.StatusBar1.Text = "StatusBar1";
			// 
			// groupBox1
			// 
			this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			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, 382);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(582, 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);
			// 
			// listBox1
			// 
			this.listBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left)));
			this.listBox1.Location = new System.Drawing.Point(8, 200);
			this.listBox1.Name = "listBox1";
			this.listBox1.ScrollAlwaysVisible = true;
			this.listBox1.Size = new System.Drawing.Size(192, 95);
			this.listBox1.TabIndex = 31;
			this.toolTip1.SetToolTip(this.listBox1, "Double-click to Clear Messages");
			this.listBox1.DoubleClick += new System.EventHandler(this.listBox1_DoubleClick);
			// 
			// listBox2
			// 
			this.listBox2.DisplayMember = "Name";
			this.listBox2.Location = new System.Drawing.Point(8, 24);
			this.listBox2.Name = "listBox2";
			this.listBox2.Size = new System.Drawing.Size(192, 121);
			this.listBox2.Sorted = true;
			this.listBox2.TabIndex = 27;
			this.listBox2.SelectedIndexChanged += new System.EventHandler(this.listBox2_SelectedIndexChanged);
			// 
			// 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);
			// 
			// GroupBox2
			// 
			this.GroupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.GroupBox2.Controls.Add(this.groupBox3);
			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(582, 366);
			this.GroupBox2.TabIndex = 14;
			this.GroupBox2.TabStop = false;
			this.GroupBox2.Text = "Open Graph";
			// 
			// groupBox3
			// 
			this.groupBox3.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.groupBox3.Controls.Add(this.listBox1);
			this.groupBox3.Controls.Add(this.textBox3);
			this.groupBox3.Controls.Add(this.Remove);
			this.groupBox3.Controls.Add(this.Add);
			this.groupBox3.Controls.Add(this.listBox2);
			this.groupBox3.Location = new System.Drawing.Point(8, 56);
			this.groupBox3.Name = "groupBox3";
			this.groupBox3.Size = new System.Drawing.Size(568, 304);
			this.groupBox3.TabIndex = 28;
			this.groupBox3.TabStop = false;
			this.groupBox3.Text = "Shared Files";
			// 
			// textBox3
			// 
			this.textBox3.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.textBox3.Location = new System.Drawing.Point(208, 24);
			this.textBox3.Multiline = true;
			this.textBox3.Name = "textBox3";
			this.textBox3.ReadOnly = true;
			this.textBox3.ScrollBars = System.Windows.Forms.ScrollBars.Both;
			this.textBox3.Size = new System.Drawing.Size(352, 272);
			this.textBox3.TabIndex = 30;
			this.textBox3.Text = "";
			this.textBox3.WordWrap = false;
			// 
			// Remove
			// 
			this.Remove.Enabled = false;
			this.Remove.Location = new System.Drawing.Point(104, 160);
			this.Remove.Name = "Remove";
			this.Remove.Size = new System.Drawing.Size(88, 24);
			this.Remove.TabIndex = 29;
			this.Remove.Text = "Remove File";
			this.Remove.Click += new System.EventHandler(this.Remove_Click);
			// 
			// Add
			// 
			this.Add.Enabled = false;
			this.Add.Location = new System.Drawing.Point(16, 160);
			this.Add.Name = "Add";
			this.Add.Size = new System.Drawing.Size(80, 24);
			this.Add.TabIndex = 28;
			this.Add.Text = "Add Folder";
			this.Add.Click += new System.EventHandler(this.Add_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(600, 470);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.StatusBar1);
			this.Controls.Add(this.GroupBox2);
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Form1";
			this.Text = "Peer Graph - Share Files Demo";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
			this.Load += new System.EventHandler(this.Form1_Load);
			((System.ComponentModel.ISupportInitialize)(this.Listening)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.Synchronized)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.HasConnections)).EndInit();
			this.groupBox1.ResumeLayout(false);
			this.GroupBox2.ResumeLayout(false);
			this.groupBox3.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;
		private string DatabaseName = @"Database";
		private string GraphName = @"Shared Files";

		private Guid FILE_RECORD_TYPE = new Guid(0x30e35534, 0x5b76, 0x4770, 0x93, 0x90, 0xf5, 0x89, 0x5, 0x32, 0xba, 0xde);

		private ArrayList SharedFiles = new ArrayList();

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

		private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			button1_Click(sender, null);
		}

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

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

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


		private void button1_Click(object sender, System.EventArgs e)
		{
			try
			{
				graph.Close();
				OpenClose(true);
				LogMessage(@"Close", @"Completed");
				listBox2.Items.Clear();
				textBox3.Text = string.Empty;
			}
			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;
		}

		private void PopulateList()
		{
			foreach (PeerRecord record in graph.Records)
			{
				FileItem item = new FileItem(record.DataAsString, record.ID);
				listBox2.Items.Add(item);
			}
		}

		private void Add_Click(object sender, System.EventArgs e)
		{
			DialogResult result = folderBrowserDialog1.ShowDialog();
			if (result == DialogResult.OK)
			{
				string path = folderBrowserDialog1.SelectedPath;
				DirectoryInfo dinfo = new DirectoryInfo(path);
				foreach (FileInfo info in dinfo.GetFiles())
				{
					PeerRecord record = graph.CreatePeerRecord(FILE_RECORD_TYPE, new TimeSpan(0,1,0));
					record.Attributes.Add(info);
					record.DataAsString = info.Name;
					Guid recordId = graph.AddRecord(record);
				}
			}
		}

		private void Remove_Click(object sender, System.EventArgs e)
		{
			if (listBox2.SelectedIndex == -1) return;

			FileItem item = (FileItem)listBox2.SelectedItem;
			graph.DeleteRecord(item.RecordId);
			listBox2.Items.Remove(item);
		}


		private FileItem FindFile(Guid RecordId)
		{
			foreach (FileItem item in listBox2.Items)
			{
				if (item.RecordId == RecordId)
				{
					return item;
				}
			}
			return null;
		}

		private void DeleteFile(Guid RecordId)
		{
			FileItem item = FindFile(RecordId);
			if (item != null) listBox2.Items.Remove(item);
		}

		private void OnRecordChanged(object sender, PeerGraphRecordChangedEventArgs e)
		{
			PeerRecord record;
			FileItem item;
			switch (e.Action)
			{
			case PeerRecordAction.Added:
				record = graph.GetRecord(e.RecordId);
				LogMessage(@"Added", record.DataAsString);
				item = new FileItem(record.DataAsString, e.RecordId);
				listBox2.Items.Add(item);
				break;
			case PeerRecordAction.Deleted:
				LogMessage(@"Deleted", e.RecordId.ToString());
				DeleteFile(e.RecordId);
				break;
			case PeerRecordAction.Expired:
				LogMessage(@"Expired", e.RecordId.ToString());
				DeleteFile(e.RecordId);
				break;
			case PeerRecordAction.Updated:
				item = FindFile(e.RecordId);
				if (item.Equals(listBox2.SelectedItem)) 
				{
					record = graph.GetRecord(item.RecordId);
					textBox3.Text = record.Attributes.Xml;
				}
				LogMessage(@"Updated", e.RecordId.ToString());
				break;
			}
		}


		private void listBox2_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if (listBox2.SelectedIndex == -1)
				textBox3.Text = string.Empty;
			else
			{
				FileItem item = (FileItem)listBox2.SelectedItem;
				PeerRecord record = graph.GetRecord(item.RecordId);
				textBox3.Text = record.Attributes.Xml;
			}
		}

		private void listBox1_DoubleClick(object sender, System.EventArgs e)
		{
			listBox1.Items.Clear();
		}
	}

	public class FileItem
	{
		private string name;
		private Guid recordid;
		public FileItem(string Name, Guid RecordId)
		{
			name = Name;
			recordid = RecordId;
		}

		public string Name
		{
			get { return name; }
		}

		public Guid RecordId
		{
			get { return recordid; }
		}
	}
}

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