using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NiApiLib;

namespace ConsoleApplication1
{
	class Program
	{
		static void Main(string[] args)
		{
			Connection c = new Connection();
			c.Connect();
			Console.ReadLine();
			c.Disconnect();
		}
	}
	class Connection
	{
		NiApiLib.SrvrSession Session = new SrvrSessionClass();
		NiApiLib.Table Tbl = new NiApiLib.TableClass();
		NiApiLib.Filter Flt = new NiApiLib.FilterClass();
		NiApiLib.Structure Stru = new NiApiLib.StructureClass();
		NiApiLib.DataRowSet DrowSet = new NiApiLib.DataRowSetClass();
		NiApiLib.DataRow Drow = new NiApiLib.DataRowClass();
		NiApiLib.StatusMessage SMessage = new NiApiLib.StatusMessageClass();
		NiApiLib.Message mes = new NiApiLib.MessageClass();

		public void Connect()
		{
			Session.HostAddr = "";
			Session.HostPort = 2900;
			Session.Name = "";
			Session.Password = "";
			Session.Journal = "Journal.txt";
			Session.CatalogName = "catalog.xml";
			Session.OnLogin += new _ISrvrSessionEvents_OnLoginEventHandler(Session_OnLogin);
			Session.OnStatusMessage += new _ISrvrSessionEvents_OnStatusMessageEventHandler(Session_OnStatusMessage);
			Session.Connect();
		}
		public void Disconnect()
		{
			Console.WriteLine("[System] Соединение разорвано");
			Session.Disconnect();
		}

		void Session_OnStatusMessage(object pStatusMessage)
		{
			SMessage = (NiApiLib.StatusMessage)pStatusMessage;
			mes = (NiApiLib.Message)SMessage.Message;
			Console.Write(SMessage.ExtendedInfo + " | " + SMessage.Class + " : ");
			for (int i = 0; i < mes.Count()-1; i++)
			{
				Console.Write(" " + mes.Value(i));
			}
			Console.WriteLine("");
		}

		public void Session_OnLogin(int LoginResult)
		{
			if (LoginResult == 0)
			{
				Session.Ping();
			}
			else
			{
				Console.WriteLine("[System] ошибка подключения");
			}
		}

	}
}