public void SubscribeToNewsHeadlineStream()
        {
            string searchKeywords = SearchTextBox.Text;

            App.ctx.BeginLogIn(App.USERNAME, App.PASSWORD, a =>
            {
                App.ctx.EndLogIn(a);

                //Next we create a connection to the streaming api, using the authenticated session
                streamingClient = App.StreamingClient;
                streamingClient.Connect();

                //And instantiate a listener for news headlines on the appropriate topic
                //You can have multiple listeners on one connection
                newsListener = streamingClient.BuildNewsHeadlinesListener(searchKeywords);
                newsListener.Start();

                //The MessageRecieved event will be triggered every time a new News headline is available,
                //so attach a handler for that event, and wait until something comes through
                NewsDTO recievedNewsHeadline = null;
                newsListener.MessageReceived += (s, e) =>
                {
                    recievedNewsHeadline = e.Data;
                    //Add this new news headline to the main items collection.
                    ItemViewModel item = new ItemViewModel();
                    item.Headline = recievedNewsHeadline.Headline;
                    item.PublishDate = recievedNewsHeadline.PublishDate.ToString();
                    recievedNewsHeadline.StoryId = recievedNewsHeadline.StoryId;

                    App.ViewModel.LoadData(item);
                };

            }, null);
        }
        private void BuildClients()
        {
            Dispatcher.BeginInvoke(() => listBox1.Items.Add("creating rpc client"));
            RpcClient = new Client(RPC_URI, "CI-WP7");
            RpcClient.BeginLogIn(USERNAME, PASSWORD, ar =>
            {
                var session = RpcClient.EndLogIn(ar);

                ThreadPool.QueueUserWorkItem(_ =>
                {
                    Dispatcher.BeginInvoke(() => listBox1.Items.Add("creating listeners"));
                    StreamingClient = StreamingClientFactory.CreateStreamingClient(STREAM_URI, USERNAME, session.Session);
                    MarketPricesStream = StreamingClient.BuildPricesListener(new[] { MarketId });
                    MarketPricesStream.MessageReceived += OnMarketPricesStreamMessageReceived;
                    OrdersStream = StreamingClient.BuildOrdersListener();
                    OrdersStream.MessageReceived += OnOrdersStreamMessageReceived;

                    Dispatcher.BeginInvoke(() => listBox1.Items.Add("getting account info"));
                    RpcClient.AccountInformation.BeginGetClientAndTradingAccount(ar2 =>
                    {
                        Account = RpcClient.AccountInformation.EndGetClientAndTradingAccount(ar2);

                        Dispatcher.BeginInvoke(() => listBox1.Items.Add("getting market info"));
                        RpcClient.Market.BeginGetMarketInformation(MarketId.ToString(), ar3 =>
                        {
                            Market = RpcClient.Market.EndGetMarketInformation(ar3).MarketInformation;
                            Dispatcher.BeginInvoke(() => button1.IsEnabled = true);
                        }, null);
                    }, null);
                });
            }, null);
        }
Ejemplo n.º 3
0
 public void SetupFixture()
 {
     _rpcClient = BuildRpcClient();
     _streamingClient = _rpcClient.CreateStreamingClient();
     _CFDmarketId = GetAvailableCFDMarkets(_rpcClient)[0].MarketId;
     _accounts = _rpcClient.AccountInformation.GetClientAndTradingAccount();
 }
Ejemplo n.º 4
0
        public MainPage()
        {
            InitializeComponent();

            Dispatcher.BeginInvoke(() =>
            {
                StartButton.IsEnabled = false;
                StopButton.IsEnabled  = false;
            });


            // need to set up the serializer to be used by stream listeners
            StreamingClientFactory.SetSerializer(new Serializer());

            // build an rpc client and log it in.
            rpcClient = new Client(new Uri(StaticTestConfig.RpcUrl), new Uri(StaticTestConfig.StreamingUrl), StaticTestConfig.AppKey);

            rpcClient.StartMetrics();
            // get a session from the rpc client
            rpcClient.BeginLogIn(StaticTestConfig.ApiUsername, StaticTestConfig.ApiPassword, ar =>
            {
                rpcClient.EndLogIn(ar);

                rpcClient.MagicNumberResolver.PreloadMagicNumbersAsync();

                Debug.WriteLine("creating client");

                // build a streaming client.
                _streamingClient = StreamingClientFactory.CreateStreamingClient(new Uri(StaticTestConfig.StreamingUrl), StaticTestConfig.ApiUsername, rpcClient.Session);

                Debug.WriteLine("connecting client");

                // note: due to internal changes the 'connect' method
                // name is a misnomer: no actual network activity is occuring,
                // only the building of the necessary client connections for
                // each of the published data adapters. Actual connection is
                // performed on demand for each adapter. This minimizes startup time.

                // the upside to this is that there is no need to run .Connect in a separate thread.



                Debug.WriteLine("client connected");


                // from this point there should be no need to stop, disconnect or dispose of the
                // client instance. But if you choose to disconnect a StreamingClient, it should
                // be disposed and reinstantiated. it is a one use object at this point as this is
                // the only usage pattern presented in the sample code.

                Dispatcher.BeginInvoke(() =>
                {
                    listBox1.Items.Add("logged in");


                    StartButton.IsEnabled = true;
                    StopButton.IsEnabled  = false;
                });
            }, null);
        }
Ejemplo n.º 5
0
 public void SetupFixture()
 {
     _rpcClient       = BuildRpcClient();
     _streamingClient = _rpcClient.CreateStreamingClient();
     _CFDmarketId     = GetAvailableCFDMarkets(_rpcClient)[0].MarketId;
     _accounts        = _rpcClient.AccountInformation.GetClientAndTradingAccount();
 }
Ejemplo n.º 6
0
        public MainPage()
        {
            InitializeComponent();

            Dispatcher.BeginInvoke(() =>
            {
                StartButton.IsEnabled = false;
                StopButton.IsEnabled = false;
            });


            // build an rpc client and log it in.
            rpcClient = new Client(new Uri(RpcServerHost));

            // get a session from the rpc client
            rpcClient.BeginLogIn(UserName, Password, ar =>
                {
                    rpcClient.EndLogIn(ar);

                    Debug.WriteLine("creating client");

                    // build a streaming client.
                    _streamingClient = StreamingClientFactory.CreateStreamingClient(new Uri(PushServerHost), UserName, rpcClient.Session);

                    Debug.WriteLine("connecting client");

                    // note: due to internal changes the 'connect' method
                    // name is a misnomer: no actual network activity is occuring,
                    // only the building of the necessary client connections for 
                    // each of the published data adapters. Actual connection is 
                    // performed on demand for each adapter. This minimizes startup time.

                    // the upside to this is that there is no need to run .Connect in a separate thread.



                    Debug.WriteLine("client connected");


                    // from this point there should be no need to stop, disconnect or dispose of the 
                    // client instance. But if you choose to disconnect a StreamingClient, it should
                    // be disposed and reinstantiated. it is a one use object at this point as this is 
                    // the only usage pattern presented in the sample code.

                    Dispatcher.BeginInvoke(() =>
                        {
                            listBox1.Items.Add("logged in");


                            StartButton.IsEnabled = true;
                            StopButton.IsEnabled = false;
                        });

                }, null);

        }
Ejemplo n.º 7
0
        public static void ConnectCIAPI()
        {
            CIAPI_Client = new CIAPI.Rpc.Client(RPC_URI);
            CIAPI_Client.LogIn(USERNAME, PASSWORD);

            StreamingClient = StreamingClientFactory.CreateStreamingClient(STREAMING_URI, USERNAME, CIAPI_Client.SessionId);
            StreamingClient.Connect();

            IsConnected = true;
        }
Ejemplo n.º 8
0
        public void InitializeStreamingClient(ReliableAsyncResult ar)
        {
            RpcClient.EndLogIn(ar);
            // need to set up the serializer to be used by stream listeners
            StreamingClientFactory.SetSerializer(new Serializer());

            StreamingClient = StreamingClientFactory.CreateStreamingClient(STREAMING_URI, RpcClient.UserName,
                                                                           RpcClient.Session);
            LogToScreen("rpc client logged in");
        }
Ejemplo n.º 9
0
        public void InitializeStreamingClient(ReliableAsyncResult ar)
        {
            RpcClient.EndLogIn(ar);
            // need to set up the serializer to be used by stream listeners
            StreamingClientFactory.SetSerializer(new Serializer());

            StreamingClient = StreamingClientFactory.CreateStreamingClient(STREAMING_URI, RpcClient.UserName,
                                                                           RpcClient.Session);
            LogToScreen("rpc client logged in");
        }
Ejemplo n.º 10
0
        private static Dictionary <int, List <PriceDTO> > ListenToPricesFor(IStreamingClient streamingClient,
                                                                            IEnumerable <int> markets, TimeSpan within, int minPricesToCollect)
        {
            var gates           = new Dictionary <int, ManualResetEvent>();
            var priceListeners  = new Dictionary <int, IStreamingListener>();
            var collectedPrices = new Dictionary <int, List <PriceDTO> >();

            foreach (var market in markets)
            {
                var gate = new ManualResetEvent(false);
                gates.Add(market, gate);
                var prices = new List <PriceDTO>();
                collectedPrices.Add(market, prices);

                var priceListener = streamingClient.BuildPricesListener(new[] { market });
                priceListener.MessageReceived += (s, e) =>
                {
                    prices.Add(e.Data);
                    if (prices.Count >= minPricesToCollect)
                    {
                        gate.Set();
                    }
                };

                priceListeners.Add(market, priceListener);
                priceListener.Start();
            }


            try
            {
                foreach (var market in markets)
                {
                    if (!gates[market].WaitOne(within))
                    {
                        Assert.Fail("Not enough prices were collected for {0} within {1}.  Required {2}.  Got {3}",
                                    market, within, minPricesToCollect, collectedPrices[market].Count);
                    }
                }
            }
            finally
            {
                foreach (var market in markets)
                {
                    priceListeners[market].Stop();
                }
            }

            return(collectedPrices);
        }
Ejemplo n.º 11
0
        private void BuildClients()
        {
            //Hook up a logger for the CIAPI.CS libraries
            LogManager.CreateInnerLogger = (logName, logLevel, showLevel, showDateTime, showLogName, dateTimeFormat)
                                           => new SimpleDebugAppender(logName, logLevel, showLevel, showDateTime, showLogName, dateTimeFormat);

            Dispatcher.BeginInvoke(() => listBox1.Items.Add("creating rpc client"));
            RpcClient = new Client(RPC_URI, STREAM_URI, "CI-WP7");

            RpcClient.BeginLogIn(USERNAME, PASSWORD, ar =>
            {
                _logger.Info("ending login");
                var session = RpcClient.EndLogIn(ar);

                ThreadPool.QueueUserWorkItem(_ =>
                {
                    Dispatcher.BeginInvoke(() => listBox1.Items.Add("creating listeners"));
                    StreamingClient    = RpcClient.CreateStreamingClient();
                    MarketPricesStream = StreamingClient.BuildPricesListener(new[] { MarketId });
                    MarketPricesStream.MessageReceived += OnMarketPricesStreamMessageReceived;
                    OrdersStream = StreamingClient.BuildOrdersListener();
                    OrdersStream.MessageReceived += OnOrdersStreamMessageReceived;

                    Dispatcher.BeginInvoke(() => listBox1.Items.Add("getting account info"));
                    RpcClient.AccountInformation.BeginGetClientAndTradingAccount(ar2 =>
                    {
                        Account = RpcClient.AccountInformation.EndGetClientAndTradingAccount(ar2);

                        Dispatcher.BeginInvoke(() => listBox1.Items.Add("getting market info"));
                        RpcClient.Market.BeginGetMarketInformation(MarketId.ToString(), ar3 =>
                        {
                            Market = RpcClient.Market.EndGetMarketInformation(ar3).MarketInformation;
                            Dispatcher.BeginInvoke(() => button1.IsEnabled = true);
                        }, null);
                    }, null);
                });
            }, null);
        }
Ejemplo n.º 12
0
        private void BuildClients()
        {
            //Hook up a logger for the CIAPI.CS libraries
            LogManager.CreateInnerLogger = (logName, logLevel, showLevel, showDateTime, showLogName, dateTimeFormat)
                         => new SimpleDebugAppender(logName, logLevel, showLevel, showDateTime, showLogName, dateTimeFormat);

            Dispatcher.BeginInvoke(() => listBox1.Items.Add("creating rpc client"));
            RpcClient = new Client(RPC_URI, STREAM_URI, "CI-WP7");
           
            RpcClient.BeginLogIn(USERNAME, PASSWORD, ar =>
            {
                _logger .Info("ending login");
                var session = RpcClient.EndLogIn(ar);

                ThreadPool.QueueUserWorkItem(_ =>
                {
                    Dispatcher.BeginInvoke(() => listBox1.Items.Add("creating listeners"));
                    StreamingClient = RpcClient.CreateStreamingClient();
                    MarketPricesStream = StreamingClient.BuildPricesListener(new[] { MarketId });
                    MarketPricesStream.MessageReceived += OnMarketPricesStreamMessageReceived;
                    OrdersStream = StreamingClient.BuildOrdersListener();
                    OrdersStream.MessageReceived += OnOrdersStreamMessageReceived;

                    Dispatcher.BeginInvoke(() => listBox1.Items.Add("getting account info"));
                    RpcClient.AccountInformation.BeginGetClientAndTradingAccount(ar2 =>
                    {
                        Account = RpcClient.AccountInformation.EndGetClientAndTradingAccount(ar2);

                        Dispatcher.BeginInvoke(() => listBox1.Items.Add("getting market info"));
                        RpcClient.Market.BeginGetMarketInformation(MarketId.ToString(), ar3 =>
                        {
                            Market = RpcClient.Market.EndGetMarketInformation(ar3).MarketInformation;
                            Dispatcher.BeginInvoke(() => button1.IsEnabled = true);
                        }, null);
                    }, null);
                });
            }, null);
        }
Ejemplo n.º 13
0
        static void Main(string[] args)
        {
            try
            {
                var adapter = new MyLoggerFactoryAdapter(null) { OnMessage = AddLogMessage };
                LogManager.Adapter = adapter;

                _client = new Client(RPC_URI);
                _client.LogIn(USERNAME, PASSWORD);

                _streamingClient = StreamingClientFactory.CreateStreamingClient(STREAMING_URI, USERNAME, _client.Session);
                _streamingClient.Connect();

                Console.WriteLine("Connected");

                var accountInfo = _client.AccountInformation.GetClientAndTradingAccount();
                var markets = _client.CFDMarkets.ListCfdMarkets("", "", accountInfo.ClientAccountId, -1).Markets;

                var marketIds = markets.Select(market => market.MarketId).ToArray();

                var listener = _streamingClient.BuildPricesListener(marketIds);
                listener.MessageReceived += OnMessageReceived;
                listener.Start();

                Console.ReadKey();

                _streamingClient.Disconnect();

                _client.LogOut();
                _client.Dispose();
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc);
                Trace.WriteLine(exc);
            }
        }
Ejemplo n.º 14
0
 public void SetupFixture()
 {
     _authenticatedClient = new Client(Settings.RpcUri, Settings.StreamingUri, AppKey);
     _authenticatedClient.LogIn(Settings.RpcUserName, Settings.RpcPassword);
     _streamingClient = _authenticatedClient.CreateStreamingClient();
 }
Ejemplo n.º 15
0
 public void SetupFixture()
 {
     var authenticatedClient = new CIAPI.Rpc.Client(Settings.RpcUri);
     authenticatedClient.LogIn(Settings.RpcUserName, Settings.RpcPassword);
     streamingClient = StreamingClientFactory.CreateStreamingClient(Settings.StreamingUri, Settings.RpcUserName, authenticatedClient.Session);
 }
Ejemplo n.º 16
0
        public void SetUp()
        {
            _streamingClient = StreamingClientFactory.CreateStreamingClient(new Uri("http://a.server.com/"),
                                                                            "username", "sessionId");

        }
Ejemplo n.º 17
0
 public void SetupFixture()
 {
     _authenticatedClient = new Client(Settings.RpcUri, Settings.StreamingUri, AppKey);
     _authenticatedClient.LogIn(Settings.RpcUserName, Settings.RpcPassword);
     _streamingClient = _authenticatedClient.CreateStreamingClient();
 }
Ejemplo n.º 18
0
        private void ToggleSubscribeButton_Click(object sender, RoutedEventArgs e)
        {
            if (ToggleSubscribeButtonlabel.Text == "Subscribe")
            {
                _rcpClient = new Rpc.Client(new Uri(RpcUriTextbox.Text));

                var userName = UserNameTextbox.Text;
                var streamingUri = new Uri(StreamingUriTextbox.Text);
                var topic = TopicTextbox.Text;
                Log("Creating session...");
                
                _rcpClient.BeginLogIn(userName, PasswordTextbox.Text, loginResult =>
                {
                    try
                    {
                        _rcpClient.EndLogIn(loginResult);

                        _logger.DebugFormat("Session is: {0}", _rcpClient.Session);
                        Log("Creating streaming client...");
                        _streamingClient = StreamingClientFactory.CreateStreamingClient(streamingUri, userName, _rcpClient.Session);
                        _streamingClient.StatusChanged += (s, message) 
                                                          => Log(string.Format("Status update: {0}", message.Status));
                        _streamingClient.Connect();

                        Log("Creating listener...");
                        _newsListener = _streamingClient.BuildNewsHeadlinesListener(topic);
                        Log("Starting listener...");
                        _newsListener.Start();
                        Log("Listening to news stream...");
                        _newsListener.MessageReceived += (s, message) =>
                                                             {
                                                                 try
                                                                 {
                                                                     NewsDTO receivedNewsHeadline = message.Data;
                                                                     Log(
                                                                         string.Format(
                                                                             "Received: NewsDTO: StoryId {0}, Headline {1}, PublishDate = {2}",
                                                                             receivedNewsHeadline.StoryId,
                                                                             receivedNewsHeadline.Headline,
                                                                             //receivedNewsHeadline.PublishDate.ToString("u")));
                                                                             // dates are currently strings
                                                                             receivedNewsHeadline.PublishDate));
                                                                 }
                                                                 catch (Exception exception)
                                                                 {
                                                                     _logger.Error("Exception occured:", exception);
                                                                 }
                                                             };
                    }
                    catch (Exception exception)
                    {
                        _logger.Error("Exception occured:", exception);
                    }

                }, null);

                ToggleSubscribeButtonlabel.Text = "Stop";
            }
            else
            {
                try
                {
                    Log("Stopping listening to news stream...");

                    // D: abbreviating conditionals makes it hard to step 
                    // and is also a good way to get bugs. you may notice that I always use
                    // blocks. 

                    if (_newsListener != null)
                    {
                        _newsListener.Stop();
                    }
                    Log("Disconnecting from streaming server...");

                    if (_streamingClient != null)
                    {
                       _streamingClient.Disconnect();
                    }

                    Log("Deleting session...");

                    if (_rcpClient != null )
                    {
                        // REMOVEME: i commented this out and still getting the ObjectDisposed exception
                        // so it is not in the RpcClient

                        _rcpClient.BeginLogOut(logoutResult =>
                                                   {
                                                       // FIXME: id/session invalid - getting back LoggedOut: false

                                                        /*do nothing*/
                                                       var breakTarget = 0;
                                                   }, null);
                    }
                }
                catch (Exception exception)
                {
                    _logger.Error("Exception occured:", exception);
                }

                ToggleSubscribeButtonlabel.Text = "Subscribe";
            }
            
        }