private void QueueAndWaitForExecution(CommandModel commandModel, bool waitPublish = false) { var waitEventComplete = new ManualResetEventSlim(); var waitEventPublish = new ManualResetEventSlim(); Mocker.GetMock <IManageCommandQueue>() .Setup(s => s.Complete(It.Is <CommandModel>(c => c == commandModel), It.IsAny <string>())) .Callback(() => waitEventComplete.Set()); Mocker.GetMock <IManageCommandQueue>() .Setup(s => s.Fail(It.Is <CommandModel>(c => c == commandModel), It.IsAny <string>(), It.IsAny <Exception>())) .Callback(() => waitEventComplete.Set()); Mocker.GetMock <IEventAggregator>() .Setup(s => s.PublishEvent <CommandExecutedEvent>(It.IsAny <CommandExecutedEvent>())) .Callback(() => waitEventPublish.Set()); _commandQueue.Add(commandModel); if (!waitEventComplete.Wait(15000)) { Assert.Fail("Command did not Complete/Fail within 15 sec"); } if (waitPublish && !waitEventPublish.Wait(500)) { Assert.Fail("Command did not Publish within 500 msec"); } }
public override void Panic() { lock (_thread_interlock) { CommandQueue.Clear(); CommandQueue.Add(new PanicCommand()); } }
public override void Reset() { lock (_thread_interlock) { CommandQueue.Clear(); CommandQueue.Add(new ReadyCommand()); } }
public override void RequestOutputData(RequestCommand request) { lock (_thread_interlock) { draining_queue = true; CommandQueue.Add(new ET232Command() { Command = CommandByte.read, Address = (AddressByte)request.Address, }); } }
public override void RawWrite(RawCommand raw) { lock (_thread_interlock) { CommandQueue.Add(new ET232Command() { Command = CommandByte.write, Address = (AddressByte)raw.Address, Data = raw.Data, }); } }
private static void InputLoop() { string input; while (true) { input = Console.ReadLine(); if (!string.IsNullOrEmpty(input)) { commandQueue.Add(new Command(input.ToString())); } } }
private void Initialize() { last_command = null; last_command_time = 0; command_retries = 0; lock (_thread_interlock) { _state = State.init; CommandQueue.Clear(); CommandQueue.Add(new ET232Command() { Command = CommandByte.write, Address = AddressByte.InputOverride, Data = ErosTek.ET232.Constants.OverrideAll, Retry = true, }); CommandQueue.Add(new ET232Command() { Command = CommandByte.write, Address = AddressByte.Pot_A, Data = 0, Retry = true, }); CommandQueue.Add(new ET232Command() { Command = CommandByte.write, Address = AddressByte.Pot_B, Data = 0, Retry = true, }); CommandQueue.Add(new ET232Command() { Command = CommandByte.write, Address = AddressByte.ModeOverride, Data = ErosTek.ET232.Constants.ForceMode | (byte)IntenseMode.Mode, Retry = true, }); CommandQueue.Add(new ET232Command() { Command = CommandByte.write, Address = AddressByte.Pot_MA, Data = IntenseMode.StartingMA, Retry = true, }); draining_queue = true; } }
void RenderFrame_Resize(object sender, EventArgs e) { commandQueue.Add( () => { device.ClearState(); foreach (var view in views) { view.Dispose(); } swapChain.ResizeBuffers(BackBufferCount, renderFrame.ClientSize.Width >> 1 << 1, renderFrame.ClientSize.Height >> 1 << 1, BackBufferFormat, SwapChainFlags.None); SetDefaultRenderingBuffer(); SetDefaultViewport(); InitBlending(); }); }
public override void SetMode(ModeCommand mode) { lock (_thread_interlock) { CommandQueue.Add(new ET232Command() { Command = CommandByte.write, Address = AddressByte.ModeOverride, Data = ErosTek.ET232.Constants.ForceMode | (byte)mode.Mode, Retry = true, }); CommandQueue.Add(new ET232Command() { Command = CommandByte.write, Address = AddressByte.Pot_MA, Data = mode.MA, Retry = true, }); draining_queue = true; } }
public override void Reset() { lock (_thread_interlock) { CommandQueue.Clear(); CommandQueue.Add(new ET232Command() { Command = CommandByte.write, Address = AddressByte.Pot_A, Data = 0, Retry = true, }); CommandQueue.Add(new ET232Command() { Command = CommandByte.write, Address = AddressByte.Pot_B, Data = 0, Retry = true, }); CommandQueue.Add(new ET232Command() { Command = CommandByte.write, Address = AddressByte.InputOverride, Data = ErosTek.ET232.Constants.OverrideNone, Retry = true, }); CommandQueue.Add(new ET232Command() { Command = CommandByte.write, Address = AddressByte.ModeOverride, Data = ErosTek.ET232.Constants.ForceReset, Retry = true, }); draining_queue = true; } }
public override void ThreadFunction() { ErrorCallback(this, "Network processing startup.", "", false); bool should_exit = false; byte[] remote_bytes = Array.Empty <byte>(); NetworkHeader read_header = null; Utf8JsonWriter jsonwriter; lock (_thread_interlock) { jsonwriter = new Utf8JsonWriter(DeviceStream); CommandQueue.Clear(); } byte[] read_buffer = new byte[block_size]; void DataReader() { try { lock (_thread_interlock) DeviceStream.BeginRead(read_buffer, 0, read_buffer.Length, delegate(IAsyncResult ar) { try { lock (_thread_interlock) { if (DeviceStream == null) { return; } int len = DeviceStream.EndRead(ar); int pos = remote_bytes.Length; Array.Resize(ref remote_bytes, pos + len); Buffer.BlockCopy(read_buffer, 0, remote_bytes, pos, len); } } catch (Exception e) { bool exiting; lock (_thread_interlock) exiting = _thread_end; if (!exiting) { ErrorCallback(this, "Network error.", e.Message, false); } } DataReader(); }, null); } catch (Exception e) { bool exiting; lock (_thread_interlock) exiting = _thread_end; if (!exiting) { ErrorCallback(this, "Network error.", e.Message, false); } } } DataReader(); while (!should_exit) { Thread.Yield(); long consumed = 0; if (read_header == null) { try { lock (_thread_interlock) { if (remote_bytes.Length > 0) { Utf8JsonReader jsonreader = new Utf8JsonReader(new ReadOnlySpan <byte>(remote_bytes)); read_header = JsonSerializer.Deserialize <NetworkHeader>(ref jsonreader); consumed = jsonreader.BytesConsumed; } } } catch (JsonException) { } } else { CommandBase received_command = null; try { lock (_thread_interlock) { if (remote_bytes.Length > 0) { Utf8JsonReader jsonreader = new Utf8JsonReader(new ReadOnlySpan <byte>(remote_bytes)); switch (read_header.ObjectType) { case TransmissionType.Nothing: break; case TransmissionType.Ready: received_command = JsonSerializer.Deserialize <ReadyCommand>(ref jsonreader); break; case TransmissionType.Panic: received_command = JsonSerializer.Deserialize <PanicCommand>(ref jsonreader); break; case TransmissionType.Levels: received_command = JsonSerializer.Deserialize <LevelsCommand>(ref jsonreader); break; case TransmissionType.Raw: received_command = JsonSerializer.Deserialize <RawCommand>(ref jsonreader); break; case TransmissionType.Mode: received_command = JsonSerializer.Deserialize <ModeCommand>(ref jsonreader); break; case TransmissionType.Response: received_command = JsonSerializer.Deserialize <DeviceResponse>(ref jsonreader); break; case TransmissionType.Request: break; case TransmissionType.Error: received_command = JsonSerializer.Deserialize <ErrorCommand>(ref jsonreader); break; } consumed = jsonreader.BytesConsumed; } } } catch (JsonException) { } if (received_command != null) { switch (read_header.ObjectType) { case TransmissionType.Nothing: break; case TransmissionType.Ready: QueueStateCallback(this, 0); break; case TransmissionType.Panic: MassLevelSetCallback(this, 0, 0, 0, true); ErrorCallback(this, "PANIC!", "Panic received from remote!", true); break; case TransmissionType.Levels: LevelsCommand levels = (LevelsCommand)received_command; LastA = levels.A; LastB = levels.B; MassLevelSetCallback(this, levels.A, levels.B, levels.MA, levels.Mode == ControlMode.absolute); break; case TransmissionType.Raw: RawCommand raw = (RawCommand)received_command; if (raw.Address == (int)AddressByte.Pot_A) { LastA = raw.Data; } if (raw.Address == (int)AddressByte.Pot_B) { LastB = raw.Data; } StateUpdatedCallback(this, raw.Address, raw.Data); break; case TransmissionType.Mode: ModeCommand mode = (ModeCommand)received_command; StateUpdatedCallback(this, (int)AddressByte.ModeOverride, mode.Mode); StateUpdatedCallback(this, (int)AddressByte.Pot_MA, mode.MA); break; case TransmissionType.Request: break; case TransmissionType.Response: DeviceResponse response = (DeviceResponse)received_command; DataReturnedCallback(this, response.Address, response.Data); double current_unixtime = UnixTime.Current(); LevelEvent level = new LevelEvent() { unixtime = current_unixtime }; lock (_thread_interlock) { if (response.Address == (int)AddressByte.PulseAmp_A) { level.level = (int)Math.Sqrt(LastA * response.Data); AmpHistoryA.Add(level); } if (response.Address == (int)AddressByte.PulseAmp_B) { level.level = (int)Math.Sqrt(LastB * response.Data); AmpHistoryB.Add(level); } } break; case TransmissionType.Error: ErrorCommand error = (ErrorCommand)received_command; ErrorCallback(this, error.Error, error.Details, false); break; } read_header = null; } } if (consumed > 0) { lock (_thread_interlock) { byte[] remaining = new byte[remote_bytes.Length - consumed]; Buffer.BlockCopy(remote_bytes, (int)consumed, remaining, 0, remaining.Length); remote_bytes = remaining; } } lock (_thread_interlock) { if (CommandQueue.Count > 0) { CommandBase command = CommandQueue.First(); CommandQueue.RemoveAt(0); NetworkHeader write_header = new NetworkHeader() { ObjectType = command.ObjectType }; try { JsonSerializer.Serialize(jsonwriter, write_header, typeof(NetworkHeader)); jsonwriter.Flush(); jsonwriter.Reset(); switch (command.ObjectType) { case TransmissionType.Nothing: break; case TransmissionType.Ready: JsonSerializer.Serialize(jsonwriter, (ReadyCommand)command, typeof(ReadyCommand)); break; case TransmissionType.Panic: JsonSerializer.Serialize(jsonwriter, (PanicCommand)command, typeof(PanicCommand)); break; case TransmissionType.Levels: JsonSerializer.Serialize(jsonwriter, (LevelsCommand)command, typeof(LevelsCommand)); break; case TransmissionType.Raw: JsonSerializer.Serialize(jsonwriter, (RawCommand)command, typeof(RawCommand)); break; case TransmissionType.Mode: JsonSerializer.Serialize(jsonwriter, (ModeCommand)command, typeof(ModeCommand)); break; case TransmissionType.Request: JsonSerializer.Serialize(jsonwriter, (RequestCommand)command, typeof(RequestCommand)); break; case TransmissionType.Response: JsonSerializer.Serialize(jsonwriter, (ResponseCommand)command, typeof(ResponseCommand)); break; case TransmissionType.Error: JsonSerializer.Serialize(jsonwriter, (ErrorCommand)command, typeof(ErrorCommand)); break; } jsonwriter.Flush(); jsonwriter.Reset(); } catch (JsonException) { } catch (IOException e) when(e.InnerException is SocketException) { _thread_end = true; ErrorCallback(this, "Network error.", e.InnerException.Message, false); } catch (IOException e) { _thread_end = true; ErrorCallback(this, "Network error.", e.Message, false); } catch (Exception e) { _thread_end = true; ErrorCallback(this, "Network error.", e.Message, false); } if (CommandQueue.Count == 0 && command.ObjectType != TransmissionType.Ready) { CommandQueue.Add(new ReadyCommand()); } } should_exit = _thread_end; } } try { lock (_thread_interlock) { if (DeviceStream != null) { DeviceStream.Close(); DeviceStream.Dispose(); } DeviceStream = null; } } catch (Exception e) { ErrorCallback(this, "Network error.", e.Message, false); } ErrorCallback(this, "Network processing shutdown.", "", false); }
public override void ReportError(ErrorCommand error) { lock (_thread_interlock) CommandQueue.Add(error); }
public override void ReportOutputData(ResponseCommand response) { lock (_thread_interlock) CommandQueue.Add(response); }
public override void RequestOutputData(RequestCommand request) { lock (_thread_interlock) CommandQueue.Add(request); }
public override void SetMode(ModeCommand mode) { lock (_thread_interlock) CommandQueue.Add(mode); }
public override void RawWrite(RawCommand write) { lock (_thread_interlock) CommandQueue.Add(write); }
static void Do() { var c = 4; var inArr = new int32 [c]; for (int _i = 0; _i < c; _i++) { inArr[_i] = 2; } inArr[c - 2] = 1; int32 size = inArr.Length; var rules = new Rule[] { new Rule(1, 2, 3, 0, 0), new Rule(2, 3, 2, 0, 0), new Rule(2, 1, 0, 0, 0), new Rule(3, 2, 0, 0, 0) }; var nTermRules = (from rule in rules where rule.c > 0 select rule).ToArray(); int32 nTerms = 3; string platformName = "*"; Cl.DeviceType deviceType = Cl.DeviceType.Default; ComputeProvider provider; try { provider = ComputeProvider.Create(platformName, deviceType); } catch (PlatformNotSupportedException ex) { Console.WriteLine(ex.Message); return; } var commandQueue = new CommandQueue(provider, provider.Devices.First()); var bArr = new int32[size * size * nTerms * cellDataRepresentationLength]; var rulesArr = new uint32[nTermRules.Length * ruleRepresentationLength]; for (int i = 0; i < size; i++) { for (int j = 0; j < rules.Length; j++) { if (inArr[i] == (rules[j]).b && (rules[j]).c == 0) { var _base = (i * nTerms + (int)(rules[j].a - 1)) * cellDataRepresentationLength; bArr[_base] = rules[j].a; //bArr[_base + 1] = 0; //bArr[_base + 2] = (rules[j].lblNum == 0 ? 1 : 0); ///bArr[_base + 3] = rules[j].lblNum; //bArr[_base + 4] = rules[j].lblWeight; } } } for (int i = 0; i < nTermRules.Length; i++) { var _base = i * ruleRepresentationLength; rulesArr[_base] = (uint32)(uint)(nTermRules[i].a << 16 | nTermRules[i].b); rulesArr[_base + 1] = (uint32)(uint)nTermRules[i].c; //rulesArr[_base + 3] = rules[i].lblNum; //rulesArr[_base + 4] = rules[i].lblWeight; } var buffer = new Buffer <int32>(provider, Operations.ReadWrite, Memory.Device, bArr); var rulesBuffer = new Buffer <uint32>(provider, Operations.ReadOnly, Memory.Device, rulesArr); int32 rLength = nTermRules.Length; var processRow = provider.Compile <_1D, int32, Buffer <int32>, Buffer <uint32> >( (range, l, a, _rules) => from r in range let i = r.GlobalID0 let nT = nTerms let _base = nT * size let res_id_base = (l * _base) + i * nT let iter = provider.Loop(0, l, kIdx => from k in kIdx let left_base_idx = (k * _base) + i * nTerms let right_base_idx = ((l - k - 1) * _base) + (k + i + 1) * nTerms let iter2 = provider.Loop(0, rLength, rIdxs => from rId in rIdxs let rule_base = rId * ruleRepresentationLength let rule_a_b = _rules[rule_base] let rule_a = (int)((rule_a_b >> 16) & 0xFFFF) let rule_b = (int)(rule_a_b & 0xFFFF) let rule_c = (int)(_rules[rule_base + 1] | 0) let res_id = res_id_base + ((int)(uint)rule_a - 1) select new[] { (rule_c != 0 & rule_c == a[right_base_idx + (rule_c - 1)] & rule_b == a[left_base_idx + (rule_b - 1)]) ? a[res_id] <= rule_a : ((Brahma.Set <int32>)null) }) select(Brahma.Set <int32>[]) null) select(Brahma.Set <int32>[]) null); for (int l = 1; l < size; l++) { commandQueue.Add(processRow.Run(new _1D(size - l), l, buffer, rulesBuffer)).Finish(); } commandQueue.Add(buffer.Read(0, size * size * nTerms * cellDataRepresentationLength, bArr)).Finish(); toMatrix(bArr, (int)(size * nTerms * cellDataRepresentationLength)); buffer.Dispose(); commandQueue.Dispose(); provider.Dispose(); }
public override void SetLevels(LevelsCommand levels) { int A, B, MA; lock (_thread_interlock) { if (levels.Mode == ControlMode.absolute) { A = levels.A; B = levels.B; } else { A = LastA + levels.A; B = LastB + levels.B; } if (A > 255) { A = 255; } if (A < 0) { A = 0; } if (B > 255) { B = 255; } if (B < 0) { B = 0; } if (A != LastA) { CommandQueue.Add(new ET232Command() { Command = CommandByte.write, Address = AddressByte.Pot_A, Data = A, }); } if (B != LastB) { CommandQueue.Add(new ET232Command() { Command = CommandByte.write, Address = AddressByte.Pot_B, Data = B, }); } if (levels.MA != 0) { MA = LastMA + levels.MA; if (MA > 255) { MA = 255; } if (MA < 0) { MA = 0; } if (MA != LastMA) { CommandQueue.Add(new ET232Command() { Command = CommandByte.write, Address = AddressByte.Pot_MA, Data = MA, }); } } } }
public override void SetLevels(LevelsCommand levels) { lock (_thread_interlock) CommandQueue.Add(levels); }
public void RunCommand(OwlCommand c) { CommandQueue.Add(c); //Need to set up a queue event to fire this automatically. //ProcessCommandQueue(); }