unit frmMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Buttons, uTestConn;

type
  TForm1 = class(TForm)
    mmoLog: TMemo;
    mmoErr: TMemo;
    btnTest1: TSpeedButton;
    btnTest2: TSpeedButton;
    btnTest3: TSpeedButton;
    btnTest4: TSpeedButton;
    btnTest5: TSpeedButton;
    btnTest6: TSpeedButton;
    btnTest7: TSpeedButton;
    btnTest8: TSpeedButton;
    btnTest9: TSpeedButton;
    btnTest10: TSpeedButton;
    btnTest11: TSpeedButton;
    btnTest12: TSpeedButton;
    shpState1: TShape;
    shpState2: TShape;
    shpState3: TShape;
    shpState4: TShape;
    shpState5: TShape;
    shpState6: TShape;
    shpState7: TShape;
    shpState8: TShape;
    shpState9: TShape;
    shpState10: TShape;
    shpState11: TShape;
    shpState12: TShape;
    btnRetry12: TSpeedButton;
    btnRetry11: TSpeedButton;
    btnRetry10: TSpeedButton;
    btnRetry9: TSpeedButton;
    btnRetry8: TSpeedButton;
    btnRetry7: TSpeedButton;
    btnRetry6: TSpeedButton;
    btnRetry5: TSpeedButton;
    btnRetry4: TSpeedButton;
    btnRetry3: TSpeedButton;
    btnRetry2: TSpeedButton;
    btnRetry1: TSpeedButton;
    procedure FormCreate(Sender: TObject);
    procedure btnTestClick(Sender: TObject);
  private
    FConnList : array[0..11] of TTestConnection;

    procedure LogEvent( AError: Boolean; S: string );
    procedure StateChanged( ASender: TTestConnection; AState: TTestConnState );
    procedure NewConn( Index: Integer );

    procedure SetShapeColor( AIndex: Integer; Color: TColor );
    function  RetryButtonDown( AIndex: Integer ): Boolean;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
  WinSock;

{$R *.dfm}

procedure TForm1.SetShapeColor( AIndex: Integer; Color: TColor );
begin
  case AIndex of
    0  : shpState1.Brush.Color := Color;
    1  : shpState2.Brush.Color := Color;
    2  : shpState3.Brush.Color := Color;
    3  : shpState4.Brush.Color := Color;
    4  : shpState5.Brush.Color := Color;
    5  : shpState6.Brush.Color := Color;
    6  : shpState7.Brush.Color := Color;
    7  : shpState8.Brush.Color := Color;
    8  : shpState9.Brush.Color := Color;
    9  : shpState10.Brush.Color := Color;
   10  : shpState11.Brush.Color := Color;
   11  : shpState12.Brush.Color := Color;
  end;
end;

function  TForm1.RetryButtonDown( AIndex: Integer ): Boolean;
begin
  case AIndex of
    0  : Result := btnRetry1.Down;
    1  : Result := btnRetry2.Down;
    2  : Result := btnRetry3.Down;
    3  : Result := btnRetry4.Down;
    4  : Result := btnRetry5.Down;
    5  : Result := btnRetry6.Down;
    6  : Result := btnRetry7.Down;
    7  : Result := btnRetry8.Down;
    8  : Result := btnRetry9.Down;
    9  : Result := btnRetry10.Down;
   10  : Result := btnRetry11.Down;
   11  : Result := btnRetry12.Down;
  end;
end;

procedure TForm1.LogEvent( AError: Boolean; S: string );
begin
  if AError then
  begin
    mmoErr.Lines.Add( S );
  end
  else begin
    mmoLog.Lines.Add( S );
  end;
end;

procedure TForm1.StateChanged( ASender: TTestConnection; AState: TTestConnState );
var
  Index : Integer;
begin
  Index := ASender.Port - 5000;
  Assert( (Index >= 0) and (Index <= High( FConnList)));

  case AState of
    tcsBind:
    begin
      SetShapeColor( Index, clWhite );
    end;

    tcsConnect:
    begin
      SetShapeColor( Index, clYellow );
    end;

    tcsData:
    begin
      SetShapeColor( Index, clGreen );
    end;

    tcsClose:
    begin
      SetShapeColor( Index, clRed );
      if RetryButtonDown( Index ) then NewConn( Index );
    end;
  end;

end;

procedure TForm1.FormCreate(Sender: TObject);
var
  WSAData : TWSAData;
begin
  WSAStartup( $0202, WSAData );
end;

procedure TForm1.NewConn( Index: Integer );
var
  TC : TTestConnection;
begin
  TC := TTestConnection.Create( '192.168.1.210', 5000 + Index );
  TC.FreeOnTerminate := True;
  TC.OnChange := StateChanged;
  TC.OnLog := LogEvent;
  TC.Resume;
end;

procedure TForm1.btnTestClick(Sender: TObject);
begin
  NewConn( TSpeedButton( Sender ).Tag );
end;

end.
