Proyecto

General

Perfil

Descargar (5,03 KB) Estadísticas
| Rama: | Revisión:
a2ba555f Elisa
program serveraulalinexalumno;

{$mode objfpc}{$H+}

uses
cthreads,
Classes, SysUtils, CustApp, globales, demonio, IdCmdTCPServer,
IdCommandHandlers, IdCustomTCPServer, IdContext, IdComponent,
IdTCPClient,clservidor;

type

{ TMyApplication }

TMyApplication = class(TCustomApplication)
private
procedure hallaUsuarioActivo();
protected
procedure DoRun; override;
public
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
procedure WriteHelp; virtual;

end;

{ TMyApplication }

procedure TMyApplication.DoRun;
var
mensaje: String;
begin
try
if (f_IpEquipo<>'') and ((f_contador>200) or (g_cambioUsuario)) then
begin
g_cambioUsuario:=false;
f_contador:=0;
//writeln('mi ip es '+f_IpEquipo+'; actualizaEstadoEquipo');
miservidor.actualizaEstadoEquipo();
end;
//writeln('f_contador: ',f_contador);

if f_contador_usuario > 247 then
begin
f_contador_usuario:=0;

if (f_IpEquipo='') then f_IpEquipo:=miservidor.ObtenerMiIp();
if (f_IpEquipo<>'') and (not g_datosEnviados) then
begin
g_datosEnviados:=true;
miservidor.EnviarDatosEquipo();

end;
hallaUsuarioActivo();
end;
if f_contador_permisos > 6000 then
begin
f_contador_permisos:=0;
if DirectoryExists('/tmp/aulalinex') then Ejecutar('chmod 777 /tmp/aulalinex -R &');
end;
inc(f_contador);
inc(f_contador_usuario);
inc(f_contador_permisos);
sleep(50);

except
Ejecutar('/etc/init.d/serveraulalinex-alumno start');
end;
end;

procedure TMyApplication.hallaUsuarioActivo();
const
PROPIETARIO_SERVERALUMNOLINEX:string='ps -fu %s| grep serveralumnolinex| grep -v "grep "| tr -s " " | cut -f1 -d" "';
CERRARPUERTO:string='fuser -k -n tcp 37000 2>>/dev/null';
CERRARSERVER:string='killall serveralumnolinex 2>>/dev/null';
INICIASERVER:STRING='su -c "export DISPLAY=:0.0;/usr/bin/serveralumnolinex & 2>>/dev/null" %s & 2>>/dev/null';
SERVERALUNMNOACTIVO:String='ps aux | grep serveralumnolinex| grep -v -c "grep"';
// SERVERALUNMNOACTIVO:String='lsof -c serveralumno | grep "/usr/bin/serveralumnolinex" | grep -v -c "grep"';

var
comando_usuario:String;
nombreusu:string;
prop_serveralumnolinex:string;
iniserver:String;
servercorrecto,serveractivo:string;
usuarioCambiado:Boolean;
iniciado_server:Boolean=false;
begin
try
comando_usuario:='who| grep -v "unknown" | grep -v "pts"| grep "\(:0\)" | head -n 1| cut -f1 -d" "';
nombreusu:=trim(HallarDatos(comando_usuario));
//writeln(nombreusu);
usuarioCambiado:= g_usarioViejo <> nombreusu;
g_usarioViejo:=nombreusu;
if g_soy_canon then g_usuario:=g_host else g_usuario:=nombreusu;
if g_usuario='' then g_usuario:=' ';
g_cambioUsuario := usuarioCambiado ;
serveractivo:=trim(HallarDatos(SERVERALUNMNOACTIVO));
if ((nombreusu<>'') and (usuarioCambiado)) then
begin
prop_serveralumnolinex:=Format(PROPIETARIO_SERVERALUMNOLINEX,[nombreusu]);
prop_serveralumnolinex:=trim(HallarDatos(prop_serveralumnolinex));
if prop_serveralumnolinex = '' then
begin
Ejecutar(CERRARPUERTO);
if serveractivo<>'0' then Ejecutar(CERRARSERVER);
iniserver:=Format(INICIASERVER,[nombreusu]);
Ejecutar(iniserver);
//writeln('El usuario ha cambiado. Iniciarmos serveralumnolinex');
end;
end
else if (nombreusu<>'') and (serveractivo='0') then
begin
Ejecutar(CERRARPUERTO);
iniserver:=Format(INICIASERVER,[nombreusu]);
Ejecutar(iniserver);
//writeln('No hay serveralumnolines. lo inicamos');
end;

except
g_usuario:= g_host;
end;
end;

constructor TMyApplication.Create(TheOwner: TComponent);
const
ORD_HOST:string='hostname | cut -f1 -d"."';
begin
inherited Create(TheOwner);
g_host:=HallarDatos(ORD_HOST);
StopOnException:=True;
hallaUsuarioActivo();
end;

destructor TMyApplication.Destroy;
begin
inherited Destroy;
end;

procedure TMyApplication.WriteHelp;
begin
writeln('Utiliza: ',ExeName,' -h');
end;

var
Application: TMyApplication;

{$IFDEF WINDOWS}{$R serveraulalinex.rc}{$ENDIF}

begin
Application:=TMyApplication.Create(nil);
Application.Run;
Application.Free;
end.