unit uDataModuleConexion;

interface

uses
  SysUtils, Classes, uRORemoteService, uDADataTable,
  uDABINAdapter, uROClient, uROBinMessage, 
  uDADataStreamer, uROWinInetHttpChannel;

const
  SERVER_URL = 'http://localhost:8099/bin'; // Dirección por defecto del servidor

type
  TdmConexion = class(TDataModule)
    ROMessage: TROBinMessage;
    ROChannel: TROWinInetHTTPChannel;
    procedure DataModuleDestroy(Sender: TObject);
    procedure ROChannel2Failure(Sender: TROTransportChannel;
      anException: Exception; var aRetry: Boolean);
    procedure DataModuleCreate(Sender: TObject);
  private
    function GetChannel: TROTransportChannel;
    function GetMessage: TROBinMessage;
    function GetTargetURL: String;
    procedure SetTargetURL(const Value: String);
    procedure ConfigurarEncriptacionConexion;
  public
    function HayConexion : Boolean;
    function ProbarConexion(const ATargetURL : String): Boolean;
    procedure ConfigurarConexion;
    property TargetURL : String read GetTargetURL write SetTargetURL;
    property Channel: TROTransportChannel read GetChannel;
    property Message: TROBinMessage read GetMessage;
  end;

var
  dmConexion: TdmConexion;

implementation

{$R *.dfm}

uses
  uROEncryption, Windows, WinInet, cxControls, uConfigurarConexion, Dialogs, Controls,
  uDMBase, FactuGES_Intf;

const
  IE_OFFLINE_ERROR = 'Unexpected error in WinInet HTTP Channel (2)';


function TdmConexion.HayConexion: Boolean;
begin
  Result := ROChannel.Connected;
end;

procedure TdmConexion.ConfigurarConexion;
begin
  with TfConfigurarConexion.Create(NIL) do
    try
      TargetURL := ROChannel.TargetURL;
      if ShowModal = mrOk then
      begin
        ROChannel.TargetURL := TargetURL;
        ROChannel.Connected := False;
        ROChannel.Connected := True;
        dmBase.SalvarConfiguracion;
      end;
    finally
      Free;
    end;
end;

procedure TdmConexion.ConfigurarEncriptacionConexion;
begin
  {
  with ROChannel.Encryption do
  begin
    EncryptionMethod := tetDES;
    EncryptionSendKey := '127C1A6A4D85F5EFE0A54E104BF7F695CD6C989C1808A57667EF1218E8ED93FC6CDC531631EB9750';
    EncryptionRecvKey := 'C793F1A04FFC8DF91FF9522951F6B6DF921C70B42D74166C6DF0B697797AAA6A243BEC35A9423A51';
    UseCompression := True;
  end;
  }
end;

procedure TdmConexion.DataModuleCreate(Sender: TObject);
begin
  ConfigurarEncriptacionConexion;
end;

procedure TdmConexion.DataModuleDestroy(Sender: TObject);
begin
  ROChannel.Connected := False;
end;

function TdmConexion.GetChannel: TROTransportChannel;
begin
  Result := ROChannel;
end;

function TdmConexion.GetMessage: TROBinMessage;
begin
  Result := ROMessage;
end;

function TdmConexion.GetTargetURL: String;
begin
  Result := ROChannel.TargetURL;
end;

function TdmConexion.ProbarConexion(const ATargetURL: String): Boolean;
var
  AHTTPChannel: TROWinInetHTTPChannel;
  AROBinMessage: TROBinMessage;
  ACoService: TRORemoteService;
begin
  if ATargetURL = '' then
    raise Exception.Create('No se ha indicado la URL del servidor (HayConexion)');

  AHTTPChannel := TROWinInetHTTPChannel.Create(Self);
  AROBinMessage := TROBinMessage.Create(Self);
  ACoService := TRORemoteService.Create(Self);

  ShowHourglassCursor;
  try
    AROBinMessage.Assign(ROMessage);

    AHTTPChannel.Assign(ROChannel);
    with AHTTPChannel do
    begin
      Name := 'HTTPChannel';

      if Length(ATargetURL) > 0 then
        TargetURL := ATargetURL
      else
        TargetURL := ROChannel.TargetURL;
    end;

{    if AHTTPChannel.Encryption.EncryptionMethod <> tetNone then
      ShowMessage('funcionando');}

    ACoService.ServiceName := 'srvLogin';
    ACoService.Message := AROBinMessage;
    ACoService.Channel := AHTTPChannel;

    try
      (ACoService as IsrvLogin).Ping;
      Result := True;
    except
      Result := False;
    end;
  finally
    FreeAndNil(AHTTPChannel);
    FreeAndNil(ACoService);
    FreeAndNil(AROBinMessage);
    HideHourglassCursor;
  end;
end;

procedure TdmConexion.ROChannel2Failure(Sender: TROTransportChannel;
  anException: Exception; var aRetry: Boolean);
begin
  if (Pos(anException.Message, IE_OFFLINE_ERROR) > 0) then
  begin
    // Preguntar al usuario si se quiere conectar
    if InternetGoOnline(PAnsiChar(ROChannel.TargetURL), GetDesktopWindow(), 0) then
      aRetry := True  // Si el usuario pulsa en 'Conectar' reintentar la operación
    else
      Abort; // Si el usuario pulsa en 'Seguir desconectado' parar todo
  end
end;

procedure TdmConexion.SetTargetURL(const Value: String);
begin
  ROChannel.TargetURL := Value;
end;

end.
