From 7f9ce08e19dd30bdf4509b71f7c624b434c1d5f1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tom=C3=A1=C5=A1=20Brada?= Date: Wed, 21 Oct 2015 11:36:14 +0200 Subject: [PATCH] 9 --- Store1.pas | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ upmgr.pas | 13 +++++----- 2 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 Store1.pas diff --git a/Store1.pas b/Store1.pas new file mode 100644 index 0000000..2bcad5a --- /dev/null +++ b/Store1.pas @@ -0,0 +1,86 @@ +UNIT Store1; +{Take tracks of files in store} +{just simple, no cleaning, etc} +INTERFACE +uses SysUtils; + +type +tfid=array [0..19] of byte; +tStoreObjectInfo=object + final:boolean; {hash matched} + rc:Word; {0=no error 1=not found, other} + length:LongWord; {the whole file} + seglen:longword; {from cur to end of segment} + + procedure Open(const fid:tfid); + procedure Close; + procedure SegSeek(ofs:LongWord); + procedure ReadAhead(cnt:Word; into:pointer); + procedure WaitRead; {wait for read to finish, rc} + private + dh:tHandle; {handle to the data file} + filename:string[80]; + procedure mkfilen(var d:string; flag:char; const fid:tfid); +end; + +IMPLEMENTATION +const prefix='object'; + +procedure tStoreObjectInfo.mkfilen(var d:string; flag:char; const fid:tfid); + function hc(b:byte):char; + begin + if b<10 then hc:=char(ord('0')+b) + else hc:=char(ord('A')-10+b); + end; + var b,i:byte; + begin + d:=prefix+flag+DirectorySeparator; + b:=system.length(d); + SetLength(d,b+40); + for i:=0 to 19 do begin + filename[b+(i*2)]:=hc(fid[i] shr 4); + filename[b+(i*2)+1]:=hc(fid[i] and $F); + end; +end; +procedure tStoreObjectInfo.Open(const fid:tfid); + begin + mkfilen(filename,'f',fid); + dh:=FileOpen(filename,fmOpenRead); + if dh<>-1 then begin + rc:=0; + final:=true; + length:=1000; + end else begin + Writeln('Store1: open failed for file ',filename,', ioresult=',IOResult); + rc:=2; + end; +end; +procedure tStoreObjectInfo.ReadAhead(cnt:Word; into:pointer); + var red:LongWord; + begin + //todo, do real async read + red:=FileRead(dh,into^,cnt); + if red=cnt then rc:=0 else begin + //todo + writeln('Store1: read ',red,' out of ',cnt,' requested bytes'); + rc:=2; + end; +end; +procedure tStoreObjectInfo.WaitRead; {wait for read to finish, rc} + begin + //todo +end; +procedure tStoreObjectInfo.Close; + begin + FileClose(dh); +end; +procedure tStoreObjectInfo.SegSeek(ofs:longword); + begin + if ofs=0 then begin + rc:=0; + seglen:=length; + end else rc:=7; +end; + +END. + \ No newline at end of file diff --git a/upmgr.pas b/upmgr.pas index c4480d3..4d1af74 100644 --- a/upmgr.pas +++ b/upmgr.pas @@ -5,7 +5,7 @@ INTERFACE USES Chat,TC,opcode,ServerLoop,MemStream,NetAddr; IMPLEMENTATION -USES ZidanStore; +USES Store1; type tAggr_ptr=^tAggr; @@ -19,7 +19,6 @@ tPrv=object isOpen,Active:boolean; seglen:LongWord; oinfo:tStoreObjectInfo; - datafile:file of byte; procedure Init(ag:tAggr_ptr; var nchat:tChat; msg: tSMsg); procedure OnMsg(msg:tSMsg; data:boolean); procedure IdleTimeout; @@ -74,7 +73,6 @@ procedure tPrv.DoGET(const fid:tfid; base,limit:LongWord); else begin err.WriteByte(upErrIO); err.WriteByte(oinfo.rc) end; ch^.Send(err); end else begin - datafile:=oinfo.hnd; isopen:=true; DoSeg(base,limit); end; @@ -94,7 +92,6 @@ procedure tPrv.DoSEG(base,limit:LongWord); if Active then Stop; end else begin err.WriteByte(upINFO); - datafile:=oinfo.hnd; err.WriteWord(0,2); err.WriteWord(oinfo.length,4); seglen:=limit; @@ -154,7 +151,7 @@ procedure tPrv.Cont; var rs:LongWord; var buf:array [1..2048] of byte; begin - writeln('upmgr: CONT! ',chan); +// writeln('upmgr: CONT! ',chan); Assert(Active and isOpen); sz:=aggr^.tcs.MaxSize(sizeof(buf))-1; if sz>SegLen then sz:=SegLen; @@ -163,8 +160,10 @@ procedure tPrv.Cont; s.Init(@buf,0,sizeof(buf)); aggr^.tcs.WriteHeaders(s); s.WriteByte(Chan); Assert(sz<=s.WrBufLen); - BlockRead(datafile,s.WrBuf^,sz,rs); s.WrEnd(rs); - Assert(RS=sz);//todo + oinfo.ReadAhead(sz,s.WrBuf); //todo + oinfo.WaitRead; + Assert(oinfo.rc=0); //todo + s.WrEnd(sz); aggr^.tcs.Send(s); //FreeMem(s.base,s.size); SegLen:=SegLen-sz; -- 2.11.4.GIT