Index: storage/.DS_Store
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Index: storage/.DS_Store
===================================================================
--- storage/.DS_Store	(nonexistent)
+++ storage/.DS_Store	(revision 14)

Property changes on: storage/.DS_Store
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+application/octet-stream
\ No newline at end of property
Index: storage/Storage.java
===================================================================
--- storage/Storage.java	(nonexistent)
+++ storage/Storage.java	(revision 14)
@@ -0,0 +1,183 @@
+package storage;
+
+import chunk.Chunk;
+import chunk.ChunkFile;
+import chunk.Global;
+import server.Server;
+
+import java.io.File;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class Storage implements Serializable {
+	private ArrayList<ChunkFile> files;
+	private ArrayList<Chunk> savedChunks;
+	private ArrayList<Chunk> receivedChunks;
+	private ConcurrentHashMap<String, Integer> storedTimes;
+	private ConcurrentHashMap<String, String> remainingChunks;
+	private int totalSpace;
+
+	public Storage() {
+		this.files = new ArrayList<>();
+		this.savedChunks = new ArrayList<>();
+		this.receivedChunks = new ArrayList<>();
+		this.storedTimes = new ConcurrentHashMap<>();
+		this.remainingChunks = new ConcurrentHashMap<>();
+		this.setTotalSpace(Global.MAX_STORAGE_SIZE);
+	}
+
+	public ArrayList<ChunkFile> getFiles() {
+		return files;
+	}
+
+	public synchronized void setFiles(ArrayList<ChunkFile> files) {
+		this.files = files;
+	}
+
+	public synchronized void addFile(ChunkFile file) {
+		this.files.add(file);
+	}
+
+	public synchronized boolean addSavedChunk(Chunk chunk) {
+		String chunkFileId;
+		int chunkNum;
+		for (Iterator<Chunk> iter = this.savedChunks.iterator(); iter.hasNext();) {
+			chunkFileId = ((Chunk) iter).getfileId();
+			chunkNum = ((Chunk) iter).getNum();
+			if (chunkFileId.equals(chunk.getfileId()) && chunkNum == chunk.getNum())
+				return false;
+		}
+		this.savedChunks.add(chunk);
+
+		return true;
+	}
+
+	public void deleteStoredChunks(String fileId) {
+		int chunkNum;
+
+		for (Iterator<Chunk> iter = this.savedChunks.iterator(); iter.hasNext();) {
+			Chunk chunk = iter.next();
+			chunkNum = chunk.getNum();
+			if (chunk.getfileId().equals(fileId)) {
+				String filename = "database/" + Server.getServerId() + "/" + fileId + "." + chunkNum;
+				File file = new File(filename);
+				file.delete();
+				removeStoredOccurrencesEntry(fileId, chunkNum);
+				incSpaceAvailable(fileId, chunkNum);
+				iter.remove();
+			}
+		}
+	}
+
+	public synchronized void incStoredOccurrences(String fileId, int chunkNum) {
+
+		String key = fileId + '.' + chunkNum;
+		ConcurrentHashMap<String, Integer> StoredTimes = Server.getStorage().getStoredTimes();
+		if (!StoredTimes.containsKey(key)) {// Checks if co
+			Server.getStorage().getStoredTimes().put(key, 1);
+		} else {
+			int total = this.storedTimes.get(key) + 1;
+			this.storedTimes.replace(key, total);
+		}
+
+	}
+
+	public synchronized void decStoredOccurrences(String fileId, int chunkNum) {
+		String key = fileId + '.' + chunkNum;
+		int total = this.storedTimes.get(key) - 1;
+		this.storedTimes.replace(key, total);
+	}
+
+	public synchronized void removeStoredOccurrencesEntry(String fileId, int chunkNum) {
+		String key = fileId + '.' + chunkNum;
+		this.storedTimes.remove(key);
+	}
+
+	public void fillCurrRDChunks() {
+		String chunkFileId;
+		int chunkNum;
+
+		for (Iterator<Chunk> iter = this.savedChunks.iterator(); iter.hasNext();) {
+			chunkFileId = ((Chunk) iter).getfileId();
+			chunkNum = ((Chunk) iter).getNum();
+
+			String key = chunkFileId + "." + chunkNum;
+			((Chunk) iter).setCurrentRepDegree(this.storedTimes.get(key));
+		}
+	}
+
+	public synchronized void incSpaceAvailable(String fileId, int chunkNum) {
+		String chunkFileId;
+		int chunkNumber;
+		int chunkSize;
+
+		for (Iterator<Chunk> iter = this.savedChunks.iterator(); iter.hasNext();) {
+			chunkFileId = ((Chunk) iter).getfileId();
+			chunkNumber = ((Chunk) iter).getNum();
+			chunkSize = ((Chunk) iter).getchunkSize();
+			if (chunkFileId.equals(fileId) && chunkNumber == chunkNum)
+				this.totalSpace = this.totalSpace + chunkSize;
+		}
+	}
+
+	public ArrayList<Chunk> getSavedChunks() {
+		return savedChunks;
+	}
+
+	public synchronized void setSavedChunks(ArrayList<Chunk> savedChunks) {
+		this.savedChunks = savedChunks;
+	}
+
+	public ArrayList<Chunk> getReceivedChunks() {
+		return receivedChunks;
+	}
+
+	public synchronized void setReceivedChunks(ArrayList<Chunk> receivedChunks) {
+		this.receivedChunks = receivedChunks;
+	}
+
+	public ConcurrentHashMap<String, Integer> getStoredTimes() {
+		return storedTimes;
+	}
+
+	public synchronized void setStoredTimes(ConcurrentHashMap<String, Integer> storedTimes) {
+		this.storedTimes = storedTimes;
+	}
+
+	public ConcurrentHashMap<String, String> getRemainingChunks() {
+		return remainingChunks;
+	}
+
+	public synchronized void setRemainingChunks(ConcurrentHashMap<String, String> remainingChunks) {
+		this.remainingChunks = remainingChunks;
+	}
+
+	public void addRemainingChunks(String fileId, int chunkNum) {
+		String key = fileId + '.' + chunkNum;
+		this.remainingChunks.put(key, "false");
+	}
+
+	public void setWantedChunkReceived(String fileId, int chunkNum) {
+		String key = fileId + '.' + chunkNum;
+		this.remainingChunks.replace(key, "true");
+	}
+
+	public int getTotalSpace() {
+		return totalSpace;
+	}
+
+	public synchronized void setTotalSpace(int totalSpace) {
+		this.totalSpace = totalSpace;
+	}
+
+	public void decSpaceAvailable(int length) {
+		totalSpace = totalSpace - length;
+	}
+
+	public synchronized int getSpaceAvailable() {
+		return this.totalSpace;
+	}
+
+}
