Project

General

Profile

Revision 2

Final delivery

View differences:

MessageParser.java
2 2
import java.io.UnsupportedEncodingException;
3 3
import java.net.DatagramPacket;
4 4
import java.net.UnknownHostException;
5
import java.nio.charset.Charset;
5 6
import java.nio.charset.StandardCharsets;
6 7
import java.util.Arrays;
7 8
import java.util.Random;
......
20 21

  
21 22
	public void run() {
22 23
		
23
		String str = new String(packet.getData(), StandardCharsets.UTF_8);
24
		
25
		String str = null;
26

  
27
		str = new String(packet.getData(),0, packet.getLength());
28
	
29
		
24 30
		String rest = null, fileID = null, messageType = null, version = null;
25 31
		int i = 0, next = 0, chunkNo = 0, replicationDeg = 0, senderID=0;
32
		byte[] body = null;
26 33

  
27 34
	     if(str.contains(" ")){
28 35
	    	i= str.indexOf(" ");
......
54 61
		
55 62
		String bodyString = rest.replaceFirst(CRLF+CRLF,"");
56 63
		
57
		String bodyString2 = bodyString.replaceAll("\0", "");
64
		bodyString = bodyString.replaceFirst(" ", "");
58 65
		
59
		bodyString = bodyString2.replaceFirst(" ", "");
60

  
61
		byte[] body = bodyString.getBytes( StandardCharsets.UTF_8);
66
			
67
			body = bodyString.getBytes();
68
	
62 69
		
63 70
		
71

  
64 72
		switch(messageType) {
65 73
		
66 74
		case "PUTCHUNK":
67 75
			
68 76
			Chunk chunk = new Chunk(fileID, chunkNo, body);
69
			System.out.println("CHUNK N: "+ chunk.getChunkN()+ " SIZE: " + chunk.getContent().length);
77
			chunk.setGoalRepDeg(replicationDeg);
70 78
			if(peer.getStorage().addChunk(chunk)) {
71 79

  
72 80
			Message stored = new Message("STORED",peer.getVersion(),peer.getPeerID(),fileID, chunkNo, 0, null);
......
104 112
			
105 113
		case "STORED":
106 114
			peer.getStorage().getBackUps().add(new BackUpInfo(fileID, chunkNo, peer.getPeerID()));
115
			peer.getStorage().increaseChunkRepDeg(fileID, chunkNo);
107 116
		
108 117
		
109 118
		break;
110 119
		
111 120
		case "GETCHUNK": //CHUNK <Version> <SenderId> <FileId> <ChunkNo> <CRLF><CRLF><Body>
112 121
			
122
			Random rand = new Random();
123
			int  n = rand.nextInt(400) + 1;
124
			
125
			
126
			try {
127
				Thread.sleep(n);
128
			} catch (InterruptedException e) {
129
				e.printStackTrace();
130
			} 
131
			
113 132
			for(Chunk chunkSend: peer.getStorage().getChunks()) {
114 133
				
115 134
			if(chunkSend.getFileID().equals(fileID) && chunkSend.getChunkN()==chunkNo) {
116 135
				
117
				Random rand = new Random();
118
				int  n = rand.nextInt(400) + 1;
119 136
				
120
				
121
				try {
122
					Thread.sleep(n);
123
				} catch (InterruptedException e) {
124
					e.printStackTrace();
125
				} 
126 137

  
127 138
				Message sendChunk = new Message("CHUNK",peer.getVersion(),peer.getPeerID(),fileID, chunkNo, 0, chunkSend.getContent());
139
				
128 140
				byte[] reply = sendChunk.sendable();
129 141
				System.out.println(sendChunk.messageToStringPrintable());
142
			
130 143
				
131 144
				try {
132 145
					peer.getMDR().sendMessage(reply);
......
144 157
		
145 158
		case "CHUNK": //GETCHUNK <Version> <SenderId> <FileId> <ChunkNo> <CRLF><CRLF>
146 159
			Chunk gotChunk = new Chunk(fileID, chunkNo, body);
160
			
147 161
			peer.getStorage().addChunk(gotChunk);
148
			if(gotChunk.getContent().length < 64000)
162
			if(gotChunk.getContent().length < 58000)				
149 163
				peer.lastChunk();
150 164
			break;
151 165
			
152 166
		case "REMOVED":
153
			Random rand = new Random();
154
			int  n = rand.nextInt(400) + 1;
167
			if(peer.getStorage().lowerRepDeg(fileID, chunkNo)) {
168
			Random rand2 = new Random();
169
			int  n2 = rand2.nextInt(400) + 1;
155 170
			
156 171
			
157 172
			try {
158
				Thread.sleep(n);
173
				Thread.sleep(n2);
159 174
			} catch (InterruptedException e) {
160 175
				e.printStackTrace();
161 176
			} 
162
            peer.getStorage().getBackUps().remove(new BackUpInfo(fileID, chunkNo, peer.getPeerID()));
177
			for(Chunk chunkSend: peer.getStorage().getChunks())
178
			{
179
				if(chunkSend.getFileID().equals(fileID) && chunkSend.getChunkN() == chunkNo)
180
				{
181
					boolean stored = false;
182
					
183
					Message msg = new Message("PUTCHUNK", version, peer.getPeerID(),chunkSend.getFileID(),chunkNo, chunkSend.getGoalRepDeg()-chunkSend.getRepDeg(),
184
							chunkSend.getContent());
185
					byte[] msgByte = msg.sendable();
186
		
187
					System.out.println(msg.messageToStringPrintable());
188
					try {
189
						peer.getMDB().sendMessage(msgByte);
190
					} catch (UnknownHostException e2) {
191
						// TODO Auto-generated catch block
192
						e2.printStackTrace();
193
					}					
194
					
195
					for(int j = 0; j < 5 && !stored; j++) {
196
						
197
					try {
198
						TimeUnit.SECONDS.sleep(1+j);
199
					} catch (InterruptedException e1) {
200
						// TODO Auto-generated catch block
201
						e1.printStackTrace();
202
					}
203
					
204
					if(peer.storedCheck(chunkNo, fileID) >= chunkSend.getGoalRepDeg()) {
205
						stored = true;
206
					} 
207
					else {
208
						try {
209
							peer.getMDB().sendMessage(msgByte);
210
						} catch (UnknownHostException e) {
211
							// TODO Auto-generated catch block
212
							e.printStackTrace();
213
						} 
214
						System.out.println(msg.messageToStringPrintable());
215
						
216
					}
217
					} 
218
					stored = false;
219
				}
220
				
221
			}
222
			}
223
		
163 224
            break;
164 225
            
165 226
		case "DELETE":
166
            //peer.getStorage().getFileInfo().remove(new FileInfo(fileID, peer.getFileInfo.getDateModified(), FileInfo.getFilename(), peer.getPeerID()));
227
            peer.getStorage().deleteChunkByFileID(fileID);
228
            peer.deleteFileFolder(fileID);
167 229
            break;
168 230
		}
169 231
	}

Also available in: Unified diff