コード例 #1
0
ファイル: RpcFixtureBase.cs プロジェクト: fagan2888/CIAPI.CS
 static RpcFixtureBase()
 {
     LogManager.CreateInnerLogger = (logName, logLevel, showLevel, showDateTime, showLogName, dateTimeFormat)
                                    => new SimpleDebugAppender(logName, logLevel, showLevel, showDateTime, showLogName, dateTimeFormat);
     // need to set up the serializer to be used by stream listeners
     StreamingClientFactory.SetSerializer(new Serializer());
 }
コード例 #2
0
ファイル: RpcFixtureBase.cs プロジェクト: fagan2888/CIAPI.CS
        public IStreamingClient BuildStreamingClient()
        {
            var authenticatedClient = new CIAPI.Rpc.Client(Settings.RpcUri, Settings.StreamingUri, AppKey);

            authenticatedClient.LogIn(Settings.RpcUserName, Settings.RpcPassword);
            return(StreamingClientFactory.CreateStreamingClient(Settings.StreamingUri, Settings.RpcUserName, authenticatedClient.Session));
        }
コード例 #3
0
ファイル: MainPage.xaml.cs プロジェクト: fagan2888/CIAPI.CS
        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);
        }
コード例 #4
0
ファイル: LoggingFixture.cs プロジェクト: fagan2888/CIAPI.CS
        public void EnsureStreamingInnerLoggerIsCalled()
        {
            //Sky TODO - test the mockInnerLogger gets called by the RPC client and the StreamingClient

            var client = StreamingClientFactory.CreateStreamingClient(new Uri("http://a.server.com/"), "username",
                                                                      "sessionId");

            try
            {
                client.BuildNewsHeadlinesListener("BOGUS.TOPIC");
                Assert.Fail("should have thrown");
            }
            catch
            {
                // swallow
            }

            new AutoResetEvent(false).WaitOne(10000);
            // this is a unit test, not integration. need to figure out how to get the ls lib to do something.
            // maybe just a bad uri?

            var lsLogger       = loggers["CIAPI.Streaming.Lightstreamer.LightstreamerClient"];
            var actionsLogger  = loggers["com.lightstreamer.ls_client.actions"];
            var streamLogger   = loggers["com.lightstreamer.ls_client.stream"];
            var sessionLogger  = loggers["com.lightstreamer.ls_client.session"];
            var protocolLogger = loggers["com.lightstreamer.ls_client.protocol"];


            Assert.Greater(lsLogger.GetItems().ToArray().Length, 0);
            Assert.Greater(actionsLogger.GetItems().ToArray().Length, 0);
            Assert.Greater(streamLogger.GetItems().ToArray().Length, 0);
            Assert.Greater(sessionLogger.GetItems().ToArray().Length, 0);
            Assert.Greater(protocolLogger.GetItems().ToArray().Length, 0);
        }
コード例 #5
0
        private static IStreamingClient BuildStreamingClient(
            string userName = "******",
            string password = "******")
        {
            var authenticatedClient = new Client(RPC_URI);

            authenticatedClient.LogIn(userName, password);

            return(StreamingClientFactory.CreateStreamingClient(STREAMING_URI, userName, authenticatedClient.Session));
        }
コード例 #6
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");
        }
コード例 #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;
        }
コード例 #8
0
        /// <summary>
        /// Connect to rpc and push servers, get client account information and preload lookup values
        /// </summary>
        public void Connect()
        {
            RpcClient = new Client(new Uri(RpcUri));
            try
            {
                RpcClient.LogIn(UserName, Password);
                Log.Info("Rpc client logged in");
            }
            catch
            {
                Log.Info("Rpc client login failed");
                throw;
            }


            SessionId = RpcClient.Session;

            Log.Info("Getting client account information");
            try
            {
                ClientAccounts = RpcClient.AccountInformation.GetClientAndTradingAccount();
                Log.Info("Client account information received");
            }
            catch
            {
                Log.Info("Failure receiving client account information");
                throw;
            }

            CFDAccount           = ClientAccounts.TradingAccounts.FirstOrDefault(a => a.TradingAccountType == "CFD");
            SpreadBettingAccount =
                ClientAccounts.TradingAccounts.FirstOrDefault(a => a.TradingAccountType == "Spread Betting");

            StreamingClient = StreamingClientFactory.CreateStreamingClient(new Uri(StreamingUri), UserName, SessionId);

            Log.Info("Connecting streaming client");
            try
            {
                Log.Info("Streaming client constructed");
            }
            catch
            {
                Log.Info("Failure constructing streaming client");
                throw;
            }



            // Making an API call within an async handler produces a deadlock and the most common source of these deadlocks
            // is resolving a magic number on a DTO you have just recieved. So lets eliminate this by preloading all of the lookups.

            Log.Info("Preloading magic number lookups");
            RpcClient.MagicNumberResolver.PreloadMagicNumbers();
        }
コード例 #9
0
        public void TestSubscribeMultiplePrices()
        {
            const string userName = "******";
            const string password = "******";

            var client = new Client(RPC_URI);

            client.LogIn(userName, password);

            var streamingClient = StreamingClientFactory.CreateStreamingClient(
                STREAMING_URI, userName, client.Session);

            streamingClient.Connect();

            var sync      = new object();
            var listeners = new List <IStreamingListener <PriceDTO> >();

            try
            {
                var i       = 0;
                var options = new ParallelOptions {
                    MaxDegreeOfParallelism = 1
                };
                Parallel.ForEach(Const.MarketIds, options,
                                 marketId =>
                {
                    lock (sync)
                    {
                        var listener = streamingClient.BuildPricesListener(new[] { marketId });
                        listeners.Add(listener);
                        listener.MessageReceived += listener_MessageReceived;
                        listener.Start();
                    }
                    Debug.WriteLine("item: {0}", i++);
                });
                Debug.WriteLine("\r\nAll listeners created ok\r\n");
            }
            finally
            {
                var i = 0;
                foreach (var listener in listeners)
                {
                    listener.Stop();
                    Debug.WriteLine("unsubscribed item: {0}", i++);
                }
                streamingClient.Disconnect();
                client.LogOut();
            }
        }
コード例 #10
0
        public void SubscribeToNewsHeadlineStream()
        {
            //First we need a valid session, obtained from the Rpc client
            var ctx = new CIAPI.Rpc.Client(RPC_URI);

            ctx.LogIn(USERNAME, PASSWORD);

            //Next we create a connection to the streaming api, using the authenticated session
            //You application should only ever have one of these
            var streamingClient = StreamingClientFactory.CreateStreamingClient(STREAMING_URI, USERNAME, ctx.SessionId);

            streamingClient.Connect();

            //And instantiate a listener for news headlines on the appropriate topic
            //You can have multiple listeners on one connection
            var newsListener = streamingClient.BuildNewsHeadlinesListener("NEWS.MOCKHEADLINES.UK");

            newsListener.Start();

            //The MessageReceived 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
            var     gate = new ManualResetEvent(false);
            NewsDTO receivedNewsHeadline = null;

            newsListener.MessageReceived += (s, e) =>
            {
                receivedNewsHeadline = e.Data;
                //Do something with the new News headline data - perhaps update a news ticker?
                gate.Set();
            };
            gate.WaitOne();

            //Clean up
            //Stop any listeners
            newsListener.Stop();
            //Shut down the connection
            streamingClient.Disconnect();
            //Destroy your session
            ctx.LogOut();
        }
コード例 #11
0
ファイル: Program.cs プロジェクト: fagan2888/CIAPI.CS
        static void Main(string[] args)
        {
            try
            {
                LogManager.CreateInnerLogger = (logName, logLevel, showLevel, showDateTime, showLogName, dateTimeFormat)
                                               => new SimpleDebugAppender(logName, logLevel, showLevel, showDateTime, showLogName, dateTimeFormat);

                var client        = new Client(new Uri("https://ciapi.cityindex.com/tradingapi"));
                var loginResponse = client.LogIn("DM715257", "password");
                if (loginResponse.PasswordChangeRequired)
                {
                    throw new Exception("must change password");
                }
                var streamingClient = StreamingClientFactory.CreateStreamingClient(new Uri("https://push.cityindex.com"), client.UserName,
                                                                                   client.Session);
                var newsStream = streamingClient.BuildNewsHeadlinesListener("UK");
                newsStream.MessageReceived += new EventHandler <StreamingClient.MessageEventArgs <CIAPI.DTO.NewsDTO> >(newsStream_MessageReceived);

                Console.WriteLine("listening for 30 seconds");
                new AutoResetEvent(false).WaitOne(30000);

                Console.WriteLine("press enter to exit");
                Console.ReadLine();
                streamingClient.TearDownListener(newsStream);
                streamingClient.Dispose();
                client.LogOut();
                client.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.WriteLine("press enter to exit");
                Console.ReadLine();
                throw;
            }
        }
コード例 #12
0
        void EnsureConnectionSync()
        {
            if (_disposing)
            {
                Thread.CurrentThread.Abort();
            }

            lock (_sync)
            {
                if (_client == null)
                {
                    _client = new Client(new Uri(ApplicationSettings.Instance.ServerUrl));
                    _client.LogIn(ApplicationSettings.Instance.UserName, ApplicationSettings.Instance.Password);
                }

                if (_streamingClient == null)
                {
                    _streamingClient = StreamingClientFactory.CreateStreamingClient(
                        new Uri(ApplicationSettings.Instance.StreamingServerUrl),
                        ApplicationSettings.Instance.UserName, _client.Session);
                    _streamingClient.Connect();
                }
            }
        }