pike.git / lib / modules / Protocols.pmod / Bittorrent.pmod / DHT.test

version» Context lines:

pike.git/lib/modules/Protocols.pmod/Bittorrent.pmod/DHT.test:220:    "8ad3b9d747a93e0a703194e1caa2284c5767ee84",    "fb917cc9b54847f90356abc135b16c6050423ed8",    "323881772a60b0eb82191082a024cdc3d72982db",    "5ca1c7a5b4136f85c54d4b0b08d8a4787cf9f883",    "f55ab43864dbbda57d4687e08ba3919ae43a3886",    "e4a0704301b0656e41d6963f42b95029f64c4c6e",    "b7b471e6af30c496dcf639dee72ca2a142bf5cb2",    "f53eeea763629e57a4167c6d3619bcc50d735280"   });    + class FakeUDPNet + { +  mapping(string:FakeUDP) ports = ([]); +  +  int(0..1) bind(int port, string addr, FakeUDP udp) +  { +  addr += " " + port; +  if (ports[addr]) return 0; +  ports[addr] = udp; +  return 1; +  } +  +  int(0..1) send(string addr, int port, mapping msg) +  { +  addr += " " + port; +  if (ports[addr]) { +  if (!ports[addr]->lossyness || +  (Crypto.Random.random(256) >= ports[addr]->lossyness)) { +  call_out(ports[addr]->internal_recv, ports[addr]->lag/65536.0, msg); +  } +  return 1; +  } +  return 0; +  } +  +  void close(string addr, int port) +  { +  m_delete(ports, addr + " " + port); +  } + } +  + FakeUDPNet fake_udp_net = FakeUDPNet(); +  + class FakeUDP + { +  string addr; +  int port; +  function read_callback; +  array extras; +  int nonblocking_mode; +  int(0..65536) lag; +  int(0..256) lossyness; +  +  void bind(int|string port, string|void addr, int(0..1) no_reuseaddr) +  { +  addr = addr || "127.0.0.1"; +  if (!fake_udp_net->bind(port, addr, this)) { +  error("Unable to bind address %O:%d.\n", addr, port); +  } +  this::addr = addr; +  this::port = port; +  } +  +  object set_nonblocking(function|void f, mixed ... extras) +  { +  if (f) { +  read_callback = f; +  this::extras = extras; +  } +  nonblocking_mode = 1; +  return this; +  } +  +  void internal_recv(mapping msg) +  { +  if (read_callback) { +  read_callback(msg, @(extras||({}))); +  } +  } +  +  object set_blocking() +  { +  nonblocking_mode = 0; +  return this; +  } +  +  string query_address() +  { +  if (!addr) { +  error("Port not bound yet.\n"); +  } +  return addr; +  } +  +  int(0..1) close() +  { +  fake_udp_net->close(addr, port); +  addr = 0; +  port = 0; +  read_callback = 0; +  extras = 0; +  nonblocking_mode = 0; +  return 1; +  } +  +  int send(string to, int|string port, string message, int|void flags) +  { +  if (!lossyness || (Crypto.Random.random(256) >= lossyness)) { +  mapping msg = ([ +  "data":message, +  "ip": addr, +  "port": this::port, +  ]); +  call_out(fake_udp_net->send, lag/65536.0, to, port, msg); +  } +  return sizeof(message); +  } + } +  + class MyDHT + { +  inherit Protocols.Bittorrent.DHT; +  +  void start(int port, void|string bind_address) +  { +  if (is_running) return 0; +  is_running = 1; +  this_program::port = FakeUDP(); +  this_program::port->bind(port, bind_address, 1); +  this_program::port->set_nonblocking(read_callback); +  } + } +    array(mapping) dhts = ({});   mapping(string:object) dhts_by_id = ([]);      System.Timer timer = System.Timer();   int good, bad;      object mt;      mixed state;   
pike.git/lib/modules/Protocols.pmod/Bittorrent.pmod/DHT.test:375:      int main(int argc, array(string) argv) {       // Generate a dummy routing table.    foreach(routing_table_data/"\n" - ({ "" });int i; string l) {    sscanf(l, "%s\t%s\t%d%*s", string node_id, string ip, int port);    node_id = String.hex2string(node_id);    mapping tmp = ([    "node_id" : node_id,    "port" : port, -  "dht" : Protocols.Bittorrent.DHT(node_id), + #ifndef DHT_TEST_USE_UDP +  "dht" : MyDHT(node_id), + #else +  "dht" : DHT(node_id), + #endif    ]);    dhts += ({ tmp });    dhts_by_id[node_id] = tmp->dht;    }       Tools.Testsuite.log_status("Binding ports for all DHT instances.");    // Go thru all the nodes and install the routing table.    foreach(dhts, mapping di){    object dht = di->dht;    restore_routing_table(routing_table_data, dht);