public Task AddFileRequestEventAsync(string sourceIp, string filename) { TvItemFile itemFile = _tvItemService.FetchTvItemFileAsync(filename).Result; if (itemFile != null) { WebClientLog wcl = new WebClientLog() { Source = sourceIp, TimeInserted = DateTime.Now, TvItemFileId = itemFile.ID, TvScreenId = KnownTvScreens.FirstOrDefault(x => x.IpAddress.Equals(sourceIp))?.ID, Type = itemFile.IsVideoFile() ? WebClientLogType.VideoRequest : WebClientLogType.ImageRequest }; Context.Add(wcl); Context.SaveChanges(); Console.WriteLine($"adding file request from ip {sourceIp} for file: " + wcl); } else { WebClientLog wcl = new WebClientLog() { Source = sourceIp, TimeInserted = DateTime.Now, Message = $"file with filename [{filename}] not found!", Type = WebClientLogType.Exception }; Context.Add(wcl); Context.SaveChanges(); Console.WriteLine("file not found on file request: " + wcl); } return(Task.CompletedTask); }
public Task AddHandShakeAsync(string sourceIp, WebClientLogType type, Location location) { if (!type.Equals(WebClientLogType.ProgramRequest) && !type.Equals(WebClientLogType.ChatRequest)) { throw new InvalidOperationException($"{type.ToString()} is not allowed type. Allowed types: [ProgramRequest, ChatRequest]"); } DateTime now = DateTime.Now; // find/add known screen according to ip and update lastHandshake TvScreen knownScreen = KnownTvScreens.FirstOrDefault(x => x.IpAddress.Equals(sourceIp)); if (knownScreen == null) { knownScreen = new TvScreen() { Location = location, LastHandshake = now, IpAddress = sourceIp, Description = "located at " + location }; Console.WriteLine("adding new known screen: " + knownScreen); Context.Add(knownScreen); } else { knownScreen.LastHandshake = now; if (knownScreen.Location != location) { // very rare and improbable case Console.WriteLine($"CHANGING LOCATION FOR {knownScreen.IpAddress} FROM {knownScreen.Location} TO {location}."); knownScreen.Location = location; } } var activeScreens = Context.TvScreenHandshake .Where(x => x.IsActive) .ToList(); // from active screens inactivate timeouted ones var timeoutedScreens = activeScreens.Where(x => (now - x.LastHandshake).TotalMinutes > 10).ToList(); var timeoutIds = timeoutedScreens.Select(x => x.ID).ToList(); activeScreens = activeScreens.Where(x => !timeoutIds.Contains(x.ID)).ToList(); if (timeoutedScreens.Count > 0) { // inactivate all timeouted screens Console.WriteLine("inactivating clientHandshakes with ids: " + string.Join(",", timeoutIds)); timeoutedScreens.ForEach(x => { x.IsActive = false; Context.Update(x); }); } // from active screens find the one with the same ip and location, if not present, create new active screen TvScreenHandshake tvScreenHandshake = activeScreens.FirstOrDefault(x => x.TvScreen.Equals(knownScreen) && x.Type.Equals(type)); if (tvScreenHandshake == null) { TvScreenHandshake tsh = new TvScreenHandshake() { FirstHandshake = now, LastHandshake = now, IsActive = true, TvScreen = knownScreen, Type = type }; Console.WriteLine("adding new active screen: " + tsh); Context.Add(tsh); } else { tvScreenHandshake.LastHandshake = now; Context.Update(tvScreenHandshake); } Context.SaveChanges(); return(Task.CompletedTask); }