package net.f4fs.persistence;

import net.f4fs.fspeer.GetListener;
import net.f4fs.fspeer.PutListener;
import net.f4fs.fspeer.RemoveListener;
import net.tomp2p.dht.FutureGet;
import net.tomp2p.dht.FuturePut;
import net.tomp2p.dht.FutureRemove;
import net.tomp2p.dht.PeerDHT;
import net.tomp2p.peers.Number160;
import net.tomp2p.storage.Data;

/**
 * Implements an adapter to store, retrieve and remove data in the DHT.
 * <br>
 * <b>Note:</b> No versions of stored data are supported
 */
public class DHTOperations
        implements IPersistence {

    @Override
    public Data getData(PeerDHT pPeer, Number160 pLocationKey)
            throws InterruptedException {
        FutureGet futureGet = pPeer.get(pLocationKey).start();
        futureGet.addListener(new GetListener(pPeer.peerAddress().inetAddress().toString(), "Get data"));

        futureGet.await();

        return futureGet.data();
    }

    @Override
    public Data getDataOfVersion(PeerDHT pPeer, Number160 pLocationKey, Number160 pVersionKey)
            throws InterruptedException {
        return this.getData(pPeer, pLocationKey);
    }

    @Override
    public void putData(PeerDHT pPeer, Number160 pLocationKey, Data pData)
            throws InterruptedException {
        FuturePut futurePut = pPeer.put(pLocationKey).data(pData).start();
        futurePut.addListener(new PutListener(pPeer.peerAddress().inetAddress().toString(), "Put data"));

        futurePut.await();
    }

    @Override
    public void removeData(PeerDHT pPeer, Number160 pKey)
            throws InterruptedException {
        FutureRemove futureRemove = pPeer.remove(pKey).start();
        futureRemove.addListener(new RemoveListener(pPeer.peerAddress().inetAddress().toString(), "Remove data"));

        futureRemove.await();
    }

    @Override
    public void removeData(PeerDHT pPeer, Number160 pKey, Number160 pVersionKey)
            throws InterruptedException {
        this.removeData(pPeer, pKey);
    }

}

@@ -1778,36 +1778,33 @@
 await();%0A    %7D%0A%0A
-    
+%09
 @Override%0A    pu
@@ -1789,36 +1789,33 @@
   %7D%0A%0A%09@Override%0A
-    
+%09
 public void remo
@@ -1812,32 +1812,41 @@
  void removeData
+OfVersion
 (PeerDHT pPeer, 
@@ -1856,25 +1856,28 @@
 ber160 pKey,
- 
+%0A%09%09%09
 Number160 pV
@@ -1882,36 +1882,24 @@
 pVersionKey)
-%0A           
  throws Inte
@@ -1917,24 +1917,18 @@
 ption %7B%0A
-        
+%09%09
 this.rem
@@ -1949,17 +1949,14 @@
  pKey);%0A
-    
+%09
 %7D%0A%0A%7D%0A

update removeDataOfVersion method