Basic implementation of the ITopicControlAddCallback.
Inheritance: ITopicControlAddCallback
示例#1
0
        /// <summary>
        /// Runs the JSON topic Control Client example.
        /// </summary>
        /// <param name="cancellationToken">A token used to end the client example.</param>
        /// <param name="args">A single string should be used for the server url.</param>
        public void Run(CancellationToken cancellationToken, string[] args)
        {
            var serverUrl     = args[0];
            var session       = Diffusion.Sessions.Principal("control").Password("password").Open(serverUrl);
            var topicControl  = session.GetTopicControlFeature();
            var updateControl = session.GetTopicUpdateControlFeature();

            // Create a JSON topic 'random/JSON'
            var topic       = "random/JSON";
            var addCallback = new AddCallback();

            topicControl.AddTopicFromValue(
                topic,
                Diffusion.DataTypes.JSON.FromJSONString("{\"date\":\"To be updated\",\"time\":\"To be updated\"}"),
                addCallback);

            // Wait for the OnTopicAdded callback, or a failure
            if (!addCallback.Wait(TimeSpan.FromSeconds(5)))
            {
                Console.WriteLine("Callback not received within timeout.");
                session.Close();
                return;
            }
            else if (addCallback.Error != null)
            {
                Console.WriteLine("Error : {0}", addCallback.Error.ToString());
                session.Close();
                return;
            }

            // Update topic every 300 ms until user requests cancellation of enxample
            var updateCallback = new UpdateCallback(topic);

            while (!cancellationToken.IsCancellationRequested)
            {
                var newValue = Diffusion.DataTypes.JSON.FromJSONString(
                    "{\"date\":\"" + DateTime.Today.Date.ToString("D") + "\"," +
                    "\"time\":\"" + DateTime.Now.TimeOfDay.ToString("g") + "\"}");
                updateControl.Updater.ValueUpdater <IJSON>().Update(topic, newValue, updateCallback);

                Thread.Sleep(300);
            }

            // Remove the JSON topic 'random/JSON'
            var removeCallback = new RemoveCallback(topic);

            topicControl.Remove(topic, removeCallback);
            if (!removeCallback.Wait(TimeSpan.FromSeconds(5)))
            {
                Console.WriteLine("Callback not received within timeout.");
            }
            else if (removeCallback.Error != null)
            {
                Console.WriteLine("Error : {0}", removeCallback.Error.ToString());
            }

            // Close the session
            session.Close();
        }
        /// <summary>
        /// Runs the JSON topic Control Client example.
        /// </summary>
        /// <param name="cancellationToken">A token used to end the client example.</param>
        /// <param name="args">A single string should be used for the server url.</param>
        public void Run( CancellationToken cancellationToken, string[] args )
        {
            var serverUrl = args[ 0 ];
            var session = Diffusion.Sessions.Principal( "control" ).Password( "password" ).Open( serverUrl );
            var topicControl = session.GetTopicControlFeature();
            var updateControl = session.GetTopicUpdateControlFeature();

            // Create a JSON topic 'random/JSON'
            var topic = "random/JSON";
            var addCallback = new AddCallback();
            topicControl.AddTopicFromValue(
                topic,
                Diffusion.DataTypes.JSON.FromJSONString( "{\"date\":\"To be updated\",\"time\":\"To be updated\"}" ),
                addCallback );

            // Wait for the OnTopicAdded callback, or a failure
            if ( !addCallback.Wait( TimeSpan.FromSeconds( 5 ) ) ) {
                Console.WriteLine( "Callback not received within timeout." );
                session.Close();
                return;
            } else if ( addCallback.Error != null ) {
                Console.WriteLine( "Error : {0}", addCallback.Error.ToString() );
                session.Close();
                return;
            }

            // Update topic every 300 ms until user requests cancellation of enxample
            var updateCallback = new UpdateCallback( topic );
            while ( !cancellationToken.IsCancellationRequested ) {
                var newValue = Diffusion.DataTypes.JSON.FromJSONString(
                    "{\"date\":\"" + DateTime.Today.Date.ToString( "D" ) + "\"," +
                    "\"time\":\"" + DateTime.Now.TimeOfDay.ToString( "g" ) + "\"}" );
                updateControl.Updater.ValueUpdater<IJSON>().Update( topic, newValue, updateCallback );

                Thread.Sleep( 300 );
            }

            // Remove the JSON topic 'random/JSON'
            var removeCallback = new RemoveCallback( topic );
            topicControl.Remove( topic, removeCallback );
            if ( !removeCallback.Wait( TimeSpan.FromSeconds( 5 ) ) ) {
                Console.WriteLine( "Callback not received within timeout." );
            } else if ( removeCallback.Error != null ) {
                Console.WriteLine( "Error : {0}", removeCallback.Error.ToString() );
            }

            // Close the session
            session.Close();
        }