root / project / src / main / java / utils / Utilities.java @ 1
History | View | Annotate | Download (5.46 KB)
1 | 1 | up20120064 | package main.java.utils; |
---|---|---|---|
2 | |||
3 | |||
4 | import main.java.file.FileChunk; |
||
5 | import main.java.file.FileChunkID; |
||
6 | import main.java.file.FileID; |
||
7 | import main.java.peer.Peer; |
||
8 | |||
9 | |||
10 | |||
11 | import java.io.File; |
||
12 | import java.io.FileInputStream; |
||
13 | import java.io.FileNotFoundException; |
||
14 | import java.io.IOException; |
||
15 | import java.net.*; |
||
16 | import java.security.MessageDigest; |
||
17 | import java.util.Enumeration; |
||
18 | import java.util.regex.Pattern; |
||
19 | |||
20 | import static main.java.utils.Constants.*; |
||
21 | |||
22 | public class Utilities { |
||
23 | |||
24 | |||
25 | public static String sha256(String base) { |
||
26 | try{
|
||
27 | MessageDigest digest = MessageDigest.getInstance("SHA-256"); |
||
28 | byte[] hash = digest.digest(base.getBytes("UTF-8")); |
||
29 | StringBuffer hexString = new StringBuffer(); |
||
30 | |||
31 | for (int i = 0; i < hash.length; i++) { |
||
32 | String hex = Integer.toHexString(0xff & hash[i]); |
||
33 | if(hex.length() == 1) hexString.append('0'); |
||
34 | hexString.append(hex); |
||
35 | } |
||
36 | |||
37 | return hexString.toString();
|
||
38 | |||
39 | } catch(Exception ex){ |
||
40 | throw new RuntimeException(ex); |
||
41 | } |
||
42 | } |
||
43 | |||
44 | |||
45 | |||
46 | |||
47 | public static InetAddress getLocalAddress() throws IOException |
||
48 | { |
||
49 | MulticastSocket socket = new MulticastSocket(); |
||
50 | socket.setTimeToLive(0);
|
||
51 | |||
52 | InetAddress addr = InetAddress.getByName("225.0.0.0"); |
||
53 | socket.joinGroup(addr); |
||
54 | |||
55 | byte[] bytes = new byte[0]; |
||
56 | DatagramPacket packet = new DatagramPacket(bytes, bytes.length, addr, |
||
57 | socket.getLocalPort()); |
||
58 | |||
59 | socket.send(packet); |
||
60 | socket.receive(packet); |
||
61 | |||
62 | socket.close(); |
||
63 | |||
64 | return packet.getAddress();
|
||
65 | } |
||
66 | |||
67 | public static boolean validateAccessPoint(String peerAP) { |
||
68 | |||
69 | Pattern pattern;
|
||
70 | pattern = Pattern.compile("^([0-9]{1,3}\\.){3}[0-9]{1,3}(\\/([A-Z]|[a-z])+)?$"); |
||
71 | |||
72 | if(!pattern.matcher(peerAP).matches()) {
|
||
73 | System.out.println("Invalid Access Point!"); |
||
74 | return false; |
||
75 | } |
||
76 | return true; |
||
77 | } |
||
78 | |||
79 | |||
80 | |||
81 | //TODO: CONFIRM MESSAGES ARE AS SPECIFIED IN DOCUMENT <- IMPORTANT FOR INTEROPERABILITY
|
||
82 | public static byte[] messageConstructor(String protocol, FileChunk chunk, FileChunkID chunkID, FileID fileID) { |
||
83 | String message = "default"; |
||
84 | |||
85 | byte[] msgToSend = new byte[0]; |
||
86 | |||
87 | //TODO: chunkgetChunkData() only works for txt. message could be byte[]?
|
||
88 | |||
89 | switch (protocol) {
|
||
90 | case "PUTCHUNK": |
||
91 | message = PUTCHUNK + MESSAGE_SEPARATOR; |
||
92 | message += Peer.getProtocolVersion() + MESSAGE_SEPARATOR; |
||
93 | message += Peer.getID() + MESSAGE_SEPARATOR; |
||
94 | message += chunk.getFileID() + MESSAGE_SEPARATOR; |
||
95 | message += chunk.getChunkNo() + MESSAGE_SEPARATOR; |
||
96 | message += chunk.getReplicationDegree() + MESSAGE_SEPARATOR; |
||
97 | message += CRLF + CRLF; |
||
98 | msgToSend = concatBytes(message.getBytes(), chunk.getChunkData()); |
||
99 | break;
|
||
100 | case "STORED": |
||
101 | message = STORED + MESSAGE_SEPARATOR; |
||
102 | message += Peer.getProtocolVersion() + MESSAGE_SEPARATOR; |
||
103 | message += Peer.getID() + MESSAGE_SEPARATOR; |
||
104 | message += chunkID.getFileID() + MESSAGE_SEPARATOR; |
||
105 | message += chunkID.getChunkNumber() + MESSAGE_SEPARATOR; |
||
106 | message += CRLF + CRLF; |
||
107 | msgToSend = message.getBytes(); |
||
108 | break;
|
||
109 | case "REMOVED": |
||
110 | message = REMOVED + MESSAGE_SEPARATOR; |
||
111 | message += Peer.getProtocolVersion() + MESSAGE_SEPARATOR; |
||
112 | message += Peer.getID() + MESSAGE_SEPARATOR; |
||
113 | message += chunkID.getFileID() + MESSAGE_SEPARATOR; |
||
114 | message += chunkID.getChunkNumber() + MESSAGE_SEPARATOR; |
||
115 | message += CRLF + CRLF; |
||
116 | msgToSend = message.getBytes(); |
||
117 | break;
|
||
118 | case "DELETE": |
||
119 | message = DELETE + MESSAGE_SEPARATOR; |
||
120 | message += Peer.getProtocolVersion() + MESSAGE_SEPARATOR; |
||
121 | message += Peer.getID() + MESSAGE_SEPARATOR; |
||
122 | message += fileID.toString() + MESSAGE_SEPARATOR; |
||
123 | message += CRLF+CRLF; |
||
124 | msgToSend = message.getBytes(); |
||
125 | break;
|
||
126 | case "GETCHUNK": |
||
127 | message = GETCHUNK + MESSAGE_SEPARATOR; |
||
128 | message += Peer.getProtocolVersion() + MESSAGE_SEPARATOR; |
||
129 | message += Peer.getID() + MESSAGE_SEPARATOR; |
||
130 | message += chunkID.getFileID() + MESSAGE_SEPARATOR; |
||
131 | message += chunkID.getChunkNumber() + MESSAGE_SEPARATOR; |
||
132 | message += CRLF + CRLF; |
||
133 | msgToSend = message.getBytes(); |
||
134 | break;
|
||
135 | case "CHUNK": |
||
136 | message = CHUNK + MESSAGE_SEPARATOR; |
||
137 | message += Peer.getProtocolVersion() + MESSAGE_SEPARATOR; |
||
138 | message += Peer.getID() + MESSAGE_SEPARATOR; |
||
139 | message += chunk.getFileID().toString() + MESSAGE_SEPARATOR; |
||
140 | message += chunkID.getChunkNumber() + MESSAGE_SEPARATOR; |
||
141 | message += CRLF + CRLF; |
||
142 | msgToSend = concatBytes(message.getBytes(), chunk.getChunkData()); |
||
143 | break;
|
||
144 | |||
145 | } |
||
146 | return msgToSend;
|
||
147 | } |
||
148 | |||
149 | |||
150 | public static byte[] concatBytes(byte[] a, byte[] b) { |
||
151 | int aLen = a.length;
|
||
152 | int bLen = b.length;
|
||
153 | |||
154 | byte[] c = new byte[aLen + bLen]; |
||
155 | |||
156 | System.arraycopy(a, 0, c, 0, aLen); |
||
157 | System.arraycopy(b, 0, c, aLen, bLen); |
||
158 | |||
159 | return c;
|
||
160 | } |
||
161 | |||
162 | } |