git-svn-id: https://192.168.0.254/svn/Proyectos.Tecsitel_FactuGES2/trunk@1168 0c75b7a4-871f-7646-8a2f-f78d34cc349f
317 lines
8.4 KiB
ObjectPascal
317 lines
8.4 KiB
ObjectPascal
unit uDataModuleServer;
|
||
|
||
interface
|
||
|
||
uses
|
||
SysUtils, Classes, SyncObjs, uDAEngine, uDAIBXDriver, JvLogFile, uROSessions,
|
||
JvComponentBase, JvAppStorage, JvAppRegistryStorage, uDAClasses,
|
||
uDADriverManager, uROClient, uROBinMessage, uROServer, uROIndyTCPServer,
|
||
uROIndyHTTPServer, uDAInterfaces;
|
||
|
||
const
|
||
SERVER_PORT = '8099'; // Puerto por defecto
|
||
DBSERVER_PORT = '3050'; // Puerto de la BD por defecto
|
||
|
||
SESION_LOGININFO = 'LoginInfo';
|
||
|
||
type
|
||
TdmServer = class(TDataModule)
|
||
HTTPServer: TROIndyHTTPServer;
|
||
BINMessage: TROBinMessage;
|
||
DriverManager: TDADriverManager;
|
||
JvAppRegistryStorage: TJvAppRegistryStorage;
|
||
SessionManager: TROInMemorySessionManager;
|
||
JvLogFile1: TJvLogFile;
|
||
DAIBXDriver: TDAIBXDriver;
|
||
ConnectionManager: TDAConnectionManager;
|
||
procedure DataModuleCreate(Sender: TObject);
|
||
procedure DataModuleDestroy(Sender: TObject);
|
||
procedure HTTPServerBeforeServerActivate(Sender: TObject);
|
||
procedure DriverManagerTraceEvent(Sender: TObject; const Text: string;
|
||
Tag: Integer);
|
||
procedure SessionManagerSessionDeleted(const aSessionID: TGUID;
|
||
IsExpired: Boolean);
|
||
procedure ConnectionManagerConnectionAcquired(Sender: TDAConnectionManager;
|
||
const Connection: IDAConnection);
|
||
private
|
||
FEscribirLog : TCriticalSection;
|
||
FConnectionName : string;
|
||
function GetConnectionString: string;
|
||
procedure IniciarLog;
|
||
procedure DetenerLog;
|
||
public
|
||
FDBServer : string;
|
||
FDBPort : string;
|
||
FDBPath : string;
|
||
FDBUser : string;
|
||
FDBPass : string;
|
||
|
||
FOSCServer : string;
|
||
|
||
FInicioWindows : Boolean;
|
||
FServerPort : string;
|
||
procedure LeerConfiguracion;
|
||
procedure SalvarConfiguracion;
|
||
procedure RefrescarConexion;
|
||
procedure EscribirLog(const AMensaje : String);
|
||
function DarNuevaConexion : IDaConnection;
|
||
property ConnectionName : string read FConnectionName write FConnectionName;
|
||
end;
|
||
|
||
|
||
function DarRutaInformes : String;
|
||
function Encrypt (const Source : string) : String;
|
||
function Decrypt (const Source : string) : String;
|
||
function GetNextAutoinc(AConnection : IDAConnection; const GeneratorName: string): integer;
|
||
|
||
var
|
||
dmServer: TdmServer;
|
||
|
||
implementation
|
||
|
||
{$R *.dfm}
|
||
|
||
uses
|
||
uROComboService, uROEncryption, DateUtils, JclFileUtils, ActiveX, Forms, Registry, Windows, Dialogs,
|
||
ExtCtrls, uSesionesUtils, uServerAppUtils;
|
||
|
||
function DarRutaInformes : String;
|
||
begin
|
||
Result := ExtractFilePath(Application.ExeName) + 'Informes' + PathDelim;
|
||
end;
|
||
|
||
function GetNextAutoinc(AConnection : IDAConnection; const GeneratorName: string): integer;
|
||
var
|
||
ds: IDADataset;
|
||
begin
|
||
ds := AConnection.NewDataset(Format('SELECT Gen_id(%s,1) FROM RDB$Database', [GeneratorName]));
|
||
ds.Open;
|
||
result := ds.Fields[0].Value;
|
||
ds.Close;
|
||
end;
|
||
|
||
procedure WriteAutoRun(Name, Value : String);
|
||
var
|
||
reg: TRegistry;
|
||
begin
|
||
reg := TRegistry.Create;
|
||
try
|
||
reg.RootKey := HKEY_LOCAL_MACHINE;
|
||
reg.LazyWrite := False;
|
||
reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run', False);
|
||
reg.WriteString(Name, Value);
|
||
reg.CloseKey;
|
||
finally
|
||
reg.free;
|
||
end;
|
||
end;
|
||
|
||
procedure DeleteAutoRun(Name : String);
|
||
var
|
||
reg: TRegistry;
|
||
begin
|
||
reg := TRegistry.Create;
|
||
try
|
||
reg.RootKey := HKEY_LOCAL_MACHINE;
|
||
reg.LazyWrite := False;
|
||
reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run', False);
|
||
if reg.ValueExists(Name) then
|
||
reg.DeleteValue(Name);
|
||
reg.CloseKey;
|
||
finally
|
||
reg.free;
|
||
end;
|
||
end;
|
||
|
||
|
||
function Encrypt (const Source : string) : String;
|
||
var
|
||
i:integer;
|
||
begin
|
||
Randseed := 28;
|
||
Result := '';
|
||
for i:=1 to length(Source) do
|
||
Result := Result + chr(ord(Source[i]) + Random(10) + 1);
|
||
end;
|
||
|
||
function Decrypt (const Source : string) : String;
|
||
var
|
||
i : integer;
|
||
begin
|
||
Randseed:= 28;
|
||
Result := '';
|
||
for i := 1 to length(Source) do
|
||
Result := Result + chr(ord(Source[i]) - Random(10) - 1);
|
||
end;
|
||
|
||
procedure TdmServer.ConnectionManagerConnectionAcquired(
|
||
Sender: TDAConnectionManager; const Connection: IDAConnection);
|
||
begin
|
||
{ Si se adquiere una conexi<78>n del spool de conexiones puede que
|
||
sea una conexi<78>n ya abierta. Aqu<71> nos aseguramos de que est<73>
|
||
cerrada y lista para usar. }
|
||
if Connection.InTransaction then
|
||
Connection.RollbackTransaction;
|
||
end;
|
||
|
||
function TdmServer.DarNuevaConexion: IDAConnection;
|
||
begin
|
||
with ConnectionManager do
|
||
Result := NewConnection(GetDefaultConnectionName, False);
|
||
if not Result.isAlive then
|
||
Result.Open;
|
||
end;
|
||
|
||
procedure TdmServer.IniciarLog;
|
||
begin
|
||
FEscribirLog := TCriticalSection.Create;
|
||
|
||
JvLogFile1.Active := False;
|
||
|
||
{
|
||
JvLogFile1.FileName := ExtractFilePath(Application.ExeName) + 'ServerLog.txt';
|
||
JvLogFile1.AutoSave := True;
|
||
JvLogFile1.Active := True;
|
||
JvLogFile1.Clear;
|
||
}
|
||
end;
|
||
|
||
procedure TdmServer.DataModuleCreate(Sender: TObject);
|
||
begin
|
||
LeerConfiguracion;
|
||
RefrescarConexion;
|
||
IniciarLog;
|
||
end;
|
||
|
||
function TdmServer.GetConnectionString: string;
|
||
begin
|
||
Result := 'IBX?Server=' + FDBServer + '/' + FDBPort
|
||
+ ';Database=' + FDBPath
|
||
+ ';UserID=' + FDBUser
|
||
+ ';Password=' + Decrypt(FDBPass)
|
||
+ ';Dialect=3;Charset=ISO8859_1;';
|
||
|
||
{ Result := 'FIB?Server=' + FDBServer + '/' + FDBPort
|
||
+ ';Database=' + FDBPath
|
||
+ ';UserID=' + FDBUser
|
||
+ ';Password=' + Decrypt(FDBPass)
|
||
+ ';Dialect=3;Charset=ISO8859_1;';}
|
||
end;
|
||
|
||
procedure TdmServer.LeerConfiguracion;
|
||
begin
|
||
with JvAppRegistryStorage do
|
||
begin
|
||
FDBServer := ReadString('ServerDB', 'localhost');
|
||
FDBPort := ReadString('PortDB', DBSERVER_PORT);
|
||
FDBPath := ReadString('PathDB');
|
||
FDBUser := ReadString('UserDB', '');
|
||
FDBPass := ReadString('PassDB', '');
|
||
|
||
FOSCServer := ReadString('ServerOSC');
|
||
|
||
FServerPort := ReadString('ServerPort', SERVER_PORT);
|
||
FInicioWindows := ReadBoolean('InicioWindows', False);
|
||
end;
|
||
end;
|
||
|
||
procedure TdmServer.SalvarConfiguracion;
|
||
begin
|
||
with JvAppRegistryStorage do
|
||
begin
|
||
BeginUpdate;
|
||
try
|
||
WriteString('ServerDB', FDBServer);
|
||
WriteString('PortDB', FDBPort);
|
||
WriteString('PathDB', FDBPath);
|
||
WriteString('UserDB', FDBUser);
|
||
WriteString('PassDB', FDBPass);
|
||
WriteString('ServerPort', FServerPort);
|
||
|
||
WriteString('ServerOSC', FOSCServer);
|
||
|
||
WriteBoolean('InicioWindows', FInicioWindows);
|
||
if FInicioWindows then
|
||
WriteAutoRun('AdminPV_Server', Application.ExeName)
|
||
else
|
||
DeleteAutoRun('AdminPV_Server');
|
||
finally
|
||
EndUpdate
|
||
end;
|
||
end;
|
||
end;
|
||
|
||
procedure TdmServer.SessionManagerSessionDeleted(const aSessionID: TGUID;
|
||
IsExpired: Boolean);
|
||
var
|
||
aSession : TROSession;
|
||
begin
|
||
aSession := SessionManager.FindSession(aSessionID);
|
||
if Assigned(aSession) then
|
||
SesionesHelper.DeleteSessionObject(aSession, SESION_LOGININFO);
|
||
end;
|
||
|
||
procedure TdmServer.DataModuleDestroy(Sender: TObject);
|
||
begin
|
||
SessionManager.ClearSessions(False);
|
||
ConnectionManager.ClearPool;
|
||
HTTPServer.Active := False;
|
||
DetenerLog;
|
||
end;
|
||
|
||
procedure TdmServer.DetenerLog;
|
||
begin
|
||
DriverManager.OnTraceEvent := NIL;
|
||
FreeAndNIL(FEscribirLog);
|
||
end;
|
||
|
||
procedure TdmServer.DriverManagerTraceEvent(Sender: TObject; const Text: string;
|
||
Tag: Integer);
|
||
begin
|
||
EscribirLog(Text + #10#13 + #10#13);
|
||
end;
|
||
|
||
procedure TdmServer.EscribirLog(const AMensaje: String);
|
||
begin
|
||
if Assigned(FEscribirLog) then
|
||
begin
|
||
FEscribirLog.Enter;
|
||
try
|
||
JvLogFile1.Add(AMensaje);
|
||
finally
|
||
FEscribirLog.Leave;
|
||
end;
|
||
end;
|
||
end;
|
||
|
||
procedure TdmServer.RefrescarConexion;
|
||
begin
|
||
HTTPServer.Active := False;
|
||
ConnectionManager.Connections.GetDefaultConnection.ConnectionString := '';
|
||
|
||
ConnectionName := ConnectionManager.GetDefaultConnectionName;
|
||
ConnectionManager.Connections.GetDefaultConnection.ConnectionString := GetConnectionString;
|
||
|
||
if not ROIsService then
|
||
ShowBalloonHint('Conectado a ' + ConnectionName, bfInfo);
|
||
|
||
CoInitialize(nil);
|
||
HTTPServer.Active := TRUE;
|
||
end;
|
||
|
||
procedure TdmServer.HTTPServerBeforeServerActivate(Sender: TObject);
|
||
begin
|
||
HTTPServer.Port := StrToInt(FServerPort);
|
||
{
|
||
with HTTPServer.Encryption do
|
||
begin
|
||
EncryptionMethod := tetDES;
|
||
EncryptionSendKey := 'C793F1A04FFC8DF91FF9522951F6B6DF921C70B42D74166C6DF0B697797AAA6A243BEC35A9423A51';
|
||
EncryptionRecvKey := '127C1A6A4D85F5EFE0A54E104BF7F695CD6C989C1808A57667EF1218E8ED93FC6CDC531631EB9750';
|
||
UseCompression := True;
|
||
end;
|
||
}
|
||
end;
|
||
|
||
end.
|