Exemplo n.º 1
0
    /**
    <summary>Creates a new TableServer object and registers it to the "dht"
    handler in the node's RpcManager.</summary>
    <param name="node">The node the dht is to serve from.</param>
    */
    public TableServer(Node node) {
      _sync = new Object();
      _transfer_sync = new Object();

      _node = node;
      _rpc = RpcManager.GetInstance(node);

      _data = new TableServerData(_node);
      lock(_transfer_sync) {
        node.ConnectionTable.ConnectionEvent += this.ConnectionHandler;
        node.ConnectionTable.DisconnectionEvent += this.ConnectionHandler;
        _node.StateChangeEvent += StateChangeHandler;
      }

      _rpc.AddHandler("dht", this);
    }
Exemplo n.º 2
0
    public void Test2() {
      TableServerData tsd = new TableServerData("0");
      RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
      MemBlock[] addresses = new MemBlock[100];
      byte[] value = new byte[20];
      rng.GetBytes(value);
      DateTime now = DateTime.UtcNow;
      DateTime lease_end = now.AddMinutes(1);
      for(int i = 0; i < addresses.Length; i++) {
        addresses[i] = (new AHAddress(rng)).ToMemBlock();
        tsd.AddEntry(new Entry(addresses[i], value, now, lease_end));
      }

      AHAddress start = new AHAddress(rng);
      AHAddress end = new AHAddress(rng);
      LinkedList<MemBlock> keys_se = tsd.GetKeysBetween(start, end);
      LinkedList<MemBlock> keys_es = tsd.GetKeysBetween(end, start);
      String output = " - " +start + ":" + end;
      if(start.IsLeftOf(end)) {
        foreach(MemBlock address in addresses) {
          AHAddress addr = new AHAddress(address);
          if(addr.IsLeftOf(end) && addr.IsRightOf(start)) {
            Assert.IsTrue(keys_se.Contains(address), addr + " in lse" + output);
            Assert.IsTrue(keys_es.Contains(address), addr + " in les" + output);
          }
          else {
            Assert.IsFalse(keys_se.Contains(address), addr + " out lse" + output);
            Assert.IsFalse(keys_es.Contains(address), addr + " out les" + output);
          }
        }
      }
      else {
        foreach(MemBlock address in addresses) {
          AHAddress addr = new AHAddress(address);
          if(addr.IsLeftOf(start) && addr.IsRightOf(end)) {
            Assert.IsTrue(keys_se.Contains(address), addr + " in rse" + output);
            Assert.IsTrue(keys_es.Contains(address), addr + " in res" + output);
          }
          else {
            Assert.IsFalse(keys_se.Contains(address), addr + " out rse" + output);
            Assert.IsFalse(keys_es.Contains(address), addr + " out res" + output);
          }
        }
      }

      LinkedList<MemBlock> keys = tsd.GetKeys();
      foreach(MemBlock addr in addresses) {
        Assert.IsTrue(keys.Contains(addr), "keys does not contain: " + (new AHAddress(addr)));
      }
    }
Exemplo n.º 3
0
    public void Test1() {
      RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
      TableServerData tsd = new TableServerData("0");
      Entry[] not_expired = new Entry[12];
      Entry[] to_expire = new Entry[12];
      DateTime now = DateTime.UtcNow;
      DateTime live = now.AddSeconds(120);
      DateTime expire = now.AddSeconds(5);

      for(int i = 0; i < 4; i++) {
        byte[] key = new byte[20];
        rng.GetBytes(key);
        for(int j = 0; j < 3; j++) {
          byte[] value = new byte[20];
          rng.GetBytes(value);
          Entry ent = new Entry(key, value, now, expire);
          to_expire[i * 3 + j] = ent;
          tsd.AddEntry(ent);
          value = new byte[20];
          rng.GetBytes(value);
          ent = new Entry(key, value, now, live);
          not_expired[i * 3 + j] = ent;
          tsd.AddEntry(ent);
          Assert.IsFalse(not_expired[i * 3 + j].Equals(to_expire[i * 3 + j]), 
                         String.Format("{0}: not_expired == to_expire.", i * 3 + j));
        }
      }

      for(int i = 0; i < 4; i++) {
        LinkedList<Entry> entries = tsd.GetEntries(not_expired[i * 3].Key);
        for(int j = 0; j < 3; j++) {
          Assert.IsTrue(entries.Contains(not_expired[i * 3 + j]), "step 0: not_expired " + (i * 3 + j));
          Assert.IsTrue(entries.Contains(to_expire[i * 3 + j]), "step 0: to_expire " + (i * 3 + j));
        }
      }

      for(int i = 0; i < 4; i++) {
        for(int j = 0; j < 3; j++) {
          int pos = i * 3 + j;
          if(pos % 2 == 0) {
            Entry ent = not_expired[pos];
            tsd.UpdateEntry(ent.Key, ent.Value, now.AddSeconds(160));
          }
        }
      }

      Entry entry = to_expire[11];
      tsd.UpdateEntry(entry.Key, entry.Value, now.AddSeconds(160));

      for(int i = 0; i < 4; i++) {
        LinkedList<Entry> entries = tsd.GetEntries(not_expired[i * 3].Key);
        for(int j = 0; j < 3; j++) {
          Assert.IsTrue(entries.Contains(not_expired[i * 3 + j]), "step 1: not_expired " + (i * 3 + j));
          Assert.IsTrue(entries.Contains(to_expire[i * 3 + j]), "step 1: to_expire " + (i * 3 + j));
        }
      }

      while(DateTime.UtcNow < expire.AddSeconds(1)) {
        for(int i = 0; i < 50000000; i++) {
          int k = i % 5;
         k += 6;
        }
      }
      for(int i = 0; i < 3; i++) {
        LinkedList<Entry> entries = tsd.GetEntries(not_expired[i * 3].Key);
        for(int j = 0; j < 3; j++) {
          Assert.IsTrue(entries.Contains(not_expired[i * 3 + j]), "step 2: not_expired " + (i * 3 + j));
          Assert.IsFalse(entries.Contains(to_expire[i * 3 + j]), "step 2: to_expire " + (i * 3 + j));
        }
      }
      Assert.AreEqual(13, tsd.Count, "Entries we didn't check are removed by CheckEntries.");
    }
Exemplo n.º 4
0
 public void Test0() {
   RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
   TableServerData tsd = new TableServerData("0");
   byte[] key = new byte[20];
   rng.GetBytes(key);
   DateTime now = DateTime.UtcNow;
   Entry ent = new Entry(key, key, now, now.AddSeconds(100));
   tsd.AddEntry(ent);
   LinkedList<Entry> entries = tsd.GetEntries(key);
   Assert.AreEqual(1, entries.Count, "Count after add");
   Assert.AreEqual(ent, entries.First.Value, "Entries are equal");
   tsd.UpdateEntry(ent.Key, ent.Value, now.AddSeconds(200));
   entries = tsd.GetEntries(key);
   Assert.AreEqual(1, entries.Count, "Count after update");
   Assert.AreEqual(ent, entries.First.Value, "Entries are equal");
   tsd.RemoveEntry(ent.Key, ent.Value);
   entries = tsd.GetEntries(key);
   Assert.AreEqual(tsd.Count, 0, "Count after remove");
   Assert.AreEqual(null, entries, "Entry after remove");
 }