2007-10-03 16:03:28 +00:00
|
|
|
|
unit uDataModuleConexion;
|
|
|
|
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
|
|
|
|
uses
|
|
|
|
|
|
SysUtils, Classes, uRORemoteService, uDADataTable,
|
2008-02-29 18:55:32 +00:00
|
|
|
|
uDABINAdapter, uROClient, uROBinMessage,
|
2008-03-03 17:37:41 +00:00
|
|
|
|
uDADataStreamer, uROWinInetHttpChannel;
|
2007-10-03 16:03:28 +00:00
|
|
|
|
|
|
|
|
|
|
type
|
|
|
|
|
|
TdmConexion = class(TDataModule)
|
|
|
|
|
|
ROMessage: TROBinMessage;
|
2008-03-03 17:37:41 +00:00
|
|
|
|
ROChannel: TROWinInetHTTPChannel;
|
2007-10-22 07:39:39 +00:00
|
|
|
|
procedure DataModuleDestroy(Sender: TObject);
|
2008-03-03 17:37:41 +00:00
|
|
|
|
procedure ROChannel2Failure(Sender: TROTransportChannel;
|
2008-02-29 18:55:32 +00:00
|
|
|
|
anException: Exception; var aRetry: Boolean);
|
|
|
|
|
|
procedure DataModuleCreate(Sender: TObject);
|
2007-10-03 16:03:28 +00:00
|
|
|
|
private
|
2012-06-11 14:31:03 +00:00
|
|
|
|
FListaConexiones : TStringList;
|
2008-02-29 18:55:32 +00:00
|
|
|
|
function GetChannel: TROTransportChannel;
|
2007-10-03 16:03:28 +00:00
|
|
|
|
function GetMessage: TROBinMessage;
|
|
|
|
|
|
function GetTargetURL: String;
|
|
|
|
|
|
procedure SetTargetURL(const Value: String);
|
2008-02-29 18:55:32 +00:00
|
|
|
|
procedure ConfigurarEncriptacionConexion;
|
2012-06-11 14:31:03 +00:00
|
|
|
|
function GetListaConexiones: TStringList;
|
|
|
|
|
|
procedure SetListaConexiones(const Value: TStringList);
|
2007-10-03 16:03:28 +00:00
|
|
|
|
public
|
2007-10-22 07:39:39 +00:00
|
|
|
|
function HayConexion : Boolean;
|
2007-10-03 16:03:28 +00:00
|
|
|
|
function ProbarConexion(const ATargetURL : String): Boolean;
|
|
|
|
|
|
procedure ConfigurarConexion;
|
2012-06-12 11:27:09 +00:00
|
|
|
|
procedure EstablecerConexion(aID : integer);
|
2007-10-03 16:03:28 +00:00
|
|
|
|
property TargetURL : String read GetTargetURL write SetTargetURL;
|
2008-02-29 18:55:32 +00:00
|
|
|
|
property Channel: TROTransportChannel read GetChannel;
|
2007-10-03 16:03:28 +00:00
|
|
|
|
property Message: TROBinMessage read GetMessage;
|
2012-06-11 14:31:03 +00:00
|
|
|
|
property ListaConexiones : TStringList read GetListaConexiones write SetListaConexiones;
|
2007-10-03 16:03:28 +00:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
var
|
|
|
|
|
|
dmConexion: TdmConexion;
|
|
|
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
|
|
|
|
{$R *.dfm}
|
|
|
|
|
|
|
|
|
|
|
|
uses
|
2008-02-29 18:55:32 +00:00
|
|
|
|
uROEncryption, Windows, WinInet, cxControls, uConfigurarConexion, Dialogs, Controls,
|
2012-06-11 14:31:03 +00:00
|
|
|
|
uDMBase, FactuGES_Intf, uDialogUtils, uEditorConexiones;
|
2007-10-03 16:03:28 +00:00
|
|
|
|
|
|
|
|
|
|
const
|
|
|
|
|
|
IE_OFFLINE_ERROR = 'Unexpected error in WinInet HTTP Channel (2)';
|
2011-04-13 19:18:01 +00:00
|
|
|
|
SIN_CONEXION_ERROR = 'No se ha podido establecer una conexi<78>n con el servidor';
|
2007-10-03 16:03:28 +00:00
|
|
|
|
|
2008-02-29 18:55:32 +00:00
|
|
|
|
|
2007-10-03 16:03:28 +00:00
|
|
|
|
function TdmConexion.HayConexion: Boolean;
|
|
|
|
|
|
begin
|
2011-04-13 19:18:01 +00:00
|
|
|
|
if not ROChannel.Connected then
|
|
|
|
|
|
begin
|
|
|
|
|
|
if ProbarConexion(ROChannel.TargetURL) then
|
|
|
|
|
|
ROChannel.Connected := True;
|
|
|
|
|
|
end;
|
2008-03-03 17:37:41 +00:00
|
|
|
|
Result := ROChannel.Connected;
|
2007-10-03 16:03:28 +00:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TdmConexion.ConfigurarConexion;
|
2012-06-11 14:31:03 +00:00
|
|
|
|
var
|
|
|
|
|
|
AEditor : TfEditorConexiones;
|
2007-10-03 16:03:28 +00:00
|
|
|
|
begin
|
2012-06-11 14:31:03 +00:00
|
|
|
|
AEditor := TfEditorConexiones.Create(NIL);
|
|
|
|
|
|
try
|
|
|
|
|
|
AEditor.ListaConexiones := FListaConexiones;
|
|
|
|
|
|
if AEditor.ShowModal = mrOk then
|
|
|
|
|
|
begin
|
|
|
|
|
|
Self.ListaConexiones := AEditor.ListaConexiones;
|
|
|
|
|
|
dmBase.SalvarConfiguracion;
|
2007-10-03 16:03:28 +00:00
|
|
|
|
end;
|
2012-06-11 14:31:03 +00:00
|
|
|
|
|
|
|
|
|
|
finally
|
|
|
|
|
|
AEditor.Release;
|
|
|
|
|
|
AEditor := NIL;
|
|
|
|
|
|
end;
|
2007-10-03 16:03:28 +00:00
|
|
|
|
end;
|
|
|
|
|
|
|
2008-02-29 18:55:32 +00:00
|
|
|
|
procedure TdmConexion.ConfigurarEncriptacionConexion;
|
|
|
|
|
|
begin
|
2008-03-03 17:37:41 +00:00
|
|
|
|
{
|
2008-02-29 18:55:32 +00:00
|
|
|
|
with ROChannel.Encryption do
|
|
|
|
|
|
begin
|
|
|
|
|
|
EncryptionMethod := tetDES;
|
|
|
|
|
|
EncryptionSendKey := '127C1A6A4D85F5EFE0A54E104BF7F695CD6C989C1808A57667EF1218E8ED93FC6CDC531631EB9750';
|
|
|
|
|
|
EncryptionRecvKey := 'C793F1A04FFC8DF91FF9522951F6B6DF921C70B42D74166C6DF0B697797AAA6A243BEC35A9423A51';
|
|
|
|
|
|
UseCompression := True;
|
|
|
|
|
|
end;
|
2008-03-03 17:37:41 +00:00
|
|
|
|
}
|
2008-02-29 18:55:32 +00:00
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TdmConexion.DataModuleCreate(Sender: TObject);
|
|
|
|
|
|
begin
|
2012-06-11 14:31:03 +00:00
|
|
|
|
FListaConexiones := TStringList.Create;
|
2008-02-29 18:55:32 +00:00
|
|
|
|
ConfigurarEncriptacionConexion;
|
2011-04-13 19:18:01 +00:00
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
This property specifies can we reuse the same TCP connection to send and
|
|
|
|
|
|
receive multiple HTTP requests/responses or not. If it set to False then
|
|
|
|
|
|
each pair request/response creates new one connection.
|
|
|
|
|
|
Uses KeepConnection = True allows to improve HTTP performance
|
|
|
|
|
|
for multiple requests.
|
|
|
|
|
|
}
|
|
|
|
|
|
ROChannel.KeepConnection := True;
|
|
|
|
|
|
ROChannel.OnException := ROChannel2Failure;
|
2008-02-29 18:55:32 +00:00
|
|
|
|
end;
|
|
|
|
|
|
|
2007-10-22 07:39:39 +00:00
|
|
|
|
procedure TdmConexion.DataModuleDestroy(Sender: TObject);
|
|
|
|
|
|
begin
|
2012-06-11 14:31:03 +00:00
|
|
|
|
{* if ROChannel.Connected then
|
|
|
|
|
|
ROChannel.Connected := False;*}
|
|
|
|
|
|
FreeAndNil(FListaConexiones);
|
2007-10-22 07:39:39 +00:00
|
|
|
|
end;
|
|
|
|
|
|
|
2012-06-12 11:27:09 +00:00
|
|
|
|
procedure TdmConexion.EstablecerConexion(aID: integer);
|
|
|
|
|
|
begin
|
|
|
|
|
|
Self.TargetURL := FListaConexiones[aID];
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
2008-02-29 18:55:32 +00:00
|
|
|
|
function TdmConexion.GetChannel: TROTransportChannel;
|
2007-10-03 16:03:28 +00:00
|
|
|
|
begin
|
|
|
|
|
|
Result := ROChannel;
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
2012-06-11 14:31:03 +00:00
|
|
|
|
function TdmConexion.GetListaConexiones: TStringList;
|
|
|
|
|
|
begin
|
|
|
|
|
|
Result := FListaConexiones;
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
2007-10-03 16:03:28 +00:00
|
|
|
|
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
|
2008-03-03 17:37:41 +00:00
|
|
|
|
AHTTPChannel: TROWinInetHTTPChannel;
|
2007-10-03 16:03:28 +00:00
|
|
|
|
AROBinMessage: TROBinMessage;
|
|
|
|
|
|
ACoService: TRORemoteService;
|
|
|
|
|
|
begin
|
|
|
|
|
|
if ATargetURL = '' then
|
|
|
|
|
|
raise Exception.Create('No se ha indicado la URL del servidor (HayConexion)');
|
|
|
|
|
|
|
2008-03-03 17:37:41 +00:00
|
|
|
|
AHTTPChannel := TROWinInetHTTPChannel.Create(Self);
|
2007-10-03 16:03:28 +00:00
|
|
|
|
AROBinMessage := TROBinMessage.Create(Self);
|
|
|
|
|
|
ACoService := TRORemoteService.Create(Self);
|
|
|
|
|
|
|
|
|
|
|
|
ShowHourglassCursor;
|
|
|
|
|
|
try
|
2008-02-29 18:55:32 +00:00
|
|
|
|
AROBinMessage.Assign(ROMessage);
|
|
|
|
|
|
|
|
|
|
|
|
AHTTPChannel.Assign(ROChannel);
|
2011-04-13 19:18:01 +00:00
|
|
|
|
AHTTPChannel.OnException := NIL;
|
|
|
|
|
|
|
2007-10-03 16:03:28 +00:00
|
|
|
|
with AHTTPChannel do
|
|
|
|
|
|
begin
|
|
|
|
|
|
Name := 'HTTPChannel';
|
2008-02-29 18:55:32 +00:00
|
|
|
|
|
2007-10-03 16:03:28 +00:00
|
|
|
|
if Length(ATargetURL) > 0 then
|
|
|
|
|
|
TargetURL := ATargetURL
|
|
|
|
|
|
else
|
|
|
|
|
|
TargetURL := ROChannel.TargetURL;
|
|
|
|
|
|
end;
|
2008-09-30 12:12:40 +00:00
|
|
|
|
|
|
|
|
|
|
{ if AHTTPChannel.Encryption.EncryptionMethod <> tetNone then
|
|
|
|
|
|
ShowMessage('funcionando');}
|
2007-10-03 16:03:28 +00:00
|
|
|
|
|
2008-02-29 18:55:32 +00:00
|
|
|
|
ACoService.ServiceName := 'srvLogin';
|
|
|
|
|
|
ACoService.Message := AROBinMessage;
|
|
|
|
|
|
ACoService.Channel := AHTTPChannel;
|
2007-10-03 16:03:28 +00:00
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
(ACoService as IsrvLogin).Ping;
|
|
|
|
|
|
Result := True;
|
|
|
|
|
|
except
|
|
|
|
|
|
Result := False;
|
|
|
|
|
|
end;
|
|
|
|
|
|
finally
|
|
|
|
|
|
FreeAndNil(AHTTPChannel);
|
|
|
|
|
|
FreeAndNil(ACoService);
|
|
|
|
|
|
FreeAndNil(AROBinMessage);
|
|
|
|
|
|
HideHourglassCursor;
|
|
|
|
|
|
end;
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
2008-03-03 17:37:41 +00:00
|
|
|
|
procedure TdmConexion.ROChannel2Failure(Sender: TROTransportChannel;
|
2008-02-29 18:55:32 +00:00
|
|
|
|
anException: Exception; var aRetry: Boolean);
|
2011-04-13 19:18:01 +00:00
|
|
|
|
var
|
|
|
|
|
|
AResult : TModalResult;
|
2007-10-03 16:03:28 +00:00
|
|
|
|
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
|
2008-02-29 18:55:32 +00:00
|
|
|
|
aRetry := True // Si el usuario pulsa en 'Conectar' reintentar la operaci<63>n
|
2011-04-13 19:18:01 +00:00
|
|
|
|
else begin
|
|
|
|
|
|
aRetry := False;
|
|
|
|
|
|
ROChannel.Connected := False;
|
2007-10-03 16:03:28 +00:00
|
|
|
|
Abort; // Si el usuario pulsa en 'Seguir desconectado' parar todo
|
2011-04-13 19:18:01 +00:00
|
|
|
|
end;
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
if (Pos(anException.Message, SIN_CONEXION_ERROR) > 0) then
|
|
|
|
|
|
begin
|
|
|
|
|
|
AResult := ShowErrorMessage('No se ha podido establecer una conexi<78>n con el servidor',
|
|
|
|
|
|
'No se puede continuar porque no se puede establecer una conexi<78>n con el servidor.' + #10#13 +
|
|
|
|
|
|
'Puede reintentar establecer la conexi<78>n o anular la operaci<63>n.',
|
|
|
|
|
|
anException, [TDlgButton_REINTENTAR, TDlgButton_ABORTAR]);
|
|
|
|
|
|
case AResult of
|
|
|
|
|
|
mrRetry : aRetry := True;
|
|
|
|
|
|
mrAbort : begin
|
|
|
|
|
|
aRetry := False;
|
|
|
|
|
|
ROChannel.Connected := False;
|
|
|
|
|
|
Abort;
|
|
|
|
|
|
end;
|
|
|
|
|
|
end;
|
|
|
|
|
|
end;
|
2007-10-03 16:03:28 +00:00
|
|
|
|
end;
|
|
|
|
|
|
|
2012-06-11 14:31:03 +00:00
|
|
|
|
procedure TdmConexion.SetListaConexiones(const Value: TStringList);
|
|
|
|
|
|
begin
|
|
|
|
|
|
FListaConexiones.Clear;
|
|
|
|
|
|
FListaConexiones.AddStrings(Value);
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
2007-10-03 16:03:28 +00:00
|
|
|
|
procedure TdmConexion.SetTargetURL(const Value: String);
|
|
|
|
|
|
begin
|
|
|
|
|
|
ROChannel.TargetURL := Value;
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
end.
|