Project

General

Profile

Statistics
| Revision:

root / proj / src / StorageSystem.java @ 1

History | View | Annotate | Download (4.71 KB)

1 1 up20160559
import java.io.File;
2
import java.io.FileInputStream;
3
import java.io.FileOutputStream;
4
import java.io.IOException;
5
import java.io.ObjectInputStream;
6
import java.io.ObjectOutputStream;
7
import java.nio.file.Files;
8
import java.nio.file.Paths;
9
import java.util.ArrayList;
10
11
public class StorageSystem{
12
13
        private long storage_capacity = 100000000;
14
        private long used_storage;
15
        private long space_available = storage_capacity - used_storage;
16
        private int peerID;
17
18
        public long getStorage_capacity() {
19
                return storage_capacity;
20
        }
21
22
        public long getUsed_storage() {
23
                return used_storage;
24
        }
25
26
        public long getSpace_available() {
27
                return space_available;
28
        }
29
30
        private ArrayList<Chunk> chunks;
31
        private ArrayList<FileInfo> fileinfo;
32
        private ArrayList<BackUpInfo> backupinfo;
33
34
        public StorageSystem(int peerID) {
35
                this.peerID = peerID;
36
                used_storage = 0;
37
                chunks = new ArrayList<Chunk>();
38
                setFileInfo(new ArrayList<FileInfo>());
39
                backupinfo = new ArrayList<BackUpInfo>();
40
41
                try {
42
                        loadFileInfo();
43
                } catch (IOException e) {
44
                        System.out.println("Load File Info Failed:" + e.toString());
45
                        e.printStackTrace();
46
                }
47
48
        }
49
50
        public boolean deleteChunk(Chunk c) {
51
52
        if(isStored(c)) {
53
54
            this.chunks.remove(c);
55
            used_storage= used_storage - c.getsize();
56
            return true;
57
58
        }
59
        System.out.println("Chunk doesn't exist");
60
        return false;
61
62
    }
63
64
65
        public boolean addChunk(Chunk c) {
66
                if(storage_capacity > used_storage + c.getsize()) {
67
                used_storage += c.getsize();
68
69
                                if(!isStored(c)) {
70
                                        chunks.add(c);
71
                                        return true;
72
                                }
73
74
                }
75
                return false;
76
        }
77
78
79
80
        public ArrayList<Chunk> getChunks() {
81
                return chunks;
82
        }
83
84
        public ArrayList<BackUpInfo> getBackUps() {
85
                return backupinfo;
86
        }
87
88
89
        public boolean isStored(Chunk c) {
90
91
                boolean isEqualChunk = false;
92
93
                for(int i = 0; i < chunks.size(); i++) {
94
                        if(chunks.get(i).getChunkId() == c.getChunkId()) {
95
                                isEqualChunk = true;
96
                        }
97
                }
98
99
                return isEqualChunk;
100
        }
101
102
        public void addFile(String filename, long data, String hash) {
103
                getFileInfo().add(new FileInfo(hash, data, filename, this.peerID));
104
        }
105
106
107
108
           public boolean moreRecent(FileInfo a, FileInfo b) {
109
                if (a.getDateModified() > b.getDateModified())
110
                    return true; // highest value first
111
                else  return false;
112
            }
113
114
           public String lookUp(String filename) {
115
                   ArrayList<FileInfo> samename = new ArrayList<FileInfo>();
116
                        String aux = null;
117
                        FileInfo auxFile = null;
118
119
                        for(int i = 0; i < getFileInfo().size(); i++) {
120
121
122
123
                        if(getFileInfo().get(i).getFilename().equals(filename))
124
                                samename.add(getFileInfo().get(i));
125
126
                        }
127
128
                        if(samename.size() == 1) {
129
                        aux = samename.get(0).getFileID();
130
131
                        }
132
133
                        else {
134
                                for(int j = 0; j < samename.size(); j++) {
135
                                        if(moreRecent(samename.get(j), auxFile)){
136
                                                auxFile = samename.get(j);
137
                                                aux = samename.get(j).getFileID();
138
                                                }
139
                                                }
140
                                        }
141
142
143
                        return aux;
144
           }
145
146
147
                //"/fileInfo/Peer"+peerID+"/"+fileID
148
                public void serializeFileInfo() {
149
150
                        for(int i = 0; i < this.getFileInfo().size(); i++ ) {
151
152
                        try{
153
154
                                File file = new File("fileInfo/Peer"+peerID+"/"+this.getFileInfo().get(i).getFileID()+".ser");
155
156
                            if (!file.exists()) {
157
                             file.getParentFile().mkdirs();
158
                             file.createNewFile();
159
                         }
160
                            ObjectOutputStream objoutput = new ObjectOutputStream(new FileOutputStream(file.getPath()));
161
                                objoutput.writeObject(this.getFileInfo().get(i));
162
163
                                objoutput.close();
164
                        }  catch (IOException e) {
165
                                // TODO Auto-generated catch block
166
                                e.printStackTrace();
167
                        }
168
                        }
169
170
171
                }
172
173
174
                public void loadFileInfo() throws IOException {
175
176
                        if(Files.exists(Paths.get("/fileInfo/Peer"+peerID+"/"))) {
177
                         Files.walk(Paths.get("/fileInfo/Peer"+peerID+"/"))
178
                     .forEach(p -> {
179
                        try {
180
                                try(ObjectInputStream objinput = new ObjectInputStream(new FileInputStream(p.toString()))){
181
                                                Object obj = objinput.readObject();
182
183
                                                if(obj instanceof FileInfo) {
184
                                                        FileInfo fileinforetrieved = (FileInfo) obj;
185
                                                        this.getFileInfo().add(fileinforetrieved);
186
                                                }
187
                                        }  catch (ClassNotFoundException e) {
188
                                                // TODO Auto-generated catch block
189
                                                e.printStackTrace();
190
                                        }
191
                        } catch (IOException e) {
192
                            e.printStackTrace();
193
                        }
194
                    });
195
                        }
196
197
198
199
                }
200
201
                public ArrayList<FileInfo> getFileInfo() {
202
                        return fileinfo;
203
                }
204
205
                public void setFileInfo(ArrayList<FileInfo> fileinfo) {
206
                        this.fileinfo = fileinfo;
207
                }
208
209
}