using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using VzSimpleClient.VZA;
namespace VzSimpleClient
{
class Program
{
Binder binder; // Binder object variable.
string session_id = ""; // Agent session ID.
// Main.
static void Main(string[] args)
{
Program vzClient = new Program();
try {
vzClient.Run();
}
catch (System.Web.Services.Protocols.SoapException ex) {
Console.WriteLine(ex.Code.ToString() + ", " + ex.Message);
Console.WriteLine("Details:" + ex.Detail.InnerText);
}
catch (System.Xml.XmlException xmlex) {
Console.WriteLine(xmlex.ToString());
}
catch (System.InvalidOperationException opex) {
Console.WriteLine(opex.Message + "\n" + opex.InnerException);
}
Console.WriteLine("Press Enter to conintinue...");
Console.Read();
}
///<summary>
/// Sample class TrustAllCertificatePolicy.
/// Used as a certificate policy provider.
/// Allows all certificates.
///</summary>
public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy
{
public TrustAllCertificatePolicy()
{ }
public bool CheckValidationResult(System.Net.ServicePoint sp,
System.Security.Cryptography.X509Certificates.X509Certificate cert,
System.Net.WebRequest req, int problem)
{
return true;
}
}
/// <summary>
/// Sample class Binder.
/// Provides methods to create the specified binding object
/// and to populate the Agent message header.
/// </summary>
public class Binder
{
string URL; // Agent server URL.
string session; // Agent session ID.
// Constructor. Sets URL and session ID values.
public Binder(string url, string sess)
{
URL = url;
session = sess;
}
/// <summary>
/// Method InitBinding (overloaded).
/// Creates a binding object.
/// <param name="bindingType">
/// The name of the proxy class from which to
/// create the object.
/// </param>
/// <returns>
/// <para>New binding object.</para>
/// </returns>
/// </summary>
public System.Object InitBinding(System.Type bindingType)
{
System.Object Binding =
bindingType.GetConstructor(System.Type.EmptyTypes).Invoke(null);
// Set URL.
bindingType.GetProperty("Url").SetValue(Binding, URL, null);
// Create the request message header object.
packet_headerType header = new packet_headerType();
// Set session ID.
header.session = session;
/* Set the "target" parameter in the Agent request
* message header. The parameter must contain the name
* of the corresponding Agent operator.
* The operator name can be obtained from the name of the
* proxy class. It is the substring from the beginning of the name
* followed by the "Binding" substring. For example, the name
* of the corresponding operator for the "filerBinding" class is
* "filer".
* All Agent requests except "system" requests must have the
* target operator value set. System is the only operator that requires
* the omission of the "target" parameter from the header.
*/
if (bindingType != typeof(systemBinding)) {
header.target = new string[1];
header.target[0] = bindingType.Name.Replace("Binding", "");
}
// Set the request message header.
bindingType.GetField("packet_header").SetValue(Binding, header);
return Binding;
}
/// <summary>
/// Method InitBinding (overloaded).
/// Creates a binding object.
/// Allows to set destination Container.
/// </summary>
/// <param name="bindingType">
/// The name of the proxy class from which
/// to create the object.
/// </param>
/// <param name="eid">
/// The Server ID of the destination Container to which to route
/// the request message for processing.
/// </param>
/// <returns>
/// <para>New binding object.</para>
/// </returns>
/// </returns>
public System.Object InitBinding(System.Type bindingType, string eid)
{
System.Object Binding =
bindingType.GetConstructor(System.Type.EmptyTypes).Invoke(null);
// Set URL.
bindingType.GetProperty("Url").SetValue(Binding, URL, null);
// Create the request message header object.
packet_headerType header = new packet_headerType();
// Set session ID.
header.session = session;
/* Set the "target" parameter in the Agent request
* message header.
*/
if (bindingType != typeof(systemBinding)) {
header.target = new string[1];
header.target[0] = bindingType.Name.Replace("Binding", "");
}
// Set the destination Server ID.
header.dst.host = eid;
// Set the request message header.
bindingType.GetField("packet_header").SetValue(Binding, header);
return Binding;
}
}
/// <summary>
/// Sample function Login.
/// Authenticates the user using the specified credentials and
/// creates a new session.
/// </summary>
/// <param name="url">Agent server URL.</param>
/// <param name="name">User name.</param>
/// <param name="domain">Domain.</param>
/// <param name="realm">Realm ID.</param>
/// <param name="password">Password</param>
/// <returns>New session ID.</returns>
///
public string Login(string url, string name, string domain, string realm, string password)
{
try {
System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy();
// Login information object.
login1 loginInfo = new login1();
/* The sessionmBinding class provides the login and
* session management functionality.
*/
sessionmBinding sessionm = new VZA.sessionmBinding();
/* Instantiate the System.Text.Encoding class that will
* be used to convert strings to byte arrays.
*/
System.Text.Encoding ascii = System.Text.Encoding.ASCII;
// Populate the connection and the login parameters.
sessionm.Url = url;
loginInfo.name = ascii.GetBytes(name);
if (domain.Length != 0) {
loginInfo.domain = ascii.GetBytes(domain);
}
if (realm.Length != 0) {
loginInfo.realm = realm;
}
loginInfo.password = ascii.GetBytes(password);
// Log the specified user in.
return sessionm.login(loginInfo).session_id;
}
catch (Exception e) {
return "Exception: " + e.Message;
}
}
/// <summary>
/// sample function GetCTList.
/// Retrieves the list of Virtuozzo Containers from the Hardware Node.
/// </summary>
/// <param name="state">Container state code.</param>
/// <returns>Container names.</returns>
///
public string GetCTList(int state)
{
string list_result = "";
try {
// Instantiate the proxy class.
vzaenvmBinding env = (vzaenvmBinding)binder.InitBinding(typeof(vzaenvmBinding));
// The main input object.
get_list1 velist = new get_list1();
/* Set the Container status parameter.
* -1 means ignore the status.
*/
env_statusType[] env_status = new env_statusType[1];
env_status[0] = new env_statusType();
if (state == -1) {
env_status[0].stateSpecified = false;
}
else {
env_status[0].state = state;
}
velist.status = env_status;
/* Get the list of the Containers then loop through it getting the
* Server ID and the name for each Container
*/
foreach (string ve_eid in env.get_list(velist)) {
get_info2 ve_info = new get_info2();
ve_info.eid = new string[1];
ve_info.eid[0] = ve_eid;
/* Get the Container name from the Container configuration structure.
* Please note that if name was not assigned to a
* Container when it was created, the "name" field will be empty.
*/
list_result += env.get_info(ve_info)[0].virtual_config.name + "\n";
}
}
catch (Exception e) {
list_result += "Exception: " + e.Message;
}
return list_result;
}
/// <summary>
/// The Run() function is called from Main().
/// It contains the code that executes other sample functions.
/// </summary>
///
public void Run()
{
/* The Agent server URL. Use the IP of
* your own Hardware Node here.
*/
string url = "http://10.30.67.54:8080/";
// User name.
string user = "root";
// Domain name.
string domain = "";
/* Realm ID.
* We are using the "system" realm here, so the
* user will be authenticated against the
* host operating system user registry.
*/
string realm = "00000000-0000-0000-0000-000000000000";
string password = "1q2w3e";
// Log the user in.
session_id = this.Login(url, user, domain, realm, password);
Console.WriteLine("Session ID: " + session_id);
Console.WriteLine();
// Create the Binder object.
if (binder == null) {
binder = new Binder(url, session_id);
}
// Get the list of Containers from the Hardware Node.
Console.WriteLine(GetCTList(-1));
Console.WriteLine();
}
}
}