public void GetNewsDetailAsync(int storyId, Action<NewsDetailDTO> onSuccess, Action<Exception> onError) { var client = new Client(RPC_URI); client.BeginLogIn(USERNAME, PASSWORD, ar => { try { client.EndLogIn(ar); client.BeginGetNewsDetail(storyId.ToString(), ar2 => { try { var resp = client.EndGetNewsDetail(ar2); _client.BeginLogOut(ar3 => _client.EndLogOut(ar3), null); onSuccess(resp.NewsDetail); } catch (Exception exc) { onError(exc); } }, null); } catch (Exception exc) { onError(exc); } }, null); }
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); }
// Constructor public MainPage() { InitializeComponent(); var rpcClient = new Client(new Uri(rpcServerHost)); rpcClient.BeginLogIn(userName, password, ar => { rpcClient.EndLogIn(ar); sessionId = rpcClient.Session; Dispatcher.BeginInvoke(() => { listBox1.Items.Add("logged in"); }); var streamingClient = StreamingClientFactory.CreateStreamingClient(new Uri(pushServerHost), userName, sessionId); streamingClient.Connect(); var listener = streamingClient.BuildPricesListener(400535967 ,81136, 400509294, 400535971, 80902, 400509295, 400193864 ,400525367, 80926, 400498641, 400193866 ,91047 ,400194551, 121766, 400172033 ,139144); listener.MessageReceived += listener_MessageReceived; listener.Start(); }, null); }
void SubscribeNewsUpdates(Action<NewsDTO> onUpdate, Action<Exception> onError) { _client = new Client(RPC_URI); _client.BeginLogIn(USERNAME, PASSWORD, ar => { try { _client.EndLogIn(ar); Debug.WriteLine("Login ok"); _streamingClient = StreamingClientFactory.CreateStreamingClient( STREAMING_URI, USERNAME, _client.SessionId); _streamingClient.Connect(); Debug.WriteLine("\r\n\r\n\r\n\r\n\r\nStreaming connected ok"); _newsListener = _streamingClient.BuildListener<NewsDTO>("NEWS.MOCKHEADLINES.UK"); _newsListener.MessageRecieved += (s, e) => { var msg = e.Data.Headline; Debug.WriteLine(msg); //onUpdate(e.Data); }; _newsListener.Start(); Debug.WriteLine("\r\n\r\n\r\n\r\n\r\nListener started ok"); } catch (Exception exc) { onError(exc); } }, null); }
private void RefreshNewsAsync(Action onSuccess, Action<Exception> onError) { var client = new Client(RPC_URI); client.BeginLogIn(USERNAME, PASSWORD, ar => { try { client.EndLogIn(ar); client.BeginListNewsHeadlines(ApplicationSettings.Instance.CategoryCode, ApplicationSettings.Instance.MaxCount, ar2 => { try { var resp = client.EndListNewsHeadlines(ar2); News = resp.Headlines; _client.BeginLogOut(ar3 => _client.EndLogOut(ar3), null); onSuccess(); } catch (Exception exc) { onError(exc); } }, null); } catch (Exception exc) { onError(exc); } }, null); }
static void Main(string[] args) { // Test if input arguments were supplied: if (args.Length == 0) { Console.WriteLine("Please enter a numeric argument for number of tests."); return; } int noTests = 10; bool test = int.TryParse(args[0], out noTests); if (test == false) { Console.WriteLine("Please enter a numeric argument."); return; } Console.WriteLine("*** BEGIN TESTS ***"); Console.WriteLine("No of trails: " + noTests); Console.WriteLine("*** BEGIN TESTS ***"); CIAPI.Rpc.Client _ctx = null; int method = 1; List<int> errorList = new List<int>(); _ctx = new CIAPI.Rpc.Client(RPC_URI); var gate = new AutoResetEvent(false); _ctx.BeginLogIn(USERNAME, PASSWORD, a => { try { Console.WriteLine("Logged in..."); _ctx.EndLogIn(a); } catch (CityIndex.JsonClient.ApiException err) { Console.WriteLine("Login failed incorrect username/password! " + err.Message); } gate.Set(); }, null); gate.WaitOne(); //_ctx.LogIn(USERNAME, PASSWORD); String[] markets = new String[] { "99498", "99500", "99502", "99504", "99506", "99508", "99510", "99553", "99554", "99555" }; String interval = "DAY"; String no = "30"; for (int j = 1; j < noTests; j++) { Console.WriteLine("Initialising test no: " + j); Thread.Sleep(2000); for (int z = 0; z <= 2; z++) { Dictionary<String, PriceBarDTO[]> priceBarResults = new Dictionary<String, PriceBarDTO[]>(); Dictionary<String, PriceBarDTO[]> priceBarResultsASync = new Dictionary<String, PriceBarDTO[]>(); method = z; int errors = 0; int timeout = 20; int count = 0; switch (method) { case 0: Console.WriteLine("Begin Sync standard..."); Thread.Sleep(1000); foreach (String market in markets) { GetPriceBarResponseDTO priceBars = _ctx.GetPriceBars(market, interval, 1, no); Console.WriteLine("Received bar from sync call:" + market); priceBarResults.Add(market, priceBars.PriceBars); } break; case 1: Console.WriteLine("Begin Sync Parallel ForEach..."); Thread.Sleep(1000); Parallel.ForEach( markets, (n, loopState, index) => { GetPriceBarResponseDTO priceBars = _ctx.GetPriceBars(markets[index], interval, 1, no); Console.WriteLine("Received bar from parallel sync call:" + markets[index]); priceBarResults.Add(markets[index], priceBars.PriceBars); } //close lambda expression ); //close method invocation break; case 2: // - Price bars via task method... Console.WriteLine("Begin Sync task..."); Thread.Sleep(1000); Task<Dictionary<String, PriceBarDTO[]>> taskWithFactoryAndState = Task.Factory.StartNew<Dictionary<String, PriceBarDTO[]>>((stateObj) => { Dictionary<String, PriceBarDTO[]> pricetasks = new Dictionary<String, PriceBarDTO[]>(); for (int i = 0; i < (int)stateObj; i++) { GetPriceBarResponseDTO priceBars = _ctx.GetPriceBars(markets[i], interval, 1, no); Console.WriteLine("Received bar from task sync call:" + markets[i]); priceBarResults.Add(markets[i], priceBars.PriceBars); } return pricetasks; }, 10); gate = new AutoResetEvent(false); try { Task.WaitAll(taskWithFactoryAndState); gate.Set(); } catch (AggregateException aggEx) { gate.Set(); foreach (Exception ex in aggEx.InnerExceptions) { Console.WriteLine(string.Format("Caught exception '{0}'", ex.Message)); } } gate.WaitOne(); break; } foreach (String market in markets) { Console.WriteLine("Begin async call:" + market); _ctx.BeginGetPriceBars(market,interval,1,no, pricebarResult => { Console.WriteLine("Received bar from async call:" + market); GetPriceBarResponseDTO pricebar = _ctx.EndGetPriceBars(pricebarResult); priceBarResultsASync.Add(market, pricebar.PriceBars); }, null); } while (count <= timeout) { Thread.Sleep(1000); count++; if (priceBarResults.Count == priceBarResultsASync.Count) break; Console.WriteLine("Waiting for async... " + count + " seconds elapsed."); } if (priceBarResults.Count == priceBarResultsASync.Count) { Console.WriteLine("Sync and Async calls completed now checking..."); foreach (KeyValuePair<String, PriceBarDTO[]> KeyPairSync in priceBarResults) { String marketid = KeyPairSync.Key; PriceBarDTO[] priceBarSync = KeyPairSync.Value; PriceBarDTO[] priceBarAsync = priceBarResultsASync[marketid]; for (int i = 0; i < priceBarSync.Count(); i++) { // Lambda expression as executable code. //Func<int, bool> deleg = i => i < 5; Func<Decimal, Decimal, bool> myFunc = (x, y) => x == y; bool result = myFunc(priceBarSync[i].Close, priceBarAsync[i].Close); if (!result) { Console.WriteLine("Sync and Async mismatch: Sync: " + priceBarSync[i].Close + " Async: " + priceBarAsync[i].Close); errors++; } } } Console.WriteLine("Sync and Async match test completed... There were: " + errors + " errors."); errorList.Add(errors); Thread.Sleep(2000); } else if(priceBarResults.Count != priceBarResultsASync.Count) { Console.WriteLine("Sync and Async match test completed... We timed out after " + count + " seconds.. Sync count: " + priceBarResults.Count + " Async count: " + priceBarResultsASync.Count); errorList.Add(9999); } } } Console.WriteLine("**** ERRORS ***"); foreach (int error in errorList) { Console.Write(error + ","); } Console.WriteLine("**** END ERRORS ***"); //String stop = Console.ReadLine(); }
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.SessionId); Log("Creating streaming client..."); _streamingClient = StreamingClientFactory.CreateStreamingClient(streamingUri, userName, _rcpClient.SessionId); _streamingClient.StatusChanged += (s, message) => Log(string.Format("Status update: {0}", message.Status)); _streamingClient.Connect(); Log("Listening to news stream..."); _newsListener = _streamingClient.BuildListener<NewsDTO>(topic); _newsListener.Start(); _newsListener.MessageRecieved += (s, message) => { try { NewsDTO recievedNewsHeadline = message.Data; Log( string.Format( "Recieved: NewsDTO: StoryId {0}, Headline {1}, PublishDate = {2}", recievedNewsHeadline.StoryId, recievedNewsHeadline.Headline, recievedNewsHeadline.PublishDate.ToString("u"))); } 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"; } }