public Receiver(Port source, Timer timer, string portString) { this.source = source; this.timer = timer; string[] details = portString.Split(':'); IEnumerable<Stream> streams = GetStreams(source, details); IEnumerable<Stream> timeStreams = from stream in streams where stream.Name == "TIME" || stream.Name == "TIMER" select stream; if (timeStreams.Count() > 1) throw new ArgumentException(string.Format("More than one timestamp stream was found in '{0}'.", source.Name)); this.timeStream = timeStreams.SingleOrDefault(); this.hasTimer = timeStream != null && timeStream.Name == "TIMER"; this.portStreams = streams.Except(Enumerables.Create(timeStream)).ToArray(); // TODO: Try and make this stateless if (HasTimer) timer.IsUpdated = false; this.reader = new Thread(Read); this.reader.Priority = ThreadPriority.AboveNormal; this.reader.Start(); }
static IEnumerable<Stream> ParseRange(Port port, string range) { string[] details = range.Split('-'); switch (details.Length) { case 1: return Enumerables.Create(ParseStream(port, details[0])); case 2: return from path in Path.Range(new Path(details[0]), new Path(details[1])) select new Stream(path); default: throw new ArgumentException("range"); } }
static Stream ParseStream(Port port, string streamString) { string[] details = streamString.Split('='); switch (details.Length) { case 1: Path path = new Path(details[0]); string name = port.GetName(path); return name == null ? new Stream(path) : new Stream(path, name); case 2: return new Stream(new Path(details[0]), details[1]); default: throw new ArgumentException("streamString"); } }
static IEnumerable<Stream> GetStreams(Port port, string[] portString) { switch (portString.Length) { case 1: return ( from path in port.ValidPaths let name = port.GetName(path) select name == null ? new Stream(path) : new Stream(path, name) ) .ToArray(); case 2: return ( from range in portString[1].Split(',') from stream in ParseRange(port, range) select stream ) .ToArray(); default: throw new ArgumentException("portString"); } }