Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            // CONNECTION SETTINGS:  In your code, move these into some form of configuration file / table
            // *** change the following lines to your user, password and feeds of interest - get this from the Network Rail Data Feeds portal ***
            string sConnectUrl             = "stomp:tcp://datafeeds.networkrail.co.uk:61618";
            string sUser                   = "******";
            string sPassword               = "******";
            string sTopic1                 = "TRAIN_MVT_ALL_TOC";
            string sTopic2                 = "VSTP_ALL";
            bool   bUseDurableSubscription = true;

            if ((sUser == "InsertYourUserIdHere") || (sPassword == "InsertYourPasswordHere"))
            {
                Console.WriteLine("NETWORK RAIL OPEN DATA RECEIVER SAMPLE: ");
                Console.WriteLine();
                Console.WriteLine("ERROR:  Please update the source code (in the Program.cs file) to use your Network Rail Open Data credentials!");
                Console.ReadLine();
                return;
            }

            // create the shared queues (into which the receiver will enqueue messages/errors)
            ConcurrentQueue <OpenRailMessage>   oMessageQueue1 = new ConcurrentQueue <OpenRailMessage>();
            ConcurrentQueue <OpenRailMessage>   oMessageQueue2 = new ConcurrentQueue <OpenRailMessage>();
            ConcurrentQueue <OpenRailException> oErrorQueue    = new ConcurrentQueue <OpenRailException>();

            // create the receiver
            OpenRailNRODReceiver oNRODReceiver = new OpenRailNRODReceiver(
                sConnectUrl, sUser, sPassword, sTopic1, sTopic2, oMessageQueue1, oMessageQueue2, oErrorQueue, bUseDurableSubscription, 100);

            // Start the receiver
            oNRODReceiver.Start();

            // Running: process the output from the receiver (in the queues) and display progress
            DateTime dtRunUntilUtc             = DateTime.UtcNow.AddSeconds(120);
            DateTime dtNextUiUpdateTime        = DateTime.UtcNow;
            int      iTextMessageCount1        = 0;
            int      iBytesMessageCount1       = 0;
            int      iUnsupportedMessageCount1 = 0;
            string   msLastTextMessage1        = null;
            int      iTextMessageCount2        = 0;
            int      iBytesMessageCount2       = 0;
            int      iUnsupportedMessageCount2 = 0;
            string   msLastTextMessage2        = null;
            int      iErrorCount     = 0;
            string   msLastErrorInfo = null;

            while (DateTime.UtcNow < dtRunUntilUtc)
            {
                // attempt to dequeue and process any errors that occurred in the receiver
                while ((oErrorQueue.Count > 0) && (DateTime.UtcNow < dtNextUiUpdateTime))
                {
                    OpenRailException oOpenRailException = null;
                    if (oErrorQueue.TryDequeue(out oOpenRailException))
                    {
                        // the code here simply counts the errors, and captures the details of the last
                        // error - your code may log details of errors to a database or log file
                        iErrorCount++;
                        msLastErrorInfo = OpenRailException.GetShortErrorInfo(oOpenRailException);
                    }
                }

                // attempt to dequeue and process some messages
                while ((oMessageQueue1.Count > 0) && (DateTime.UtcNow < dtNextUiUpdateTime))
                {
                    OpenRailMessage oMessage = null;
                    if (oMessageQueue1.TryDequeue(out oMessage))
                    {
                        // All Network Rail Open Data Messages should be text
                        OpenRailTextMessage oTextMessage = oMessage as OpenRailTextMessage;
                        if (oTextMessage != null)
                        {
                            iTextMessageCount1++;
                            msLastTextMessage1 = oTextMessage.Text;
                        }

                        // Network Rail Open Data Messages should not be bytes messages (code is here just in case)
                        OpenRailBytesMessage oBytesMessage = oMessage as OpenRailBytesMessage;
                        if (oBytesMessage != null)
                        {
                            iBytesMessageCount1++;
                        }

                        // All Network Rail Open Data Messages should be text (code is here just in case)
                        OpenRailUnsupportedMessage oUnsupportedMessage = oMessage as OpenRailUnsupportedMessage;
                        if (oUnsupportedMessage != null)
                        {
                            iUnsupportedMessageCount1++;
                        }
                    }
                }
                while ((oMessageQueue2.Count > 0) && (DateTime.UtcNow < dtNextUiUpdateTime))
                {
                    OpenRailMessage oMessage = null;
                    if (oMessageQueue2.TryDequeue(out oMessage))
                    {
                        // All Network Rail Open Data Messages should be text
                        OpenRailTextMessage oTextMessage = oMessage as OpenRailTextMessage;
                        if (oTextMessage != null)
                        {
                            iTextMessageCount2++;
                            msLastTextMessage2 = oTextMessage.Text;
                        }

                        // Network Rail Open Data Messages should not be bytes messages (code is here just in case)
                        OpenRailBytesMessage oBytesMessage = oMessage as OpenRailBytesMessage;
                        if (oBytesMessage != null)
                        {
                            iBytesMessageCount2++;
                        }

                        // All Network Rail Open Data Messages should be text (code is here just in case)
                        OpenRailUnsupportedMessage oUnsupportedMessage = oMessage as OpenRailUnsupportedMessage;
                        if (oUnsupportedMessage != null)
                        {
                            iUnsupportedMessageCount2++;
                        }
                    }
                }

                if (dtNextUiUpdateTime < DateTime.UtcNow)
                {
                    Console.Clear();
                    Console.WriteLine("NETWORK RAIL OPEN DATA RECEIVER SAMPLE: ");
                    Console.WriteLine();
                    Console.WriteLine("Remaining Run Time = " + dtRunUntilUtc.Subtract(DateTime.UtcNow).TotalSeconds.ToString("###0.0") + " seconds");
                    Console.WriteLine();
                    Console.WriteLine("Receiver Status:");
                    Console.WriteLine("  Running = " + oNRODReceiver.IsRunning.ToString() + ", Connected To Data Feed = " + oNRODReceiver.IsConnected.ToString());
                    Console.WriteLine("  Size of local In-Memory Queue 1 (" + sTopic1 + ") = " + oMessageQueue1.Count.ToString()); // i.e. messages received from the feed but not yet processed locally
                    Console.WriteLine("  Size of local In-Memory Queue 2 (" + sTopic2 + ") = " + oMessageQueue2.Count.ToString()); // i.e. messages received from the feed but not yet processed locally
                    Console.WriteLine("  Last Message Received At = " + oNRODReceiver.LastMessageReceivedAtUtc.ToLocalTime().ToString("HH:mm:ss.fff ddd dd MMM yyyy"));
                    Console.WriteLine("  Msg Counts:  (1: {0}) = {1}, (2: {2}) = {3}", sTopic1, oNRODReceiver.MessageCount1, sTopic2, oNRODReceiver.MessageCount2);
                    Console.WriteLine();
                    Console.WriteLine("Processing Status 1 (" + sTopic1 + "):");
                    Console.WriteLine("  Msg Counts: Text = {0}, Bytes = {1}, Unsupported = {2}", iTextMessageCount1, iBytesMessageCount1, iUnsupportedMessageCount1);
                    Console.WriteLine("  Last JSON = " + (msLastTextMessage1 == null ? "" : (msLastTextMessage1.Length > 40 ? msLastTextMessage1.Substring(0, 40) + "..." : msLastTextMessage1)));
                    Console.WriteLine();
                    Console.WriteLine("Processing Status 2 (" + sTopic2 + "):");
                    Console.WriteLine("  Msg Counts: Text = {0}, Bytes = {1}, Unsupported = {2}", iTextMessageCount2, iBytesMessageCount2, iUnsupportedMessageCount2);
                    Console.WriteLine("  Last JSON = " + (msLastTextMessage2 == null ? "" : (msLastTextMessage2.Length > 40 ? msLastTextMessage2.Substring(0, 40) + "..." : msLastTextMessage2)));
                    Console.WriteLine();
                    Console.WriteLine("Errors:  Total Errors = " + iErrorCount.ToString());
                    Console.WriteLine("  Last Error = " + (msLastErrorInfo == null ? "" : msLastErrorInfo));
                    Console.WriteLine();
                    dtNextUiUpdateTime = DateTime.UtcNow.AddMilliseconds(500);
                }

                if ((oMessageQueue1.Count < 10) && (oMessageQueue2.Count < 10))
                {
                    Thread.Sleep(50);
                }
            }

            Console.WriteLine("Stopping Receiver...");

            oNRODReceiver.RequestStop();

            while (oNRODReceiver.IsRunning)
            {
                Thread.Sleep(50);
            }

            Console.WriteLine("Receiver stopped.");
            Console.WriteLine("Finished.");
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            // CONNECTION SETTINGS:  In your code, move these into some form of configuration file / table
            // *** change the lines below to match your personal details *** 
            string sConnectUrl = "activemq:tcp://InsertYourHostHere:61616?connection.watchTopicAdvisories=false";
            string sUser = "******";
            string sPassword = "******";
            string sTopic = "darwin.pushport-v16";

            if ((sUser == "InsertYourUserNameHere") || (sPassword == "InsertYourPasswordHere") || (sConnectUrl.Contains("InsertYourHostHere")))
            {
                Console.WriteLine("DARWIN PUSH PORT RECEIVER SAMPLE: ");
                Console.WriteLine();
                Console.WriteLine("ERROR:  Please update the source code (in the Program.cs file) to use your user name and password!");
                Console.ReadLine();
                return;
            }

            // create the shared queues (into which the receiver will enqueue messages/errors)
            ConcurrentQueue<OpenRailMessage> oMessageQueue = new ConcurrentQueue<OpenRailMessage>();
            ConcurrentQueue<OpenRailException> oErrorQueue = new ConcurrentQueue<OpenRailException>();

            // create the receiver
            OpenRailDarwinPushPortReceiver oDarwinReceiver = new OpenRailDarwinPushPortReceiver(
                sConnectUrl, sUser, sPassword, sTopic, oMessageQueue, oErrorQueue, 100);

            // Start the receiver
            oDarwinReceiver.Start();

            // Running: process the output from the receiver (in the queues) and display progress
            DateTime dtRunUntilUtc = DateTime.UtcNow.AddSeconds(120);
            DateTime dtNextUiUpdateTime = DateTime.UtcNow;
            int iTextMessageCount = 0;
            int iBytesMessageCount = 0;
            int iUnsupportedMessageCount = 0;
            string msLastBytesMessageContent = null;
            int iErrorCount = 0;
            string msLastErrorInfo = null;
            while (DateTime.UtcNow < dtRunUntilUtc)
            {
                // attempt to dequeue and process any errors that occurred in the receiver
                while ((oErrorQueue.Count > 0) && (DateTime.UtcNow < dtNextUiUpdateTime))
                {
                    OpenRailException oOpenRailException = null;
                    if (oErrorQueue.TryDequeue(out oOpenRailException))
                    {
                        // the code here simply counts the errors, and captures the details of the last 
                        // error - your code may log details of errors to a database or log file
                        iErrorCount++;
                        msLastErrorInfo = OpenRailException.GetShortErrorInfo(oOpenRailException);
                    }
                }

                // attempt to dequeue and process some messages
                while ((oMessageQueue.Count > 0) && (DateTime.UtcNow < dtNextUiUpdateTime))
                {
                    OpenRailMessage oMessage = null;
                    if (oMessageQueue.TryDequeue(out oMessage))
                    {
                        // Darwin should not be sending text messages (code is here just in case)
                        OpenRailTextMessage oTextMessage = oMessage as OpenRailTextMessage;
                        if (oTextMessage != null) iTextMessageCount++;

                        // All Darwin push port messages should be byte messages
                        OpenRailBytesMessage oBytesMessage = oMessage as OpenRailBytesMessage;
                        if (oBytesMessage != null)
                        {
                            iBytesMessageCount++;

                            // the processing here simply deserializes the message to objects
                            // and gets a description of each object.  Your code here
                            // could write to a database, files, etc.
                            Pport oPPort = DarwinMessageHelper.GetMessageAsObjects(oBytesMessage.Bytes);
                            msLastBytesMessageContent = DarwinMessageHelper.GetMessageDescription(oPPort);
                        }

                        // Darwin should not be sending any other message types (code is here just in case)
                        OpenRailUnsupportedMessage oUnsupportedMessage = oMessage as OpenRailUnsupportedMessage;
                        if (oUnsupportedMessage != null) iUnsupportedMessageCount++;
                    }
                }

                if (dtNextUiUpdateTime < DateTime.UtcNow)
                {
                    Console.Clear();
                    Console.WriteLine("DARWIN PUSH PORT RECEIVER SAMPLE: ");
                    Console.WriteLine();
                    Console.WriteLine("Remaining Run Time = " + dtRunUntilUtc.Subtract(DateTime.UtcNow).TotalSeconds.ToString("###0.0") + " seconds");
                    Console.WriteLine();
                    Console.WriteLine("Receiver Status:");
                    Console.WriteLine("  Message Receiver Running = " + oDarwinReceiver.IsRunning.ToString());
                    Console.WriteLine("  Message Receiver Connected To Data Feed = " + oDarwinReceiver.IsConnected.ToString());
                    Console.WriteLine("  Size of local In-Memory Queue = " + oMessageQueue.Count.ToString()); // i.e. messages received from the feed but not yet processed locally
                    Console.WriteLine("  Last Message Received At = " + oDarwinReceiver.LastMessageReceivedAtUtc.ToLocalTime().ToString("HH:mm:ss.fff ddd dd MMM yyyy"));
                    Console.WriteLine("  Total Messages Received = " + oDarwinReceiver.MessageCount.ToString());
                    Console.WriteLine();
                    Console.WriteLine("Processing Status:");
                    Console.WriteLine("  Text Message Count = " + iTextMessageCount.ToString());
                    Console.WriteLine("  Bytes Message Count = " + iBytesMessageCount.ToString());
                    Console.WriteLine("  Unsupported Message Count = " + iUnsupportedMessageCount.ToString());
                    Console.WriteLine("  Last Bytes Message Parsed = " + (msLastBytesMessageContent == null ? "" : msLastBytesMessageContent));
                    Console.WriteLine();
                    Console.WriteLine("Errors:");
                    Console.WriteLine("  Total Errors = " + iErrorCount.ToString());
                    Console.WriteLine("  Last Error = " + (msLastErrorInfo == null ? "" : msLastErrorInfo));
                    Console.WriteLine();
                    dtNextUiUpdateTime = DateTime.UtcNow.AddMilliseconds(500);
                }

                if (oMessageQueue.Count < 10) Thread.Sleep(50);
            }

            Console.WriteLine("Stopping Receiver...");

            oDarwinReceiver.RequestStop();

            while (oDarwinReceiver.IsRunning)
            {
                Thread.Sleep(50);
            }

            Console.WriteLine("Receiver stopped.");
            Console.WriteLine("Finished.");
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }