using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EMDServer.Common;
using System.Diagnostics;

namespace EMDServer.Connection.Visualization
{
    public class RemotingServer : MarshalByRefObject, IRemotingIPC
    {
        #region Class variables
        // ----------------------------------------
        private IRemotingClient iClient;
        private iEMDConnection con;
        // ----------------------------------------
        #endregion

        #region IRemotingIPC Members
        // ----------------------------------------
        public RemotingServer(iEMDConnection emdConnection)
        {
            this.con = emdConnection;
        }

        public void Send(Command cmd)
        {
            //Debug.WriteLine("REMOTINGSERVER - Daten empfangen: ID=" + cmd.Id + ", CMD=" +(Constants.EMD_COMMAND)cmd.Cmd + ", VALUE=" + cmd.Val);

            this.con.onCommandReceived(cmd);
        }

        public void SendDisplay(Command cmd)
        {
            try
            {
                this.iClient.Send(cmd);
            }
            catch (Exception e)
            {
                Debug.WriteLine("REMOTINGSERVER - SendDisplay:\t" + e.Message);
            }
        }

        public void Register(IRemotingClient client)
        {
            this.iClient = client;
        }
        // ----------------------------------------
        #endregion

        /// <summary>
        /// Prevents that the remoting connection will be closed
        /// caused by a timeout.
        /// </summary>
        /// <returns></returns>
        public override object InitializeLifetimeService()
        {
            return null;
        }
    }
}
