Base class for the gateway. This is the class to instantiate to create a gateway.
Inheritance: IDisposable
 public GwTcpListener(Gateway gateway, ChainSide side, IPEndPoint ipSource)
 {
     this.gateway = gateway;
     this.ipSource = ipSource;
     this.side = side;
     tcpListener = new TcpListener(ipSource);
     tcpListener.Start(10);
     tcpListener.BeginAcceptSocket(ReceiveConn, tcpListener);
     if (Log.WillDisplay(System.Diagnostics.TraceEventType.Start))
         Log.TraceEvent(System.Diagnostics.TraceEventType.Start, -1, "TCP Listener " + side.ToString() + " on " + ipSource);
 }
        public void BeaconDoRequestLevel1()
        {
            Beacon target = new Beacon();

            Gateway gateway = new Gateway();
            gateway.Configuration.LocalAddressSideA = "127.0.0.1:1234";
            gateway.Configuration.LocalAddressSideB = "127.0.0.1:4321";

            var beaconB = new Mock<IBeaconResetter>();
            gateway.beaconB = beaconB.Object;

            WorkerChain chain = WorkerChain.UdpBeaconReceiver(gateway, ChainSide.SIDE_A, gateway.Configuration.LocalSideA, new List<IPEndPoint>(new IPEndPoint[] { gateway.Configuration.LocalSideA }));

            DataPacket packet = DataPacket.Create(16, chain);
            packet.Parameter1 = 1;
            packet.Sender = PBCaGw.Configurations.Configuration.ParseAddress("168.1.1.2:1234");

            target.DoRequest(packet, chain, pk => { throw new Exception("Sent something!"); });
            beaconB.Verify(fw => fw.ResetBeacon(), Times.Never());
        }
        private static void SimpleServer()
        {
            Console.WindowWidth = 120;
            Console.BufferWidth = 120;
            Console.WindowHeight = 60;
            Console.BufferHeight = 3000;

            server = new CAServer(IPAddress.Parse(ip), 5777, 5777);
            CAIntRecord record = server.CreateRecord<CAIntRecord>("MXI1:ILOG:2");
            record.Value = 5;

            gateway = new Gateway();
            gateway.Configuration.GatewayName = "TESTGW";
            gateway.Configuration.LocalAddressSideA = ip + ":5432";
            //gateway.Configuration.LocalAddressSideA = "129.129.130.87:5555";
            gateway.Configuration.RemoteAddressSideA = ip + ":5552";
            gateway.Configuration.LocalAddressSideB = ip + ":5888";
            //gateway.Configuration.RemoteAddressSideB = remoteIp + ":5777";
            gateway.Configuration.RemoteAddressSideB = ip + ":5777";
            gateway.Configuration.ConfigurationType = PBCaGw.Configurations.ConfigurationType.UNIDIRECTIONAL;
            gateway.SaveConfig();

            Gateway.AutoCreateChannel = false;
            Gateway.RestoreCache = false;
            gateway.Start();
            Console.ReadKey();
            gateway.Dispose();
        }
        static void ReconnectProscan()
        {
            gateway = new Gateway();
            gateway.Configuration.GatewayName = "TESTGW";
            gateway.Configuration.LocalAddressSideA = ip + ":5555";
            //gateway.Configuration.LocalAddressSideA = "129.129.130.87:5555";
            gateway.Configuration.RemoteAddressSideA = remoteIp + ":5552";
            gateway.Configuration.LocalAddressSideB = ip + ":5888";
            //gateway.Configuration.RemoteAddressSideB = remoteIp + ":5777";
            gateway.Configuration.RemoteAddressSideB = "172.25.60.67:5062";
            gateway.Configuration.ConfigurationType = PBCaGw.Configurations.ConfigurationType.UNIDIRECTIONAL;
            gateway.SaveConfig();
            gateway.Start();

            client = new EpicsClient();
            client.Configuration.WaitTimeout = 2000;
            client.Configuration.SearchAddress = ip + ":5555";

            EpicsChannel<ExtGraphic<string>> ch = client.CreateChannel<ExtGraphic<string>>("MMAP10Y:CMODE:1");
            ch.StatusChanged += new EpicsStatusDelegate(ch_StatusChanged);
            ch.MonitorChanged += new EpicsDelegate<ExtGraphic<string>>(ch_MonitorChangedView);

            EpicsChannel<ExtGraphic<string>> ch2 = client.CreateChannel<ExtGraphic<string>>("MMAP12Y:CNSAMPLES:1");
            ch2.StatusChanged += new EpicsStatusDelegate(ch_StatusChanged);
            ch2.MonitorChanged += new EpicsDelegate<ExtGraphic<string>>(ch_MonitorChangedView);

            Thread.Sleep(1000);

            Console.WriteLine("------------------------------------------------------------------------");
            gateway.Dispose();
            //Thread.Sleep(5000);

            gateway = new Gateway();
            gateway.Configuration.LocalAddressSideA = ip + ":5555";
            //gateway.Configuration.LocalAddressSideA = "129.129.130.87:5555";
            gateway.Configuration.RemoteAddressSideA = remoteIp + ":5552";
            gateway.Configuration.LocalAddressSideB = ip + ":5888";
            //gateway.Configuration.RemoteAddressSideB = remoteIp + ":5777";
            gateway.Configuration.RemoteAddressSideB = "172.25.60.67:5062";
            gateway.Configuration.ConfigurationType = PBCaGw.Configurations.ConfigurationType.UNIDIRECTIONAL;
            gateway.Start();

            /*EpicsChannel restart = client.CreateChannel("TESTGW:RESTART");
            try
            {
                restart.Put<int>(2);
            }
            catch
            {
            }*/

            Thread.Sleep(5000);
            Console.ReadKey();
        }
        static void ReconnectIoc()
        {
            InitServer();

            gateway = new Gateway();
            gateway.Configuration.GatewayName = "TESTGW";
            gateway.Configuration.LocalAddressSideA = ip + ":5555";
            //gateway.Configuration.LocalAddressSideA = "129.129.130.87:5555";
            gateway.Configuration.RemoteAddressSideA = ip + ":5552";
            gateway.Configuration.LocalAddressSideB = ip + ":5888";
            //gateway.Configuration.RemoteAddressSideB = remoteIp + ":5777";
            gateway.Configuration.RemoteAddressSideB = ip + ":5777";
            gateway.Configuration.ConfigurationType = PBCaGw.Configurations.ConfigurationType.UNIDIRECTIONAL;
            gateway.SaveConfig();

            Gateway.AutoCreateChannel = false;
            Gateway.RestoreCache = false;
            gateway.Start();

            client = new EpicsClient();
            client.Configuration.SearchAddress = ip + ":5555";
            client.Configuration.WaitTimeout = 200;
            chans = new List<EpicsChannel>();

            for (int i = 0; i < 1; i++)
            {
                EpicsChannel<int> intChan = client.CreateChannel<int>("PCT:INT-" + i);
                chans.Add(intChan);
                intChan.MonitorChanged += new EpicsDelegate<int>(intChan_MonitorChanged);
            }

            Thread t = new Thread(ChanToGet);
            t.IsBackground = true;
            t.Start();

            //Thread.Sleep(2000);
            //chans[0].Get<int>();

            //for (int i = 0; i < 100; i++)
            while(true)
            {
                Thread.Sleep(5000);
                server.Dispose();
                Thread.Sleep(2000);
                InitServer();
            }

            Console.ReadKey();
        }
        private static void QuickMonitor()
        {
            gateway = new Gateway();
            gateway.Configuration.GatewayName = "TESTGW";
            gateway.Configuration.LocalAddressSideA = "127.0.0.1:5432";
            //gateway.Configuration.LocalAddressSideA = "129.129.130.87:5555";
            gateway.Configuration.RemoteAddressSideA = "127.0.0.1:5552";
            gateway.Configuration.LocalAddressSideB = "129.129.130.87:5064";
            //gateway.Configuration.LocalAddressSideB = "172.22.200.116:5432";
            //gateway.Configuration.RemoteAddressSideB = remoteIp + ":5777";
            //gateway.Configuration.RemoteAddressSideB = "172.22.255.255:5064";
            gateway.Configuration.RemoteAddressSideB = "172.22.200.117:5062";
            gateway.Configuration.ConfigurationType = PBCaGw.Configurations.ConfigurationType.UNIDIRECTIONAL;
            gateway.SaveConfig();
            gateway.Start();

            Thread.Sleep(1000);

            EpicsClient client = new EpicsClient();
            string clientConfig = "127.0.0.1:5432";
            //string clientConfig="172.22.100.101:5064";
            client.Configuration.SearchAddress = clientConfig;

            //string[] channelNames = { "ZPSAF101-VME:CAL.EGU", "ZPSAF101-VME:CAL", "ZPSAF101-VME:CALNCONN.EGU", "ZPSAF101-VME:CALDCONN.EGU", "ZPSAF101-VME:LOAD", "ZPSAF101-VME:CAL.EGU", "ZPSAF101-VME:CAL.EGU", "ZPSAF101-VME:CAL.EGU", "ZPSAF101-VME:CAL.EGU", "ZPSAF101-VME:CAL.EGU", "ZPSAF101-VME:CAL.EGU", "ZPSAF101-VME:HBT" };
            //string[] channelNames = { "ZPSAF101-VME:CAL.EGU", "ZPSAF101-VME:CALNCONN.EGU", "ZPSAF101-VME:CALDCONN.EGU", "ZPSAF101-VME:CAL.EGU", "ZPSAF101-VME:CAL.EGU", "ZPSAF101-VME:CAL.EGU", "ZPSAF101-VME:CAL.EGU", "ZPSAF101-VME:CAL.EGU", "ZPSAF101-VME:CAL.EGU" };
            string[] channelNames = { "ZPSAF101-VME:CAL.EGU", "ZPSAF101-VME:CALNCONN.EGU", "ZPSAF101-VME:CALDCONN.EGU", "ZPSAF101-VME:CAL.EGU", "ZPSAF101-VME:CAL.EGU", "ZPSAF101-VME:CAL.EGU", "ZPSAF101-VME:CAL.EGU", "ZPSAF101-VME:CAL.EGU", "ZPSAF101-VME:CAL.EGU", "ZPSAF101-VME:HBT.EGU", "ZPSAF101-VME:HBT.EGU", "ZPSAF101-VME:LOAD.EGU" };
            //string[] channelNames = { "ZPSAF101-VME:CAL.EGU" };
            while (true)
            {
                for (int l = 0; l < 10; l++)
                {
                    List<EpicsChannel<ExtGraphic<string>>> channels = new List<EpicsChannel<ExtGraphic<string>>>();
                    foreach (var i in channelNames)
                    {
                        EpicsChannel<ExtGraphic<string>> c = client.CreateChannel<ExtGraphic<string>>(i);
                        c.MonitorChanged += new EpicsDelegate<ExtGraphic<string>>(QuickMonitor);
                        channels.Add(c);
                    }
                    foreach (var i in channels)
                        i.Dispose();
                    client.Dispose();
                    client = new EpicsClient();
                    client.Configuration.SearchAddress = clientConfig;
                }

                if (true)
                {
                    bool gotError = false;
                    List<EpicsChannel<ExtGraphic<string>>> channels = new List<EpicsChannel<ExtGraphic<string>>>();
                    foreach (var i in channelNames)
                    {
                        EpicsChannel<ExtGraphic<string>> c = client.CreateChannel<ExtGraphic<string>>(i);
                        channels.Add(c);
                    }

                    CountdownEvent multiActionCountDown = new CountdownEvent(channelNames.Count());
                    Dictionary<uint, bool> gotMonitor = new Dictionary<uint, bool>();

                    foreach (EpicsChannel<ExtGraphic<string>> c in channels)
                    {
                        c.MonitorChanged += delegate(EpicsChannel<ExtGraphic<string>> sender, ExtGraphic<string> newValue)
                        {
                            if ((newValue.Value != "#" && sender.ChannelName.Contains(":CAL")) ||
                                (newValue.Value != "%" && sender.ChannelName.Contains(":LOAD")) ||
                                (newValue.Value != "ticks" && sender.ChannelName.Contains(":HBT")))
                            {
                                Console.WriteLine("Wrong data on CID (" + sender.ChannelName + "): " + sender.CID + ", " + newValue.Value);
                                gotError = true;
                            }
                            lock (gotMonitor)
                            {
                                if (!gotMonitor.ContainsKey(sender.CID))
                                {
                                    gotMonitor.Add(sender.CID, true);
                                    multiActionCountDown.Signal();
                                }
                            }
                        };

                        c.MonitorChanged += new EpicsDelegate<ExtGraphic<string>>(QuickMonitor);
                    }

                    multiActionCountDown.Wait(2500);

                    bool allConnected = true;
                    foreach (var i in channels)
                    {
                        if (i.Status != ChannelStatus.CONNECTED)
                        {
                            allConnected = false;
                            Console.WriteLine(i.ChannelName + " not connected.");
                            //gotError = true;
                        }
                    }

                    if (!allConnected)
                    {
                        needToShow = false;
                        //gotError = true;
                        Console.WriteLine("Not all connected!!!!!");
                    }

                    foreach (var i in channels)
                        i.Dispose();
                    client.Dispose();
                    client = new EpicsClient();
                    client.Configuration.SearchAddress = clientConfig;
                    multiActionCountDown.Dispose();

                    if (gotError)
                    {
                        Console.Beep();
                        Console.WriteLine("Press any key to continue...");
                        Console.ReadKey();
                        needToShow = true;
                    }
                }
            }
        }
        static void ProscanWave()
        {
            PBCaGw.Services.Log.Enabled = false;
            gateway = new Gateway();
            gateway.Configuration.GatewayName = "TESTGW";
            gateway.Configuration.LocalAddressSideA = ip + ":5555";
            //gateway.Configuration.LocalAddressSideA = "129.129.130.87:5555";
            gateway.Configuration.RemoteAddressSideA = remoteIp + ":5552";
            gateway.Configuration.LocalAddressSideB = ip + ":5888";
            //gateway.Configuration.RemoteAddressSideB = remoteIp + ":5777";
            gateway.Configuration.RemoteAddressSideB = "172.25.60.67:5062";
            gateway.Configuration.ConfigurationType = PBCaGw.Configurations.ConfigurationType.UNIDIRECTIONAL;
            gateway.SaveConfig();
            gateway.Start();

            Console.WriteLine("Gateway up");
            //gateway.Configuration.ConfigurationType = PBCaGw.Configurations.ConfigurationType.BIDIRECTIONAL;

            client = new EpicsClient();
            client.Configuration.WaitTimeout = 2000;
            client.Configuration.SearchAddress = ip + ":5555";
            List<EpicsChannel<ExtGraphic<string>>> channels = new List<EpicsChannel<ExtGraphic<string>>>();
            foreach (string i in channelsToConnect)
            {
                EpicsChannel<ExtGraphic<string>> ch = client.CreateChannel<ExtGraphic<string>>(i);
                ch.MonitorChanged += new EpicsDelegate<ExtGraphic<string>>(ch_MonitorChanged);
                channels.Add(ch);
            }
            Thread.Sleep(2000);

            gateway.Dispose();
            PBCaGw.Services.Log.Enabled = true;
            //Thread.Sleep(5000);

            gateway = new Gateway();
            gateway.Configuration.LocalAddressSideA = ip + ":5555";
            //gateway.Configuration.LocalAddressSideA = "129.129.130.87:5555";
            gateway.Configuration.RemoteAddressSideA = remoteIp + ":5552";
            gateway.Configuration.LocalAddressSideB = ip + ":5888";
            //gateway.Configuration.RemoteAddressSideB = remoteIp + ":5777";
            gateway.Configuration.RemoteAddressSideB = "172.25.60.67:5062";
            gateway.Configuration.ConfigurationType = PBCaGw.Configurations.ConfigurationType.UNIDIRECTIONAL;
            gateway.Start();

            Thread.Sleep(5000);

            foreach (var i in channels)
            {
                if (i.Status != ChannelStatus.CONNECTED)
                {
                    Console.WriteLine(i.ChannelName + " not connected.");
                }
            }
            //client.Dispose();

            Console.ReadKey();
            //Console.WriteLine(ch.Get<string>());
        }
        private static void MultipleCreate()
        {
            Console.WindowWidth = 120;
            Console.BufferWidth = 120;
            Console.WindowHeight = 60;
            Console.BufferHeight = 3000;

            server = new CAServer(IPAddress.Parse(ip), 5777, 5777);
            CAStringRecord record = server.CreateRecord<CAStringRecord>("PCTEST:STR");
            record.Value = "Hello there!";

            gateway = new Gateway();
            gateway.Configuration.GatewayName = "TESTGW";
            gateway.Configuration.LocalAddressSideA = ip + ":5555";
            //gateway.Configuration.LocalAddressSideA = "129.129.130.87:5555";
            gateway.Configuration.RemoteAddressSideA = ip + ":5552";
            gateway.Configuration.LocalAddressSideB = ip + ":5888";
            //gateway.Configuration.RemoteAddressSideB = remoteIp + ":5777";
            gateway.Configuration.RemoteAddressSideB = ip + ":5777";
            gateway.Configuration.ConfigurationType = PBCaGw.Configurations.ConfigurationType.UNIDIRECTIONAL;
            gateway.SaveConfig();

            Gateway.AutoCreateChannel = false;
            Gateway.RestoreCache = false;
            gateway.Start();

            Thread.Sleep(2000);

            EpicsClient c1 = new EpicsClient();
            c1.Configuration.SearchAddress = ip + ":5555";
            EpicsClient c2 = new EpicsClient();
            c2.Configuration.SearchAddress = ip + ":5555";

            EpicsChannel ca1 = c1.CreateChannel("PCTEST:STR");
            EpicsChannel ca2 = c2.CreateChannel("PCTEST:STR");

            Console.WriteLine("Reading...");

            ca1.MonitorChanged += new EpicsDelegate(ca1_MonitorChanged);
            ca2.MonitorChanged += new EpicsDelegate(ca2_MonitorChanged);

            Console.ReadKey();
        }
        static void MonAll()
        {
            //PBCaGw.Services.Log.Enabled = false;
            Stopwatch sw = new Stopwatch();
            for (int loop = 0; loop < 10; loop++)
            {
                gateway = new Gateway();
                gateway.Configuration.GatewayName = "TESTGW";
                gateway.Configuration.LocalAddressSideA = ip + ":5555";
                //gateway.Configuration.LocalAddressSideA = "129.129.130.87:5555";
                gateway.Configuration.RemoteAddressSideA = remoteIp + ":5552";
                gateway.Configuration.LocalAddressSideB = ip + ":5888";
                //gateway.Configuration.RemoteAddressSideB = remoteIp + ":5777";
                gateway.Configuration.RemoteAddressSideB = "172.25.60.67:5062";
                gateway.Configuration.ConfigurationType = PBCaGw.Configurations.ConfigurationType.UNIDIRECTIONAL;
                gateway.SaveConfig();
                gateway.Start();

                Console.WriteLine("Gateway up");
                //gateway.Configuration.ConfigurationType = PBCaGw.Configurations.ConfigurationType.BIDIRECTIONAL;

                Gateway.BufferedSockets = false;

                Thread.Sleep(1000);
                multiActionCountDown = new CountdownEvent(channelsToConnect.Count(row => !row.Contains("TRACE")));
                hadValue = new Dictionary<string, bool>();
                sw.Start();
                client = new EpicsClient();
                client.Configuration.WaitTimeout = 2000;
                client.Configuration.SearchAddress = ip + ":5555";
                List<EpicsChannel<ExtGraphic<string>>> channels = new List<EpicsChannel<ExtGraphic<string>>>();
                foreach (string i in channelsToConnect.Where(row => !row.Contains("TRACE")))
                {
                    EpicsChannel<ExtGraphic<string>> ch = client.CreateChannel<ExtGraphic<string>>(i);
                    ch.MonitorChanged += new EpicsDelegate<ExtGraphic<string>>(ch_MonitorChangedGotValue);
                    channels.Add(ch);
                }
                if (multiActionCountDown.Wait(5000) == false)
                    Console.WriteLine("Didn't got it...");
                client.Dispose();
                sw.Stop();
                gateway.Dispose();
            }

            Console.WriteLine("Time: " + sw.Elapsed.ToString());
            Console.ReadKey();
        }
        static void InternTest()
        {
            InitServer();

            gateway = new Gateway();
            gateway.Configuration.LocalAddressSideA = ip + ":5555";
            //gateway.Configuration.LocalAddressSideA = "129.129.130.87:5555";
            gateway.Configuration.RemoteAddressSideA = remoteIp + ":5552";
            gateway.Configuration.LocalAddressSideB = ip + ":5888";
            gateway.Configuration.RemoteAddressSideB = remoteIp + ":5777";
            gateway.Configuration.ConfigurationType = PBCaGw.Configurations.ConfigurationType.UNIDIRECTIONAL;
            //gateway.Configuration.ConfigurationType = PBCaGw.Configurations.ConfigurationType.BIDIRECTIONAL;
            gateway.Start();

            client = new EpicsClient();
            client.Configuration.SearchAddress = ip + ":5555";
            //client.Configuration.SearchAddress = "129.129.130.255:5555";
            //client.Configuration.SearchAddress = "127.0.0.1:5777";

            //MultiConnect();
            //Checks2();

            EpicsChannel ca = client.CreateChannel("PCT:INT-1");
            Console.WriteLine(ca.Get<string>());

            //GetDisconnect();

            Console.WriteLine("Press a key to continue...");
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            Console.WriteLine("PBCaGW " + Gateway.Version);
            Console.WriteLine("Build on " + DiagnosticServer.BuildTime.ToString(CultureInfo.InvariantCulture));
            Console.WriteLine("(c) Paul Scherrer Institute - GFA IT - 2012");

            // Setup the console look
            try
            {
                Console.Title = "PBCaGW - " + System.Configuration.ConfigurationManager.AppSettings["gatewayName"];
                Console.WindowWidth = 120;
                Console.BufferWidth = 120;
                Console.WindowHeight = 60;
                Console.BufferHeight = 3000;
            }
            catch
            {
            }

            if (System.Diagnostics.Debugger.IsAttached)
            {
                Console.WriteLine("Press return to stop the gateway...");
            }
            else
            {
                Console.WriteLine("Press Ctrl+C to stop the gateway...");
                Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
            }
            Console.WriteLine("");

            /*Gateway.AutoCreateChannel = false;
            Gateway.RestoreCache = false;*/

            /*Gateway.AutoCreateChannel = false;
            Gateway.RestoreCache = false;*/
            //Gateway.AutoCreateChannel = false;
            //Gateway.BufferedSockets = false;

            gateway = new Gateway();
            gateway.LoadConfig();
            gateway.Start();

            while (true)
            {
                ConsoleKeyInfo k = Console.ReadKey();
                switch (k.Key)
                {
                    case ConsoleKey.Spacebar:
                        DataPacket.ShowPools();
                        break;
                    case ConsoleKey.L:
                        Log.ShowAll = !Log.ShowAll;
                        break;
                    default:
                        if (System.Diagnostics.Debugger.IsAttached)
                        {
                            gateway.Dispose();
                            return;
                        }
                        break;
                }
            }

            /*if (System.Diagnostics.Debugger.IsAttached)
            {
                while (true)
                {
                    ConsoleKeyInfo k = Console.ReadKey();
                    switch (k.Key)
                    {
                        case ConsoleKey.Spacebar:
                            DataPacket.ShowPools();
                            break;
                        default:
                            gateway.Dispose();
                            return;
                    }
                }
            }
            else
            {
                while (true)
                    Console.ReadKey();
            }*/
        }
 protected override void OnStop()
 {
     if(gateway != null)
         gateway.Dispose();
     gateway = null;
 }
 protected override void OnStart(string[] args)
 {
     gateway = new Gateway();
     gateway.LoadConfig();
     gateway.Start();
 }
        public DiagnosticServer(Gateway gateway, IPAddress address)
        {
            this.gateway = gateway;
            // Starts the diagnostic server
            // using the CAServer library
            diagServer = new CAServer(address, 7890, 7890);
            if (Log.WillDisplay(TraceEventType.Start))
                Log.TraceEvent(TraceEventType.Start, 0, "Starting debug server on " + 7890);
            // CPU usage
            channelCpu = diagServer.CreateRecord<CADoubleRecord>(gateway.Configuration.GatewayName + ":CPU");
            channelCpu.EngineeringUnits = "%";
            channelCpu.CanBeRemotlySet = false;
            channelCpu.Scan = CaSharpServer.Constants.ScanAlgorithm.SEC5;
            channelCpu.PrepareRecord += new EventHandler(channelCPU_PrepareRecord);
            cpuCounter = new PerformanceCounter();
            cpuCounter.CategoryName = "Processor";
            cpuCounter.CounterName = "% Processor Time";
            cpuCounter.InstanceName = "_Total";
            // Mem free
            channelMem = diagServer.CreateRecord<CADoubleRecord>(gateway.Configuration.GatewayName + ":MEM-FREE");
            channelMem.CanBeRemotlySet = false;
            channelMem.Scan = CaSharpServer.Constants.ScanAlgorithm.SEC5;
            channelMem.EngineeringUnits = "Mb";
            channelMem.PrepareRecord += new EventHandler(channelMEM_PrepareRecord);
            ramCounter = new PerformanceCounter("Memory", "Available MBytes");
            // NB Client connections
            channelNbClientConn = diagServer.CreateRecord<CAIntRecord>(gateway.Configuration.GatewayName + ":NBCLIENTS");
            channelNbClientConn.CanBeRemotlySet = false;
            channelNbClientConn.Scan = CaSharpServer.Constants.ScanAlgorithm.SEC5;
            channelNbClientConn.PrepareRecord += new EventHandler(channelNbClientConn_PrepareRecord);
            // NB Server connections
            channelNbServerConn = diagServer.CreateRecord<CAIntRecord>(gateway.Configuration.GatewayName + ":NBSERVERS");
            channelNbServerConn.CanBeRemotlySet = false;
            channelNbServerConn.Scan = CaSharpServer.Constants.ScanAlgorithm.SEC5;
            channelNbServerConn.PrepareRecord += new EventHandler(channelNbServerConn_PrepareRecord);
            // Known channels (PV keept)
            channelKnownChannels = diagServer.CreateRecord<CAIntRecord>(gateway.Configuration.GatewayName + ":PVTOTAL");
            channelKnownChannels.CanBeRemotlySet = false;
            channelKnownChannels.Scan = CaSharpServer.Constants.ScanAlgorithm.SEC5;
            channelKnownChannels.PrepareRecord += new EventHandler(channelKnownChannels_PrepareRecord);
            // Open monitors
            channelOpenMonitor = diagServer.CreateRecord<CAIntRecord>(gateway.Configuration.GatewayName + ":MONITORS");
            channelOpenMonitor.CanBeRemotlySet = false;
            channelOpenMonitor.Scan = CaSharpServer.Constants.ScanAlgorithm.SEC5;
            channelOpenMonitor.PrepareRecord += new EventHandler(channelOpenMonitor_PrepareRecord);
            // Searches per sec
            channelNbSearchPerSec = diagServer.CreateRecord<CAIntRecord>(gateway.Configuration.GatewayName + ":SEARCH-SEC");
            channelNbSearchPerSec.CanBeRemotlySet = false;
            channelNbSearchPerSec.Scan = CaSharpServer.Constants.ScanAlgorithm.SEC5;
            channelNbSearchPerSec.PrepareRecord += new EventHandler(channelNbSearchPerSec_PrepareRecord);
            // Messages per sec
            channelNbMessagesPerSec = diagServer.CreateRecord<CAIntRecord>(gateway.Configuration.GatewayName + ":MESSAGES-SEC");
            channelNbMessagesPerSec.CanBeRemotlySet = false;
            channelNbMessagesPerSec.Scan = CaSharpServer.Constants.ScanAlgorithm.SEC5;
            channelNbMessagesPerSec.PrepareRecord += new EventHandler(channelNbMessagesPerSec_PrepareRecord);
            // DataPacket created per sec
            channelNbCreatedPacketPerSec = diagServer.CreateRecord<CAIntRecord>(gateway.Configuration.GatewayName + ":NEWDATA-SEC");
            channelNbCreatedPacketPerSec.CanBeRemotlySet = false;
            channelNbCreatedPacketPerSec.Scan = CaSharpServer.Constants.ScanAlgorithm.SEC5;
            channelNbCreatedPacketPerSec.PrepareRecord += new EventHandler(channelNbCreatedPacketPerSec_PrepareRecord);
            // DataPacket created per sec
            channelNbPooledPacket = diagServer.CreateRecord<CAIntRecord>(gateway.Configuration.GatewayName + ":POOLED-DATA");
            channelNbPooledPacket.CanBeRemotlySet = false;
            channelNbPooledPacket.Scan = CaSharpServer.Constants.ScanAlgorithm.SEC5;
            channelNbPooledPacket.PrepareRecord += new EventHandler(channelNbPooledPacket_PrepareRecord);
            // TCP Created from startup
            channelNbTcpCreated = diagServer.CreateRecord<CAIntRecord>(gateway.Configuration.GatewayName + ":COUNT-TCP");
            channelNbTcpCreated.CanBeRemotlySet = false;
            channelNbTcpCreated.Scan = CaSharpServer.Constants.ScanAlgorithm.SEC5;
            channelNbTcpCreated.PrepareRecord += new EventHandler(channelNbTcpCreated_PrepareRecord);
            // MAX CID (not reused)
            channelMaxCid = diagServer.CreateRecord<CAIntRecord>(gateway.Configuration.GatewayName + ":MAX-CID");
            channelMaxCid.CanBeRemotlySet = false;
            channelMaxCid.Scan = CaSharpServer.Constants.ScanAlgorithm.SEC5;
            channelMaxCid.PrepareRecord += new EventHandler(channelMaxCID_PrepareRecord);
            // Average CPU usage
            channelAverageCpu = diagServer.CreateRecord<CADoubleRecord>(gateway.Configuration.GatewayName + ":AVG-CPU");
            channelAverageCpu.CanBeRemotlySet = false;
            channelAverageCpu.EngineeringUnits = "%";
            channelAverageCpu.Scan = CaSharpServer.Constants.ScanAlgorithm.SEC5;
            channelAverageCpu.PrepareRecord += new EventHandler(channelAverageCpu_PrepareRecord);

            // Restart channel
            channelRestartGateway = diagServer.CreateRecord<CAIntRecord>(gateway.Configuration.GatewayName + ":RESTART");
            channelRestartGateway.Value = 0;
            channelRestartGateway.PropertySet += new EventHandler<PropertyDelegateEventArgs>(channelRestartGateway_PropertySet);
            // Gateway Version channel
            channelVersion = diagServer.CreateRecord<CAStringRecord>(gateway.Configuration.GatewayName + ":VERSION");
            channelVersion.CanBeRemotlySet = false;
            channelVersion.Value = Gateway.Version;
            // Gateway build date channel
            channelBuild = diagServer.CreateRecord<CAStringRecord>(gateway.Configuration.GatewayName + ":BUILD");
            channelBuild.CanBeRemotlySet = false;
            channelBuild.Value = BuildTime.ToString(CultureInfo.InvariantCulture);
        }