using System;
using System.ServiceProcess;
using HaaSMiddleware.BackgroundThread;
using HaaSMiddleware.ServiceTier.EtchProxy;
using log4net.Config;
using log4net;
using Org.Apache.Etch.Bindings.Csharp.Support;
using Org.Apache.Etch.Bindings.Csharp.Util;
using org.apache.etch.EtchServiceTier;

namespace HaaSMiddleware.MiddlewareService
{
	public partial class MiddlewareService : ServiceBase
	{
		private readonly MiddlewareBackgroundTaskRunner timer;
		private readonly ServerFactory listener;
		private ILog log;

        private const string URI = "tls://dhirelease.it4i.cz:4001?TlsConnection.certName=dhirelease.it4i.cz&Packetizer.maxPktSize=0";
		//private const string URI = "tls://haasdhi.it4i.cz:4001?TlsConnection.certName=haasdhi.it4i.cz&Packetizer.maxPktSize=0";
		//private const string URI = "tcp://localhost:4001";
		
		public MiddlewareService()
		{
			// Logging setup
			XmlConfigurator.Configure();
			this.log = LogManager.GetLogger("MiddlewareService Main");
			InitializeComponent();
			this.timer = new MiddlewareBackgroundTaskRunner();
			this.listener = EtchServiceTierHelper.NewListener(URI, null, new ServiceTierServerFactory());
		}

		protected override void OnStart(string[] args)
		{
			log.Info("Service starting...");
			// Timer start
			timer.Start();
			// Etch setup
			// Start the Listener
			listener.TransportControl(TransportConsts.START_AND_WAIT_UP, 4000);
			log.Info("Service started and listening.");
		}

		protected override void OnStop()
		{
			// Stop timer
			timer.Stop();
			log.Info("Service stopped.");
		}
	}
}