logo search
Дипломы2 / Андрей / Чужие дипломы / Диплом1 / диплом

Приложение 3

Листинг шлюза

{===== Проект " Шлюз ВТК-12 " ======}

{ ОАО "Морион" ИТЦ СТ 2003г. }

{ Автор: Бажгин М.С. }

{========================================}

library gate;

uses

Classes,

Windows,

shlusobj,

SysUtils;

const PackType:array [1..3] of EQUIP_Type = ('OK','VD','VDN');

E1name:array [1..6] of char = ('A','B','C','D','E','F');

var NEList:TList;

CPackList:TList;

OKList:TList;

AGList:TList;

E1List:TList;

LinkEndList:TList;

CTPList:TList;

VTKList:TStringList;

GateID:word;

IPID:OID;

InitPath:string;

{$R *.RES}

//-------------------------------------------------------------------------

Function LoadVTK(pathip: string):byte; export; stdcall;

//загружает и декомпилирует конфигурация

var GPEnabled:boolean;

i,j,z:integer;

tmpid:OID;

tmpfile:TMemoryStream;

t1,t2,k,buf1,buf11:byte;

VDNEn,b1,b2:boolean;

SlotNumb,ChanelNumb:byte;

s:string;

Sink,Source: set of byte;

boardType:array[1..9] of byte;

TableSink:array [0..255] of integer;

TableSource:array [0..255] of integer;

tmpNE:TNE;

tmpCPack:TCPack;

tmpAG:TAG;

tmpOK:TTTP;

tmpOKLIst:TList;

tmpTTPBid:TTTPBid;

tmpTTPSink:TTTPSink;

tmpTTPSource:TTTPSource;

tmpLinkEndSource:TLinkEndSource;

tmpLinkEndSink:TLinkEndSink;

tmpCTPSource:TCTPSource;

tmpCTPSink:TCTPSink;

TTPindex,LinkEndindex,CTPindex:integer;

Function PortNormalise(Port:byte):byte;

//приведение значения к стандартному для удобства

begin

case Port of

$0B: Result:=0; //Cor

$0A: Result:=1; //First

$0F: Result:=2; //Second or F

$06: Result:=2; //Second or F

$0E: Result:=3; //E

$0D: Result:=4; //D

$09: Result:=5; //C

$05: Result:=6; //B

$01: Result:=7; //A

else Result:=0; //вообще-то это не допустимо

end;

end;

unction GetID(i1,i2,i3:byte):int64;

begin

GetID:=(i1 mod 16)*4096+(i2 mod 16)*256+i3+Int64(GateID)*65536*65536*65536

+IPID*65536;

end;

Function GetSink(oldID:OID):OID;

begin

case oldID of

224..255: Result:=GetID(10,0,oldID-224);

192..223: Result:=GetID(10,1,oldID-192);

160..191: Result:=GetID(10,2,oldID-160);

128..159: Result:=GetID(10,3,oldID-128);

96..127: Result:=GetID(10,4,oldID-96);

64..95 : if GPEnabled then

Result:=GetID(5,0,oldID-64)

else

Result:=GetID(10,5,oldID-64);

32..63 : Result:=GetID(5,0,oldID-32);

else Result:=0;

end;

end;

Function GetSource(oldID:OID):OID;

begin

case oldID of

224..255: Result:=GetID(9,0,oldID-224);

192..223: Result:=GetID(9,1,oldID-192);

160..191: Result:=GetID(9,2,oldID-160);

128..159: Result:=GetID(9,3,oldID-128);

96..127: Result:=GetID(9,4,oldID-96);

64..95 : if GPEnabled then

Result:=GetID(6,0,oldID-64)

else

Result:=GetID(9,5,oldID-64);

32..63 : Result:=GetID(6,0,oldID-32);

else Result:=0;

end;

end;

begin

NEList.Clear;

AGList.Clear;

CPackList.Clear;

OKList.Clear;

LinkEndList.Clear;

E1List.Clear;

CTPList.Clear;

i:=0;

if (FileExists(InitPath+PathIP+'\vtk.cfg')=false) or

(FileExists(InitPath+PathIP+'\switch.cfg')=false) then i:=2;

if i <> 0 then //если нет хотя бы одного файла, то ошибка

begin

Result:=i;

exit;

end;

//------- начало загрузки -----------------

IPID:=0;

for i:=0 to 3 do

IPID:=IPID*256+StrToInt(copy(PathIP,i*4+1,3));

GPEnabled:=False;

tmpNE:=TNE.Create;

tmpNE.ID:=GetID(1,0,0);

tmpNE.TypeNE:='VTK-12';

tmpNE.Name:=PathIP;

tmpNE.operationalState:=enabled;

tmpNE.TypeOBJ:=1;

tmpFile:=TMemoryStream.Create;

tmpFile.LoadFromFile(InitPath+PathIP+'\vtk.cfg');

tmpFile.Position:=$02;

for i:=1 to 9 do //опознаем типы плат

begin

tmpFile.Read(buf1,1);

boardtype[i]:=buf1;

if buf1<>0 then

begin

tmpCPack:=TCPack.Create;

tmpCPack.TypecPack:=PackType[buf1];

tmpCPack.ID:=GetID(2,0,i-1);

tmpCPack.TypeObj:=2;

tmpCPack.operationalState:=enabled;

CPackList.Add(tmpCPack);

end;

end;

tmpNE.numCPacks:=CPackList.Count;

tmpNE.numAGs:=2;

VDNEn:=false;

t1:=0;

t2:=0;

tmpOKList:=TList.Create;

for k:=0 to 1 do

begin

tmpFile.Position:=$0B+$20*k;

for i:=0 to 31 do // читаем шины G и GP

begin

tmpFile.Read(Buf1,1);

if Buf1<>0 then

begin

tmpOK:=TTTP.Create;

if k=1 then GPEnabled:=true;

SlotNumb:=1+(buf1-1) div 4;

ChanelNumb:=buf1-4*((buf1-1) div 4);

case BoardType[SlotNumb] of

1: s:='OK';

2: s:='VD';

3: s:='VDN';

end;

s:=s+IntToStr(SlotNumb)+'['+IntToStr(ChanelNumb)+']';

tmpOK.Id:=(i+k*32+32);

tmpOK.CPack_id:=GetID(2,0,SlotNumb-1);

if not ((SlotNumb=t1) and (ChanelNumb=t2))

then

begin

buf11:=1;

case BoardType[SlotNumb] of

2: s:=s+'.1';

3: begin s:=s+'.1'; VDNEn:=true; end;

end;

end

else

begin

inc(buf11);

case BoardType[SlotNumb] of

2: s:=s+'.2';

3: begin

VDNEn:=false;

s:=s+'.'+IntToStr(buf11);

end;

end;

end;

tmpOK.Name:=s;

tmpOK.SignalId:=e0CI;

tmpOKList.Add(tmpOK);

t1:=SlotNumb;

t2:=ChanelNumb;

end

else //если плата VDN

if VDNEn then

begin

tmpOK:=TTTP.Create;

inc(Buf11);

tmpOK.Id:=i+k*32+32;

tmpOK.CPack_id:=GetID(2,0,t1);

s:='VDN'+IntToStr(t1)+'['+IntToStr(t2)+'].'+IntToStr(buf11);

tmpOK.Name:=s;

tmpOK.SignalId:=e0CI;

tmpOKList.Add(tmpOK);

end;

end;

end;

//-----коммутация-------------

tmpFile.LoadFromFile(InitPath+PathIP+'\switch.cfg');

tmpFile.Position:=$16; //метка на адрес первой конфигурации

tmpFile.Read(buf1,1); //читаем адрес первой конфигурации

tmpFile.Read(buf11,1);

tmpFile.Position:=buf1*256+buf11;

for i:=0 to 255 do //загрузка таблицы коммутации

begin

tmpFile.Read(buf1,1);

if buf1=$10 then //в таблице недопустимое значение - возникает, когда участник в конференции

begin

TableSink[i]:=0; //заводим его на 0

tmpFile.Position:=tmpFile.Position+1;

end

else

begin

tmpFile.Read(buf11,1);

TableSink[i]:=PortNormalise(buf1)*32+buf11;

end;

if (TableSink[i]=31) or (TableSink[i]=i) then TableSink[i]:=0; //если нет связи или заведен на себя

end;

tmpFile.Destroy;

for i:=32 to 255 do //избавляемся от ЦОРов

if (TableSink[i]>0) and (TableSink[i]<32)

then TableSink[i]:=TableSink[TableSink[i]];

Sink:=’’;

Source:=’’;

for i:=32 to 255 do

if TableSink[i]<>0 then

begin

Include(Sink,i);

Include(Source,TableSink[i]);

TableSource[TableSink[i]]:=i;

end;

TTPindex:=0;

for i:=0 to tmpOKList.Count-1 do

begin

tmpID:=TTTP(tmpOKList[i]).ID;

if (tmpID in Sink) and (tmpID in Source) then

begin

dec(TTPindex);

tmpTTPBid:=TTTPBid.Create;

with tmpTTPBid do begin

TypeObj:=4;

Name:=TTTP(tmpOKList[i]).Name+'_bid';

ID:=GetID(4,0,tmpID-32);

TTPSource_ID:=GetID(5,0,tmpID-32);

TTPSink_ID:=GetID(6,0,tmpID-32);

end;

OKList.Add(tmpTTPBid);

end;

if (tmpID in Source) then

begin

inc(TTPindex);

tmpTTPSource:=TTTPSource.Create;

with tmpTTPSource do begin

TypeObj:=5;

ID:=GetID(5,0,tmpID-32);

Name:=TTTP(tmpOKList[i]).Name;

tp_id:=GetSource(TableSource[tmpID]);

CPack_id:=TTTP(tmpOKList[i]).CPack_id;

SignalID:=TTTP(tmpOKList[i]).SignalID;

end;

OKList.Add(tmpTTPSource);

end;

if (tmpID in Sink) then

begin

inc(TTPindex);

tmpTTPSink:=TTTPSink.Create;

with tmpTTPSink do begin

TypeObj:=6;

ID:=GetID(6,0,tmpID-32);

Name:=TTTP(tmpOKList[i]).Name;

tp_id:=GetSink(TableSink[tmpID]);

CPack_id:=TTTP(tmpOKList[i]).CPack_id;

SignalID:=TTTP(tmpOKList[i]).SignalID;

end;

OKList.Add(tmpTTPSink);

end;

end;

tmpAG:=TAG.Create;

tmpAG.ID:=GetID(3,0,0);

tmpAG.TypeObj:=3;

tmpAG.numTTPs:=TTPindex;

AGList.Add(tmpAG);

if GPEnabled then //если задействована шина GP

k:=5

else

k:=6;

TTPindex:=0;

LinkEndindex:=0;

//---------------------------------

for i:=1 to k do

begin

b1:=false;

b2:=false;

for j:=0 to 31 do

begin

tmpID:=256-i*32+j;

if (tmpID in Source) then b2:=true;

if (tmpID in Sink) then b1:=true;

end;

if b1 and b2 then //если порт двунаправленный

begin

inc(TTPindex);

tmpTTPBid:=TTTPBid.Create;

with tmpTTPBid do begin

Name:=E1Name[i]+'_bid';

ID:=GetID(4,1,i-1);

TypeObj:=4;

TTPSource_ID:=GetID(5,1,i-1);

TTPSink_ID:=GetID(6,1,i-1);

end;

E1List.Add(tmpTTPBid);

end;

if b1 then

begin

if b1<>b2 then inc(TTPindex);

tmpTTPSource:=TTTPSource.Create;

with tmpTTPSource do begin

TypeObj:=5;

ID:=GetID(5,1,i-1);

Name:=E1Name[i];

LinkEnd_id:=GetID(7,0,i-1);

SignalID:=e1CI;

end;

E1List.Add(tmpTTPSource);

tmpLinkEndSource:=TLinkEndSource.Create;

with tmpLinkEndSource do begin

TypeOBJ:=7;

ID:=GetID(7,0,i-1);

TTP_ID:=GetID(5,1,i-1);

CTPIndex:=0;

for z:=0 to 1 do

for j:=1 to 15 do

begin

tmpCTPSource:=TCTPSource.Create;

with tmpCTPSource do begin

TypeOBJ:=9;

ID:=GetID(9,i-1,j+z*16);

tp_id:=GetSink(TableSink[256-i*32+j+z*16]);

SignalID:=e0CI;

end;

inc(CTPIndex);

CTPList.Add(tmpCTPSource);

end;

numCTPs:=CTPIndex;

end;

LinkEndList.Add(tmpLinkEndSource);

end;

if b2 then

begin

if b1<>b2 then inc(TTPindex);

tmpTTPSink:=TTTPSink.Create;

with tmpTTPSink do begin

TypeObj:=6;

ID:=GetID(6,1,i-1);

Name:=E1Name[i];

LinkEnd_id:=GetID(8,0,i-1);

SignalID:=e1CI;

end;

E1List.Add(tmpTTPSink);

tmpLinkEndSink:=TLinkEndSink.Create;

with tmpLinkEndSink do begin

TypeOBJ:=8;

ID:=GetID(8,0,i-1);

TTP_ID:=GetID(6,1,i-1);

CTPIndex:=0;

for z:=0 to 1 do

for j:=1 to 15 do

begin

tmpCTPSink:=TCTPSink.Create;

with tmpCTPSink do begin

TypeOBJ:=10;

ID:=GetID(10,i-1,j+z*16);

tp_id:=GetSource(TableSource[256-i*32+j+z*16]);

SignalID:=e0CI;

end;

inc(CTPIndex);

CTPList.Add(tmpCTPSink);

end;

numCTPs:=CTPIndex;

end;

LinkEndList.Add(tmpLinkEndSink);

end;

end;

tmpAG:=TAG.Create;

tmpAG.ID:=GetID(3,0,1);

tmpAG.TypeObj:=3;

tmpAG.numTTPs:=TTPindex;

AGList.Add(tmpAG);

tmpNE.numLinkEnds:=LinkEndList.Count;

NEList.Add(tmpNE);

//-------конец загрузки-----------------

tmpOKList.Clear;

tmpOKList.Free;

LoadVTK:=0;

end;

//-------------------------------------------------------------------------

Procedure GateInit(idGate:word; Path:PChar; var PathList:TStringList); export; stdcall;

//инициализация шлюза

var sr:TSearchRec;

ires:integer;

Function VTKExists(pathvtk: string):boolean;

//поиск конфигурации VTK

begin

if FileExists(Pathvtk+'\vtk.cfg') and FileExists(Pathvtk+'\switch.cfg') then

Result:=true

else

Result:=false;

end;

Аunction PathIsIP(pathvtk: string):boolean;

const digit = ['0'..'9'];

var i,j:byte;

begin

Result:=true;

if Length(pathvtk)<>15 then //если длина не соответствует IP-адресу

begin

Result:=false;

exit;

end;

for i:=1 to 3 do

if pathvtk[i*4]<>'.' then Result:=false; //если точки в нужном месте

for i:=0 to 3 do //цифры на нужных позициях

for j:=1 to 3 do

if (pathvtk[i*4+j] in digit) = false then

begin

Result:=false;

exit;

end;

for i:=0 to 3 do //числа не более 255

if StrToInt(copy(pathvtk,i*4+1,3))>255 then

Result:=false;

end;

begin

GateID:=idGate;

InitPath:=Path+'\';

PathList.Clear;

VTKList.Clear;

ires:=FindFirst(InitPath+'*',faDirectory,sr);

while ires=0 do

begin

if (sr.name<>'.') and (sr.name<>'..') and (sr.attr and $10 <>0) then

if VTKExists(InitPath+sr.name) and PathIsIP(sr.name) then

begin

VTKList.Add(sr.name);

PathList.Add(sr.name);

end;

ires:=FindNext(sr);

end;

FindClose(sr);

end;

//-------------------------------------------------------------------------

Function GetNEData(index: LongInt; var List: TList):byte; export; stdcall;

var i,j,CTPIndex:integer;

begin

if (index > VTKList.Count) or (index <= 0) then

begin

Result:=1;

Exit;

end;

List.Clear;

Result:=LoadVTK(VTKList.strings[index-1]);

List.Add(NEList[0]);

for i:=1 to CPackList.Count do

List.Add(CPackList[i-1]);

List.Add(AGList[0]);

for i:=0 to OKList.Count-1 do

List.Add(OKList[i]);

List.Add(AGList[1]);

for i:=0 to E1List.Count-1 do

List.Add(E1List[i]);

CTPIndex:=0;

for i:=0 to LinkEndList.Count-1 do

begin

List.Add(LinkEndList[i]);

for j:=1 to TLinkEnd(LinkEndList[i]).numCTPs do

begin

List.Add(CTPList[CTPIndex]);

inc(CTPIndex);

end;

end;

end;

//-------------------------------------------------------------------------

Procedure GateClose; export; stdcall;

begin

VTKList.Clear;

end;

//-------------------------------------------------------------------------

Function GetGateInfo:PChar; export; stdcall;

begin

Result:='Gate VTK-12 v1.1';

end;

//-------------------------------------------------------------------------

procedure DLLProcService(Reason: Integer);

begin

if Reason = DLL_PROCESS_DETACH then

begin

VTKList.Free;

NEList.Free;

CTPList.Free;

CPackList.Free;

LinkEndList.Free;

OKList.Free;

E1List.Free;

AGList.Free;

end;

end;

//-------------------------------------------------------------------------

exports

GetGateInfo,

GateInit,

GetNEData,

GateClose;

begin

DLLProc := @DLLProcService;

GateID:=0;

NEList:=TList.Create;

CPackList:=TList.Create;

AGList:=TList.Create;

OKList:=TList.Create;

E1List:=TList.Create;

LinkEndList:=TList.Create;

CTPList:=TList.Create;

VTKList:=TStringList.Create;

end.