private AircraftListJson BuildAircraftList(AircraftListJsonBuilderArgs builderArgs) { if (builderArgs.IsFlightSimulatorList) { builderArgs.AircraftList = Factory.Singleton.ResolveSingleton <IFlightSimulatorAircraftList>(); builderArgs.SourceFeedId = -1; } builderArgs.IsInternetClient = PipelineRequest.IsInternet; //TODO: Stop creating builders, share one between all instances of the controller var builder = Factory.Singleton.Resolve <IAircraftListJsonBuilder>(); builder.Initialise(new WebSiteProvider()); var result = builder.Build(builderArgs); var sharedConfiguration = Factory.Singleton.ResolveSingleton <ISharedConfiguration>(); var configLastChanged = JavascriptHelper.ToJavascriptTicks(sharedConfiguration.GetConfigurationChangedUtc()); if (configLastChanged > builderArgs.ServerTimeTicks) { result.ServerConfigChanged = true; } return(result); }
private void PreviousAircraftFromPostBody(AircraftListJsonBuilderArgs args) { var bodyStream = PipelineContext.Request.Body; if (bodyStream != null && bodyStream != Stream.Null) { using (var reader = new StreamReader(bodyStream)) { var content = reader.ReadToEnd()?.Trim(); if (!String.IsNullOrEmpty(content)) { var icaosIdx = content.IndexOf("icaos=", StringComparison.OrdinalIgnoreCase); var icaos = icaosIdx == -1 ? "" : content.Substring(icaosIdx + 6).Trim(); if (!String.IsNullOrEmpty(icaos)) { foreach (var icao in icaos.Split('-')) { var id = CustomConvert.Icao24(icao); if (id != -1) { args.PreviousAircraft.Add(id); } } } } } } }
private AircraftListJson BuildAircraftList(AircraftListJsonBuilderArgs builderArgs) { if (builderArgs.IsFlightSimulatorList) { builderArgs.AircraftList = Factory.ResolveSingleton <IFlightSimulatorAircraftList>(); builderArgs.SourceFeedId = -1; } builderArgs.IsInternetClient = PipelineRequest.IsInternet; //TODO: Stop creating builders, share one between all instances of the controller var builder = Factory.Resolve <IAircraftListJsonBuilder>(); var result = builder.Build(builderArgs, ignoreInvisibleFeeds: true, fallbackToDefaultFeed: true); var sharedConfiguration = Factory.ResolveSingleton <ISharedConfiguration>(); var tileServerSettingsManager = Factory.ResolveSingleton <ITileServerSettingsManager>(); var latestConfiguration = sharedConfiguration.GetConfigurationChangedUtc(); if (tileServerSettingsManager.LastDownloadUtc > latestConfiguration) { latestConfiguration = tileServerSettingsManager.LastDownloadUtc; } var configLastChanged = JavascriptHelper.ToJavascriptTicks(latestConfiguration); if (configLastChanged > builderArgs.ServerTimeTicks) { result.ServerConfigChanged = true; } return(result); }
/// <summary> /// Sends the aircraft list to the connection passed across. /// </summary> /// <param name="connection">Null the established connection should be used, otherwise the new connection /// that the connector has created.</param> private void SendAircraftList(IConnection connection) { var feed = RebroadcastServer.Feed; var aircraftList = feed == null ? null : feed.AircraftList; var connector = RebroadcastServer.Connector; var isNewConnection = connection != null; if (aircraftList != null && RebroadcastServer.Online && connector != null && (connector.HasConnection || isNewConnection)) { long timestamp, dataVersion; var snapshot = aircraftList.TakeSnapshot(out timestamp, out dataVersion); var args = new AircraftListJsonBuilderArgs() { AlwaysShowIcao = true, FeedsNotRequired = true, IgnoreUnchanged = true, OnlyIncludeMessageFields = true, SourceFeedId = feed.UniqueId, PreviousDataVersion = isNewConnection ? -1 : _PreviousDataVersion, }; if (!isNewConnection) { if (_PreviousAircraftList != null) { args.PreviousAircraft.AddRange(_PreviousAircraftList); } _PreviousAircraftList = snapshot.Select(r => r.UniqueId).ToArray(); _PreviousDataVersion = dataVersion; } var json = _AircraftListJsonBuilder.Build(args, ignoreInvisibleFeeds: false, fallbackToDefaultFeed: false); if (json.Aircraft.Count > 0) { var jsonText = JsonConvert.SerializeObject(json, _AircraftListJsonSerialiserSettings); // The json text can include some entries that have ICAO codes and nothing else. When I // first wrote this I was taking them out at this point... but that is a mistake, if the // aircraft is sending messages between refreshes but not changing any message values (e.g. // test beacons that transmit a constant callsign, altitude etc. then by removing their // entry here we make the receiving end think that they've gone off the radar. We need to // send ICAOs for aircraft that aren't changing message values. var bytes = Encoding.UTF8.GetBytes(jsonText); if (connection != null) { connection.Write(bytes); } else { connector.Write(bytes); } } } }
private void PreviousAircraftFromModel(AircraftListJsonBuilderArgs args, GetAircraftListModel model) { if(model != null && model.PreviousAircraft != null && model.PreviousAircraft.Count > 0) { foreach(var icao in model.PreviousAircraft) { var id = CustomConvert.Icao24(icao); if(id != -1) { args.PreviousAircraft.Add(id); } } } }
private void SortByFromModel(AircraftListJsonBuilderArgs args, GetAircraftListModel model) { if(model == null || model.SortBy == null || model.SortBy.Count == 0) { SetDefaultAircraftListSortBy(args); } else { for(var i = 0;i < Math.Min(2, model.SortBy.Count);++i) { var sortBy = model.SortBy[i]; args.SortBy.Add(new KeyValuePair<string, bool>(sortBy.Col, sortBy.Asc)); } } }
private void PreviousAircraftFromHeader(AircraftListJsonBuilderArgs args) { var previousAircraftIds = PipelineRequest.Headers["X-VirtualRadarServer-AircraftIds"]; if(!String.IsNullOrEmpty(previousAircraftIds)) { var decodedPreviousAircraftIds = HttpUtility.UrlDecode(previousAircraftIds); foreach(var chunk in decodedPreviousAircraftIds.Split(',')) { if(int.TryParse(chunk, out var id)) { args.PreviousAircraft.Add(id); } } } }
public void TestInitialise() { _OriginalFactory = Factory.TakeSnapshot(); _Clock = new ClockMock(); _Timer = TestUtilities.CreateMockImplementation <ITimer>(); _Compressor = TestUtilities.CreateMockImplementation <IBaseStationMessageCompressor>(); _Server = Factory.Resolve <IRebroadcastServer>(); _Feed = TestUtilities.CreateMockInstance <IFeed>(); _Listener = TestUtilities.CreateMockInstance <IListener>(); _Feed.SetupGet(r => r.Listener).Returns(_Listener.Object); _Connector = new MockConnector <INetworkConnector, INetworkConnection>(); _AircraftListJsonBuilder = TestUtilities.CreateMockImplementation <IAircraftListJsonBuilder>(); _AircraftListJson = new AircraftListJson(); _AircraftListJsonBuilderArgs = null; _IgnoreInvisibleFeeds = null; _FallbackToDefault = null; _AircraftListJsonBuilder.Setup(r => r.Build(It.IsAny <AircraftListJsonBuilderArgs>(), It.IsAny <bool>(), It.IsAny <bool>())).Callback((AircraftListJsonBuilderArgs args, bool ignoreInvisibleFeeds, bool fallbackToDefault) => { _AircraftListJsonBuilderArgs = args; _IgnoreInvisibleFeeds = ignoreInvisibleFeeds; _FallbackToDefault = fallbackToDefault; }).Returns(_AircraftListJson); _AircraftList = TestUtilities.CreateMockImplementation <IBaseStationAircraftList>(); _Feed.SetupGet(r => r.AircraftList).Returns(_AircraftList.Object); _SnapshotAircraft = new List <IAircraft>(); long of1, of2; _AircraftList.Setup(m => m.TakeSnapshot(out of1, out of2)).Returns(_SnapshotAircraft); _Server.UniqueId = 1; _Server.Name = "It's the code word"; _Server.Format = RebroadcastFormat.Port30003; _Server.Feed = _Feed.Object; _Server.Connector = _Connector.Object; _ExceptionCaughtEvent = new EventRecorder <EventArgs <Exception> >(); _ExceptionCaughtEvent.EventRaised += DefaultExceptionCaughtHandler; _OnlineChangedEvent = new EventRecorder <EventArgs>(); _Server.ExceptionCaught += _ExceptionCaughtEvent.Handler; _Server.OnlineChanged += _OnlineChangedEvent.Handler; _Port30003Message = new BaseStationMessage() { Icao24 = "313233" }; }
private void ExtractPreviousAircraftFromBody(AircraftListJsonBuilderArgs args, string icaos) { if (!String.IsNullOrEmpty(icaos)) { foreach (var icao in icaos.Split('-')) { var id = CustomConvert.Icao24(icao); if (id != -1) { args.PreviousAircraft.Add(id); } } } }
private void SortByFromQueryString(AircraftListJsonBuilderArgs args) { var query = PipelineContext.Request.Query; var sortBy1 = (query["sortBy1"] ?? "").ToUpper(); if(sortBy1 == "") { SetDefaultAircraftListSortBy(args); } else { var sortOrder1 = (query["sortOrder1"] ?? "").ToUpper(); args.SortBy.Add(new KeyValuePair<string, bool>(sortBy1, sortOrder1 == "ASC")); var sortBy2 = (query["sortBy2"] ?? "").ToUpper(); if(sortBy2 != "") { var sortOrder2 = (query["sortOrder2"] ?? "").ToUpper(); args.SortBy.Add(new KeyValuePair<string, bool>(sortBy2, sortOrder2 == "ASC")); } } }
public AircraftListJson AircraftList(GetAircraftListModel model, int feedId = -1) { var args = new AircraftListJsonBuilderArgs() { BrowserLatitude = model?.Latitude, BrowserLongitude = model?.Longitude, IsFlightSimulatorList = model?.FlightSimulator ?? false, PreviousDataVersion = model?.LastDataVersion ?? -1L, ResendTrails = model?.ResendTrails ?? false, SelectedAircraftId = model?.SelectedAircraft ?? -1, ServerTimeTicks = model?.ServerTicks ?? -1L, SourceFeedId = feedId, Filter = ExpandModelFilters(model?.Filters), TrailType = model?.TrailType ?? TrailType.None, }; SortByFromModel(args, model); PreviousAircraftFromModel(args, model); return BuildAircraftList(args); }
[Route("FlightSimList.json")] // pre-version 3 route public AircraftListJson AircraftListV2Post([FromUri] int feed = -1, double? lat = null, double? lng = null, long ldv = -1, long stm = -1, byte refreshTrails = 0, int selAc = -1, string trFmt = null) { var isFlightSimList = RequestContext.RouteData.Route.RouteTemplate == "FlightSimList.json"; var args = new AircraftListJsonBuilderArgs() { BrowserLatitude = lat, BrowserLongitude = lng, IsFlightSimulatorList = isFlightSimList, PreviousDataVersion = ldv, ResendTrails = refreshTrails == 1, SelectedAircraftId = selAc, ServerTimeTicks = stm, SourceFeedId = feed, Filter = AircraftListJsonBuilderFilterFromQueryString(), TrailType = TrailTypeFromCode(trFmt), }; SortByFromQueryString(args); PreviousAircraftFromPostBody(args); return BuildAircraftList(args); }
public void AircraftListJsonBuilderArgs_Constructor_Initialses_Properties_To_Known_Values() { var args = new AircraftListJsonBuilderArgs(); Assert.AreEqual(0, args.PreviousAircraft.Count); Assert.AreEqual(0, args.SortBy.Count); TestUtilities.TestProperty(args, r => r.AircraftList, null, TestUtilities.CreateMockInstance <IAircraftList>().Object); TestUtilities.TestProperty(args, r => r.AlwaysShowIcao, false); TestUtilities.TestProperty(args, r => r.BrowserLatitude, null, 1.1); TestUtilities.TestProperty(args, r => r.BrowserLongitude, null, 2.2); TestUtilities.TestProperty(args, r => r.FeedsNotRequired, false); TestUtilities.TestProperty(args, r => r.Filter, null, new AircraftListJsonBuilderFilter()); TestUtilities.TestProperty(args, r => r.IgnoreUnchanged, false); TestUtilities.TestProperty(args, r => r.IsFlightSimulatorList, false); TestUtilities.TestProperty(args, r => r.IsInternetClient, false); TestUtilities.TestProperty(args, r => r.OnlyIncludeMessageFields, false); TestUtilities.TestProperty(args, r => r.PreviousDataVersion, -1L, 1L); TestUtilities.TestProperty(args, r => r.ResendTrails, false); TestUtilities.TestProperty(args, r => r.SelectedAircraftId, -1, 1); TestUtilities.TestProperty(args, r => r.SourceFeedId, -1, 2); TestUtilities.TestProperty(args, r => r.TrailType, TrailType.None, TrailType.Full); }
private static void SetDefaultAircraftListSortBy(AircraftListJsonBuilderArgs args) { args.SortBy.Add(new KeyValuePair <string, bool>(AircraftComparerColumn.FirstSeen, false)); }