MainForm.pas 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. { $HDR$}
  2. {**********************************************************************}
  3. { Unit archived using Team Coherence }
  4. { Team Coherence is Copyright 2002 by Quality Software Components }
  5. { }
  6. { For further information / comments, visit our WEB site at }
  7. { http://www.TeamCoherence.com }
  8. {**********************************************************************}
  9. {}
  10. { $Log: 22954: MainForm.pas
  11. {
  12. { Rev 1.0 09/10/2003 3:10:44 PM Jeremy Darling
  13. { Project Checked into TC for the first time
  14. }
  15. unit MainForm;
  16. interface
  17. uses
  18. Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  19. IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, StdCtrls, ClientThread,
  20. IdAntiFreezeBase, IdAntiFreeze, SyncObjs, ExtCtrls, ComCtrls, IniFiles;
  21. type
  22. TfrmMain = class(TForm)
  23. SampleClient: TIdTCPClient;
  24. Button1: TButton;
  25. Label1: TLabel;
  26. Label2: TLabel;
  27. Label3: TLabel;
  28. edHost: TEdit;
  29. edPort: TEdit;
  30. Label4: TLabel;
  31. edThreads: TEdit;
  32. IdAntiFreeze1: TIdAntiFreeze;
  33. lblConCons: TLabel;
  34. lblMaxCons: TLabel;
  35. lblTotalCons: TLabel;
  36. lvStatus: TListView;
  37. Bevel1: TBevel;
  38. procedure FormCreate(Sender: TObject);
  39. procedure Button1Click(Sender: TObject);
  40. procedure edPortKeyPress(Sender: TObject; var Key: Char);
  41. procedure edThreadsKeyPress(Sender: TObject; var Key: Char);
  42. procedure edThreadsChange(Sender: TObject);
  43. procedure FormDestroy(Sender: TObject);
  44. procedure SampleClientConnected(Sender: TObject);
  45. procedure SampleClientDisconnected(Sender: TObject);
  46. procedure SampleClientWork(Sender: TObject; AWorkMode: TWorkMode;
  47. const AWorkCount: Integer);
  48. private
  49. { Private declarations }
  50. FDefaultCaption : String;
  51. fThreads : TList;
  52. FClientsConnected : Boolean;
  53. uiLock : TCriticalSection;
  54. CurrentConnections,
  55. MaxConnections,
  56. ConnectionsMade : Integer;
  57. procedure SetClientsConnected(const Value: Boolean);
  58. procedure LoadIniSettings;
  59. procedure WriteIniSettings;
  60. public
  61. { Public declarations }
  62. procedure StartThreads;
  63. procedure StopThreads;
  64. property ClientsConnected : Boolean read FClientsConnected write SetClientsConnected;
  65. end;
  66. var
  67. frmMain: TfrmMain;
  68. Ini : TIniFile;
  69. implementation
  70. {$R *.DFM}
  71. procedure TfrmMain.FormCreate(Sender: TObject);
  72. begin
  73. Randomize;
  74. Ini := TIniFile.Create(ChangeFileExt(ParamStr(0), '.ini'));
  75. LoadIniSettings;
  76. CurrentConnections := 0;
  77. ConnectionsMade := 0;
  78. MaxConnections := 0;
  79. lblConCons.Caption := 'Current Concurrent Connections: 0';
  80. lblMaxCons.Caption := 'Max Concurrent Connections: 0';
  81. lblTotalCons.Caption := 'Total Connections Made: 0';
  82. FDefaultCaption := Caption;
  83. uiLock := TCriticalSection.Create;
  84. fThreads := TList.Create;
  85. end;
  86. procedure TfrmMain.Button1Click(Sender: TObject);
  87. begin
  88. SampleClient.Host := edHost.Text;
  89. SampleClient.Port := StrToIntDef(edPort.Text, 8800);
  90. if ClientsConnected then
  91. StopThreads
  92. else
  93. StartThreads;
  94. end;
  95. procedure TfrmMain.edPortKeyPress(Sender: TObject; var Key: Char);
  96. begin
  97. if not (Key in ['0', '1'..'9', #8]) then
  98. Key := #0;
  99. end;
  100. procedure TfrmMain.edThreadsKeyPress(Sender: TObject; var Key: Char);
  101. begin
  102. if not (Key in ['0', '1'..'9', #8]) then
  103. Key := #0;
  104. end;
  105. procedure TfrmMain.edThreadsChange(Sender: TObject);
  106. begin
  107. if edThreads.Text = '' then
  108. edThreads.Text := '0';
  109. end;
  110. procedure TfrmMain.FormDestroy(Sender: TObject);
  111. begin
  112. StopThreads;
  113. fThreads.Free;
  114. uiLock.Free;
  115. WriteIniSettings;
  116. Ini.Free;
  117. end;
  118. procedure TfrmMain.StartThreads;
  119. var
  120. i : Integer;
  121. st: Integer;
  122. begin
  123. lvStatus.Items.Clear;
  124. CurrentConnections := 0;
  125. ConnectionsMade := 0;
  126. MaxConnections := 0;
  127. lblConCons.Caption := 'Current Concurrent Connections: 0';
  128. lblMaxCons.Caption := 'Max Concurrent Connections: 0';
  129. lblTotalCons.Caption := 'Total Connections Made: 0';
  130. st := StrToIntDef(edThreads.Text, 0);
  131. if st < 10 then
  132. st := 10;
  133. for i := 0 to StrToIntDef(edThreads.Text, 0) -1 do
  134. begin
  135. with TClientThread(fThreads[fThreads.Add(TClientThread.Create(true))]) do
  136. begin
  137. AssignClient(SampleClient);
  138. ListItem := lvStatus.Items.Add;
  139. ListItem.Caption := IntToStr(i);
  140. ListItem.SubItems.Add('Creating');
  141. SleepTime := random(st);
  142. if SleepTime < 5 then
  143. while SleepTime < 5 do
  144. SleepTime := random(st);
  145. uiLock := self.uiLock;
  146. Client.Tag := Integer(Pointer(TClientThread(fThreads[i])));
  147. State := -2;
  148. Resume;
  149. end;
  150. end;
  151. ClientsConnected := true;
  152. end;
  153. procedure TfrmMain.StopThreads;
  154. begin
  155. ClientsConnected := false;
  156. if fThreads.Count > 0 then
  157. while fThreads.Count > 0 do
  158. begin
  159. TClientThread(fThreads[0]).FreeOnTerminate := true;
  160. TClientThread(fThreads[0]).Terminate;
  161. fThreads.Delete(0);
  162. end;
  163. end;
  164. procedure TfrmMain.SampleClientConnected(Sender: TObject);
  165. var
  166. ct : TClientThread;
  167. begin
  168. uiLock.Enter;
  169. try
  170. ct := Pointer(TIdTCPClient(Sender).Tag);
  171. ct.State := 0;
  172. Inc(CurrentConnections);
  173. Inc(ConnectionsMade);
  174. lblConCons.Caption := 'Current Concurrent Connections: ' + IntToStr(CurrentConnections);
  175. if MaxConnections < CurrentConnections then
  176. begin
  177. MaxConnections := CurrentConnections;
  178. lblMaxCons.Caption := 'Max Concurrent Connections: ' + IntToStr(MaxConnections);
  179. end;
  180. lblTotalCons.Caption := 'Total Connections Made: ' + IntToStr(ConnectionsMade);
  181. finally
  182. uiLock.Leave;
  183. end;
  184. end;
  185. procedure TfrmMain.SetClientsConnected(const Value: Boolean);
  186. begin
  187. FClientsConnected := Value;
  188. if Value then
  189. Button1.Caption := 'Disconnect'
  190. else
  191. Button1.Caption := 'Connect';
  192. edHost.Enabled := not Value;
  193. edPort.Enabled := not Value;
  194. edThreads.Enabled := not Value;
  195. end;
  196. procedure TfrmMain.SampleClientDisconnected(Sender: TObject);
  197. var
  198. ct : TClientThread;
  199. begin
  200. uiLock.Enter;
  201. try
  202. ct := Pointer(TIdTCPClient(Sender).Tag);
  203. ct.State := -2;
  204. Dec(CurrentConnections);
  205. lblConCons.Caption := 'Current Concurrent Connections: ' + IntToStr(CurrentConnections);
  206. finally
  207. uiLock.Leave;
  208. end;
  209. end;
  210. procedure TfrmMain.SampleClientWork(Sender: TObject; AWorkMode: TWorkMode;
  211. const AWorkCount: Integer);
  212. begin
  213. // Do Nothing
  214. end;
  215. procedure TfrmMain.LoadIniSettings;
  216. begin
  217. edHost.Text := Ini.ReadString('Connection', 'Host', edHost.Text);
  218. edPort.Text := Ini.ReadString('Connection', 'Port', edPort.Text);
  219. edThreads.Text := Ini.ReadString('Threads', 'Threads', edThreads.Text);
  220. end;
  221. procedure TfrmMain.WriteIniSettings;
  222. begin
  223. Ini.WriteString('Connection', 'Host', edHost.Text);
  224. Ini.WriteString('Connection', 'Port', edPort.Text);
  225. Ini.WriteString('Threads', 'Threads', edThreads.Text);
  226. end;
  227. end.