/***********************************************************************************************
* EnumServer.cs
*
* Developed by     : SafeNet, Inc
*
* Copyright (C) 2009 SafeNet, Inc. All rights reserved.
*
* Purpose          : EnumServer API user input/output form source file.

***********************************************************************************************/

// Include namespace for Source code
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Sproeval
{
   /// <summary>
   /// EnumServer API user input output form source file .This is source file for
   /// EnumServer Form.
   /// </summary>
   public class EnumServer : System.Windows.Forms.Form
   {
      // Duffrent resource control declaration here
      private System.Windows.Forms.Label label1;
      private System.Windows.Forms.ComboBox comboBox1;
      private System.Windows.Forms.Label label2;
      private System.Windows.Forms.TextBox E_iDevID;
      private System.Windows.Forms.Label label3;
      private System.Windows.Forms.TextBox E_i_NumServers;
      private System.Windows.Forms.Label label4;
      private System.Windows.Forms.ComboBox C_oServers;
      private System.Windows.Forms.Button EXECUTEBtn;
      private System.Windows.Forms.Button CANECLBtn;
      public const int NSPRO_MAX__BUFFER_SIZE = 340;
      public const int NSPRO_STRUCT_SIZE = 34;

      private System.ComponentModel.Container components = null;

      public EnumServer()
      {
         //
         // Required for Windows Form Designer support
         //
         // call to initialize diffrent resources in Form.
         InitializeComponent();
      }

      // Distroy Form resource at time of closing the form.
      protected override void Dispose(bool disposing)
      {
         if (disposing)
         {
            if (components != null)
            {
               components.Dispose();
            }
         }
         base.Dispose(disposing);
      }

      #region Windows Form Designer generated code
      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeComponent()
      {
         System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(EnumServer));
         this.CANECLBtn = new System.Windows.Forms.Button();
         this.comboBox1 = new System.Windows.Forms.ComboBox();
         this.C_oServers = new System.Windows.Forms.ComboBox();
         this.E_i_NumServers = new System.Windows.Forms.TextBox();
         this.label3 = new System.Windows.Forms.Label();
         this.label4 = new System.Windows.Forms.Label();
         this.EXECUTEBtn = new System.Windows.Forms.Button();
         this.label1 = new System.Windows.Forms.Label();
         this.label2 = new System.Windows.Forms.Label();
         this.E_iDevID = new System.Windows.Forms.TextBox();
         this.SuspendLayout();
         // 
         // CANECLBtn
         // 
         this.CANECLBtn.Location = new System.Drawing.Point(320, 160);
         this.CANECLBtn.Name = "CANECLBtn";
         this.CANECLBtn.Size = new System.Drawing.Size(75, 25);
         this.CANECLBtn.TabIndex = 5;
         this.CANECLBtn.Text = "Cancel";
         this.CANECLBtn.Click += new System.EventHandler(this.CANECLBtn_Click);
         // 
         // comboBox1
         // 
         this.comboBox1.DropDownWidth = 208;
         this.comboBox1.Items.AddRange(new object[] {
														   "NSPRO_RET_ON_FIRST",
														   "NSPRO_GET_ALL_SERVERS",
														   "NSPRO_RET_ON_FIRST_AVAILABLE"});
         this.comboBox1.Location = new System.Drawing.Point(128, 32);
         this.comboBox1.Name = "comboBox1";
         this.comboBox1.Size = new System.Drawing.Size(272, 21);
         this.comboBox1.TabIndex = 0;
         // 
         // C_oServers
         // 
         this.C_oServers.AllowDrop = true;
         this.C_oServers.DropDownWidth = 208;
         this.C_oServers.Location = new System.Drawing.Point(128, 128);
         this.C_oServers.Name = "C_oServers";
         this.C_oServers.Size = new System.Drawing.Size(271, 21);
         this.C_oServers.TabIndex = 3;
         // 
         // E_i_NumServers
         // 
         this.E_i_NumServers.Location = new System.Drawing.Point(128, 94);
         this.E_i_NumServers.MaxLength = 2;
         this.E_i_NumServers.Name = "E_i_NumServers";
         this.E_i_NumServers.Size = new System.Drawing.Size(269, 20);
         this.E_i_NumServers.TabIndex = 2;
         this.E_i_NumServers.Text = "10";
         // 
         // label3
         // 
         this.label3.Location = new System.Drawing.Point(8, 98);
         this.label3.Name = "label3";
         this.label3.Size = new System.Drawing.Size(90, 15);
         this.label3.TabIndex = 4;
         this.label3.Text = "Num of Servers";
         // 
         // label4
         // 
         this.label4.Location = new System.Drawing.Point(8, 132);
         this.label4.Name = "label4";
         this.label4.Size = new System.Drawing.Size(70, 15);
         this.label4.TabIndex = 6;
         this.label4.Text = "Server Info";
         // 
         // EXECUTEBtn
         // 
         this.EXECUTEBtn.Location = new System.Drawing.Point(224, 160);
         this.EXECUTEBtn.Name = "EXECUTEBtn";
         this.EXECUTEBtn.Size = new System.Drawing.Size(75, 25);
         this.EXECUTEBtn.TabIndex = 4;
         this.EXECUTEBtn.Text = "Execute";
         this.EXECUTEBtn.Click += new System.EventHandler(this.EXECUTEBtn_Click);
         // 
         // label1
         // 
         this.label1.Location = new System.Drawing.Point(8, 30);
         this.label1.Name = "label1";
         this.label1.Size = new System.Drawing.Size(112, 15);
         this.label1.TabIndex = 0;
         this.label1.Text = "Enum Server Flags";
         // 
         // label2
         // 
         this.label2.Location = new System.Drawing.Point(8, 64);
         this.label2.Name = "label2";
         this.label2.Size = new System.Drawing.Size(72, 15);
         this.label2.TabIndex = 2;
         this.label2.Text = "Dev ID(Hex) ";
         // 
         // E_iDevID
         // 
         this.E_iDevID.Location = new System.Drawing.Point(128, 64);
         this.E_iDevID.MaxLength = 4;
         this.E_iDevID.Name = "E_iDevID";
         this.E_iDevID.Size = new System.Drawing.Size(269, 20);
         this.E_iDevID.TabIndex = 1;
         this.E_iDevID.Text = "FFFF";
         // 
         // EnumServer
         // 
         //this.AutoScale = false;
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         this.ClientSize = new System.Drawing.Size(402, 191);
         this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.CANECLBtn,
																		  this.EXECUTEBtn,
																		  this.C_oServers,
																		  this.label4,
																		  this.E_i_NumServers,
																		  this.label3,
																		  this.E_iDevID,
																		  this.label2,
																		  this.comboBox1,
																		  this.label1});
         this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
         this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
         this.MaximizeBox = false;
         this.MinimizeBox = false;
         this.Name = "EnumServer";
         this.ShowInTaskbar = false;
         this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
         this.Text = "EnumServer";
         this.Load += new System.EventHandler(this.EnumServer_Load);
         this.ResumeLayout(false);

      }
      #endregion

      /***************************************************************************************
        * Procedure : CANECLBtn_Click
        *
        * Purpose   : Called on Cancel button click
        *
        * Descrip   : Close form
        ***************************************************************************************/
      private void CANECLBtn_Click(object sender, System.EventArgs e)
      {
         Close(); //Close the form.
      }

      /***************************************************************************************
      * Procedure : EXECUTEBtn_Click
      *
      * Purpose   : Called on Execute button click
      *
      * Descrip   : Validation for diffrent user inputs.RNBOsproEnmuServer API call here.
      ***************************************************************************************/
      private void EXECUTEBtn_Click(object sender, System.EventArgs e)
      {
         ushort status = 0;
         int enumFlag;
         ushort DevID = 0;
         ushort numServers = 0;
         Superpro.NSPRO_SERVER_INFO rec = new Superpro.NSPRO_SERVER_INFO();
         byte[] nsproSrvrBuffer = new byte[NSPRO_MAX__BUFFER_SIZE];
         int i_IsValid = -1;
         string sDevID = "";
         string sNumServers = "";
         System.Text.ASCIIEncoding AE = new System.Text.ASCIIEncoding();
         //Get EnmuFlag value selected by user
         enumFlag = comboBox1.SelectedIndex;
         // If EnumFlag not selected 
         C_oServers.Text = "";
         if (enumFlag == -1)
         {
            MessageBox.Show("Select one of the EnumFlag.", "EnumServer", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
         }
         //Convert EnumFlag value in Standard EnumFlag constant definded in SuperPro.cs
         switch (enumFlag)
         {
            case 0:
               enumFlag = (int)Superpro.ENUM_SERVER_FLAG.NSPRO_RET_ON_FIRST;
               break;
            case 1:
               enumFlag = (int)Superpro.ENUM_SERVER_FLAG.NSPRO_GET_ALL_SERVERS;
               break;
            case 2:
               enumFlag = (int)Superpro.ENUM_SERVER_FLAG.NSPRO_RET_ON_FIRST_AVAILABLE;
               break;
         }

         //Get DevID entered by user as string
         sDevID = E_iDevID.Text;
         //Perform validation for DEVID string value and convert into integer.
         i_IsValid = MainForm.isValidData(sDevID.Trim(), MainForm.VALIDDATA_LENGTH, ref DevID);

         if (i_IsValid == 1) // If DevID is blank
         {
            MessageBox.Show("Specify DeveloperID in input box to proceed.", "EnumServer", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
         }
         if (i_IsValid == 2) // If DevID string is of invalid length
         {
            MessageBox.Show("DeveloperID must be in hex with length not exceeding 4 digits.", "EnumServer", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
         }
         if (i_IsValid == 3)// If DevID string is not valid HexString 
         {
            MessageBox.Show("DeveloperID must be in hex with length not exceeding 4 digits.", "EnumServer", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
         }
         // Get numServers value entered by user as string
         sNumServers = E_i_NumServers.Text;
         if (sNumServers.Length == 0) // If value is blank
         {
            MessageBox.Show("Specify number of server to proceed.", "EnumServer", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
         }
         // Exeception handling during convertion of string to integer
         try
         {
            numServers = Convert.ToUInt16(sNumServers, 10);
         }
         catch (OverflowException)
         {
            MessageBox.Show("Number of server must be between 1 to 10.", "EnumServer", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
         }
         catch (FormatException)
         {
            MessageBox.Show("Number of server must be between 1 to 10.", "EnumServer", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
         }
         // check for valid integer value for numServers
         if ((numServers < 1 || numServers > 10))
         {
            MessageBox.Show("Number of server must be between 1 to 10.", "EnumServer", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
         }

         //**************RNBOsproEnumServer API **********************/  
         status = MainForm.oSuperPro.RNBOsproEnumServer(enumFlag, DevID, nsproSrvrBuffer, ref numServers);
         //If API get success then 
         if (status == 0)
         {
            MessageBox.Show("EnumServer successful! Click Server Info box to see details.", "EnumServer", MessageBoxButtons.OK, MessageBoxIcon.Information);
            E_i_NumServers.Text = numServers.ToString();
            // structure for server Info value

            // Clear Server Info List box elements
            C_oServers.Items.Clear();
            // Loop for displaying Server information in ListBox    
            for (int i = 0; i < numServers; i++)
            {
               //Incrementing offset Value to get server info from ServerInfo struct   
               int offset = i * NSPRO_STRUCT_SIZE;
               // Get Server info  
               rec.GetServerInfo(nsproSrvrBuffer, offset);
               //Display in List box for output    
               C_oServers.Items.Add("License: " + rec.numLicAvail.ToString() + " , Server : " + AE.GetString(rec.serverAddress));
            }
         }
         // IF API get fail
         else
         {
            MessageBox.Show("EnumServer failed!\n Return code is " + status + ".", "EnumServer", MessageBoxButtons.OK, MessageBoxIcon.Error);
         }
      }

      /***************************************************************************************
      * Procedure : EnumServer_Load
      *
      * Purpose   : Called AT TIME OF Form Load
      *
      * Descrip   : Assign some default value in EnumServer form textBox
      ***************************************************************************************/
      private void EnumServer_Load(object sender, System.EventArgs e)
      {
         E_iDevID.Text = "FFFF"; // DevID value
         E_i_NumServers.Text = "10";
         comboBox1.SelectedIndex = 1;
         // NumServers value
      }
   }
}
// End Of File