root / src / MDB.java
History | View | Annotate | Download (11 KB)
1 | 1 | up20150487 | |
---|---|---|---|
2 | import java.net.DatagramPacket; |
||
3 | import java.net.InetAddress; |
||
4 | import java.util.ArrayList; |
||
5 | import java.util.Random; |
||
6 | import java.util.concurrent.ConcurrentHashMap; |
||
7 | import java.io.ByteArrayOutputStream; |
||
8 | import java.io.File; |
||
9 | import java.io.FileOutputStream; |
||
10 | |||
11 | public class MDB extends Channel { |
||
12 | public MDB(PeersInfo peersInfo,ChannelsInfo cInfo, int id, InetAddress addr, int port) { |
||
13 | super(peersInfo, cInfo, id, addr, port);
|
||
14 | this.type = ChannelType.MDB;
|
||
15 | System.out.println("MDB channel initialized."); |
||
16 | } |
||
17 | |||
18 | @Override
|
||
19 | public void run() { |
||
20 | |||
21 | byte[] msgData = new byte[65000]; |
||
22 | while (true) { |
||
23 | |||
24 | try {
|
||
25 | DatagramPacket receivedPacket = new DatagramPacket(msgData, msgData.length); |
||
26 | cInfo.socketMDB.receive(receivedPacket); |
||
27 | |||
28 | Message msg = new Message(msgData, receivedPacket.getLength());
|
||
29 | |||
30 | if (msg.getSenderID() != this.peerId) { |
||
31 | System.out.println("_________________________________"); |
||
32 | if(msg.getProtocolVersion().equals("2.0")){ |
||
33 | |||
34 | |||
35 | handleMessageENH(msg); |
||
36 | }else if(msg.getProtocolVersion().equals("1.0")){ |
||
37 | handleMessage(msg); // por alterar
|
||
38 | } |
||
39 | |||
40 | for (ConcurrentHashMap.Entry<String, Chunk> entry : peersInfo.storage.entrySet()) { |
||
41 | String key = entry.getKey().toString();
|
||
42 | Chunk currChunk = entry.getValue(); |
||
43 | System.out.println("::::::::::::::::::::::::::::"); |
||
44 | System.out.println("File id e No: " + key); |
||
45 | for (int i = 0; i < currChunk.getPeersId().size(); i++) { |
||
46 | System.out.println("PeerId(que guardaram a chunk)-> peer" + currChunk.getPeersId().get(i)); |
||
47 | |||
48 | } |
||
49 | System.out.println("CurrReplicationDegree->"+ currChunk.getCurrReplicationDegree()); |
||
50 | System.out.println("ReplicationDegree->"+ currChunk.getReplicationDegree()); |
||
51 | } |
||
52 | |||
53 | System.out.println("--------------------------"); |
||
54 | } |
||
55 | } catch (Exception e) { |
||
56 | e.printStackTrace(); |
||
57 | } |
||
58 | |||
59 | } |
||
60 | |||
61 | } |
||
62 | public void handleMessage(Message msg) { |
||
63 | |||
64 | boolean doBackup = false; |
||
65 | System.out.println(msg.getFileID());
|
||
66 | |||
67 | if (peersInfo.storage.get(msg.getFileID() + ":" + msg.getChunkNo()) == null) { |
||
68 | System.out.println("A chunk->" + Integer.toString(msg.getChunkNo()) + " não existe."); |
||
69 | System.out.println("Criar com ReplicationDegree de ->" + Integer.toString(msg.getReplicationDegree())); |
||
70 | Chunk chunk = new Chunk(msg.getSenderID(), msg.getFileID(), msg.getChunkNo(), msg.getBody().length, msg.getBody(),
|
||
71 | msg.getReplicationDegree()); |
||
72 | peersInfo.storage.put(msg.getFileID() + ":" + Integer.toString(msg.getChunkNo()), chunk); |
||
73 | doBackup = true;
|
||
74 | } else {
|
||
75 | ArrayList<Integer> peers = peersInfo.storage.get(msg.getFileID() + ":" +Integer.toString( msg.getChunkNo())).getPeersId(); |
||
76 | if (peers.contains(this.peerId)) { |
||
77 | } else {
|
||
78 | doBackup = true;
|
||
79 | } |
||
80 | |||
81 | } |
||
82 | Chunk chunk = peersInfo.storage.get(msg.getFileID() + ":" + Integer.toString(msg.getChunkNo())); |
||
83 | if (doBackup) {
|
||
84 | System.out.println("Entra em doBackup chunk Nº->" + Integer.toString(chunk.getChunkNo())); |
||
85 | System.out.println("CurrRepricationDegree->" |
||
86 | + Integer.toString(chunk.getCurrReplicationDegree()));
|
||
87 | System.out.println("ReplicationDegree->" + Integer.toString(chunk.getReplicationDegree())); |
||
88 | Random random = new Random(); |
||
89 | int delay = random.nextInt(200); |
||
90 | try {
|
||
91 | Thread.sleep(delay);
|
||
92 | } catch (Exception e) { |
||
93 | |||
94 | } |
||
95 | System.out.println("Faz Thread.sleep()->"+Integer.toString(delay) ); |
||
96 | System.out.println("CurrRepricationDegree->" |
||
97 | + Integer.toString(chunk.getCurrReplicationDegree()));
|
||
98 | System.out.println("ReplicationDegree->" + Integer.toString(chunk.getReplicationDegree())); |
||
99 | |||
100 | |||
101 | System.out.println("O peer"+ this.peerId +" guarda chunk" + Integer.toString(chunk.getChunkNo())); |
||
102 | String chunkno = String.valueOf(chunk.getChunkNo()); |
||
103 | String chunkFileName = "./peer" + this.peerId + "/" + "backup/" + chunk.getFileID() + "/chk" + chunkno |
||
104 | + ".chunk";
|
||
105 | File newChunkFile = new File(chunkFileName); |
||
106 | try {
|
||
107 | System.out.println("cria o ficheiro em " + chunkFileName); |
||
108 | if (!newChunkFile.exists()) {
|
||
109 | |||
110 | newChunkFile.getParentFile().mkdirs(); |
||
111 | newChunkFile.createNewFile(); |
||
112 | } |
||
113 | FileOutputStream putChunkBody = new FileOutputStream(newChunkFile); |
||
114 | putChunkBody.write(chunk.getData()); |
||
115 | putChunkBody.close(); |
||
116 | |||
117 | if(peersInfo.spaceUsedOnDisk.get(this.peerId)==null){ |
||
118 | peersInfo.spaceUsedOnDisk.put(this.peerId, 0); |
||
119 | } |
||
120 | int space = peersInfo.spaceUsedOnDisk.get(this.peerId); |
||
121 | space = space + chunk.getData().length; |
||
122 | peersInfo.spaceUsedOnDisk.replace(this.peerId, space);
|
||
123 | |||
124 | |||
125 | |||
126 | String[] args = new String[] { String.valueOf(chunk.getChunkNo()) }; |
||
127 | byte[] body = new byte[1]; |
||
128 | peersInfo.storage.get(msg.getFileID() + ":" + Integer.toString(msg.getChunkNo())).addPeersId(this.peerId); |
||
129 | peersInfo.storage.get(msg.getFileID() + ":" + Integer.toString(msg.getChunkNo())).updateCurrReplicationDegree(1);// aumenta 1 |
||
130 | |||
131 | try {
|
||
132 | |||
133 | byte[] msgstore = createMessage("STORED", msg.getProtocolVersion(), this.peerId, msg.getFileID(), args, null); |
||
134 | Message storedMessage = new Message(msgstore, msgstore.length);
|
||
135 | this.cInfo.send(storedMessage, this.cInfo.socketMC, this.cInfo.MCa, this.cInfo.MCp); |
||
136 | |||
137 | } catch (Exception e) { |
||
138 | e.printStackTrace(); |
||
139 | } |
||
140 | |||
141 | } catch (Exception e) { |
||
142 | e.printStackTrace(); |
||
143 | } |
||
144 | |||
145 | |||
146 | } |
||
147 | Peer.serialize_storage(this.peersInfo, this.peerId); |
||
148 | |||
149 | } |
||
150 | |||
151 | public void handleMessageENH(Message msg) { |
||
152 | |||
153 | boolean doBackup = false; |
||
154 | |||
155 | if (peersInfo.storage.get(msg.getFileID() + ":" + msg.getChunkNo()) == null) { |
||
156 | System.out.println("A chunk->" + Integer.toString(msg.getChunkNo()) + " não existe."); |
||
157 | System.out.println("Criar com ReplicationDegree de ->" + Integer.toString(msg.getReplicationDegree())); |
||
158 | Chunk chunk = new Chunk(msg.getSenderID(), msg.getFileID(), msg.getChunkNo(), 3, msg.getBody(), |
||
159 | msg.getReplicationDegree()); |
||
160 | peersInfo.storage.put(msg.getFileID() + ":" + Integer.toString(msg.getChunkNo()), chunk); |
||
161 | doBackup = true;
|
||
162 | } else {
|
||
163 | ArrayList<Integer> peers = peersInfo.storage.get(msg.getFileID() + ":" +Integer.toString( msg.getChunkNo())).getPeersId(); |
||
164 | if (peers.contains(this.peerId)) { |
||
165 | |||
166 | } else {
|
||
167 | doBackup = true;
|
||
168 | } |
||
169 | |||
170 | } |
||
171 | Chunk chunk = peersInfo.storage.get(msg.getFileID() + ":" + Integer.toString(msg.getChunkNo())); |
||
172 | if (doBackup) {
|
||
173 | System.out.println("Entra em doBackup chunk Nº->" + Integer.toString(chunk.getChunkNo())); |
||
174 | System.out.println("CurrRepricationDegree->" |
||
175 | + Integer.toString(chunk.getCurrReplicationDegree()));
|
||
176 | System.out.println("ReplicationDegree->" + Integer.toString(chunk.getReplicationDegree())); |
||
177 | Random random = new Random(); |
||
178 | int delay = random.nextInt(200); |
||
179 | try {
|
||
180 | Thread.sleep(delay);
|
||
181 | } catch (Exception e) { |
||
182 | |||
183 | } |
||
184 | System.out.println("Faz Thread.sleep()->"+Integer.toString(delay) ); |
||
185 | System.out.println("CurrRepricationDegree->" |
||
186 | + Integer.toString(chunk.getCurrReplicationDegree()));
|
||
187 | System.out.println("ReplicationDegree->" + Integer.toString(chunk.getReplicationDegree())); |
||
188 | |||
189 | if (peersInfo.storage.get(msg.getFileID() + ":" + Integer.toString(msg.getChunkNo())).getCurrReplicationDegree() < peersInfo.getStorage().get(msg.getFileID() + ":" + Integer.toString(msg.getChunkNo())).getReplicationDegree()) { |
||
190 | System.out.println("O peer"+ this.peerId +" guarda chunk" + Integer.toString(chunk.getChunkNo())); |
||
191 | String chunkno = String.valueOf(chunk.getChunkNo()); |
||
192 | String chunkFileName = "./peer" + this.peerId + "/" + "backup/" + chunk.getFileID() + "/chk" + chunkno |
||
193 | + ".chunk";
|
||
194 | File newChunkFile = new File(chunkFileName); |
||
195 | try {
|
||
196 | System.out.println("cria o ficheiro em " + chunkFileName); |
||
197 | if (!newChunkFile.exists()) {
|
||
198 | |||
199 | newChunkFile.getParentFile().mkdirs(); |
||
200 | newChunkFile.createNewFile(); |
||
201 | } |
||
202 | FileOutputStream putChunkBody = new FileOutputStream(newChunkFile); |
||
203 | putChunkBody.write(chunk.getData()); |
||
204 | putChunkBody.close(); |
||
205 | |||
206 | if(peersInfo.spaceUsedOnDisk.get(this.peerId)==null){ |
||
207 | peersInfo.spaceUsedOnDisk.put(this.peerId, 0); |
||
208 | } |
||
209 | int space = peersInfo.spaceUsedOnDisk.get(this.peerId); |
||
210 | space = space + chunk.getData().length; |
||
211 | peersInfo.spaceUsedOnDisk.replace(this.peerId, space);
|
||
212 | |||
213 | String[] args = new String[] { String.valueOf(chunk.getChunkNo()) }; |
||
214 | byte[] body = new byte[1]; |
||
215 | peersInfo.storage.get(msg.getFileID() + ":" + Integer.toString(msg.getChunkNo())).addPeersId(this.peerId); |
||
216 | peersInfo.storage.get(msg.getFileID() + ":" + Integer.toString(msg.getChunkNo())).updateCurrReplicationDegree(1);// aumenta 1 |
||
217 | |||
218 | try {
|
||
219 | byte[] msgstore = createMessage("STORED", msg.getProtocolVersion(), this.peerId, msg.getFileID(), args, null); |
||
220 | Message storedMessage = new Message(msgstore, msgstore.length);
|
||
221 | this.cInfo.send(storedMessage, this.cInfo.socketMC, this.cInfo.MCa, this.cInfo.MCp); |
||
222 | |||
223 | } catch (Exception e) { |
||
224 | e.printStackTrace(); |
||
225 | } |
||
226 | |||
227 | } catch (Exception e) { |
||
228 | e.printStackTrace(); |
||
229 | } |
||
230 | |||
231 | } else {}
|
||
232 | } |
||
233 | Peer.serialize_storage(this.peersInfo, this.peerId); |
||
234 | } |
||
235 | } |