iNetwork Tutorial #1: Incrementor

back to iNetwork

Readings

Prior to beginning this tutorial example, the following provides a short reading list that contains discussions and detailed descriptions of different aspects of the iNetworking Toolkit. If you have not already done so, it is recommended that you read through these before setting out to attempt the provided tutorials.

Introduction

Summary

The Incrementor example demonstrates how a number of different clients can manipulate the same variable by using the server as a means to communicate. In this example, each user is given the ability to increment a value. When one user increments this value it is reflected back to the other users, who are, in turn, also able to increment that same value.

Features

  • Users are able to see the updated value whenever another user clicks on the button.
  • When a client connects it receives the most up to date value.
  • When a client disconnects the value is not reset to zero for the remaining clients.

High Level Overview

For each client that connects to the server, the user associated with each client is provided a window that contains a button that, when clicked on, increments a value. This value is tracked by the server rather than the clients.

When a user clicks on the button the client creates a new message to send to the server telling it to increment the value. The server then updates the value and creates a new message to send out to all connected clients. Upon receiving this message from the server, each client updates the text in the button to show the most up-to-date value to the users.

In the case that a new client connects in the middle of an ongoing session wherein one or more clients are already connected, the server sends another message. Within this message is the current value. This way the new client will not have to start from zero. Instead, they start with the most up-to-date value similar to the other clients.

Reminder: The executable files, called IncrementorClient and IncrementorServer, for this example have been provided in a folder named Tutotrial1 when the toolkit was installed. You will find these in the Tutorial Examples folder within the GroupLab iNetwork Toolkit directory. Be sure to extract the files before opening them.

back to top

Setting-Up the Form

  1. Begin this tutorial by using a . Name the client project IncrementorClient and the server project IncrementorServer.
  2. Once you have completed the initial set-up above, from the Solution Explorer open the XAML file labelled MainWindow.xaml for the IncrementorClient.
  3. In the designer view add a Button just as in the image below.
    • In the Properties tab, change the button's name to _incrementButton.

back to top

Adding Code

This part of the tutorial will build on the "skeleton" code in the client and server templates.

Client

Open up the C# file for the client.

Constructors Region
  1. In the MainWindow constructor add a Click event handler.
    this._incrementButton.Click +=new RoutedEventHandler(OnButtonClick);

Initialization Region
  1. The client needs to be able to discover the server. Change Name-of-Service to Increment in the InitializeConnection method.
    Connection.Discover("Increment", new SingleConnectionDiscoveryEventHandler(OnConnectionDiscovered));
Main Body Region

Note: This region is called Incrementing within the executable files provided during installation.

  1. Next, in the OnMessageReceived event handler change Name-of-Message to ValueIncremented. Retrieve the integer value contained in the message. We will use that value to update what the client sees to show the new value using the GUI thread.
    1. case "ValueIncremented":
    2.     int value = msg.GetIntField("value");
    3.     this.Dispatcher.Invoke(
    4.         new Action(
    5.             delegate()
    6.             {
    7.                 this._incrementButton.Content = "Increment " + value.ToString();
    8.             }
    9.         ));
    10.     break;
  2. When the _incrementButton is clicked, the OnButtonClick event handler method is called. Create a new message called IncrementMessage and send it to the server.
    1. private void OnButtonClick(object sender, RoutedEventArgs e)
    2. {
    3.     Message msg = new Message("IncrementMessage");
    4.  
    5.     this._connection.SendMessage(msg);
    6. }
back to top

Server

Open up the C# file for the server.

Class Instance Variables Region
  1. Begin by declaring an integer variable that will keep track of the value that the users increment.
    private int _valueToIncrement;
Constructors Region
  1. Make sure to instantiate this variable in the MainWindow constructor.
    this._valueToIncrement = 0;
Initialization Region
  1. In the InitializeServer method be sure to rename Name-of-Service to Increment. You are also free to change the port number as long as the it is larger than 1024 and does not exceed 65535.
    this._server = new Server("Increment", 12346);
  2. Next, in the OnServerConnection event handler method call the SendIncrementedValue method. This ensures that when a client connects they are able to see the most-up-to-date value. Call this method within the first if statement after an event listener is added for the MessageReceived event.
    1. ...
    2. e.Connection.MessageReceived += new ConnectionMessageEventHandler(OnConnectionMessage);
    3. SendIncrementedValue();
    4. ...
Main Body Region

Note: This region is called Incrementing within the executable files provided during installation.

  1. In the OnConnectionMessage change Name-of-Message to IncrementMessage. Here, the server increments the value in _valueToIncrement then sends the updated value to all clients.
    1. case "IncrementMessage":
    2.     this._valueToIncrement += 1;
    3.     SendIncrementedValue();
    4.     break;
  2. Finally, create the SendIncrementedValue method. Create a message called ValueIncremented. Add a field which contains the manipulated value then broadcast the message to all clients.
    1. private void SendIncrementedValue()
    2. {
    3.     Message valueMsg = new Message("ValueIncremented");
    4.     valueMsg.AddField("value", this._valueToIncrement);
    5.  
    6.     this._server.BroadcastMessage(valueMsg);
    7. }
back to top

Result

Reminder: Run the server first before the client. To add more instances of the client simply right click on the IncrementorClient in the Solution Explorer. Select Debug then Start new instance.

You should get something that looks like the following:

back to top