Home > Articles > Programming > C#

  • Print
  • + Share This
Like this article? We recommend

Putting It All Together

Let’s look at the last file of interest: Program.cs. Listing 4 shows the main program.

Listing 4 The main program for Program.cs.

class Program
{
static void Main(string[] args)
{
// Instantiate an LSP
Lsp lsp = new Lsp("LSP123", 1, 5, "R2R3R4R5", 100, 150);

// Register event handlers.
lsp.FullyCongested += new Lsp.LspEventHandler(FullyCongested);
lsp.GettingCongested += new Lsp.LspEventHandler(GettingCongested);
lsp.CannotForwardData += new Lsp.LspEventHandler(CannotForwardData);

// Forward some blocks of data (to generate some events.)
Console.WriteLine("\n***** Forwarding data *****");
for (int i = 0; i < 3; i++)
  lsp.ForwardSomeData(20);

Console.ReadLine();
}
}

Notice the way Listing 4 defines three event handlers, all of which are attached to the object called lsp. Once the event handlers are in place, we call our old friend, the method ForwardSomeData(). The latter implements the rules and generates the associated events.

The event handlers are defined as part of Program.cs as illustrated in Listing 5.

Listing 5 Event handlers.

public static void FullyCongested(string msg)
{
Console.WriteLine(msg);
}

public static void GettingCongested(string msg)
{
Console.WriteLine(msg);
Console.WriteLine("Service Suggestion:");
Console.WriteLine("To maintain service, please allocate more or " +
"free some bandwidth.");
Console.WriteLine();
}

public static void CannotForwardData(string msg)
{
Console.WriteLine(msg);
Console.WriteLine("Service Suggestion:");
Console.WriteLine("To restart the forwarding service, please allocate more or " + "free some bandwidth.");
Console.WriteLine();
}

If you look back at Figures 2 and 3, you can see the text from the methods in Listing 5. Notice that the event handlers in Listing 5 try to provide some suggestions to go along with the raw event data. Rather than just reporting the problem, the event handlers try to help the network manager.

  • + Share This
  • 🔖 Save To Your Account