This example demonstrates how two clients can share and manipulate a value through a Shared Dictionary using GroupLab.Networking.
Download the source code and executable |
1. Start a new C# Windows Application in Visual Studio and add a reference to GroupLab.Networking.dll. For detailed instructions click here.
2. Add a Button like in the image below.
3. Drag a Shared Dictionary and a Subscription from the toolbox to the form.
1. Initialization: A SharedDictionary can be viewed as a remote Hashtable where clients can connect, store values at certain keys and retrieve these values. The dictionary uses paths as keys. Thus, something like '/home/user1' is a valid path and can store a value. Other clients can access this value if they chose to subscribe to this path. Subscriptions are exactly that: the client lets the Shared Dictionary know that it subscribed to a path, and the dictionary will notify that client whenever the value at that path changes or is removed. The Dictionary also notifies the client of any new subpaths that are added. For example, a client subscribed to '/home' will be notified when '/home/user1' is added. For this example we will use the "/value" path where a value will be stored. Clients will connect and will be able to read/write this value.
We will need to set the URL of a remote Shared Dictionary and open a connection to it. It will then subscribe to "/value". An event handler, subscr_notified, is added to notify the application when the value of "/value" has changed. All instances of the application will access the "/value" path. First they will read the value stored in "/value" then they will increment it and replace it in the path.Add the following code to the Form1() constructor:
InitializeComponent();
this.sharedDictionary1.Url = "tcp://localhost:increment";
// connect the shared dictionary to some default server, it will spawn a server or client
this.sharedDictionary1.Open();
// signal beginning of initialization for subscription1
this.subscription1.BeginInit();
// register the pattern with the shared dictionary server
this.subscription1.Pattern = "/value";
// register the event handler
this.subscription1.Notified += new SubscriptionEventHandler(subscr_notified);
// signal the end of initialization
this.subscription1.EndInit();
2. Increment: Whenever the button is hit, the value will be grabbed from the dictionary, incremented and replaced.
Add the following code to the button1_Click event:
// method called when the button is clicked
private void button1_Click(object sender, System.EventArgs e)
{
// check if we have an entry in the dictionary that matches the pattern
if(!this.sharedDictionary1.Contains("/value"))
this.sharedDictionary1["/value"] = 0; // then we create one and set its value to 0
else
// increment the existing value
this.sharedDictionary1["/value"] = (int)this.sharedDictionary1.GetEntry("/value").Value+1;
}
3. Receiving the value:
When the value of "/value" changes, the subscr_notified event will be triggered. The new value is retrieved from the dictionary and displayed on the button.
Add the following code to the subscr_notified event:
// event handler for subscription notifications
// called whenever a value in the path changes
private void subscr_notified(object sender, SubscriptionEventArgs args)
{
// get the value from the dictionary
int val = (int) this.sharedDictionary1.GetEntry("/value").Value;
this.button1.Text = "Increment "+val.ToString(); // and display it on the button
}
1.Open an instance of the application. Start Incrementing.
2. Open one more instance of the application. You will see that the second instance grabs the value of the first instance. No matter which instance you increment, both instances will have the same value.