163 lines
7.0 KiB
C#

//----------------------------------------------------------------------------
//
// Copyright (c) Intel Corporation, 2011 All Rights Reserved.
//
// File: MPSClientSample.cs
//
// Contents: Sample code for Intel(R) Active Management Technology
// (Intel® AMT) MPSClient Sample.
//
//----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Intel.Manageability.Utils;
namespace MPSInterfaceClient
{
class MPSClientSample
{
#region PRIVATE DATA MEMBERS
private const string SUBSCRIBE = "subscribe"; // Subscribe machine
private const string UNSUBSCRIBE = "unsubscribe"; // Unsubscribe machine
private const string URL = "url"; // Url of MPS server
private const string GET_ALL_CONTEXTS = "getallcontexts"; //Get all contexts
private const string IS_MACHINE_CONNECTED = "ismachineconnected"; // Cheack if specific machine is connected
private static CmdLineArguments Params = new CmdLineArguments();
private static MpsInterfaceClientApi mps;
#endregion
#region MAIN
private static string CreateUsage()
{
string usageStr = "";
string assembly = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
usageStr += "Usage:\n";
usageStr += " " + assembly + " -url <mpsUrl> <opt> [-subscribe <uri>] [-unsubscribe <uri>] " +
"[-getallcontexts] [-ismachineconnected <uri>]\n" + "\n\n";
usageStr += "When <opt> is:\n";
usageStr += " -" + SUBSCRIBE + "<value>:\t\tenables subscribing a system to receive\n\t\t\t\tMpsNotification events whenever a machine\n\t\t\t\tconnects to or disconnects from the MPS.\n";
usageStr += "\t\t\t\tvalue is the uri of a specific machine.\n\n";
usageStr += " -" + UNSUBSCRIBE + "<value>:\t\tremoves a system from the subscribers list.\n";
usageStr += "\t\t\t\tvalue is the uri of a specific machine.\n\n";
usageStr += " -" + GET_ALL_CONTEXTS + ":\t\tenumerates the names of machines that are\n\t\t\t\tcurrently connected to the MPS via the APF\n\t\t\t\tprotocol.\n\t\t\t\t\n\n";
usageStr += " -" + IS_MACHINE_CONNECTED + " <value>:\tchecks whether a given machine is currently\n\t\t\t\tconnected to the MPS.\n";
usageStr += "\t\t\t\tvalue is the uri of a specific machine.\n";
return usageStr;
}
static void Main(string[] args)
{
Params.AddArg(SUBSCRIBE, true, false);
Params.AddArg(UNSUBSCRIBE, true, false);
Params.AddArg(URL, true, true);
Params.AddArg(GET_ALL_CONTEXTS, false, false);
Params.AddArg(IS_MACHINE_CONNECTED, true, false);
string usage = CreateUsage();
try
{
// If no params were given, just print the usage
if (args.Length == 0)
{
Console.WriteLine("\n" + usage);
return;
}
Params.Parse(args);
if (!(Uri.TryCreate(Params.Value(URL), 0, out Uri uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps)))
{
Console.WriteLine("\n--------------CmdLineArguments Error--------------");
Console.WriteLine("Invalid value for URL parameter.");
Console.WriteLine(usage);
}
else
{
mps = new MpsInterfaceClientApi(Params.Value(URL));
if (Params.Selected(SUBSCRIBE))
{
if (!Uri.IsWellFormedUriString(Params.Value(SUBSCRIBE), 0))
{
Console.WriteLine("\n--------------CmdLineArguments Error--------------");
Console.WriteLine("Invalid value for Subscribe option.");
Console.WriteLine(usage);
}
else
mps.subscribeForNotification(Params.Value(SUBSCRIBE));
}
else if (Params.Selected(UNSUBSCRIBE))
{
if (!Uri.IsWellFormedUriString(Params.Value(UNSUBSCRIBE), 0))
{
Console.WriteLine("\n--------------CmdLineArguments Error--------------");
Console.WriteLine("Invalid value for Unsubscribe option.");
Console.WriteLine(usage);
}
else
mps.unsubscribeForNotification(Params.Value(UNSUBSCRIBE));
}
else if (Params.Selected(GET_ALL_CONTEXTS))
{
mps.getAllContexts();
}
else if (Params.Selected(IS_MACHINE_CONNECTED))
{
if (!Uri.IsWellFormedUriString(Params.Value(IS_MACHINE_CONNECTED), 0))
{
Console.WriteLine("\n--------------CmdLineArguments Error--------------");
Console.WriteLine("Invalid value for IsMachineConnected option.");
Console.WriteLine(usage);
}
else
mps.isMachineConnected(Params.Value(IS_MACHINE_CONNECTED));
}
else
{
Console.WriteLine("\n--------------CmdLineArguments Error--------------");
Console.WriteLine("could not find a value for the option.");
Console.WriteLine(usage);
}
}
}
catch (System.Net.WebException e)
{
Console.WriteLine("\n--------------MpsInterface.Exception--------------");
Console.WriteLine(e.Message);
Console.WriteLine(usage);
}
catch (System.Web.Services.Protocols.SoapException e)
{
Console.WriteLine("\n--------------SoapException.Exception--------------");
Console.WriteLine(e.Message);
Console.WriteLine(usage);
}
catch (CmdLineArguments.Exception e)
{
Console.WriteLine("\n--------------CmdLineArguments.Exception--------------");
Console.WriteLine(e.Message);
Console.WriteLine(usage);
}
catch (Exception e)
{
Console.WriteLine("\n--------------General Exception--------------");
Console.WriteLine(e.Message);
Console.WriteLine(usage);
}
}
#endregion Main
}
}