Index: Peer.java
===================================================================
--- Peer.java	(revision 1)
+++ Peer.java	(revision 2)
@@ -1,6 +1,8 @@
 
 import java.io.BufferedInputStream;
 import java.io.BufferedWriter;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -10,7 +12,11 @@
 import java.io.InputStream;
 import java.io.ObjectOutputStream;
 import java.io.OutputStream;
+import java.io.StringReader;
 import java.net.UnknownHostException;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Paths;
@@ -19,6 +25,7 @@
 import java.rmi.server.UnicastRemoteObject;
 import java.security.MessageDigest;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.concurrent.TimeUnit;
 
 
@@ -97,6 +104,7 @@
 	}
 	
 	
+	
 	public void operation(String operation, String file_path, int rep_degree, double space) { //operator is space for reclaim, rep_degree for back up
 
 		
@@ -131,12 +139,16 @@
 			}
 		
 		}
-		else if(operation == "RECLAIM")
-		{
-			initiateReclaim(space);
+		else if(operation.equals("RECLAIM"))
+		{			
+			initiateReclaim(space);			
 		}
+		else if(operation.equals("STATE"))
+			System.out.print(state());
+	
 
 
+
 	}
 	
 
@@ -143,33 +155,49 @@
 
 
 
-	 
 private void initiateReclaim(double space) {
-           
-             
-               
-                for(int i = 0 ; i < storage.getChunks().size();i++) {
-                    if(space > 0) {
-                        Chunk chunk = storage.getChunks().get(i);
-                        space -= chunk.getsize();
+                      long before = storage.getUsed_storage();
+	while(space < storage.getUsed_storage()) {
+	
+		//int size = storage.getChunks().get(0).getsize();
+		String fileID = storage.getChunks().get(0).getFileID();
+		int chunkN = storage.getChunks().get(0).getChunkN();
+		
+		storage.deleteChunkByFileIDAndChunkN(fileID, chunkN);
+		
                        
-                        Message msg = new Message("REMOVED", getVersion(), this.getPeerID(), chunk.getFileID(), chunk.getChunkN(), 0 , null);
-                        byte[] msgByte = msg.sendable();
-                       
-                        try {
-                            mc.sendMessage(msgByte);
-                        } catch (UnknownHostException e) {
-                            // TODO Auto-generated catch block
+        Message msg = new Message("REMOVED", getVersion(), this.getPeerID(),fileID, chunkN, 0 , null);
+        byte[] msgByte = msg.sendable();
+       
+        try {
+            mc.sendMessage(msgByte);
+        } catch (UnknownHostException e) {
+            // TODO Auto-generated catch block
                             e.printStackTrace();
                         }  
-                        String newfilename = "Peer"+this.getPeerID() + "/"+ chunk.getFileID()+ "/chk" + chunk.getChunkN();
+                     
  
-                        File file = new File(newfilename);
-                        file.delete();
-                        storage.deleteChunk(chunk);
-                       
-                    }
-                }
+       String filename = "Peer"+this.getPeerID()+ "/"+ fileID + "/chk" + chunkN;
+	
+	 File folder = new File(filename);
+	 
+	 if (folder.exists()) {
+		 String[]entries = folder.list();
+		 for(String s: entries){
+		     File currentFile = new File(folder.getPath(),s);
+		     currentFile.delete();
+		 }
+		 
+		    folder.delete();
+		    
+		}
+	}
+                    
+	long reclaimed = before-storage.getUsed_storage();
+	 System.out.println("RECLAIMED " +reclaimed+"B of data.");
+                
+                
+	
        
     }
 
@@ -196,11 +224,10 @@
 
 	public void lastChunk() {
 		this.lastChunk = true;
-		System.out.println("LAST CHUNK");
+		
 	}
 
-
-	private void initiateRestore(String file_path) throws IOException { //GETCHUNK <Version> <SenderId> <FileId> <ChunkNo> <CRLF><CRLF>
+private void initiateRestore(String file_path) throws IOException { //GETCHUNK <Version> <SenderId> <FileId> <ChunkNo> <CRLF><CRLF>
 		
 		String hash = storage.lookUp(file_path);
 		int chunkn = 0;
@@ -207,7 +234,8 @@
 		byte[] msgByte = null;
 		Message msg;
 			///getChunksFromFile(hash);
-		while(!this.lastChunk) {
+		boolean lastChunkAux = this.lastChunk;
+		while(!lastChunkAux) {
 			
 			chunkn++;
 			
@@ -226,7 +254,7 @@
 				e.printStackTrace();
 			}
 			
-			
+			lastChunkAux = this.lastChunk;
 		}
 		
 		String newfilename = "Peer"+this.getPeerID() + "/restore/"+file_path;
@@ -260,6 +288,29 @@
 	}
 
 
+public String state() {
+	String s = "";
+	  s += "\n PEER "+this.getPeerID()+" HAS INITIATED PROTOCOL BACK UP ON THE FOLLOWING FILES: \n";
+    for(FileInfo file: storage.getFileInfo()) {
+    	s+="File Pathname: "+file.getFilename()+"\n";
+    	s+="File ID: "+file.getFileID()+"\n";
+    	s+="Desired Replication Degree: "+file.getDesiredRepDeg()+"\n ";
+    	s+="Initiated Chunks: \n";
+    	for(BackUpInfo chunk: storage.getBackUps()) {
+    		s+= "Chunk ID: "+ chunk.getFileID() + "chunk" + chunk.getChunkN()+"\n";
+    		s+="Perceived Replication Degree: "+ storage.perceivedRepDeg(chunk.getFileID(), chunk.getChunkN())+"\n";
+    	}
+    s+="\n PEER "+this.getPeerID()+" IS STORING THE FOLLOWING CHUNKS: \n";
+    	for(Chunk storedchunk: storage.getChunks()) {
+    		s+="Chunk ID: "+ storedchunk.getChunkId()+"\n";
+    		s+="Size: "+storedchunk.getsize()+"\n";
+     		s+="Perceived Replication Degree: "+storedchunk.getRepDeg()+"\n";
+    	}
+  	s+="PEER STORAGE CAPACITY: " + storage.getSpace_available()+"\n";
+    	
+    }
+    return s;
+}
 
 
 	private void initiateBackup(String file_path, int rep_degree) throws InterruptedException {
@@ -267,28 +318,28 @@
 		File file = new File(file_path);
 		String fileID = generateFileID(file);
 		byte[] msgByte = null;
-		Message msg;
 		boolean stored = false;
 	
 		try {
-			storage.addFile(file_path, file.lastModified(), fileID);
+			storage.addFile(file_path, file.lastModified(), fileID, rep_degree);
 			storage.serializeFileInfo();
-			ArrayList<Chunk> chunks = splitIntoChunksExternal(file, fileID, 64000);
+			ArrayList<Chunk> chunks = splitIntoChunks(file, fileID, 64000);
 			
 		
-				for(Chunk chunk : chunks) {
+				for(int i = 0; i < chunks.size(); i++) {
 					
-					System.out.println("CHUNK N: "+ chunk.getChunkN()+ " SIZE: " + chunk.getContent().length);
-					msg = new Message("PUTCHUNK", getVersion(), this.getPeerID(), chunk.getFileID(),chunk.getChunkN(), rep_degree, chunk.getContent());
+					Message msg = new Message("PUTCHUNK", getVersion(), this.getPeerID(), chunks.get(i).getFileID(),chunks.get(i).getChunkN(), rep_degree,
+							chunks.get(i).getContent());
 					msgByte = msg.sendable();
+		
 					System.out.println(msg.messageToStringPrintable());
 					mdb.sendMessage(msgByte);					
 					
-					for(int i = 0; i < 5 && !stored; i++) {
+					for(int j = 0; j < 5 && !stored; j++) {
 						
-					TimeUnit.SECONDS.sleep(i);
+					TimeUnit.SECONDS.sleep(1+j);
 					
-					if(storedCheck(chunk.getChunkN(), chunk.getFileID()) >= rep_degree) {
+					if(storedCheck(chunks.get(i).getChunkN(), chunks.get(i).getFileID()) >= rep_degree) {
 						stored = true;
 					} 
 					else {
@@ -312,7 +363,7 @@
 		
 }
 	
-	private int storedCheck(int chunkN, String fileID) {
+	public int storedCheck(int chunkN, String fileID) {
 		int timesSaved = 0;
 		
 		for(BackUpInfo info : storage.getBackUps()) {
@@ -344,6 +395,22 @@
 
 
 
+	public void deleteFileFolder(String hash) {
+		 String foldername = "Peer"+this.getPeerID() + "/backup/"+hash;
+		 
+		 File folder = new File(foldername);
+		 
+		 if (folder.exists()) {
+			 String[]entries = folder.list();
+			 for(String s: entries){
+			     File currentFile = new File(folder.getPath(),s);
+			     currentFile.delete();
+			 }
+			 
+			    folder.delete();
+			    
+			}
+	}
 
 	public void saveChunks() throws IOException{
 		
@@ -369,7 +436,7 @@
 	 public static String sha256hashing(String base) {
 	        try{
 	            MessageDigest md = MessageDigest.getInstance("SHA-256");
-	            byte[] hashed = md.digest(base.getBytes("UTF-8"));
+	            byte[] hashed = md.digest(base.getBytes("UTF8"));
 	            StringBuffer hexString = new StringBuffer();
 
 	            for (int i = 0; i < hashed.length; i++) {
@@ -431,7 +498,7 @@
 			return storage;
 		}
 
-		public ArrayList<Chunk> splitIntoChunksExternal(File file, String fileID, int chunk_size) throws IOException
+		public ArrayList<Chunk> splitIntoChunks(File file, String fileID, int chunk_size) throws IOException
 	    {
 			ArrayList<Chunk> filechunks = new ArrayList<Chunk>();
 		 
@@ -464,7 +531,7 @@
 	        
 	        temporary = new byte[chunk_size]; //Temporary Byte Array
 	        int bytesRead = inStream.read(temporary, 0, chunk_size);
-	        String temp = new String(temporary, StandardCharsets.UTF_8);
+	      
 	    
 	        if ( bytesRead > 0) // If bytes read is not empty
 	        {
@@ -502,4 +569,5 @@
 	     return filechunks;
 	    }
 		
+		
 }
\ No newline at end of file
