diff --git a/GPIBServer/Configuration.cs b/GPIBServer/Configuration.cs index 9d480b6..e8cdffb 100644 --- a/GPIBServer/Configuration.cs +++ b/GPIBServer/Configuration.cs @@ -27,6 +27,7 @@ public Configuration(bool init) OutputSeparationLabelFormat = "{0}_"; OutputRetries = 3; OutputRetryDelayMilliseconds = 300; + PipeName = "GPIBServer_Broadcast_Pipe"; } public string ScriptsFilter { get; set; } @@ -45,6 +46,7 @@ public Configuration(bool init) public string OutputSeparationLabelFormat { get; set; } public int OutputRetries { get; set; } public int OutputRetryDelayMilliseconds { get; set; } + public string PipeName { get; set; } public string GetFullyQualifiedLogPath() { diff --git a/GPIBServer/Output.cs b/GPIBServer/Output.cs index 5547223..9837122 100644 --- a/GPIBServer/Output.cs +++ b/GPIBServer/Output.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using System.Threading; +using NamedPipeWrapper; namespace GPIBServer { @@ -58,17 +59,36 @@ public static void Dispose() } } - public static void Initialize(string dataPath, string terminalPath, CancellationToken cancel) + public static void Initialize(string dataPath, string terminalPath, string pipeName, CancellationToken cancel) { _Cancel = cancel; TerminalLogPath = terminalPath; DataPath = dataPath; + _PipeQueue = new BlockingCollection>(); + _Pipe = new NamedPipeServer(pipeName); + _Pipe.Start(); + _PipeThread = new Thread(() => + { + try + { + while (!cancel.IsCancellationRequested) + { + var d = _PipeQueue.Take(cancel); + _Pipe.PushMessage(DataConverter(d.Item1, d.Item2)); + } + } + catch (OperationCanceledException) + { } + }); + _PipeThread.Start(); } public static void QueueData(object sender, GpibResponseEventArgs e) { if (!e.Command.OutputResponse) return; CheckInitialization(); + var tuple = new Tuple(sender, e); + _PipeQueue.Add(tuple); string p = Separation switch { OutputSeparation.None => string.Empty, @@ -86,7 +106,7 @@ public static void QueueData(object sender, GpibResponseEventArgs e) _DataWriters.TryAdd(p, t); } } - _DataWriters[p].Queue(new Tuple(sender, e)); + _DataWriters[p].Queue(tuple); } public static void QueueTerminal(object sender, string data) @@ -119,9 +139,18 @@ private static readonly ConcurrentDictionary _DataWrit = new ConcurrentDictionary(); private static readonly ConcurrentDictionary _TerminalWriters = new ConcurrentDictionary(); + private static NamedPipeServer _Pipe; + private static Thread _PipeThread; + private static BlockingCollection> _PipeQueue; private static CancellationToken _Cancel; + private static string DataConverter(object s, GpibResponseEventArgs e) + { + return string.Format(LineFormat, e.TimeReceived, + (s as GpibController).Name, e.Instrument.Name, e.Command.CommandString, e.Response); + } + private static void CheckInitialization() { if (_Cancel == null) throw new InvalidOperationException("Output module not initialized."); @@ -134,9 +163,7 @@ public DataWriterThread(string path, CancellationToken token) : base(path, token protected override string ConvertData(Tuple data) { - return string.Format(LineFormat, data.Item2.TimeReceived, - (data.Item1 as GpibController).Name, data.Item2.Instrument.Name, data.Item2.Command.CommandString, - data.Item2.Response); + return DataConverter(data.Item1, data.Item2); } } diff --git a/GPIBServer/Program.cs b/GPIBServer/Program.cs index 1966b98..25fd036 100644 --- a/GPIBServer/Program.cs +++ b/GPIBServer/Program.cs @@ -140,6 +140,7 @@ private static ExitCodes MainHelper(string[] args) Output.ErrorOccurred += ErrorMessageSink; Output.Initialize(Configuration.Instance.GetFullyQualifiedOutputPath(), Configuration.Instance.GetFullyQualifiedLogPath(), + Configuration.Instance.PipeName, Cancel.Token); Script.ErrorOccured += ErrorMessageSink; Serializer.ErrorOccured += ErrorMessageSink;