GroupLab.Networking :: Code Cookbook

This page contains handy bits of code that may be useful when using GroupLab.Networking.

Code Recipes:

  1. Connect Dialog
  2. Event Handlers
  3. Setting Up a Subscription
  4. Adding a Value to the Shared Dictionary
  5. Retrieving a Value from the Shared Dictionary
  6. Iterating Through the Shared Dictionary

I. Connect Dialog

GroupLab.Networking has a built in dialog that will ask the user for the URL to connect to.

string url = "tcp://localhost:port";
if(DialogResult.OK == GroupLab.Networking.UrlPrompt.Show(ref url))
{
   // add your code here
   // it might look something like this


   this.SD.Url = url;
   this.SD.Opened += new EventHandler(SD_Opened);
   this.SD.Closed += new EventHandler(SD_Closed);
   this.SD.Open(); 
}

II. Event Handlers

The following lines set up the commonly used event handlers for a Shared Dictionary:

this.SD.Connected += new EventHandler(SD_Connected);
this.SD.Disconnected +=new DisconnectedEventHandler(SD_Disconnected);

this.SD.Opened +=new EventHandler(SD_Opened);
this.SD.Closed +=new EventHandler(SD_Closed);

The following line sets up the commonly used event handler for a Subscription:

this.sub.Notified+=new SubscriptionEventHandler(sub_Notified);

III. Setting Up a Subscription

This code is the general style for setting up a subscription:

this.sub.BeginInit();
this.sub.Pattern = "/path";
this.sub.Notified += new SubscriptionEventHandler(sub_Notified);
this.sub.EndInit();

IV. Adding a Value to the Shared Dictionary

Integer: places an integer into a shared dictionary called SD at the path /value:

    this.SD["/value"] = 5;

String: places a string into a shared dictionary called SD at the path /value:

    this.SD["/value"] = "hello";

Vector: creates a vector and places it into a shared dictionary called SD at the path /value:

    SharedDictionary.Vector v = new SharedDictionary.Vector();
    this.SD["/value"] = v;

Vector Elements: places an integer into the last element of a vector.  The vector is in the shared dictionary called SD at the path /value.

    this.SD["value#-0"] = element;

V. Retrieving a Value from the Shared Dictionary

Integer: retrieves an integer from a shared dictionary called SD at the path /value:

    int x = (int) this.SD.GetEntry("/value").Value;

String: retrieves a string from a shared dictionary called SD at the path /value:

    string y = this.SD.GetEntry("/value").Value.ToString();

Vector: retrieves a vector from a shared dictionary called SD at the path /value:

    SharedDictionary.Vector v = this.SD["/value"] as SharedDictionary.Vector;

Vector Elements: retrieves an integer from the last element of a vector.  The vector is in the shared dictionary called SD at the path /value.

    SharedDictionary.Vector v = this.SD["/value"] as SharedDictionary.Vector;
    int z = (int) v[0];

Subscription Notifications: retrieves a string from the shared dictionary during a subscription notification.

    private void sub_Notified(object sender, SubscriptionEventArgs e)
    {
         string location = (string) e.Entry.Value;
    }

VI. Iterating Through the Shared Dictionary

You can iterate through entries in a shared dictionary.  The following code grabs each item in the dictionary and adds it to an array list.  Then, it removes each item from the dictionary.

ArrayList removalKeys = new ArrayList();

foreach(GroupLab.Networking.SharedDictionary.Entry i in
    (this.SD["/value/?"] as IEnumerable))
{

    // note: instead of actually removing each key here
    // we add it to a list of keys to remove after
    // this is necessary because when iterating over an
    // enumeration you cannot modify the shared dictionary

    removalKeys.Add(i.Path);
 
}
foreach(string path in removalKeys)
{
    this.SD.Remove(path);
}