Index: MessageParser.java
===================================================================
--- MessageParser.java	(revision 1)
+++ MessageParser.java	(revision 2)
@@ -2,6 +2,7 @@
 import java.io.UnsupportedEncodingException;
 import java.net.DatagramPacket;
 import java.net.UnknownHostException;
+import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 import java.util.Random;
@@ -20,9 +21,15 @@
 
 	public void run() {
 		
-		String str = new String(packet.getData(), StandardCharsets.UTF_8);
+		
+		String str = null;
+
+		str = new String(packet.getData(),0, packet.getLength());
+	
+		
 		String rest = null, fileID = null, messageType = null, version = null;
 		int i = 0, next = 0, chunkNo = 0, replicationDeg = 0, senderID=0;
+		byte[] body = null;
 
 	     if(str.contains(" ")){
 	    	i= str.indexOf(" ");
@@ -54,19 +61,20 @@
 		
 		String bodyString = rest.replaceFirst(CRLF+CRLF,"");
 		
-		String bodyString2 = bodyString.replaceAll("\0", "");
+		bodyString = bodyString.replaceFirst(" ", "");
 		
-		bodyString = bodyString2.replaceFirst(" ", "");
-
-		byte[] body = bodyString.getBytes( StandardCharsets.UTF_8);
+			
+			body = bodyString.getBytes();
+	
 		
 		
+
 		switch(messageType) {
 		
 		case "PUTCHUNK":
 			
 			Chunk chunk = new Chunk(fileID, chunkNo, body);
-			System.out.println("CHUNK N: "+ chunk.getChunkN()+ " SIZE: " + chunk.getContent().length);
+			chunk.setGoalRepDeg(replicationDeg);
 			if(peer.getStorage().addChunk(chunk)) {
 
 			Message stored = new Message("STORED",peer.getVersion(),peer.getPeerID(),fileID, chunkNo, 0, null);
@@ -104,6 +112,7 @@
 			
 		case "STORED":
 			peer.getStorage().getBackUps().add(new BackUpInfo(fileID, chunkNo, peer.getPeerID()));
+			peer.getStorage().increaseChunkRepDeg(fileID, chunkNo);
 		
 		
 		break;
@@ -110,23 +119,27 @@
 		
 		case "GETCHUNK": //CHUNK <Version> <SenderId> <FileId> <ChunkNo> <CRLF><CRLF><Body>
 			
+			Random rand = new Random();
+			int  n = rand.nextInt(400) + 1;
+			
+			
+			try {
+				Thread.sleep(n);
+			} catch (InterruptedException e) {
+				e.printStackTrace();
+			} 
+			
 			for(Chunk chunkSend: peer.getStorage().getChunks()) {
 				
 			if(chunkSend.getFileID().equals(fileID) && chunkSend.getChunkN()==chunkNo) {
 				
-				Random rand = new Random();
-				int  n = rand.nextInt(400) + 1;
 				
-				
-				try {
-					Thread.sleep(n);
-				} catch (InterruptedException e) {
-					e.printStackTrace();
-				} 
 
 				Message sendChunk = new Message("CHUNK",peer.getVersion(),peer.getPeerID(),fileID, chunkNo, 0, chunkSend.getContent());
+				
 				byte[] reply = sendChunk.sendable();
 				System.out.println(sendChunk.messageToStringPrintable());
+			
 				
 				try {
 					peer.getMDR().sendMessage(reply);
@@ -144,26 +157,75 @@
 		
 		case "CHUNK": //GETCHUNK <Version> <SenderId> <FileId> <ChunkNo> <CRLF><CRLF>
 			Chunk gotChunk = new Chunk(fileID, chunkNo, body);
+			
 			peer.getStorage().addChunk(gotChunk);
-			if(gotChunk.getContent().length < 64000)
+			if(gotChunk.getContent().length < 58000)				
 				peer.lastChunk();
 			break;
 			
 		case "REMOVED":
-			Random rand = new Random();
-			int  n = rand.nextInt(400) + 1;
+			if(peer.getStorage().lowerRepDeg(fileID, chunkNo)) {
+			Random rand2 = new Random();
+			int  n2 = rand2.nextInt(400) + 1;
 			
 			
 			try {
-				Thread.sleep(n);
+				Thread.sleep(n2);
 			} catch (InterruptedException e) {
 				e.printStackTrace();
 			} 
-            peer.getStorage().getBackUps().remove(new BackUpInfo(fileID, chunkNo, peer.getPeerID()));
+			for(Chunk chunkSend: peer.getStorage().getChunks())
+			{
+				if(chunkSend.getFileID().equals(fileID) && chunkSend.getChunkN() == chunkNo)
+				{
+					boolean stored = false;
+					
+					Message msg = new Message("PUTCHUNK", version, peer.getPeerID(),chunkSend.getFileID(),chunkNo, chunkSend.getGoalRepDeg()-chunkSend.getRepDeg(),
+							chunkSend.getContent());
+					byte[] msgByte = msg.sendable();
+		
+					System.out.println(msg.messageToStringPrintable());
+					try {
+						peer.getMDB().sendMessage(msgByte);
+					} catch (UnknownHostException e2) {
+						// TODO Auto-generated catch block
+						e2.printStackTrace();
+					}					
+					
+					for(int j = 0; j < 5 && !stored; j++) {
+						
+					try {
+						TimeUnit.SECONDS.sleep(1+j);
+					} catch (InterruptedException e1) {
+						// TODO Auto-generated catch block
+						e1.printStackTrace();
+					}
+					
+					if(peer.storedCheck(chunkNo, fileID) >= chunkSend.getGoalRepDeg()) {
+						stored = true;
+					} 
+					else {
+						try {
+							peer.getMDB().sendMessage(msgByte);
+						} catch (UnknownHostException e) {
+							// TODO Auto-generated catch block
+							e.printStackTrace();
+						} 
+						System.out.println(msg.messageToStringPrintable());
+						
+					}
+					} 
+					stored = false;
+				}
+				
+			}
+			}
+		
             break;
             
 		case "DELETE":
-            //peer.getStorage().getFileInfo().remove(new FileInfo(fileID, peer.getFileInfo.getDateModified(), FileInfo.getFilename(), peer.getPeerID()));
+            peer.getStorage().deleteChunkByFileID(fileID);
+            peer.deleteFileFolder(fileID);
             break;
 		}
 	}
