/// <summary> /// /// </summary> /// <param name="requestStream"></param> /// <param name="context"></param> /// <returns></returns> public async override Task <PlayerSearchResponse> SearchPlayer_ClientStream(IAsyncStreamReader <PlayerSearchRequest> requestStream, ServerCallContext context) { string callCounter = GetCallCounter(context); IDictionary <string, Team> teams = SampleDataRepository.GetTeams(10); PlayerSearchResponse response = new PlayerSearchResponse(); int initial = 1; while (await requestStream.MoveNext().ConfigureAwait(false)) { ThrowExceptionIfRequestedCancel(context); PlayerSearchRequest request = requestStream.Current; s_Logger.Info(string.Format("[{0}] Requested {1} players.", callCounter, request.ExpectedDataCount)); foreach (Player player in SampleDataRepository.GeneratePlayers(initial, request.ExpectedDataCount, teams)) { ThrowExceptionIfRequestedCancel(context); response.Players.Add(player); if (!response.Teams.ContainsKey(player.TeamCode)) { response.Teams.Add(player.TeamCode, teams[player.TeamCode]); } } initial += request.ExpectedDataCount; } return(response); }
/// <summary> /// /// </summary> /// <param name="requests"></param> /// <param name="callOptions"></param> /// <returns></returns> private IList <PlayerSearchResponse> SearchPlayerServerStreamUseHttp(PlayerSearchRequest request, CallOptions callOptions) { AppendLog("[HttpGateway] PlayerSearch/SearchPlayer_ServerStream start"); IList <PlayerSearchResponse> responses = CallHttpGateway <PlayerSearchRequest, PlayerSearchResponse[]>("PlayerSearch/SearchPlayer_ServerStream", request, callOptions); AppendLog("[HttpGateway] PlayerSearch/SearchPlayer_ServerStream complete"); return(responses); }
/// <summary> /// /// </summary> /// <returns></returns> private PlayerSearchRequest CreatePlayerSearchRequest() { PlayerSearchRequest request = new PlayerSearchRequest(); request.PlayerName = ""; request.TeamName = ""; request.Position = ""; request.ExpectedDataCount = GetExpectedDataCount(); return(request); }
/// <summary> /// SearchPlayer_ServerStream /// </summary> public async override Task SearchPlayer_ServerStream(PlayerSearchRequest request, IServerStreamWriter <PlayerSearchResponse> responseStream, ServerCallContext context) { logger.Info(string.Format("[{0}] Requested {1} players.", request.PlayerName, request.ExpectedDataCount)); PlayerSearchResponse response = new PlayerSearchResponse(); int fetchCount = 0; var Players = new List <Player>() { new Player { Age = 1, Name = "11", TeamCode = "11", }, new Player { Age = 2, Name = "22", TeamCode = "22", }, new Player { Age = 3, Name = "33", TeamCode = "33", } }; foreach (Player player in Players) { response.Players.Add(player); ++fetchCount; if (fetchCount == s_FetchSize) { await Task.Delay(s_DelayMilliseconds).ConfigureAwait(false); await responseStream.WriteAsync(response).ConfigureAwait(false); response.Players.Clear(); response.Teams.Clear(); fetchCount = 0; } } if (response.Players.Count > 0) { await Task.Delay(s_DelayMilliseconds).ConfigureAwait(false); await responseStream.WriteAsync(response).ConfigureAwait(false); } }
/// <summary> /// SearchPlayer_ClientStream /// </summary> public async override Task <PlayerSearchResponse> SearchPlayer_ClientStream(IAsyncStreamReader <PlayerSearchRequest> requestStream, ServerCallContext context) { PlayerSearchResponse response = new PlayerSearchResponse(); int initial = 1; while (await requestStream.MoveNext().ConfigureAwait(false)) { PlayerSearchRequest request = requestStream.Current; logger.Info(string.Format("[{0}] Requested {1} players.", "", request.ExpectedDataCount)); var Players = new List <Player>() { new Player { Age = 1, Name = "11", TeamCode = "11", }, new Player { Age = 2, Name = "22", TeamCode = "22", }, new Player { Age = 3, Name = "33", TeamCode = "33", } }; foreach (Player player in Players) { response.Players.Add(player); if (!response.Teams.ContainsKey(player.TeamCode)) { response.Teams.Add(player.TeamCode, new Team { Code = player.TeamCode, Country = player.TeamCode, Name = player.TeamCode }); } } initial += request.ExpectedDataCount; } return(response); }
/// <summary> /// /// </summary> /// <param name="request"></param> /// <param name="responseStream"></param> /// <param name="context"></param> /// <returns></returns> public async override Task SearchPlayer_ServerStream(PlayerSearchRequest request, IServerStreamWriter <PlayerSearchResponse> responseStream, ServerCallContext context) { string callCounter = GetCallCounter(context); s_Logger.Info(string.Format("[{0}] Requested {1} players.", callCounter, request.ExpectedDataCount)); IDictionary <string, Team> teams = SampleDataRepository.GetTeams(10); PlayerSearchResponse response = new PlayerSearchResponse(); int fetchCount = 0; foreach (Player player in SampleDataRepository.GeneratePlayers(1, request.ExpectedDataCount, teams)) { if (ExitIfRequestedCancel(context)) { return; } response.Players.Add(player); if (!response.Teams.ContainsKey(player.TeamCode)) { response.Teams.Add(player.TeamCode, teams[player.TeamCode]); } ++fetchCount; if (fetchCount == s_FetchSize) { await Task.Delay(s_DelayMilliseconds).ConfigureAwait(false); await responseStream.WriteAsync(response).ConfigureAwait(false); response.Players.Clear(); response.Teams.Clear(); fetchCount = 0; } } if (response.Players.Count > 0) { await Task.Delay(s_DelayMilliseconds).ConfigureAwait(false); await responseStream.WriteAsync(response).ConfigureAwait(false); } }
public async Task <List <Player> > FindPlayers(PlayerSearchRequest request) { if (request == null) { throw new ArgumentNullException(nameof(request)); } List <Player> players = new List <Player>(); if (!string.IsNullOrEmpty(request.PlayerName)) { var playerEntities = await _player.FindPlayersByNameAsync(request.PlayerName); players.AddRange(playerEntities); } else if (!string.IsNullOrEmpty(request.PlayerCode)) { var ply = await _player.GetPlayerAsync(request.PlayerCode); if (ply != null) { players.Add(ply); } } foreach (var player in players) { //Calculate advanced batting metrics player.AdvancedBattingStatistics = player.BattingStatistics.Select(a => new PlayerAdvancedBattingStatistics { PlayerIdentifier = a.PlayerIdentifier, TeamIdentifier = a.TeamIdentifier, TeamName = a.TeamName, TeamYear = a.TeamYear, BattingAverage = a.BattingAverage, BattingAvgOfBallsInPlay = BaseballStatisticCalculation.GetBattingAvgOfBallsInPlay(a.AtBats, a.Hits, a.Homeruns, a.Strikeouts, a.SacrificeFlies), ExtraBaseHitPercentage = BaseballStatisticCalculation.GetRatio(a.Doubles + a.Triples + a.Homeruns, a.PlateAppearances), HomeRunPercentage = BaseballStatisticCalculation.GetRatio(a.Homeruns, a.PlateAppearances), InPlayPercentage = BaseballStatisticCalculation.GetRatio(a.AtBats - a.Strikeouts - a.Homeruns - a.SacrificeFlies, a.PlateAppearances), OnBasePercentage = BaseballStatisticCalculation.GetRatio(a.Hits + a.Walks + a.HitByPitch, a.AtBats + a.Walks + a.HitByPitch + a.SacrificeFlies), SluggingPercentage = BaseballStatisticCalculation.GetRatio(a.Hits + (2 * a.Doubles) + (3 * a.Triples) + (4 * a.Homeruns), a.AtBats), StrikeoutPercentage = BaseballStatisticCalculation.GetRatio(a.Strikeouts, a.PlateAppearances), WalkPercentage = BaseballStatisticCalculation.GetRatio(a.Walks, a.PlateAppearances) }).ToList(); } return(players); }
/// <summary> /// /// </summary> private async Task SearchPlayerDuplexStreamAsync() { PlayerSearchRequest request = CreatePlayerSearchRequest(); CallOptions callOptions = CreateCallOptions(); if (this.UseHttpGateway) { IList <PlayerSearchResponse> responses = SearchPlayerDuplexStreamUseHttp(GetPlayerSearchRequests(), callOptions); for (int i = 0; i < responses.Count; ++i) { if (i == 0) { ShowPlayers(responses[i].Players, responses[i].Teams); } else { AppendPlayers(responses[i].Players, responses[i].Teams); } } } else { using (var call = m_Client.SearchPlayer_DuplexStream(callOptions)) { bool firstTime = true; await call.WriteAndForEachAsync(GetPlayerSearchRequestsAsync(), delegate(PlayerSearchResponse response) { if (firstTime) { firstTime = false; ShowPlayers(response.Players, response.Teams); } else { AppendPlayers(response.Players, response.Teams); } }); } } }
/// <summary> /// /// </summary> /// <param name="request"></param> /// <param name="responseStream"></param> /// <param name="context"></param> /// <returns></returns> public async override Task PushPlayer(PlayerSearchRequest request, IServerStreamWriter <PlayerSearchResponse> responseStream, ServerCallContext context) { string callCounter = GetCallCounter(context); s_Logger.Info(string.Format("[{0}] Requested {1} players.", callCounter, request.ExpectedDataCount)); IDictionary <string, Team> teams = SampleDataRepository.GetTeams(10); PlayerSearchResponse response = new PlayerSearchResponse(); Random r = new Random(); while (true) { await Task.Delay(r.Next(5, 20) * 1000); if (ExitIfRequestedCancel(context)) { return; } foreach (Player player in SampleDataRepository.GeneratePlayers(1, request.ExpectedDataCount, teams)) { response.Players.Add(player); if (!response.Teams.ContainsKey(player.TeamCode)) { response.Teams.Add(player.TeamCode, teams[player.TeamCode]); } } if (response.Players.Count > 0) { await Task.Delay(s_DelayMilliseconds).ConfigureAwait(false); await responseStream.WriteAsync(response).ConfigureAwait(false); } } }
/// <summary> /// SearchPlayer_DuplexStream /// </summary> public async override Task SearchPlayer_DuplexStream(IAsyncStreamReader <PlayerSearchRequest> requestStream, IServerStreamWriter <PlayerSearchResponse> responseStream, ServerCallContext context) { int initial = 1; while (await requestStream.MoveNext().ConfigureAwait(false)) { PlayerSearchResponse response = new PlayerSearchResponse(); PlayerSearchRequest request = requestStream.Current; logger.Info(string.Format("[{0}] Requested {1} players.", "", request.ExpectedDataCount)); int fetchCount = 0; var Players = new List <Player>() { new Player { Age = 1, Name = "11", TeamCode = "11", }, new Player { Age = 2, Name = "22", TeamCode = "22", }, new Player { Age = 3, Name = "33", TeamCode = "33", } }; foreach (Player player in Players) { response.Players.Add(player); if (!response.Teams.ContainsKey(player.TeamCode)) { response.Teams.Add(player.TeamCode, new Team { Code = player.TeamCode, Country = player.TeamCode, Name = player.TeamCode }); } ++fetchCount; if (fetchCount == s_FetchSize) { await Task.Delay(s_DelayMilliseconds).ConfigureAwait(false); await responseStream.WriteAsync(response).ConfigureAwait(false); response.Players.Clear(); response.Teams.Clear(); fetchCount = 0; } } if (response.Players.Count > 0) { await Task.Delay(s_DelayMilliseconds).ConfigureAwait(false); await responseStream.WriteAsync(response).ConfigureAwait(false); } initial += request.ExpectedDataCount; } }
/// <summary> /// /// </summary> private async Task PushPlayerAsync() { await Task.Yield(); if (this.UseHttpGateway) { return; } if (m_Observable != null) { IObserver <PlayerSearchResponse> observer = GrpcObservable.CreateObserver <PlayerSearchResponse>( response => System.Diagnostics.Debug.WriteLine(string.Format("IObserver: Received {0} requests.", response.Players.Count)) ); m_Observable.Subscribe(observer); } else { PlayerSearchRequest request = CreatePlayerSearchRequest(); CallOptions callOptions = CreateCallOptions(); Task nowait = Task.Run(async() => { IDisposable subscriver = null; try { using (GrpcServerStreamingObservable <PlayerSearchResponse> observable = m_Client.PushPlayer(request, callOptions).ToObservable()) { m_Observable = observable; IObserver <PlayerSearchResponse> observer = GrpcObservable.CreateObserver <PlayerSearchResponse>( response => ShowPlayers(response.Players, response.Teams) , ex => System.Diagnostics.Debug.WriteLine("IObserver:" + ex.Message) , () => System.Diagnostics.Debug.WriteLine("IObserver: completed") ); subscriver = observable.Subscribe(observer); m_Subscriver = subscriver; await observable.ObserveAsync().ConfigureAwait(false); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("IObservable: " + ex.Message); } finally { if (subscriver != null) { subscriver.Dispose(); } } } ); } //Task wait = Task.Run(async () => //{ // using (var call = m_Client.PushPlayer(request, callOptions)) // { // while (await call.ResponseStream.MoveNext().ConfigureAwait(false)) // { // ShowPlayers(call.ResponseStream.Current.Players, call.ResponseStream.Current.Teams); // throw new Exception("!"); // } // } //} //); }