private IStreamInput ReadAndLinkOutput( string streamId, List <string> parameterIdentifiers, Writer writer, List <string> outputFeedNames, TelemetryDataHandler handler) { var input = new SessionTelemetryDataInput(streamId, DataFormatClient); Debug.WriteLine($"Linking stream {streamId}"); // automatically propagate session metadata and lifecycle input.LinkToOutput(writer.SessionOutput, identifier => identifier + "_" + writer.TopicName); // react to data input.DataInput.BindDefaultFeed(parameterIdentifiers).DataBuffered += (sender, e) => { var data = e.Buffer.GetData(); handler(data); foreach (var outputFeedName in outputFeedNames) { writer.Write(outputFeedName, data); } }; input.StreamFinished += HandleStreamFinished; return(input); }
/// <summary> /// Read telemetry data from the stream for the given dataformat parameter and handle the data by the /// TelemetryDataHandler delegate /// </summary> /// <param name="parameterIdentifier">The dataformat parameter identifier</param> /// <param name="handler">Use this for the streamed data handling, updating etc.</param> /// <returns>Pipeline representing the network resource streaming messages to the inputs.</returns> public IStreamPipeline ReadTData(string parameterIdentifier, TelemetryDataHandler handler) { var pipeline = StreamPipelineBuilder.Into(streamId => Read(streamId, parameterIdentifier, handler)); if (!pipeline.WaitUntilConnected(connectionTimeoutInSeconds, CancellationToken.None)) { throw new Exception("Couldn't connect"); } return(pipeline); }
/// <summary> /// Read telemetry data from the stream for the given dataformat parameter and handle the data by the /// TelemetryDataHandler delegate /// and link to another output, using the <paramref name="writer" /> and the <paramref name="outputFeedNames" /> /// </summary> /// <param name="parameterIdentifiers">List of dataformat parameter identifiers</param> /// <param name="handler">Use this for the streamed data handling, updating etc.</param> /// <param name="writer">Writer object used for writing the streamed input to another output.</param> /// <param name="outputFeedNames"> /// List of output feed names where the streamed input will be linked to, using the /// <paramref name="writer" /> /// </param> /// <returns>Pipeline representing the network resource streaming messages to the inputs.</returns> public IStreamPipeline ReadAndLinkTData(List <string> parameterIdentifiers, TelemetryDataHandler handler, Writer writer, List <string> outputFeedNames) { var pipeline = StreamPipelineBuilder.Into( streamId => ReadAndLinkOutput(streamId, parameterIdentifiers, writer, outputFeedNames, handler)); if (!pipeline.WaitUntilConnected(connectionTimeoutInSeconds, CancellationToken.None)) { throw new Exception("Couldn't connect"); } return(pipeline); }
private IStreamInput Read( string streamId, string parameterIdentifier, TelemetryDataHandler handler) { var input = new SessionTelemetryDataInput(streamId, DataFormatClient); input.DataInput.BindDefaultFeed(parameterIdentifier).DataBuffered += (sender, e) => { handler(e.Buffer.GetData()); }; input.StreamFinished += HandleStreamFinished; return(input); }
private IStreamInput ReadTData( string streamId, List <string> parameterIdentifiers, TelemetryDataHandler handler) { var input = new SessionTelemetryDataInput(streamId, DataFormatClient); input.DataInput.BindDefaultFeed(parameterIdentifiers).DataBuffered += (sender, e) => //Subscribing to data event { handler(e.Buffer.GetData()); }; input.StreamFinished += HandleStreamFinished; return(input); }
private void Initialise() { _webSocketServer = new WebSocketSharp.Server.HttpServer(_port); _webSocketServer.RootPath = _rootDirectory + "\\"; _webSocketServer.AddWebSocketService <TelemetryService>("/Telemetry", () => new TelemetryService(_telemetryData)); _webSocketServer.AddWebSocketService <HeartBeatService>("/HeartBeat", () => new HeartBeatService(_telemetryData)); _webSocketServer.AddWebSocketService <TimingService>("/Timing", () => new TimingService(_timingData)); _webSocketServer.Start(); _webSocketServer.OnPost += (sender, e) => { lock (lockObject) { var request = e.Request; var response = e.Response; DataHandlerResult result = null; var uri = request.Url; if (uri.Segments.Length == 2) { var action = uri.Segments[1]; string postedText = ""; using (var reader = new StreamReader(request.InputStream, request.ContentEncoding)) { postedText = reader.ReadToEnd(); } NameValueCollection postData = HttpUtility.ParseQueryString(postedText); switch (action) { case "File": result = ActionsDataHandler.ProcessFileRequest(_telemetryData, _plugins, postData); break; case "Track": result = TrackDataHandler.ProcessRequest(_telemetryData, _plugins, postData); break; case "TrackRecorder": result = TrackDataHandler.ProcessRequest(_telemetryData, _plugins, postData); break; case "Connected": result = ConnectedDataHandler.ProcessConnectedRequest(_telemetryData, postData); break; case "HeartBeat": result = HeartBeatDataHandler.ProcessConnectedRequest(_telemetryData, postData); break; case "Telemetry": result = TelemetryDataHandler.ProcessConnectedRequest(_telemetryData, postData); break; case "Timing": result = TimingDataHandler.ProcessConnectedRequest(_timingData, postData); break; case "Startup": result = StartupDataHandler.ProcessStartupRequest(_telemetryData, _plugins, postData); break; } response.StatusCode = (int)HttpStatusCode.OK; } else { result = new DataHandlerError(); response.StatusCode = (int)HttpStatusCode.NotFound; } string json = new JavaScriptSerializer().Serialize(result); response.ContentType = "application/json"; byte[] content = Encoding.UTF8.GetBytes(json); response.ContentLength64 = content.Length; System.IO.Stream output = response.OutputStream; output.Write(content, 0, content.Length); result.Dispose(); } }; _webSocketServer.OnGet += (sender, e) => { lock (lockObject) { var request = e.Request; var response = e.Response; DataHandlerResult result = null; string[] rawUrlArray = request.RawUrl.Split('?'); string rawUrl = rawUrlArray[0]; _httpServerPath = rawUrl; if (string.IsNullOrEmpty(_httpServerPath) || _httpServerPath == "/") { foreach (string indexFile in indexFiles) { if (File.Exists(Path.Combine(_rootDirectory, indexFile))) { _httpServerPath = indexFile; break; } } } byte[] content = _webSocketServer.GetFile(_httpServerPath); if (content == null) { List <string> urlBits = rawUrl.Split('/').ToList().Where(s => !string.IsNullOrWhiteSpace(s)).ToList(); switch (urlBits[0]) { case "Startup": result = StartupDataHandler.ProcessStartupRequest(_telemetryData, _plugins, request.QueryString); break; } string json = new JavaScriptSerializer().Serialize(result); content = Encoding.UTF8.GetBytes(json); } string mime; response.ContentType = mimeTypeMappings.TryGetValue(Path.GetExtension(_httpServerPath), out mime) ? mime : "application/octet-stream"; response.ContentEncoding = Encoding.UTF8; if (content != null) { int start = 0; int end = -1; int byteRange = -1; if (request.Headers.GetValues("Range") != null) { string rangeHeader = request.Headers.GetValues("Range")[0].Replace("bytes=", ""); string[] range = rangeHeader.Split('-'); start = int.Parse(range[0]); if (range[1].Trim().Length > 0) { int.TryParse(range[1], out end); } if (end == -1) { end = content.Length; } byteRange = end - start; response.AddHeader("Accept-Ranges", "bytes"); response.AddHeader("Content-Range", string.Format("bytes {0}-{1}/{2}", start, byteRange - 1, byteRange)); response.AddHeader("X-Content-Duration", "0.0"); response.AddHeader("Content-Duration", "0.0"); response.ContentLength64 = byteRange; response.StatusCode = (int)HttpStatusCode.PartialContent; Console.WriteLine(rangeHeader); Console.WriteLine(string.Format("bytes {0}-{1}/{2}", start, byteRange - 1, byteRange)); Console.WriteLine("Content length" + byteRange.ToString()); var tt = 56; //http://stackoverflow.com/questions/25898922/handle-range-requests-with-httplistener //http://www.stevesouders.com/blog/2013/04/21/html5-video-bytes-on-ios/ } else { // response.AddHeader("cache-control", "no-store, must-revalidate, private"); // response.AddHeader("Pragma", "no-cache"); response.ContentLength64 = content.Length; response.StatusCode = (int)HttpStatusCode.OK; byteRange = content.Length; } System.IO.Stream output = response.OutputStream; output.Write(content, start, byteRange); response = null; } else { response.StatusCode = (int)HttpStatusCode.NotFound; } } }; }