示例#1
0
        static void Main(string[] args)
        {
            /*
             * See the configuration example for how a typical application
             * might obtain a configuration.
             */
            var configuration = ExampleConfiguration.Obtain(args);

            /*
             * Open a session and subscribe to a couple of topics. Once
             * subscribed, the server will begin to queue messages published
             * to these topics. They remain queued until read by the session.
             */
            Console.WriteLine("Opening message read session...");

            using (var readSession = MessageSession.Open(configuration))
            {
                readSession.Subscribe("example.data");
                readSession.Subscribe("example.exit");

                /*
                 * The read session will receive all messages for the the
                 * subscribed topics, but not the message for the unknown
                 * topic. The messages are read in order they are queued on the
                 * server regardless of topic. There may be a small delay
                 * between the message being published and it being queued and
                 * available for reading.
                 */
                PublishMessages(configuration);
                Console.WriteLine("Message queue size: {0} of {1} bytes",
                                  readSession.QueueSize, readSession.MaxQueueSize);
                Console.WriteLine("Reading back messages...");
                bool receivedExitMessage = false;

                while (!receivedExitMessage)
                {
                    /*
                     * Blocks until a message is available in the queue or a
                     * brief amount of time has passed. Returns null when there
                     * are no queued messages.
                     */
                    var message = readSession.Read();
                    receivedExitMessage = HandleMessage(message);
                }

                /*
                 * The server will discard any remaining queued messages when
                 * disposing the session.
                 */
                Console.WriteLine("Remaining bytes in the message queue: {0}",
                                  readSession.QueueSize);
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            /*
             * See the configuration example for how a typical application
             * might obtain a configuration.
             */
            var configuration = ExampleConfiguration.Obtain(args, allowCloud: false);

            /*
             * Open a session and subscribe to a topic. Once subscribed, the
             * server will begin to queue messages published by Host.vi.
             */
            Console.WriteLine("Opening message session...");
            int messagesSent;
            int messagesReceived;

            using (var session = MessageSession.Open(configuration))
                using (var exitSource = new CancellationTokenSource())
                {
                    session.Subscribe("Example to Client");

                    /*
                     * Set up a CancelKeyPress handler to clean up the session
                     * when pressing Ctrl+C to stop the example. The handler will
                     * cancel our CancellationTokenSource, signalling our async
                     * loops to stop.
                     */
                    Console.CancelKeyPress += (s, e) =>
                    {
                        if (e.SpecialKey == ConsoleSpecialKey.ControlBreak)
                        {
                            // Allow Ctrl+Break to terminate the process.
                            return;
                        }

                        exitSource.Cancel();
                        Console.WriteLine("Stopping...");
                        e.Cancel = true;
                    };

                    /*
                     * Begin asynchronous tasks to read and publish messages until
                     * the token is canceled, then wait for the tasks to complete.
                     */
                    var mainLoopTask = RunLoopsAsync(session, exitSource.Token);
                    (messagesSent, messagesReceived) = mainLoopTask.Result;
                }

            Console.WriteLine("Message session closed");
            Console.WriteLine("Published {0} and received {1} messages",
                              messagesSent, messagesReceived);
        }
示例#3
0
        private static void PublishMessages(IHttpConfiguration configuration)
        {
            /*
             * Open a separate session for publishing messages. This is not
             * required, but simulates how two separate applications can
             * communicate with each other using messages.
             */
            Console.WriteLine("Publishing messages...");

            using (var writeSession = MessageSession.Open(configuration))
            {
                /*
                 * Messages can contain any string. It's up to the application
                 * to determine the data format to use for each message topic.
                 * In this example, the example.data topic contains a JSON
                 * representation of the MessageData class.
                 */
                var data = JsonConvert.SerializeObject(new MessageData
                {
                    Message = "Hello World! PI=",
                    Value   = Math.PI
                });
                writeSession.Publish("example.data", data);

                data = JsonConvert.SerializeObject(new MessageData
                {
                    Message = "Another message, E=",
                    Value   = Math.E
                });
                writeSession.Publish("example.data", data);

                /*
                 * The server will ignore messages published to a topic with no
                 * subscribers, but it's not an error.
                 */
                writeSession.Publish("example.unknown", "no subscribers");

                /*
                 * Publish messages to the example.exit topic to signal the
                 * reader to stop. Both are queued, but only one is read.
                 */
                writeSession.Publish("example.exit", "first exit");
                writeSession.Publish("example.exit", "second exit");
            }
        }
示例#4
0
        static void Main(string[] args)
        {
            try
            {
                /*
                 * When SystemLink Server is installed locally, or when running
                 * on a system managed by a SystemLink Server, the
                 * HttpConfigurationManager provides access to an automatic
                 * configuration to communicate with the SystemLink Server.
                 */
                var manager           = new HttpConfigurationManager();
                var autoConfiguration = manager.GetConfiguration();
                Console.WriteLine("Found automatic configuration for {0}",
                                  autoConfiguration.ServerUri);

                /*
                 * Each class that takes in a configuration also has a default
                 * constructor that uses the automatic configuration instead.
                 */
                using (var autoManager = new TagManager())
                {
                }
            }
            catch (ApiException ex)
            {
                Console.WriteLine("Automatic configuration: {0}",
                                  ex.Message);
            }

            /*
             * When an automatic configuration isn't available (often during
             * application development) the HttpConfiguration class can
             * reference any SystemLink Server available over HTTP/HTTPS.
             *
             * Ideally, the username and password would be read from the user at
             * run time or from a file rather than checked into source.
             */
            var serverConfiguration = new HttpConfiguration(
                new Uri("https://myserver"), "my_user", "my_password");

            /*
             * To access SystemLink Cloud instead of a SystemLink Server
             * installation, log into https://www.systemlinkcloud.com and
             * generate an API key. Then use that API key with the
             * CloudHttpConfiguration class.
             *
             * Ideally, the API key would be read from a file or otherwise
             * protected rather than checked into source.
             */
            var cloudConfiguration = new CloudHttpConfiguration("apikey");

            /*
             * Configurations are shared across all SystemLink client APIs.
             */
            var exampleConfiguration = ExampleConfiguration.Obtain(args);

            using (var manager = new TagManager(exampleConfiguration))
                using (var session = MessageSession.Open(exampleConfiguration))
                {
                }

            /*
             * Mixing configurations enables applications to synchronize data
             * across multiple servers and/or SystemLink Cloud.
             */
            using (var cloudManager = new TagManager(cloudConfiguration))
                using (var serverManager = new TagManager(serverConfiguration))
                {
                }

            // See the tag and message API examples for specific usage.
        }