-
Notifications
You must be signed in to change notification settings - Fork 0
/
dhlt.pas
118 lines (107 loc) · 3.02 KB
/
dhlt.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
unit dhlt;
{DHT Lookup Test}
INTERFACE
IMPLEMENTATION
uses Classes,ServerLoop,ObjectModel,opcode,DHT;
TYPE
tSampleSearch=class(tCustomSearch)
Values:ansiString;
constructor Create(const iTarget:tPID);
protected
procedure Cleanup; override;
procedure Exhausted; override;
procedure HandleReply(var sAddr: tNetAddr; var sPID: tPID; op:byte; st:tStream); override;
end;
t=object
SampleSearch:tSampleSearch;
procedure DoIt;
procedure SearchResult( task:tTask; event:tTaskEvent; data:pointer );
end;
procedure t.DoIt;
const id1:array [0..19] of byte=($CD,$BF,$75,$83,$B1,$59,$44,$60,$A9,$A5,$CC,$F3,$E8,$E0,$B7,$F1,$3D,$1A,$6B,$DB);
begin
SampleSearch:=tSampleSearch.Create(id1);
SampleSearch.Attach(@SearchResult);
writeln('dhlt: start lookup');
end;
procedure t.SearchResult( task:tTask; event:tTaskEvent; data:pointer );
begin
writeln('dhlt: called back ',event,assigned(data),SampleSearch.Values);
end;
constructor tSampleSearch.Create(const iTarget:tPID);
const cReq:string='Hello!';
begin
inherited Create;
Target:=iTarget;
Query:=tMemoryStream.Create;
with Query do begin
w1(opcode.dhtTestQuery);
wb(TrID,2);
wb(DHT.MyID,20);
wb(Target,20);
wb(cReq[1],system.length(cReq));
end;
LoadNodes;
Shedule(1,@Step);
end;
procedure tSampleSearch.Exhausted;
begin
if Values=''
then SendEvent(tevError,nil)
else SendEvent(tevComplete,nil);
end;
procedure tSampleSearch.Cleanup;
begin
values:='';
inherited;
end;
procedure tSampleSearch.HandleReply(var sAddr: tNetAddr; var sPID: tPID; op:byte; st:tStream);
var node:integer;
begin
node:=self.AddNode(sPID, sAddr);
if OP=opcode.dhtNodes then begin
writeln('SampleSearch@',string(@self),': Nodes from ',string(sAddr));
if node<>-1 then nodes[node].rplc:=2;
self.AddNodes(st);
end else if OP=opcode.dhtTestResult then begin
writeln('SampleSearch@',string(@self),': Result from ',string(sAddr));
if node<>-1 then nodes[node].rplc:=1;
setlength(self.values,st.left);
st.read(self.values[1],length(self.values));
SendEvent(tevComplete,nil); exit;
end else begin
writeln('SampleSearch@',string(@self),': Unknown from ',string(sAddr));
{warning, prehaps?}
end;
end;
procedure TestQueryHandler(msg:tSMsg);
var r:tMemoryStream;
const cResp:string='Schmeterling!';
var trid:word;
var sID:tPID;
var Target:tKey20;
var strmsg:string;
begin
msg.st.skip(1);
msg.st.rb(trid,2);
msg.st.rb(sID,20);
if not DHT.CheckNode(sID, msg.source, true) then exit;
msg.st.rb(Target,20);
strmsg:=msg.st.ReadStringAll;
writeln('dhlt.TestQueryHandler: ',strmsg);
r:=tMemoryStream.Create; try
r.w1(opcode.dhtTestResult);
r.wb(trid,2);
r.wb(dht.MyID,20);
r.wb(cResp[1],length(cResp));
SendMessage(r.Memory^,r.size,msg.source);
finally r.Free end;
end;
var o:t;
BEGIN
if OptIndex('-dhlt')>0 then begin
writeln('dhlt: giving dht time to initialize: 3s');
shedule(3000,@o.DOIT);
end;
ServerLoop.SetupOpcode(opcode.dhtTestQuery,@TestQueryHandler);
end.