1# This is an example for the usage of IOHubs:
2LoadPackage("orb");
3HashServer := function(pt,size,addr,port,chunksize)
4  local done,h,ht,i,l,len,p,r,todo,val;
5  ht := HTCreate(pt,rec( treehashsize := size ));
6  todo := [];
7  h := IOHub();
8  AttachServingSocket(h,addr,port);
9  done := false;
10  while not(done) do
11      DoIO(h,true);       # this is blocking
12      #Print("done\n");
13      while true do
14          r := GetInput(h,0);   # a pair [connection,string]
15          #Print(r,"\n");
16          if r = false then break; fi;
17          if r[2] = "exit" then done := true; break; fi;
18          if r[2] = "gettask" then
19              len := Length(todo);
20              if len = 0 then
21                  l := [];
22              elif len >= chunksize then
23                  l := todo{[len-chunksize+1..len]};
24                  for i in [len,len-1..len-chunksize+1] do
25                      Unbind(l[i]);
26                  od;
27              else
28                  l := ShallowCopy(todo);
29                  for i in [len,len-1..1] do
30                      Unbind(l[i]);
31                  od;
32              fi;
33              SubmitOutput(h,r[1],IO_Pickle(l));
34          else   # some new points
35              l := IO_Unpickle(r[2]);
36              for p in l do
37                  val := HTValue(ht,p);
38                  if val = fail then
39                      HTAdd(ht,p,true);
40                      Add(todo,p);
41                  fi;
42              od;
43          fi;
44          DoIO(h,false);   # non-blocking!
45      od;
46  od;
47  Shutdown(h);
48end;
49