AnsiRequestsScenario.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace UICatalog.Scenarios;
  6. [ScenarioMetadata ("AnsiEscapeSequenceRequest", "Ansi Escape Sequence Request")]
  7. [ScenarioCategory ("Tests")]
  8. public sealed class AnsiEscapeSequenceRequests : Scenario
  9. {
  10. private GraphView _graphView;
  11. private ScatterSeries _sentSeries;
  12. private ScatterSeries _answeredSeries;
  13. private readonly List<DateTime> _sends = new ();
  14. private readonly object _lockAnswers = new object ();
  15. private readonly Dictionary<DateTime, string> _answers = new ();
  16. private Label _lblSummary;
  17. public override void Main ()
  18. {
  19. // Init
  20. Application.Init ();
  21. TabView tv = new TabView
  22. {
  23. Width = Dim.Fill (),
  24. Height = Dim.Fill (),
  25. CanFocus = true
  26. };
  27. Tab single = new Tab ();
  28. single.DisplayText = "Single";
  29. single.View = BuildSingleTab ();
  30. Tab bulk = new ();
  31. bulk.DisplayText = "Multi";
  32. bulk.View = BuildBulkTab ();
  33. tv.AddTab (single, true);
  34. tv.AddTab (bulk, false);
  35. // Setup - Create a top-level application window and configure it.
  36. Window appWindow = new ()
  37. {
  38. Title = GetQuitKeyAndName (),
  39. };
  40. appWindow.Add (tv);
  41. // Run - Start the application.
  42. Application.Run (appWindow);
  43. bulk.View.Dispose ();
  44. single.View.Dispose ();
  45. appWindow.Dispose ();
  46. // Shutdown - Calling Application.Shutdown is required.
  47. Application.Shutdown ();
  48. }
  49. private View BuildSingleTab ()
  50. {
  51. View w = new View ()
  52. {
  53. Width = Dim.Fill (),
  54. Height = Dim.Fill (),
  55. CanFocus = true
  56. };
  57. w.Padding.Thickness = new (1);
  58. var scrRequests = new List<string>
  59. {
  60. "CSI_SendDeviceAttributes",
  61. "CSI_ReportTerminalSizeInChars",
  62. "CSI_RequestCursorPositionReport",
  63. "CSI_SendDeviceAttributes2"
  64. };
  65. // TODO: This UI would be cleaner/less rigid if Pos.Align were used
  66. var cbRequests = new ComboBox () { Width = 40, Height = 5, ReadOnly = true, Source = new ListWrapper<string> (new (scrRequests)) };
  67. w.Add (cbRequests);
  68. var label = new Label { Y = Pos.Bottom (cbRequests) + 1, Text = "Request:" };
  69. var tfRequest = new TextField { X = Pos.Left (label), Y = Pos.Bottom (label), Width = 20 };
  70. w.Add (label, tfRequest);
  71. label = new Label { X = Pos.Right (tfRequest) + 1, Y = Pos.Top (tfRequest) - 1, Text = "Value:" };
  72. var tfValue = new TextField { X = Pos.Left (label), Y = Pos.Bottom (label), Width = 6 };
  73. w.Add (label, tfValue);
  74. label = new Label { X = Pos.Right (tfValue) + 1, Y = Pos.Top (tfValue) - 1, Text = "Terminator:" };
  75. var tfTerminator = new TextField { X = Pos.Left (label), Y = Pos.Bottom (label), Width = 4 };
  76. w.Add (label, tfTerminator);
  77. cbRequests.SelectedItemChanged += (s, e) =>
  78. {
  79. if (cbRequests.SelectedItem == -1)
  80. {
  81. return;
  82. }
  83. var selAnsiEscapeSequenceRequestName = scrRequests [cbRequests.SelectedItem];
  84. AnsiEscapeSequence selAnsiEscapeSequenceRequest = null;
  85. switch (selAnsiEscapeSequenceRequestName)
  86. {
  87. case "CSI_SendDeviceAttributes":
  88. selAnsiEscapeSequenceRequest = EscSeqUtils.CSI_SendDeviceAttributes;
  89. break;
  90. case "CSI_ReportTerminalSizeInChars":
  91. selAnsiEscapeSequenceRequest = EscSeqUtils.CSI_ReportWindowSizeInChars;
  92. break;
  93. case "CSI_RequestCursorPositionReport":
  94. selAnsiEscapeSequenceRequest = EscSeqUtils.CSI_RequestCursorPositionReport;
  95. break;
  96. case "CSI_SendDeviceAttributes2":
  97. selAnsiEscapeSequenceRequest = EscSeqUtils.CSI_SendDeviceAttributes2;
  98. break;
  99. }
  100. tfRequest.Text = selAnsiEscapeSequenceRequest is { } ? selAnsiEscapeSequenceRequest.Request : "";
  101. tfValue.Text = selAnsiEscapeSequenceRequest is { } ? selAnsiEscapeSequenceRequest.Value ?? "" : "";
  102. tfTerminator.Text = selAnsiEscapeSequenceRequest is { } ? selAnsiEscapeSequenceRequest.Terminator : "";
  103. };
  104. // Forces raise cbRequests.SelectedItemChanged to update TextFields
  105. cbRequests.SelectedItem = 0;
  106. label = new Label { Y = Pos.Bottom (tfRequest) + 2, Text = "Response:" };
  107. var tvResponse = new TextView { X = Pos.Left (label), Y = Pos.Bottom (label), Width = 40, Height = 4, ReadOnly = true };
  108. w.Add (label, tvResponse);
  109. label = new Label { X = Pos.Right (tvResponse) + 1, Y = Pos.Top (tvResponse) - 1, Text = "Error:" };
  110. var tvError = new TextView { X = Pos.Left (label), Y = Pos.Bottom (label), Width = 40, Height = 4, ReadOnly = true };
  111. w.Add (label, tvError);
  112. label = new Label { X = Pos.Right (tvError) + 1, Y = Pos.Top (tvError) - 1, Text = "Value:" };
  113. var tvValue = new TextView { X = Pos.Left (label), Y = Pos.Bottom (label), Width = 6, Height = 4, ReadOnly = true };
  114. w.Add (label, tvValue);
  115. label = new Label { X = Pos.Right (tvValue) + 1, Y = Pos.Top (tvValue) - 1, Text = "Terminator:" };
  116. var tvTerminator = new TextView { X = Pos.Left (label), Y = Pos.Bottom (label), Width = 4, Height = 4, ReadOnly = true };
  117. w.Add (label, tvTerminator);
  118. var btnResponse = new Button { X = Pos.Center (), Y = Pos.Bottom (tvResponse) + 2, Text = "Send Request", IsDefault = true };
  119. var lblSuccess = new Label { X = Pos.Center (), Y = Pos.Bottom (btnResponse) + 1 };
  120. w.Add (lblSuccess);
  121. btnResponse.Accepting += (s, e) =>
  122. {
  123. var ansiEscapeSequenceRequest = new AnsiEscapeSequence
  124. {
  125. Request = tfRequest.Text,
  126. Terminator = tfTerminator.Text,
  127. Value = string.IsNullOrEmpty (tfValue.Text) ? null : tfValue.Text
  128. };
  129. Application.Driver.QueueAnsiRequest (
  130. new ()
  131. {
  132. Request = ansiEscapeSequenceRequest.Request,
  133. Terminator = ansiEscapeSequenceRequest.Terminator,
  134. ResponseReceived = (s) => OnSuccess (s, tvResponse, tvError, tvValue, tvTerminator, lblSuccess),
  135. Abandoned = () => OnFail (tvResponse, tvError, tvValue, tvTerminator, lblSuccess)
  136. });
  137. };
  138. w.Add (btnResponse);
  139. w.Add (new Label { Y = Pos.Bottom (lblSuccess) + 2, Text = "You can send other requests by editing the TextFields." });
  140. return w;
  141. }
  142. private void OnSuccess (string response, TextView tvResponse, TextView tvError, TextView tvValue, TextView tvTerminator, Label lblSuccess)
  143. {
  144. tvResponse.Text = response;
  145. tvError.Text = string.Empty;
  146. tvValue.Text = string.Empty;
  147. tvTerminator.Text = string.Empty;
  148. lblSuccess.SchemeName = SchemeManager.SchemesToSchemeName (Schemes.Base);
  149. lblSuccess.Text = "Successful";
  150. }
  151. private void OnFail (TextView tvResponse, TextView tvError, TextView tvValue, TextView tvTerminator, Label lblSuccess)
  152. {
  153. tvResponse.Text = string.Empty;
  154. tvError.Text = "No Response";
  155. tvValue.Text = string.Empty;
  156. tvTerminator.Text = string.Empty;
  157. lblSuccess.SchemeName = SchemeManager.SchemesToSchemeName (Schemes.Error);
  158. lblSuccess.Text = "Error";
  159. }
  160. private View BuildBulkTab ()
  161. {
  162. View w = new View ()
  163. {
  164. Width = Dim.Fill (),
  165. Height = Dim.Fill (),
  166. CanFocus = true
  167. };
  168. var lbl = new Label ()
  169. {
  170. Text = "This scenario tests Ansi request/response processing. Use the TextView to ensure regular user interaction continues as normal during sends",
  171. Height = 2,
  172. Width = Dim.Fill ()
  173. };
  174. Application.AddTimeout (
  175. TimeSpan.FromMilliseconds (1000),
  176. () =>
  177. {
  178. lock (_lockAnswers)
  179. {
  180. UpdateGraph ();
  181. UpdateResponses ();
  182. }
  183. return true;
  184. });
  185. var tv = new TextView ()
  186. {
  187. Y = Pos.Bottom (lbl),
  188. Width = Dim.Percent (50),
  189. Height = Dim.Fill ()
  190. };
  191. var lblDar = new Label ()
  192. {
  193. Y = Pos.Bottom (lbl),
  194. X = Pos.Right (tv) + 1,
  195. Text = "DAR per second",
  196. };
  197. var cbDar = new NumericUpDown ()
  198. {
  199. X = Pos.Right (lblDar),
  200. Y = Pos.Bottom (lbl),
  201. Value = 0,
  202. };
  203. cbDar.ValueChanging += (s, e) =>
  204. {
  205. if (e.NewValue < 0 || e.NewValue > 20)
  206. {
  207. e.Cancel = true;
  208. }
  209. };
  210. w.Add (cbDar);
  211. int lastSendTime = Environment.TickCount;
  212. object lockObj = new object ();
  213. Application.AddTimeout (
  214. TimeSpan.FromMilliseconds (50),
  215. () =>
  216. {
  217. lock (lockObj)
  218. {
  219. if (cbDar.Value > 0)
  220. {
  221. int interval = 1000 / cbDar.Value; // Calculate the desired interval in milliseconds
  222. int currentTime = Environment.TickCount; // Current system time in milliseconds
  223. // Check if the time elapsed since the last send is greater than the interval
  224. if (currentTime - lastSendTime >= interval)
  225. {
  226. SendDar (); // Send the request
  227. lastSendTime = currentTime; // Update the last send time
  228. }
  229. }
  230. }
  231. return true;
  232. });
  233. _graphView = new GraphView ()
  234. {
  235. Y = Pos.Bottom (cbDar),
  236. X = Pos.Right (tv),
  237. Width = Dim.Fill (),
  238. Height = Dim.Fill (1)
  239. };
  240. _lblSummary = new Label ()
  241. {
  242. Y = Pos.Bottom (_graphView),
  243. X = Pos.Right (tv),
  244. Width = Dim.Fill ()
  245. };
  246. SetupGraph ();
  247. w.Add (lbl);
  248. w.Add (lblDar);
  249. w.Add (cbDar);
  250. w.Add (tv);
  251. w.Add (_graphView);
  252. w.Add (_lblSummary);
  253. return w;
  254. }
  255. private void UpdateResponses ()
  256. {
  257. _lblSummary.Text = GetSummary ();
  258. _lblSummary.SetNeedsDraw ();
  259. }
  260. private string GetSummary ()
  261. {
  262. if (_answers.Count == 0)
  263. {
  264. return "No requests sent yet";
  265. }
  266. var last = _answers.Last ().Value;
  267. var unique = _answers.Values.Distinct ().Count ();
  268. var total = _answers.Count;
  269. return $"Last:{last} U:{unique} T:{total}";
  270. }
  271. private void SetupGraph ()
  272. {
  273. _graphView.Series.Add (_sentSeries = new ScatterSeries ());
  274. _graphView.Series.Add (_answeredSeries = new ScatterSeries ());
  275. _sentSeries.Fill = new GraphCellToRender (new Rune ('.'), new Attribute (ColorName16.BrightGreen, ColorName16.Black));
  276. _answeredSeries.Fill = new GraphCellToRender (new Rune ('.'), new Attribute (ColorName16.BrightRed, ColorName16.Black));
  277. // Todo:
  278. // _graphView.Annotations.Add (_sentSeries new PathAnnotation {});
  279. _graphView.CellSize = new PointF (1, 1);
  280. _graphView.MarginBottom = 2;
  281. _graphView.AxisX.Increment = 1;
  282. _graphView.AxisX.Text = "Seconds";
  283. _graphView.GraphColor = new Attribute (Color.Green, Color.Black);
  284. }
  285. private void UpdateGraph ()
  286. {
  287. _sentSeries.Points = _sends
  288. .GroupBy (ToSeconds)
  289. .Select (g => new PointF (g.Key, g.Count ()))
  290. .ToList ();
  291. _answeredSeries.Points = _answers.Keys
  292. .GroupBy (ToSeconds)
  293. .Select (g => new PointF (g.Key, g.Count ()))
  294. .ToList ();
  295. // _graphView.ScrollOffset = new PointF(,0);
  296. _graphView.SetNeedsDraw ();
  297. }
  298. private int ToSeconds (DateTime t)
  299. {
  300. return (int)(DateTime.Now - t).TotalSeconds;
  301. }
  302. private void SendDar ()
  303. {
  304. Application.Driver.QueueAnsiRequest (
  305. new ()
  306. {
  307. Request = EscSeqUtils.CSI_SendDeviceAttributes.Request,
  308. Terminator = EscSeqUtils.CSI_SendDeviceAttributes.Terminator,
  309. ResponseReceived = HandleResponse
  310. });
  311. _sends.Add (DateTime.Now);
  312. }
  313. private void HandleResponse (string response)
  314. {
  315. lock (_lockAnswers)
  316. {
  317. _answers.Add (DateTime.Now, response);
  318. }
  319. }
  320. }