Index: src/objs/3Ctrl.ser
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Index: src/objs/3Ctrl.ser
===================================================================
--- src/objs/3Ctrl.ser	(nonexistent)
+++ src/objs/3Ctrl.ser	(revision 2)

Property changes on: src/objs/3Ctrl.ser
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+application/octet-stream
\ No newline at end of property
Index: src/Channel/Channel.class
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Index: src/compile.sh
===================================================================
--- src/compile.sh	(nonexistent)
+++ src/compile.sh	(revision 2)
@@ -0,0 +1,3 @@
+#!/usr/bin/env bash
+javac TestApp.java
+javac Peers/Peer.java
Index: src/Channel/Channel.java
===================================================================
--- src/Channel/Channel.java	(revision 1)
+++ src/Channel/Channel.java	(revision 2)
@@ -4,10 +4,11 @@
 import Peers.Peer;
 
 import java.io.IOException;
+import java.io.Serializable;
 import java.net.*;
 import java.util.concurrent.TimeUnit;
 
-public class Channel implements Runnable {
+public class Channel implements Runnable, Serializable {
 
     Peer peer;
 
@@ -34,7 +35,6 @@
 
         } catch (UnknownHostException e) {
 
-            e.printStackTrace();
 
         }
 
@@ -48,6 +48,7 @@
 
             byte[] mes = message.messagePacket();
             DatagramPacket data = new DatagramPacket(mes, mes.length, this.address, this.port);
+            System.out.println("Sending " + message.getType().toString() + " Packet");
             socket.send(data);
         } catch (IOException e) {
             e.printStackTrace();
Index: src/Message/DeleteMsg.class
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Index: src/Message/DeleteMsg.class
===================================================================
--- src/Message/DeleteMsg.class	(nonexistent)
+++ src/Message/DeleteMsg.class	(revision 2)

Property changes on: src/Message/DeleteMsg.class
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+application/octet-stream
\ No newline at end of property
Index: src/Message/Message$1.class
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Index: src/Message/Message.class
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Index: src/Message/Message.java
===================================================================
--- src/Message/Message.java	(revision 1)
+++ src/Message/Message.java	(revision 2)
@@ -61,16 +61,12 @@
         try {
             String head = buffer.readLine();
             int size = head.length() + 4;
-            System.out.println("Size = " + size);
-            System.out.println("MSlength = " + messageLength);
 
+
             this.body = new byte[messageLength - size];
             System.arraycopy(data, size, this.body, 0, messageLength - size);
 
-            System.out.println("body size = " + this.body.length);
 
-
-
             String[] l = head.split(" ");
             ms(l);
 
@@ -80,7 +76,7 @@
 
 
         } catch (IOException e) {
-            e.printStackTrace();
+            //e.printStackTrace();
         }
 
     }
@@ -160,7 +156,7 @@
 
             return messageByte;
         } catch (Exception e) {
-            e.printStackTrace();
+           // e.printStackTrace();
         }
         return null;
     }
Index: src/Message/WorkMessage$1.class
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Index: src/Message/WorkMessage.class
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Index: src/Message/WorkMessage.java
===================================================================
--- src/Message/WorkMessage.java	(revision 1)
+++ src/Message/WorkMessage.java	(revision 2)
@@ -12,8 +12,10 @@
     public WorkMessage(DatagramPacket packet, Peer peer) {
         try {
             this.packet = new Message(packet);
+            System.out.println("Received " + this.packet.getType().toString() + " Packet");
+
         } catch (Exception e) {
-            e.printStackTrace();
+            //e.printStackTrace();
         }
 
         this.peer = peer;
Index: src/Peers/Control.class
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Index: src/Peers/Control.class
===================================================================
--- src/Peers/Control.class	(nonexistent)
+++ src/Peers/Control.class	(revision 2)

Property changes on: src/Peers/Control.class
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+application/octet-stream
\ No newline at end of property
Index: src/Peers/Control.java
===================================================================
--- src/Peers/Control.java	(revision 1)
+++ src/Peers/Control.java	(revision 2)
@@ -3,6 +3,7 @@
 import Message.*;
 import Storage.Chunk;
 
+import java.io.Serializable;
 import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentSkipListSet;
@@ -9,7 +10,7 @@
 import java.util.concurrent.TimeUnit;
 
 
-public class Control {
+public class Control implements Serializable {
 
     // chunkNo + fileid, nº of stored
     ConcurrentHashMap<String, Integer> storedMessages = new ConcurrentHashMap<>();
@@ -20,6 +21,8 @@
     ConcurrentHashMap<String, ConcurrentSkipListSet<Chunk>> restoredChunks = new ConcurrentHashMap<>();
 
 
+    ConcurrentHashMap<String, String> backedUpFiles = new ConcurrentHashMap<>();
+
     private Peer peer;
 
     Control(Peer peer) {
@@ -51,7 +54,7 @@
 
         } catch (Exception e) {
 
-            e.printStackTrace();
+         //   e.printStackTrace();
         }
 
     }
@@ -107,7 +110,7 @@
                 }
             }
         } catch (Exception e) {
-            e.printStackTrace();
+            //e.printStackTrace();
         }
     }
 
@@ -170,4 +173,9 @@
 
 
     }
+
+    public ConcurrentHashMap<String, String> getBackedUpFiles() {
+        return backedUpFiles;
+    }
+
 }
Index: src/Peers/Peer.class
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Index: src/Peers/Peer.class
===================================================================
--- src/Peers/Peer.class	(nonexistent)
+++ src/Peers/Peer.class	(revision 2)

Property changes on: src/Peers/Peer.class
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+application/octet-stream
\ No newline at end of property
Index: src/Peers/Peer.java
===================================================================
--- src/Peers/Peer.java	(revision 1)
+++ src/Peers/Peer.java	(revision 2)
@@ -1,10 +1,7 @@
 package Peers;
 
 
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.ObjectOutputStream;
-import java.io.Serializable;
+import java.io.*;
 import java.rmi.RemoteException;
 import java.rmi.registry.LocateRegistry;
 import java.rmi.registry.Registry;
@@ -46,7 +43,6 @@
 
     Control control;
 
-    ConcurrentHashMap<String, String> backedUpFiles = new ConcurrentHashMap<>();
 
     public Peer(String version, String peerId, String remoteName, String mcAddr, int mcPort, String mdbAddr, int mdbPort, String mdrAddr, int mdrPort) {
 
@@ -60,9 +56,17 @@
             mc_channel = new Channel(mcAddr, mcPort, this);
             mdb_channel = new Channel(mdbAddr, mdbPort, this);
             mdr_channel = new Channel(mdrAddr, mdrPort, this);
-            this.control = new Control(this);
+
+            if(!loadStatus())
+                this.control = new Control(this);
+
+            // save data every 2 seconds
+            threadPool.scheduleAtFixedRate(() -> {
+                this.saveState();
+            }, 0, 2, TimeUnit.SECONDS);
+
         } catch (IOException e) {
-            e.printStackTrace();
+           // e.printStackTrace();
         }
 
 
@@ -117,6 +121,7 @@
         Peer peer = null;
 
         try {
+
             peer = new Peer(args[0], args[1], args[2], args[3], Integer.parseInt(args[4]), args[5], Integer.parseInt(args[6]), args[7], Integer.parseInt(args[8]));
             Service serv = (Service) UnicastRemoteObject.exportObject(peer, 0);
             Registry registry = LocateRegistry.getRegistry();
@@ -129,7 +134,7 @@
         try {
             peer.threadPool.awaitTermination(1, TimeUnit.DAYS);
         } catch (Exception e) {
-            e.printStackTrace();
+          //  e.printStackTrace();
         }
     }
 
@@ -165,20 +170,48 @@
         return control;
     }
 
-    public ConcurrentHashMap<String, String> getBackedUpFiles() {
-        return backedUpFiles;
-    }
 
     private void saveState() {
         try {
-            FileOutputStream file = new FileOutputStream(Paths.get(System.getProperty("user.dir")).toString() + "/objs/");
 
-            ObjectOutputStream out = new ObjectOutputStream(file);
+            FileOutputStream fileCtrl = new FileOutputStream(Paths.get(System.getProperty("user.dir")).toString() + "/objs/" + id + "Ctrl.ser");
+
+            ObjectOutputStream out2 = new ObjectOutputStream(fileCtrl);
+
+            out2.writeObject(this.control);
+
+            out2.close();
+            fileCtrl.close();
         } catch (IOException e) {
-            e.printStackTrace();
+            new File(Paths.get(System.getProperty("user.dir")).toString() + "/objs").mkdir();
+            try {
+                new File(Paths.get(System.getProperty("user.dir")).toString() + "/objs/" + id + "Ctrl.ser").createNewFile();
+            } catch (IOException e1) {
+               // e1.printStackTrace();
+            }
+
+            //e.printStackTrace();
         }
     }
 
+    private boolean loadStatus() {
+
+        try {
+            FileInputStream fileCtrl = new FileInputStream(Paths.get(System.getProperty("user.dir")).toString() + "/objs/" + id + "Ctrl.ser");
+
+            ObjectInputStream obj2 = new ObjectInputStream(fileCtrl);
+            this.control = (Control) obj2.readObject();
+
+            obj2.close();
+            fileCtrl.close();
+            return true;
+        } catch (Exception e) {
+            System.out.println("no files found");
+        }
+
+        return false;
+    }
+
 }
 
 
Index: src/Protocols/BackUpProtocol.class
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Index: src/Protocols/BackUpProtocol.java
===================================================================
--- src/Protocols/BackUpProtocol.java	(revision 1)
+++ src/Protocols/BackUpProtocol.java	(revision 2)
@@ -65,7 +65,7 @@
         try {
             String id = calculateFileId(peer, filepath);
             String[] name = filepath.split("/");
-            peer.getBackedUpFiles().put(name[name.length - 1], id);
+            peer.getControl().getBackedUpFiles().put(name[name.length - 1], id);
 
 
 
@@ -85,7 +85,6 @@
                 }
                 for (Message k : messages) {
                     peer.getMdb_channel().send(k);
-                    System.out.println("sending...");
                 }
                 i++;
             } while (confirmMessage(messages, sleep));
Index: src/Protocols/DeleteProtocol.class
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Index: src/Protocols/DeleteProtocol.java
===================================================================
--- src/Protocols/DeleteProtocol.java	(revision 1)
+++ src/Protocols/DeleteProtocol.java	(revision 2)
@@ -19,12 +19,14 @@
     @Override
     public void run() {
         String[] name = filepath.split("/");
-        String fid = this.peer.getBackedUpFiles().get(name[name.length - 1]);
+        String fid = this.peer.getControl().getBackedUpFiles().get(name[name.length - 1]);
 
 
         File n = new File(filepath);
-        Message ms = new DeleteMsg(peer.getVersionProtocol(), peer.getId(), peer.getBackedUpFiles().get(name[name.length - 1]));
+        Message ms = new DeleteMsg(peer.getVersionProtocol(), peer.getId(), peer.getControl().getBackedUpFiles().get(name[name.length - 1]));
 
         peer.getMc_channel().send(ms);
+
+        System.out.println("Deleted");
     }
 }
Index: src/Protocols/RestoreProtocol.class
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Index: src/Protocols/RestoreProtocol.java
===================================================================
--- src/Protocols/RestoreProtocol.java	(revision 1)
+++ src/Protocols/RestoreProtocol.java	(revision 2)
@@ -44,7 +44,7 @@
 
         String[] name = file.split("/");
 
-        if (!peer.getBackedUpFiles().containsKey(name[name.length - 1])) {
+        if (!peer.getControl().getBackedUpFiles().containsKey(name[name.length - 1])) {
             System.out.println("wrong in ");
             return;
         }
@@ -58,7 +58,7 @@
             return;
         }
 
-        ArrayList<Message> messages = createMessages(size, peer.getBackedUpFiles().get(name[name.length - 1]));
+        ArrayList<Message> messages = createMessages(size, peer.getControl().getBackedUpFiles().get(name[name.length - 1]));
 
 
         do {
@@ -83,11 +83,13 @@
                 my_stream.write(i.getInfo());
             }
             fileInfo = my_stream.toByteArray();
-            peer.getStorage().saveFile(peer.getBackedUpFiles().get(name[name.length - 1]), fileInfo);
+            peer.getStorage().saveFile(peer.getControl().getBackedUpFiles().get(name[name.length - 1]), fileInfo);
         } catch (Exception e) {
             e.printStackTrace();
         }
 
+        System.out.println("Restored");
+
     }
 
 
Index: src/Storage/ChunksManager.class
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Index: src/Storage/ChunksManager.java
===================================================================
--- src/Storage/ChunksManager.java	(revision 1)
+++ src/Storage/ChunksManager.java	(revision 2)
@@ -63,7 +63,7 @@
 
             fileInfo.close();
         } catch (IOException e) {
-            e.printStackTrace();
+            //e.printStackTrace();
         }
     }
 
Index: src/Storage/FileSystem.class
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Index: src/Storage/FileSystem.java
===================================================================
--- src/Storage/FileSystem.java	(revision 1)
+++ src/Storage/FileSystem.java	(revision 2)
@@ -24,10 +24,6 @@
 
     private String base;
 
-    private List<Chunk> chunks;
-
-    private List<String> FilesStored;
-
     public FileSystem(int maxSize, String directory, int peerID, Peer peer) {
 
         this.maxSize = maxSize;
@@ -103,7 +99,6 @@
         }
     }
 
-
     public boolean saveChunk(Chunk chunk, String fileId) {
         if (chunk.getInfo().length > availableSpace())
             return false;
@@ -137,7 +132,6 @@
         return false;
     }
 
-
     public HashMap<String, byte[]> getChunks(String fileId) {
 
         HashMap<String, byte[]> chunksInfo = new HashMap<>();
@@ -154,7 +148,7 @@
                 }
             }
         } catch (Exception e) {
-            e.printStackTrace();
+        //    e.printStackTrace();
         }
 
         return chunksInfo;
@@ -169,7 +163,7 @@
             Files.createDirectories(pathres);
             Files.createDirectory(pathchunks);
         } catch (IOException e) {
-            e.printStackTrace();
+           // e.printStackTrace();
         }
 
     }
@@ -210,7 +204,6 @@
            }catch (Exception e){
                e.printStackTrace();
 
-               System.out.println("");
            }
 
            return false;
Index: src/objs/1Ctrl.ser
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Index: src/objs/1Ctrl.ser
===================================================================
--- src/objs/1Ctrl.ser	(nonexistent)
+++ src/objs/1Ctrl.ser	(revision 2)

Property changes on: src/objs/1Ctrl.ser
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+application/octet-stream
\ No newline at end of property
Index: src/objs/2Ctrl.ser
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Index: src/objs/2Ctrl.ser
===================================================================
--- src/objs/2Ctrl.ser	(nonexistent)
+++ src/objs/2Ctrl.ser	(revision 2)

Property changes on: src/objs/2Ctrl.ser
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+application/octet-stream
\ No newline at end of property
Index: src/Read.txt
===================================================================
--- src/Read.txt	(nonexistent)
+++ src/Read.txt	(revision 2)
@@ -0,0 +1,25 @@
+Compiling:
+javac TestApp.java
+javac Peers/Peer.java
+
+Start RMI:
+rmiregistry
+
+Run Program:
+> Peer:
+java Peer.Peer <version> <server_id> <access_point> <MC_IP_address> <MC_port> <MDB_IP_address> <MDB_port> <MDR_IP_address> <MDR_port>
+E.g.: Peer 2.0 1 Peer1 224.0.0.15 8001 224.0.0.16 8002 224.0.0.17 8003
+
+> Backup:
+java TestApp <host> <peer_access_point> BACKUP <file_path> <desired_replication_degree>
+E.g.:  App localhost Peer1 BACKUP /home/daniel/Desktop/Faculdade/SDIS/FEUP-SDIS/project1/Proj/src/test.txt 2
+
+> Restore:
+TestApp <host> <peer_access_point> RESTORE <file_path>
+E.g.:  App localhost/Peer1 RESTORE /home/daniel/Desktop/Faculdade/SDIS/FEUP-SDIS/project1/Proj/src/test.txt
+
+> Delete:
+java TestApp <host> <peer_access_point> DELETE <file_path>
+E.g.:  App localhost/Peer1 DELETE /home/daniel/Desktop/Faculdade/SDIS/FEUP-SDIS/project1/Proj/src/test.txt
+
+
Index: src/run.sh
===================================================================
--- src/run.sh	(nonexistent)
+++ src/run.sh	(revision 2)
@@ -0,0 +1,3 @@
+#!/usr/bin/env bash
+java Peers.Peer "$1" "$2" "$3" "${4:-224.0.0.15}" "${5:-8001}" "${6:-224.0.0.16}" "${7:-8003}" "${8:-224.0.0.17}" "${9:-8004}" &
+
