Revision 1
final
src/.idea/encodings.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<project version="4"> |
|
3 |
<component name="Encoding" addBOMForNewFiles="with NO BOM" /> |
|
4 |
</project> |
src/.idea/misc.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<project version="4"> |
|
3 |
<component name="ProjectRootManager" version="2" languageLevel="JDK_10" project-jdk-name="10" project-jdk-type="JavaSDK"> |
|
4 |
<output url="file://$PROJECT_DIR$/out" /> |
|
5 |
</component> |
|
6 |
</project> |
src/peer/Peer.java | ||
---|---|---|
1 |
package peer; |
|
2 |
|
|
3 |
import channels.McChannel; |
|
4 |
import channels.MdbChannel; |
|
5 |
import protocols.BackupProtocol; |
|
6 |
import protocols.RestoreProtocol; |
|
7 |
import utils.Utils; |
|
8 |
|
|
9 |
import java.io.File; |
|
10 |
import java.io.IOException; |
|
11 |
import java.net.*; |
|
12 |
import java.security.NoSuchAlgorithmException; |
|
13 |
|
|
14 |
import utils.*; |
|
15 |
|
|
16 |
public class Peer { |
|
17 |
|
|
18 |
private static int senderId; //id of the server that has sent the message |
|
19 |
|
|
20 |
private static InetAddress mcChannel; |
|
21 |
private static InetAddress mdbChannel; |
|
22 |
private static InetAddress mdrChannel; |
|
23 |
|
|
24 |
private static int mcPort; |
|
25 |
private static int mdbPort; |
|
26 |
private static int mdrPort; |
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
//BACKUP, DELETE, RECLAIM, RESTORE; |
|
31 |
|
|
32 |
|
|
33 |
public static void main(String[] args) throws IOException, NoSuchAlgorithmException, InterruptedException { |
|
34 |
if(!parseArgs(args)) return; |
|
35 |
|
|
36 |
joinMulticastGroup(); |
|
37 |
startThreads(); |
|
38 |
getRequests(); |
|
39 |
|
|
40 |
} |
|
41 |
|
|
42 |
private static void getRequests() throws IOException, NoSuchAlgorithmException, InterruptedException { |
|
43 |
DatagramSocket request = new DatagramSocket(); |
|
44 |
int portNumber = request.getLocalPort(); |
|
45 |
System.out.println("Socket Peer opened in port number: " + portNumber); |
|
46 |
|
|
47 |
while(true){ |
|
48 |
byte[] buffer = new byte[Utils.BUFFER_SIZE]; |
|
49 |
DatagramPacket packet = new DatagramPacket(buffer, buffer.length); |
|
50 |
|
|
51 |
request.receive(packet); |
|
52 |
|
|
53 |
InetAddress clientAddress = packet.getAddress(); |
|
54 |
String requestString = new String(packet.getData(), 0, packet.getLength()); |
|
55 |
|
|
56 |
System.out.println(); |
|
57 |
System.out.println("---------------------------------------------------------------------------"); |
|
58 |
System.out.println("Request from TestApp: " + requestString); |
|
59 |
System.out.println(); |
|
60 |
|
|
61 |
String[] requestArgs = requestString.split("\\s+"); |
|
62 |
|
|
63 |
switch(requestArgs[0]){ |
|
64 |
case "BACKUP": |
|
65 |
new BackupProtocol(requestArgs); |
|
66 |
break; |
|
67 |
case "RESTORE": |
|
68 |
new RestoreProtocol(requestArgs); |
|
69 |
break; |
|
70 |
default: |
|
71 |
break; |
|
72 |
} |
|
73 |
|
|
74 |
int port = packet.getPort(); |
|
75 |
String answer = "Got your request"; |
|
76 |
buffer = answer.getBytes(); |
|
77 |
packet = new DatagramPacket(buffer, 0, buffer.length, clientAddress, port); |
|
78 |
|
|
79 |
request.send(packet); |
|
80 |
} |
|
81 |
} |
|
82 |
|
|
83 |
public static void startThreads(){ //only subscribed mc channel |
|
84 |
Utils.mcChannel = new McChannel(); |
|
85 |
new Thread(Utils.mcChannel).start(); |
|
86 |
|
|
87 |
Utils.mdbChannel = new MdbChannel(); |
|
88 |
new Thread(Utils.mdbChannel).start(); |
|
89 |
|
|
90 |
Utils.mdrChannel = new channels.MdrChannel(); |
|
91 |
new Thread(Utils.mdrChannel).start(); |
|
92 |
} |
|
93 |
|
|
94 |
public static void joinMulticastGroup() throws IOException{ |
|
95 |
//mc channel |
|
96 |
Utils.mcSocket = new MulticastSocket(getMcPort()); |
|
97 |
Utils.mcSocket.joinGroup(getMcAddress()); |
|
98 |
//mdb channel |
|
99 |
Utils.mdbSocket = new MulticastSocket(getMdbPort()); |
|
100 |
Utils.mdbSocket.joinGroup(getMdbAddress()); |
|
101 |
//mdr channel |
|
102 |
Utils.mdrSocket = new MulticastSocket(getMdrPort()); |
|
103 |
Utils.mdrSocket.joinGroup(getMdrAddress()); |
|
104 |
} |
|
105 |
|
|
106 |
public static boolean parseArgs(String[] args) throws UnknownHostException{ |
|
107 |
if(args == null || args.length != 4){ |
|
108 |
System.out.println("Usage: java peer.peer <SenderId> <McAddress>:<McPortNumber> <MdbAddress>:<MdbPortNumber> <MdrAddress>:<MdrPortNumber>"); |
|
109 |
return false; |
|
110 |
} |
|
111 |
|
|
112 |
//sets sender id |
|
113 |
setSenderId(Integer.parseInt(args[0])); |
|
114 |
|
|
115 |
//sets address and port for channel MC |
|
116 |
String[] mcPart = args[1].split(":"); |
|
117 |
setMcAddress(InetAddress.getByName(mcPart[0])); |
|
118 |
setMcPort(Integer.parseInt(mcPart[1])); |
|
119 |
|
|
120 |
//sets address and port for channel MDB |
|
121 |
String[] mdbPart = args[2].split(":"); |
|
122 |
setMdbAddress(InetAddress.getByName(mdbPart[0])); |
|
123 |
setMdbPort(Integer.parseInt(mdbPart[1])); |
|
124 |
|
|
125 |
//sets address and port for channel MDR |
|
126 |
String[] mdrPart = args[3].split(":"); |
|
127 |
setMdrAddress(InetAddress.getByName(mdrPart[0])); |
|
128 |
setMdrPort(Integer.parseInt(mdrPart[1])); |
|
129 |
|
|
130 |
return true; |
|
131 |
} |
|
132 |
|
|
133 |
//getters and setters |
|
134 |
|
|
135 |
public static InetAddress getMcAddress(){ |
|
136 |
return mcChannel; |
|
137 |
} |
|
138 |
|
|
139 |
public static InetAddress getMdbAddress(){ |
|
140 |
return mdbChannel; |
|
141 |
} |
|
142 |
|
|
143 |
public static InetAddress getMdrAddress(){ |
|
144 |
return mdrChannel; |
|
145 |
} |
|
146 |
|
|
147 |
public static int getMcPort(){ |
|
148 |
return mcPort; |
|
149 |
} |
|
150 |
|
|
151 |
public static int getMdbPort(){ |
|
152 |
return mdbPort; |
|
153 |
} |
|
154 |
|
|
155 |
public static int getMdrPort(){ |
|
156 |
return mdrPort; |
|
157 |
} |
|
158 |
|
|
159 |
public static void setMcAddress(InetAddress mcAddress){ |
|
160 |
mcChannel = mcAddress; |
|
161 |
} |
|
162 |
|
|
163 |
public static void setMdbAddress(InetAddress mdbAddress){ |
|
164 |
mdbChannel = mdbAddress; |
|
165 |
} |
|
166 |
|
|
167 |
public static void setMdrAddress(InetAddress mdrAddress){ |
|
168 |
mdrChannel = mdrAddress; |
|
169 |
} |
|
170 |
|
|
171 |
public static void setMcPort(int port){ |
|
172 |
mcPort = port; |
|
173 |
} |
|
174 |
|
|
175 |
public static void setMdbPort(int port){ |
|
176 |
mdbPort = port; |
|
177 |
} |
|
178 |
|
|
179 |
public static void setMdrPort(int port){ |
|
180 |
mdrPort = port; |
|
181 |
} |
|
182 |
|
|
183 |
public static void setSenderId(int id){ |
|
184 |
senderId = id; |
|
185 |
} |
|
186 |
|
|
187 |
public static int getSenderId(){ |
|
188 |
return senderId; |
|
189 |
} |
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
} |
|
198 |
|
src/TestApp.java | ||
---|---|---|
1 |
import java.io.IOException; |
|
2 |
import java.net.*; |
|
3 |
import java.util.Scanner; |
|
4 |
|
|
5 |
import static utils.Utils.BUFFER_SIZE; |
|
6 |
|
|
7 |
//TODO: parse args for reclaim and delete not fully functional |
|
8 |
//grupo 11 |
|
9 |
|
|
10 |
public class TestApp { |
|
11 |
|
|
12 |
private static String hostname = "localhost"; |
|
13 |
private static int portNumber; |
|
14 |
private static String protocolRequest; |
|
15 |
private static String filePath; |
|
16 |
private static int replicationDegree; |
|
17 |
|
|
18 |
|
|
19 |
public static void main(String[] args) throws SocketException, UnknownHostException { |
|
20 |
mainMenu(); |
|
21 |
} |
|
22 |
|
|
23 |
private static void mainMenu() throws SocketException, UnknownHostException { |
|
24 |
|
|
25 |
System.out.println("1- Backup a file"); |
|
26 |
System.out.println("2- Restore a file"); |
|
27 |
System.out.println("0- Exit"); |
|
28 |
|
|
29 |
|
|
30 |
Scanner in = new Scanner(System.in); |
|
31 |
int option = in.nextInt(); |
|
32 |
|
|
33 |
switch(option){ |
|
34 |
case 0: |
|
35 |
System.out.println("exiting application..."); |
|
36 |
break; |
|
37 |
case 1: |
|
38 |
backupMenu(); |
|
39 |
break; |
|
40 |
case 2: |
|
41 |
restoreMenu(); |
|
42 |
break; |
|
43 |
|
|
44 |
} |
|
45 |
} |
|
46 |
|
|
47 |
private static void restoreMenu() throws SocketException, UnknownHostException { |
|
48 |
String[] args = new String[4]; |
|
49 |
|
|
50 |
System.out.println("Please insert the peer port number which you want to send the request"); |
|
51 |
|
|
52 |
Scanner in = new Scanner(System.in); |
|
53 |
String portnumber = in.nextLine(); |
|
54 |
|
|
55 |
args[0] = portnumber; |
|
56 |
|
|
57 |
args[1] = "RESTORE"; |
|
58 |
|
|
59 |
System.out.println("Specify the file name you want to restore "); |
|
60 |
|
|
61 |
String filename = in.nextLine(); |
|
62 |
|
|
63 |
args[2] = filename; |
|
64 |
|
|
65 |
if(!parseArgs(args)) return; |
|
66 |
|
|
67 |
sendRequest(args); |
|
68 |
|
|
69 |
|
|
70 |
} |
|
71 |
|
|
72 |
private static void backupMenu() throws SocketException, UnknownHostException { |
|
73 |
String[] args = new String[4]; |
|
74 |
|
|
75 |
System.out.println("Please insert the peer port number which you want to send the request"); |
|
76 |
|
|
77 |
Scanner in = new Scanner(System.in); |
|
78 |
String portnumber = in.nextLine(); |
|
79 |
|
|
80 |
args[0] = portnumber; |
|
81 |
|
|
82 |
args[1] = "BACKUP"; |
|
83 |
|
|
84 |
System.out.println("Specify the file name you want to backup (must be inside test folder) "); |
|
85 |
|
|
86 |
String filename = in.nextLine(); |
|
87 |
|
|
88 |
args[2] = filename; |
|
89 |
|
|
90 |
System.out.println("Now enter the replication degree"); |
|
91 |
String replicD = in.nextLine(); |
|
92 |
|
|
93 |
args[3] = replicD; |
|
94 |
|
|
95 |
if(!parseArgs(args)) return; |
|
96 |
|
|
97 |
sendRequest(args); |
|
98 |
|
|
99 |
} |
|
100 |
|
|
101 |
private static void sendRequest(String[] args) throws SocketException, UnknownHostException { |
|
102 |
DatagramSocket socket = new DatagramSocket(); |
|
103 |
byte[] buffer; |
|
104 |
String message = ""; |
|
105 |
|
|
106 |
for(int i = 1; i < args.length; i++){ |
|
107 |
if(i == args.length -1){ |
|
108 |
message += args[i]; |
|
109 |
break; |
|
110 |
} |
|
111 |
message += args[i] + " "; |
|
112 |
} |
|
113 |
|
|
114 |
buffer = message.getBytes(); |
|
115 |
|
|
116 |
InetAddress address = InetAddress.getByName(hostname); |
|
117 |
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, address, portNumber); |
|
118 |
|
|
119 |
try { |
|
120 |
socket.send(packet); |
|
121 |
} catch (IOException e) { |
|
122 |
e.printStackTrace(); |
|
123 |
} |
|
124 |
|
|
125 |
buffer = new byte[BUFFER_SIZE]; |
|
126 |
packet = new DatagramPacket(buffer, buffer.length); |
|
127 |
|
|
128 |
// |
|
129 |
try { |
|
130 |
socket.receive(packet); |
|
131 |
} catch (IOException e) { |
|
132 |
e.printStackTrace(); |
|
133 |
} |
|
134 |
|
|
135 |
String answer = new String(packet.getData(), 0, packet.getLength()); |
|
136 |
System.out.println("Peer said: " + answer); |
|
137 |
socket.close(); |
|
138 |
|
|
139 |
//after request sent and done, come back to main menu |
|
140 |
mainMenu(); |
|
141 |
} |
|
142 |
|
|
143 |
private static boolean parseArgs(String[] args) { |
|
144 |
if(args == null || args.length < 3) { |
|
145 |
System.out.println("Usage: java TestApp <peer_ap> <sub_protocol> <opnd_1> <opnd_2>"); |
|
146 |
return false; |
|
147 |
} |
|
148 |
String[] peer_ap = args[0].split(":"); |
|
149 |
|
|
150 |
if(peer_ap.length == 1) { //user just gave the port number |
|
151 |
portNumber = Integer.parseInt(peer_ap[0]); |
|
152 |
} |
|
153 |
else if(peer_ap.length == 2) { |
|
154 |
hostname = peer_ap[0]; |
|
155 |
portNumber = Integer.parseInt(peer_ap[1]); |
|
156 |
} |
|
157 |
else { |
|
158 |
System.out.println("Usage: java TestApp <peer_ap> <sub_protocol> <opnd_1> <opnd_2>"); |
|
159 |
return false; |
|
160 |
} |
|
161 |
|
|
162 |
switch(args[1]) { |
|
163 |
case "BACKUP": |
|
164 |
protocolRequest = "BACKUP"; |
|
165 |
filePath = args[2]; |
|
166 |
if(args[3] == null){ |
|
167 |
System.out.println("In backup protocol, you have to specify also the replication degree"); |
|
168 |
return false; |
|
169 |
} |
|
170 |
replicationDegree = Integer.parseInt(args[3]); |
|
171 |
break; |
|
172 |
case "RESTORE": |
|
173 |
protocolRequest = "RESTORE"; |
|
174 |
filePath = args[2]; |
|
175 |
break; |
|
176 |
case "DELETE": |
|
177 |
protocolRequest = "DELETE"; |
|
178 |
filePath = args[2]; |
|
179 |
break; |
|
180 |
case "RECLAIM": |
|
181 |
protocolRequest = "RECLAIM"; |
|
182 |
break; |
|
183 |
default: return false; |
|
184 |
} |
|
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
return true; |
|
190 |
} |
|
191 |
|
|
192 |
} |
|
193 |
|
src/.idea/modules.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<project version="4"> |
|
3 |
<component name="ProjectModuleManager"> |
|
4 |
<modules> |
|
5 |
<module fileurl="file://$PROJECT_DIR$/SIDS.iml" filepath="$PROJECT_DIR$/SIDS.iml" /> |
|
6 |
</modules> |
|
7 |
</component> |
|
8 |
</project> |
peer4.sh | ||
---|---|---|
1 |
cd src |
|
2 |
javac *.java |
|
3 |
java peer.Peer 4 224.0.0.0:8000 224.0.0.0:8001 224.0.0.0:8002 |
|
4 |
|
|
5 |
|
|
0 | 6 |
src/.idea/vcs.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<project version="4"> |
|
3 |
<component name="VcsDirectoryMappings"> |
|
4 |
<mapping directory="$PROJECT_DIR$/.." vcs="Git" /> |
|
5 |
</component> |
|
6 |
</project> |
src/.idea/workspace.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<project version="4"> |
|
3 |
<component name="ChangeListManager"> |
|
4 |
<list default="true" id="d51d95c7-4d11-4ecd-934b-ad1c8b084f2c" name="Default Changelist" comment=""> |
|
5 |
<change beforePath="$PROJECT_DIR$/peer/Peer.java" beforeDir="false" afterPath="$PROJECT_DIR$/peer/Peer.java" afterDir="false" /> |
|
6 |
<change beforePath="$PROJECT_DIR$/protocols/RestoreProtocol.java" beforeDir="false" afterPath="$PROJECT_DIR$/protocols/RestoreProtocol.java" afterDir="false" /> |
|
7 |
</list> |
|
8 |
<ignored path="$PROJECT_DIR$/out/" /> |
|
9 |
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" /> |
|
10 |
<option name="SHOW_DIALOG" value="false" /> |
|
11 |
<option name="HIGHLIGHT_CONFLICTS" value="true" /> |
|
12 |
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> |
|
13 |
<option name="LAST_RESOLUTION" value="IGNORE" /> |
|
14 |
</component> |
|
15 |
<component name="FileEditorManager"> |
|
16 |
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300"> |
|
17 |
<file pinned="false" current-in-tab="true"> |
|
18 |
<entry file="file://$PROJECT_DIR$/protocols/RestoreProtocol.java"> |
|
19 |
<provider selected="true" editor-type-id="text-editor"> |
|
20 |
<state relative-caret-position="195"> |
|
21 |
<caret line="37" column="100" selection-start-line="37" selection-start-column="100" selection-end-line="37" selection-end-column="100" /> |
|
22 |
</state> |
|
23 |
</provider> |
|
24 |
</entry> |
|
25 |
</file> |
|
26 |
<file pinned="false" current-in-tab="false"> |
|
27 |
<entry file="file://$PROJECT_DIR$/channels/McChannel.java"> |
|
28 |
<provider selected="true" editor-type-id="text-editor"> |
|
29 |
<state relative-caret-position="541"> |
|
30 |
<caret line="61" column="27" selection-start-line="61" selection-start-column="27" selection-end-line="61" selection-end-column="27" /> |
|
31 |
</state> |
|
32 |
</provider> |
|
33 |
</entry> |
|
34 |
</file> |
|
35 |
<file pinned="false" current-in-tab="false"> |
|
36 |
<entry file="file://$PROJECT_DIR$/channels/MdrChannel.java"> |
|
37 |
<provider selected="true" editor-type-id="text-editor"> |
|
38 |
<state relative-caret-position="511"> |
|
39 |
<caret line="73" column="13" selection-start-line="73" selection-start-column="13" selection-end-line="73" selection-end-column="13" /> |
|
40 |
</state> |
|
41 |
</provider> |
|
42 |
</entry> |
|
43 |
</file> |
|
44 |
<file pinned="false" current-in-tab="false"> |
|
45 |
<entry file="file://$PROJECT_DIR$/utils/Chunk.java"> |
|
46 |
<provider selected="true" editor-type-id="text-editor"> |
|
47 |
<state relative-caret-position="376"> |
|
48 |
<caret line="30" column="42" selection-start-line="30" selection-start-column="42" selection-end-line="30" selection-end-column="42" /> |
|
49 |
</state> |
|
50 |
</provider> |
|
51 |
</entry> |
|
52 |
</file> |
|
53 |
<file pinned="false" current-in-tab="false"> |
|
54 |
<entry file="file://$PROJECT_DIR$/utils/FileController.java"> |
|
55 |
<provider selected="true" editor-type-id="text-editor"> |
|
56 |
<state relative-caret-position="-89"> |
|
57 |
<caret line="38" column="70" selection-start-line="38" selection-start-column="70" selection-end-line="38" selection-end-column="70" /> |
|
58 |
<folding> |
|
59 |
<element signature="method#saveToDisk#0;class#FileController#0" /> |
|
60 |
</folding> |
|
61 |
</state> |
|
62 |
</provider> |
|
63 |
</entry> |
|
64 |
</file> |
|
65 |
<file pinned="false" current-in-tab="false"> |
|
66 |
<entry file="file://$PROJECT_DIR$/peer/Peer.java"> |
|
67 |
<provider selected="true" editor-type-id="text-editor"> |
|
68 |
<state relative-caret-position="-645"> |
|
69 |
<caret line="37" column="12" selection-start-line="37" selection-start-column="12" selection-end-line="37" selection-end-column="12" /> |
|
70 |
</state> |
|
71 |
</provider> |
|
72 |
</entry> |
|
73 |
</file> |
|
74 |
<file pinned="false" current-in-tab="false"> |
|
75 |
<entry file="file://$PROJECT_DIR$/utils/Utils.java"> |
|
76 |
<provider selected="true" editor-type-id="text-editor"> |
|
77 |
<state relative-caret-position="300"> |
|
78 |
<caret line="26" column="52" selection-start-line="26" selection-start-column="52" selection-end-line="26" selection-end-column="52" /> |
|
79 |
</state> |
|
80 |
</provider> |
|
81 |
</entry> |
|
82 |
</file> |
|
83 |
<file pinned="false" current-in-tab="false"> |
|
84 |
<entry file="file://$PROJECT_DIR$/TestApp.java"> |
|
85 |
<provider selected="true" editor-type-id="text-editor"> |
|
86 |
<state relative-caret-position="90"> |
|
87 |
<caret line="10" selection-start-line="10" selection-end-line="10" /> |
|
88 |
</state> |
|
89 |
</provider> |
|
90 |
</entry> |
|
91 |
</file> |
|
92 |
<file pinned="false" current-in-tab="false"> |
|
93 |
<entry file="file://$PROJECT_DIR$/protocols/BackupProtocol.java"> |
|
94 |
<provider selected="true" editor-type-id="text-editor"> |
|
95 |
<state relative-caret-position="180"> |
|
96 |
<caret line="12" column="13" selection-start-line="12" selection-start-column="13" selection-end-line="12" selection-end-column="13" /> |
|
97 |
<folding> |
|
98 |
<element signature="imports" expanded="true" /> |
|
99 |
</folding> |
|
100 |
</state> |
|
101 |
</provider> |
|
102 |
</entry> |
|
103 |
</file> |
|
104 |
<file pinned="false" current-in-tab="false"> |
|
105 |
<entry file="file://$PROJECT_DIR$/channels/MdbChannel.java"> |
|
106 |
<provider selected="true" editor-type-id="text-editor"> |
|
107 |
<state relative-caret-position="405"> |
|
108 |
<caret line="35" column="62" selection-start-line="35" selection-start-column="62" selection-end-line="35" selection-end-column="62" /> |
|
109 |
</state> |
|
110 |
</provider> |
|
111 |
</entry> |
|
112 |
</file> |
|
113 |
</leaf> |
|
114 |
</component> |
|
115 |
<component name="FindInProjectRecents"> |
|
116 |
<findStrings> |
|
117 |
<find>store</find> |
|
118 |
<find>stored</find> |
|
119 |
<find>hashData</find> |
|
120 |
<find>Utils</find> |
|
121 |
</findStrings> |
|
122 |
</component> |
|
123 |
<component name="Git.Settings"> |
|
124 |
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/.." /> |
|
125 |
</component> |
|
126 |
<component name="IdeDocumentHistory"> |
|
127 |
<option name="CHANGED_PATHS"> |
|
128 |
<list> |
|
129 |
<option value="$PROJECT_DIR$/protocols/BackupProtocol.java" /> |
|
130 |
<option value="$PROJECT_DIR$/utils/Chunk.java" /> |
|
131 |
<option value="$PROJECT_DIR$/utils/FileController.java" /> |
|
132 |
<option value="$PROJECT_DIR$/channels/MdbChannel.java" /> |
|
133 |
<option value="$PROJECT_DIR$/channels/MdrChannel.java" /> |
|
134 |
<option value="$PROJECT_DIR$/utils/Utils.java" /> |
|
135 |
<option value="$PROJECT_DIR$/channels/McChannel.java" /> |
|
136 |
<option value="$PROJECT_DIR$/peer/Peer.java" /> |
|
137 |
<option value="$PROJECT_DIR$/protocols/RestoreProtocol.java" /> |
|
138 |
</list> |
|
139 |
</option> |
|
140 |
</component> |
|
141 |
<component name="ProjectFrameBounds" extendedState="6"> |
|
142 |
<option name="x" value="1849" /> |
|
143 |
<option name="width" value="1356" /> |
|
144 |
<option name="height" value="528" /> |
|
145 |
</component> |
|
146 |
<component name="ProjectLevelVcsManager" settingsEditedManually="true" /> |
|
147 |
<component name="ProjectView"> |
|
148 |
<navigator proportions="" version="1"> |
|
149 |
<foldersAlwaysOnTop value="true" /> |
|
150 |
</navigator> |
|
151 |
<panes> |
|
152 |
<pane id="Scope" /> |
|
153 |
<pane id="PackagesPane" /> |
|
154 |
<pane id="ProjectPane"> |
|
155 |
<subPane> |
|
156 |
<expand> |
|
157 |
<path> |
|
158 |
<item name="src" type="b2602c69:ProjectViewProjectNode" /> |
|
159 |
<item name="src" type="462c0819:PsiDirectoryNode" /> |
|
160 |
</path> |
|
161 |
<path> |
|
162 |
<item name="src" type="b2602c69:ProjectViewProjectNode" /> |
|
163 |
<item name="src" type="462c0819:PsiDirectoryNode" /> |
|
164 |
<item name="channels" type="462c0819:PsiDirectoryNode" /> |
|
165 |
</path> |
|
166 |
<path> |
|
167 |
<item name="src" type="b2602c69:ProjectViewProjectNode" /> |
|
168 |
<item name="src" type="462c0819:PsiDirectoryNode" /> |
|
169 |
<item name="peer" type="462c0819:PsiDirectoryNode" /> |
|
170 |
</path> |
|
171 |
<path> |
|
172 |
<item name="src" type="b2602c69:ProjectViewProjectNode" /> |
|
173 |
<item name="src" type="462c0819:PsiDirectoryNode" /> |
|
174 |
<item name="protocols" type="462c0819:PsiDirectoryNode" /> |
|
175 |
</path> |
|
176 |
<path> |
|
177 |
<item name="src" type="b2602c69:ProjectViewProjectNode" /> |
|
178 |
<item name="src" type="462c0819:PsiDirectoryNode" /> |
|
179 |
<item name="utils" type="462c0819:PsiDirectoryNode" /> |
|
180 |
</path> |
|
181 |
</expand> |
|
182 |
<select /> |
|
183 |
</subPane> |
|
184 |
</pane> |
|
185 |
</panes> |
|
186 |
</component> |
|
187 |
<component name="PropertiesComponent"> |
|
188 |
<property name="com.android.tools.idea.instantapp.provision.ProvisionBeforeRunTaskProvider.myTimeStamp" value="1555276281623" /> |
|
189 |
</component> |
|
190 |
<component name="RunDashboard"> |
|
191 |
<option name="ruleStates"> |
|
192 |
<list> |
|
193 |
<RuleState> |
|
194 |
<option name="name" value="ConfigurationTypeDashboardGroupingRule" /> |
|
195 |
</RuleState> |
|
196 |
<RuleState> |
|
197 |
<option name="name" value="StatusDashboardGroupingRule" /> |
|
198 |
</RuleState> |
|
199 |
</list> |
|
200 |
</option> |
|
201 |
</component> |
|
202 |
<component name="RunManager" selected="Application.TestApp"> |
|
203 |
<configuration name="Peer1" type="Application" factoryName="Application" temporary="true"> |
|
204 |
<option name="MAIN_CLASS_NAME" value="peer.Peer" /> |
|
205 |
<module name="SIDS" /> |
|
206 |
<option name="PROGRAM_PARAMETERS" value="1 224.0.0.0:8000 224.0.0.0:8001 224.0.0.0:8002" /> |
|
207 |
<extension name="coverage"> |
|
208 |
<pattern> |
|
209 |
<option name="PATTERN" value="peer.*" /> |
|
210 |
<option name="ENABLED" value="true" /> |
|
211 |
</pattern> |
|
212 |
</extension> |
|
213 |
<method v="2"> |
|
214 |
<option name="Make" enabled="true" /> |
|
215 |
</method> |
|
216 |
</configuration> |
|
217 |
<configuration name="Peer2" type="Application" factoryName="Application"> |
|
218 |
<option name="MAIN_CLASS_NAME" value="peer.Peer" /> |
|
219 |
<module name="SIDS" /> |
|
220 |
<option name="PROGRAM_PARAMETERS" value="2 224.0.0.0:8000 224.0.0.0:8001 224.0.0.0:8002" /> |
|
221 |
<extension name="coverage"> |
|
222 |
<pattern> |
|
223 |
<option name="PATTERN" value="peer.*" /> |
|
224 |
<option name="ENABLED" value="true" /> |
|
225 |
</pattern> |
|
226 |
</extension> |
|
227 |
<method v="2"> |
|
228 |
<option name="Make" enabled="true" /> |
|
229 |
</method> |
|
230 |
</configuration> |
|
231 |
<configuration name="Peer3" type="Application" factoryName="Application"> |
|
232 |
<option name="MAIN_CLASS_NAME" value="peer.Peer" /> |
|
233 |
<module name="SIDS" /> |
|
234 |
<option name="PROGRAM_PARAMETERS" value="3 224.0.0.0:8000 224.0.0.0:8001 224.0.0.0:8002" /> |
|
235 |
<extension name="coverage"> |
|
236 |
<pattern> |
|
237 |
<option name="PATTERN" value="peer.*" /> |
|
238 |
<option name="ENABLED" value="true" /> |
|
239 |
</pattern> |
|
240 |
</extension> |
|
241 |
<method v="2"> |
|
242 |
<option name="Make" enabled="true" /> |
|
243 |
</method> |
|
244 |
</configuration> |
|
245 |
<configuration name="TestApp" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true"> |
|
246 |
<option name="MAIN_CLASS_NAME" value="TestApp" /> |
|
247 |
<module name="SIDS" /> |
|
248 |
<method v="2"> |
|
249 |
<option name="Make" enabled="true" /> |
|
250 |
</method> |
|
251 |
</configuration> |
|
252 |
<list> |
|
253 |
<item itemvalue="Application.Peer2" /> |
|
254 |
<item itemvalue="Application.Peer3" /> |
|
255 |
<item itemvalue="Application.Peer1" /> |
|
256 |
<item itemvalue="Application.TestApp" /> |
|
257 |
</list> |
|
258 |
<recent_temporary> |
|
259 |
<list> |
|
260 |
<item itemvalue="Application.TestApp" /> |
|
261 |
<item itemvalue="Application.Peer1" /> |
|
262 |
</list> |
|
263 |
</recent_temporary> |
|
264 |
</component> |
|
265 |
<component name="SvnConfiguration"> |
|
266 |
<configuration /> |
|
267 |
</component> |
|
268 |
<component name="TaskManager"> |
|
269 |
<task active="true" id="Default" summary="Default task"> |
|
270 |
<changelist id="d51d95c7-4d11-4ecd-934b-ad1c8b084f2c" name="Default Changelist" comment="" /> |
|
271 |
<created>1555096072834</created> |
|
272 |
<option name="number" value="Default" /> |
|
273 |
<option name="presentableId" value="Default" /> |
|
274 |
<updated>1555096072834</updated> |
|
275 |
</task> |
|
276 |
<servers /> |
|
277 |
</component> |
|
278 |
<component name="ToolWindowManager"> |
|
279 |
<frame x="1366" y="-2" width="1920" height="1082" extended-state="6" /> |
|
280 |
<editor active="true" /> |
|
281 |
<layout> |
|
282 |
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.1814301" /> |
|
283 |
<window_info id="Structure" order="1" side_tool="true" weight="0.25" /> |
|
284 |
<window_info id="Image Layers" order="2" /> |
|
285 |
<window_info id="Designer" order="3" /> |
|
286 |
<window_info id="UI Designer" order="4" /> |
|
287 |
<window_info id="Capture Tool" order="5" /> |
|
288 |
<window_info id="Favorites" order="6" side_tool="true" /> |
|
289 |
<window_info anchor="bottom" id="Message" order="0" /> |
|
290 |
<window_info anchor="bottom" id="Find" order="1" /> |
|
291 |
<window_info active="true" anchor="bottom" x="74" y="129" width="1204" height="551" id="Run" order="2" type="WINDOWED" visible="true" weight="0.45960125" /> |
|
292 |
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" /> |
|
293 |
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" /> |
|
294 |
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" /> |
|
295 |
<window_info anchor="bottom" id="TODO" order="6" /> |
|
296 |
<window_info anchor="bottom" id="Terminal" order="7" /> |
|
297 |
<window_info anchor="bottom" id="Event Log" order="8" side_tool="true" /> |
|
298 |
<window_info anchor="bottom" id="Version Control" order="9" /> |
|
299 |
<window_info anchor="bottom" id="Messages" order="10" weight="0.32948583" /> |
|
300 |
<window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" /> |
|
301 |
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" /> |
|
302 |
<window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" /> |
|
303 |
<window_info anchor="right" id="Palette" order="3" /> |
|
304 |
<window_info anchor="right" id="Maven" order="4" /> |
|
305 |
<window_info anchor="right" id="Theme Preview" order="5" /> |
|
306 |
<window_info anchor="right" id="Capture Analysis" order="6" /> |
|
307 |
<window_info anchor="right" id="Palette	" order="7" /> |
|
308 |
</layout> |
|
309 |
</component> |
|
310 |
<component name="editorHistoryManager"> |
|
311 |
<entry file="file://$PROJECT_DIR$/TestApp.java"> |
|
312 |
<provider selected="true" editor-type-id="text-editor"> |
|
313 |
<state relative-caret-position="90"> |
|
314 |
<caret line="10" selection-start-line="10" selection-end-line="10" /> |
|
315 |
</state> |
|
316 |
</provider> |
|
317 |
</entry> |
|
318 |
<entry file="file://$PROJECT_DIR$/channels/MdbChannel.java"> |
|
319 |
<provider selected="true" editor-type-id="text-editor"> |
|
320 |
<state relative-caret-position="405"> |
|
321 |
<caret line="35" column="62" selection-start-line="35" selection-start-column="62" selection-end-line="35" selection-end-column="62" /> |
|
322 |
</state> |
|
323 |
</provider> |
|
324 |
</entry> |
|
325 |
<entry file="file://$PROJECT_DIR$/utils/FileController.java"> |
|
326 |
<provider selected="true" editor-type-id="text-editor"> |
|
327 |
<state relative-caret-position="-89"> |
|
328 |
<caret line="38" column="70" selection-start-line="38" selection-start-column="70" selection-end-line="38" selection-end-column="70" /> |
|
329 |
<folding> |
|
330 |
<element signature="method#saveToDisk#0;class#FileController#0" /> |
|
331 |
</folding> |
|
332 |
</state> |
|
333 |
</provider> |
|
334 |
</entry> |
|
335 |
<entry file="file://$PROJECT_DIR$/utils/Chunk.java"> |
|
336 |
<provider selected="true" editor-type-id="text-editor"> |
|
337 |
<state relative-caret-position="376"> |
|
338 |
<caret line="30" column="42" selection-start-line="30" selection-start-column="42" selection-end-line="30" selection-end-column="42" /> |
|
339 |
</state> |
|
340 |
</provider> |
|
341 |
</entry> |
|
342 |
<entry file="file://$PROJECT_DIR$/utils/Utils.java"> |
|
343 |
<provider selected="true" editor-type-id="text-editor"> |
|
344 |
<state relative-caret-position="300"> |
|
345 |
<caret line="26" column="52" selection-start-line="26" selection-start-column="52" selection-end-line="26" selection-end-column="52" /> |
|
346 |
</state> |
|
347 |
</provider> |
|
348 |
</entry> |
|
349 |
<entry file="jrt:///usr/lib/jvm/java-1.11.0-openjdk-amd64!/java.base/java/io/File.class"> |
|
350 |
<provider selected="true" editor-type-id="text-editor" /> |
|
351 |
</entry> |
|
352 |
<entry file="file://$PROJECT_DIR$/protocols/BackupProtocol.java"> |
|
353 |
<provider selected="true" editor-type-id="text-editor"> |
|
354 |
<state relative-caret-position="180"> |
|
355 |
<caret line="12" column="13" selection-start-line="12" selection-start-column="13" selection-end-line="12" selection-end-column="13" /> |
|
356 |
<folding> |
|
357 |
<element signature="imports" expanded="true" /> |
|
358 |
</folding> |
|
359 |
</state> |
|
360 |
</provider> |
|
361 |
</entry> |
|
362 |
<entry file="file://$PROJECT_DIR$/peer/Peer.java"> |
|
363 |
<provider selected="true" editor-type-id="text-editor"> |
|
364 |
<state relative-caret-position="-645"> |
|
365 |
<caret line="37" column="12" selection-start-line="37" selection-start-column="12" selection-end-line="37" selection-end-column="12" /> |
|
366 |
</state> |
|
367 |
</provider> |
|
368 |
</entry> |
|
369 |
<entry file="file://$PROJECT_DIR$/channels/MdrChannel.java"> |
|
370 |
<provider selected="true" editor-type-id="text-editor"> |
|
371 |
<state relative-caret-position="511"> |
|
372 |
<caret line="73" column="13" selection-start-line="73" selection-start-column="13" selection-end-line="73" selection-end-column="13" /> |
|
373 |
</state> |
|
374 |
</provider> |
|
375 |
</entry> |
|
376 |
<entry file="file://$PROJECT_DIR$/channels/McChannel.java"> |
|
377 |
<provider selected="true" editor-type-id="text-editor"> |
|
378 |
<state relative-caret-position="541"> |
|
379 |
<caret line="61" column="27" selection-start-line="61" selection-start-column="27" selection-end-line="61" selection-end-column="27" /> |
|
380 |
</state> |
|
381 |
</provider> |
|
382 |
</entry> |
|
383 |
<entry file="file://$PROJECT_DIR$/protocols/RestoreProtocol.java"> |
|
384 |
<provider selected="true" editor-type-id="text-editor"> |
|
385 |
<state relative-caret-position="195"> |
|
386 |
<caret line="37" column="100" selection-start-line="37" selection-start-column="100" selection-end-line="37" selection-end-column="100" /> |
|
387 |
</state> |
|
388 |
</provider> |
|
389 |
</entry> |
|
390 |
</component> |
|
391 |
<component name="masterDetails"> |
|
392 |
<states> |
|
393 |
<state key="ProjectJDKs.UI"> |
|
394 |
<settings> |
|
395 |
<last-edited>10</last-edited> |
|
396 |
<splitter-proportions> |
|
397 |
<option name="proportions"> |
|
398 |
<list> |
|
399 |
<option value="0.2" /> |
|
400 |
</list> |
|
401 |
</option> |
|
402 |
</splitter-proportions> |
|
403 |
</settings> |
|
404 |
</state> |
|
405 |
</states> |
|
406 |
</component> |
|
407 |
</project> |
src/channels/McChannel.java | ||
---|---|---|
1 |
package channels; |
|
2 |
|
|
3 |
|
|
4 |
import peer.Peer; |
|
5 |
import utils.Chunk; |
|
6 |
import utils.Utils; |
|
7 |
import static utils.Utils.BUFFER_SIZE; |
|
8 |
import static utils.Utils.chunkReceived; |
|
9 |
|
|
10 |
import java.io.File; |
|
11 |
import java.io.FileInputStream; |
|
12 |
import java.io.FileNotFoundException; |
|
13 |
import java.io.IOException; |
|
14 |
import java.net.DatagramPacket; |
|
15 |
import java.util.Arrays; |
|
16 |
import java.util.Random; |
|
17 |
|
|
18 |
public class McChannel implements Runnable{ |
|
19 |
|
|
20 |
|
|
21 |
public void run() { |
|
22 |
while (true) { |
|
23 |
byte[] buf = new byte[BUFFER_SIZE]; |
|
24 |
|
|
25 |
DatagramPacket receiveDatagram = new DatagramPacket(buf, buf.length); |
|
26 |
|
|
27 |
try { |
|
28 |
Utils.mcSocket.receive(receiveDatagram); |
|
29 |
String receivedMessage = new String(buf, 0, buf.length).trim(); |
|
30 |
String[] receivedArgs = receivedMessage.split("\\s+"); |
|
31 |
|
|
32 |
switch (receivedArgs[0]) { |
|
33 |
case "GETCHUNK": |
|
34 |
if (!receivedArgs[2].equals((Integer.toString(Peer.getSenderId())))) { //if not init peer send chunk message to other peers |
|
35 |
Utils.chunkReceived = false; |
|
36 |
sendChunk(receivedArgs); |
|
37 |
} |
|
38 |
break; |
|
39 |
case "STORED": |
|
40 |
//System.out.println("STORED <Version> <SenderId> <FileId> <ChunkNo> <CRLF><CRLF>"); |
|
41 |
break; |
|
42 |
} |
|
43 |
|
|
44 |
} catch (IOException e) { |
|
45 |
System.out.println(e.getMessage()); |
|
46 |
|
|
47 |
} catch (InterruptedException e) { |
|
48 |
e.printStackTrace(); |
|
49 |
} |
|
50 |
} |
|
51 |
} |
|
52 |
|
|
53 |
private void sendChunk(String[] receivedArgs) throws IOException, InterruptedException { |
|
54 |
String chunkyNo = receivedArgs[4]; |
|
55 |
String cleanChunkNo = chunkyNo.substring(0, chunkyNo.length() - 4); |
|
56 |
|
|
57 |
File toCheck = new File("storage/Backup/Peer" + Peer.getSenderId() + "/" + receivedArgs[3] + "/" + cleanChunkNo); |
|
58 |
if(toCheck.exists() && !toCheck.isDirectory()){ //go get the chunk in memory |
|
59 |
FileInputStream stream = new FileInputStream(toCheck); |
|
60 |
byte[] buffer = new byte[64000]; |
|
61 |
int sizeRead = stream.read(buffer,0,64000); |
|
62 |
stream.close(); |
|
63 |
byte[] realBuffer = Arrays.copyOf(buffer,sizeRead); |
|
64 |
|
|
65 |
Chunk toSend = new Chunk(realBuffer,"CHUNK",receivedArgs[3], cleanChunkNo); |
|
66 |
sendTheChunkMdr(toSend); |
|
67 |
} |
|
68 |
} |
|
69 |
private void sendTheChunkMdr(Chunk toSend) throws InterruptedException, IOException { |
|
70 |
int delay = new Random().nextInt(400); // waits before sending chunk message |
|
71 |
Thread.sleep(delay); |
|
72 |
if(!Utils.chunkReceived){ |
|
73 |
System.out.println("McChannel: " + toSend.getHeaderMsg()); |
|
74 |
MdrChannel.sendMessage(toSend.getBuffer()); |
|
75 |
} |
|
76 |
} |
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
} |
src/channels/MdbChannel.java | ||
---|---|---|
1 |
package channels; |
|
2 |
|
|
3 |
import peer.Peer; |
|
4 |
import utils.FileController; |
|
5 |
import utils.Utils; |
|
6 |
|
|
7 |
import java.io.File; |
|
8 |
import java.io.IOException; |
|
9 |
import java.net.DatagramPacket; |
|
10 |
import java.util.ArrayList; |
|
11 |
import java.util.Random; |
|
12 |
|
|
13 |
public class MdbChannel implements Runnable{ |
|
14 |
public void run() { |
|
15 |
while(true){ |
|
16 |
byte[] buf = new byte[65000]; //header + body |
|
17 |
|
|
18 |
DatagramPacket receivedPacket = new DatagramPacket(buf,buf.length); |
|
19 |
|
|
20 |
try { |
|
21 |
//received chunck |
|
22 |
Utils.mdbSocket.receive(receivedPacket); |
|
23 |
String received = new String(receivedPacket.getData(),0,receivedPacket.getLength()); |
|
24 |
String[] receivedArray = received.split("\\r\\n\\r\\n"); //separate header from body |
|
25 |
int offsetOfBody = receivedArray[0].length() + 4; |
|
26 |
|
|
27 |
String receivedChunkData = received.substring(offsetOfBody, received.length()); |
|
28 |
|
|
29 |
int size = received.length() - offsetOfBody; |
|
30 |
byte[] toRet = new byte[size]; |
|
31 |
|
|
32 |
for(int i = offsetOfBody; i < received.length(); i++) { |
|
33 |
toRet[i - offsetOfBody] = receivedPacket.getData()[i]; |
|
34 |
} |
|
35 |
|
|
36 |
FileController receivedFile = new FileController(receivedArray, toRet); |
|
37 |
|
|
38 |
|
|
39 |
//addToChunkDatabase |
|
40 |
if(!Utils.storedChunks.containsKey(receivedFile.getHashedFileId() + ":" + receivedFile.getChunkNo())){ |
|
41 |
Utils.storedChunks.put(receivedFile.getHashedFileId() + ":" + receivedFile.getChunkNo(),new ArrayList<Integer>()); |
|
42 |
Utils.storedChunks.get(receivedFile.getHashedFileId() + ":" + receivedFile.getChunkNo()).add(Integer.parseInt(receivedFile.getReplicationDeg())); |
|
43 |
} |
|
44 |
|
|
45 |
//saveFileIfSentFromAnotherPeer |
|
46 |
if(! (receivedFile.getSenderId() == Peer.getSenderId())) { |
|
47 |
|
|
48 |
//System.out.println("MDB: Received CMD - " + receivedFile.getStringInformation()); |
|
49 |
if(!checkIfFileExists(receivedFile.getHashedFileId() + receivedFile.getChunkNo())) |
|
50 |
receivedFile.saveToDisk(Peer.getSenderId(),receivedFile.getChunkNo()); |
|
51 |
try { |
|
52 |
Thread.sleep(new Random().nextInt(400)); |
|
53 |
} catch (InterruptedException e) { |
|
54 |
e.printStackTrace(); |
|
55 |
} |
|
56 |
sendStoredMessage(receivedFile); |
|
57 |
} |
|
58 |
} |
|
59 |
catch(IOException e){ |
|
60 |
e.getCause(); |
|
61 |
} |
|
62 |
|
|
63 |
|
|
64 |
} |
|
65 |
|
|
66 |
|
|
67 |
} |
|
68 |
|
|
69 |
private void sendStoredMessage(FileController receivedFile) throws IOException { |
|
70 |
String message = ("STORED " + receivedFile.getVersion() + " " + |
|
71 |
Peer.getSenderId() + " " + receivedFile.getHashedFileId() |
|
72 |
+ " " + receivedFile.getChunkNo() + Utils.crlf); |
|
73 |
byte[] buf = new byte[256]; |
|
74 |
buf = message.getBytes(); |
|
75 |
|
|
76 |
DatagramPacket packet = new DatagramPacket(buf,buf.length, Peer.getMcAddress(),Peer.getMcPort()); |
|
77 |
Utils.mcSocket.send(packet); |
|
78 |
} |
|
79 |
|
|
80 |
public static void sendMessage(byte[] buf) throws IOException{ |
|
81 |
DatagramPacket packet = new DatagramPacket(buf,buf.length, Peer.getMdbAddress(),Peer.getMdbPort()); |
|
82 |
Utils.mdbSocket.send(packet); |
|
83 |
} |
|
84 |
|
|
85 |
private boolean checkIfFileExists(String string) { |
|
86 |
File f = new File("src/test/" + Peer.getSenderId() + "/" + string); |
|
87 |
if(f.exists() && !f.isDirectory()) |
|
88 |
return true; |
|
89 |
return false; |
|
90 |
} |
|
91 |
} |
src/channels/MdrChannel.java | ||
---|---|---|
1 |
package channels; |
|
2 |
|
|
3 |
import peer.Peer; |
|
4 |
import utils.FileController; |
|
5 |
import utils.Utils; |
|
6 |
|
|
7 |
import java.io.*; |
|
8 |
import java.net.DatagramPacket; |
|
9 |
|
|
10 |
public class MdrChannel implements Runnable{ |
|
11 |
public void run(){ |
|
12 |
while(true){ |
|
13 |
byte[] buf = new byte[64000 + 1000]; |
|
14 |
DatagramPacket receivePacket = new DatagramPacket(buf, buf.length); |
|
15 |
try{ |
|
16 |
Utils.mdrSocket.receive(receivePacket); |
|
17 |
String received = new String(receivePacket.getData(),0,receivePacket.getLength()); |
|
18 |
|
|
19 |
String[] receivedArray = received.split("\\r\\n\\r\\n"); |
|
20 |
int offsetOfBody = receivedArray[0].length() + 4; |
|
21 |
byte[] bodyByteArray = getArrayFromOffset(receivePacket.getData(), offsetOfBody, receivePacket.getLength()); |
|
22 |
|
|
23 |
FileController receivedFile = new FileController(receivedArray, bodyByteArray); |
|
24 |
|
|
25 |
if(Peer.getSenderId() == Utils.restorePeer){ //save to init peer |
|
26 |
saveFile(receivedFile); |
|
27 |
} |
|
28 |
} catch (IOException e) { |
|
29 |
e.printStackTrace(); |
|
30 |
} |
|
31 |
} |
|
32 |
} |
|
33 |
|
|
34 |
private byte[] getArrayFromOffset(byte[] data, int offsetOfBody, int length) { |
|
35 |
int size = length - offsetOfBody; |
|
36 |
byte[] toRet = new byte[size]; |
|
37 |
|
|
38 |
for(int i = offsetOfBody; i < length; i++) { |
|
39 |
toRet[i - offsetOfBody] = data[i]; |
|
40 |
} |
|
41 |
|
|
42 |
return toRet; |
|
43 |
} |
|
44 |
|
|
45 |
private void saveFile(FileController receivedFile) throws IOException { |
|
46 |
|
|
47 |
File theDir = new File("storage/Restored"); |
|
48 |
|
|
49 |
// if the directory does not exist, create it |
|
50 |
if (!theDir.exists()) { |
|
51 |
boolean result = false; |
|
52 |
|
|
53 |
try{ |
|
54 |
theDir.mkdir(); |
|
55 |
result = true; |
|
56 |
} |
|
57 |
catch(SecurityException se){ |
|
58 |
} |
|
59 |
if(result) { |
|
60 |
} |
|
61 |
} |
|
62 |
|
|
63 |
File dir = new File("storage/Restored/Peer" + Peer.getSenderId()); |
|
64 |
|
|
65 |
// if the directory does not exist, create it |
|
66 |
if (!dir.exists()) { |
|
67 |
boolean result = false; |
|
68 |
|
|
69 |
try{ |
|
70 |
dir.mkdir(); |
|
71 |
result = true; |
|
72 |
} |
|
73 |
catch(SecurityException se){ |
|
74 |
} |
|
75 |
if(result) { |
|
76 |
} |
|
77 |
} |
|
78 |
|
|
79 |
File f = new File("storage/Restored/Peer" + Peer.getSenderId() + "/" + receivedFile.getChunkNo()); |
|
80 |
|
|
81 |
if(!f.exists()) { //if file is not saved yet by other peer... |
|
82 |
f.createNewFile(); |
|
83 |
} |
|
84 |
OutputStream out = new FileOutputStream(f); |
|
85 |
out.write(receivedFile.getBody(), 0, receivedFile.getBody().length); |
|
86 |
out.close(); |
|
87 |
Utils.chunkReceived = true; |
|
88 |
} |
|
89 |
|
|
90 |
public static void sendMessage(byte[] buf) throws IOException { |
|
91 |
DatagramPacket packet = new DatagramPacket(buf, buf.length, Peer.getMdrAddress(), Peer.getMdrPort()); |
|
92 |
Utils.mdrSocket.send(packet); |
|
93 |
} |
|
94 |
} |
src/out/production/SIDS/.idea/encodings.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<project version="4"> |
|
3 |
<component name="Encoding" addBOMForNewFiles="with NO BOM" /> |
|
4 |
</project> |
src/out/production/SIDS/.idea/misc.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<project version="4"> |
|
3 |
<component name="ProjectRootManager" version="2" languageLevel="JDK_10" project-jdk-name="10" project-jdk-type="JavaSDK"> |
|
4 |
<output url="file://$PROJECT_DIR$/out" /> |
|
5 |
</component> |
|
6 |
</project> |
src/protocols/RestoreProtocol.java | ||
---|---|---|
1 |
package protocols; |
|
2 |
|
|
3 |
|
|
4 |
import peer.Peer; |
|
5 |
import utils.FileController; |
|
6 |
import utils.Utils; |
|
7 |
|
|
8 |
import java.io.*; |
|
9 |
import java.net.DatagramPacket; |
|
10 |
import java.util.ArrayList; |
|
11 |
|
|
12 |
public class RestoreProtocol { |
|
13 |
String hashId; |
|
14 |
String name; |
|
15 |
int numberOfChunks; |
|
16 |
|
|
17 |
public RestoreProtocol(String[] args) throws IOException, InterruptedException { |
|
18 |
this.name = args[1]; |
|
19 |
this.numberOfChunks = 0; |
|
20 |
|
|
21 |
//check in hash database |
|
22 |
this.hashId = Utils.hashDatabase.get(this.name); |
|
23 |
|
|
24 |
if(this.hashId == null) { |
|
25 |
System.out.println("no such file exists to restore, exiting ..."); |
|
26 |
return; |
|
27 |
} |
|
28 |
|
|
29 |
getNumberOfChunks(); |
|
30 |
|
|
31 |
System.out.println("RESTORE Protocol || Restoring: " + this.name + " || IdHashed: " + this.hashId); |
|
32 |
|
|
33 |
Utils.restorePeer = Peer.getSenderId(); |
|
34 |
|
|
35 |
//send get chunck message to MC channel |
|
36 |
sendGetChunk(0); |
|
37 |
|
|
38 |
Thread.sleep(10000); //waits a bit for chunks to be stored in the folder before merging them |
|
39 |
System.out.println("All chunks stored, merging into one single file"); |
|
40 |
mergeChunks(); |
|
41 |
} |
|
42 |
|
|
43 |
private void getNumberOfChunks() { |
|
44 |
for (String name: Utils.storedChunks.keySet()){ |
|
45 |
String [] key = name.split(":"); |
|
46 |
if(this.hashId.equals(key[0])) this.numberOfChunks++; |
|
47 |
} |
|
48 |
} |
|
49 |
|
|
50 |
private void sendGetChunk(int chunkNo) throws IOException { |
|
51 |
|
|
52 |
if(chunkNo > this.numberOfChunks -1) return; |
|
53 |
|
|
54 |
System.out.println("Sending getchunk message to mc..."); |
|
55 |
String msg = "GETCHUNK" + " " + Utils.version + " " + Peer.getSenderId() + " " + this.hashId + " " + chunkNo + Utils.crlf; |
|
56 |
|
|
57 |
DatagramPacket packet = new DatagramPacket(msg.getBytes(), msg.getBytes().length, Peer.getMcAddress(),Peer.getMcPort()); |
|
58 |
|
|
59 |
Utils.mcSocket.send(packet); |
|
60 |
chunkNo += 1; |
|
61 |
sendGetChunk(chunkNo); |
|
62 |
} |
|
63 |
|
|
64 |
private void mergeChunks() throws IOException { |
|
65 |
File outputFile = new File("storage/Restored/Peer" + Peer.getSenderId() + "/" + this.name); |
|
66 |
outputFile.getParentFile().mkdirs(); |
|
67 |
outputFile.createNewFile(); |
|
68 |
|
|
69 |
FileOutputStream outStream = new FileOutputStream(outputFile); |
|
70 |
|
|
71 |
for(int i = 0; i < this.numberOfChunks; i++) { |
|
72 |
File inputFile = new File("storage/Restored/Peer" + Peer.getSenderId() + "/" + i); |
|
73 |
FileInputStream inStream = new FileInputStream(inputFile); |
|
74 |
byte[] buf = new byte[64000]; |
|
75 |
int readBytes = inStream.read(buf); |
|
76 |
outStream.write(buf, 0, readBytes); |
|
77 |
|
|
78 |
inStream.close(); |
|
79 |
|
|
80 |
} |
|
81 |
|
|
82 |
for(int i = 0; i < this.numberOfChunks; i++) { //delete chunks (we dont need them anymore) |
|
83 |
File inputFile = new File("storage/Restored/Peer" + Peer.getSenderId() + "/" + i); |
|
84 |
inputFile.delete(); |
|
85 |
} |
|
86 |
|
|
87 |
outStream.close(); |
|
88 |
} |
|
89 |
} |
src/out/production/SIDS/.idea/modules.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<project version="4"> |
|
3 |
<component name="ProjectModuleManager"> |
|
4 |
<modules> |
|
5 |
<module fileurl="file://$PROJECT_DIR$/SIDS.iml" filepath="$PROJECT_DIR$/SIDS.iml" /> |
|
6 |
</modules> |
|
7 |
</component> |
|
8 |
</project> |
src/out/production/SIDS/.idea/vcs.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<project version="4"> |
|
3 |
<component name="VcsDirectoryMappings"> |
|
4 |
<mapping directory="$PROJECT_DIR$/.." vcs="Git" /> |
|
5 |
</component> |
|
6 |
</project> |
src/test/teste.txt | ||
---|---|---|
1 |
Este é um ficheiro de teste. |
|
2 |
DFDJFDSF |
|
3 |
DFSDFDSFDS |
|
4 |
DFDSFSFFD |
|
5 |
DFSDFSDFSDFDS |
|
6 |
DFSFSDFSFSDFSFDSF |
|
7 |
FDSFSDFDSFSDFDSFDSFSDF |
|
8 |
...... |
|
9 |
-------- |
|
10 |
´´´´´´´´´ |
|
11 |
~~~~~~~~~~1323658769 |
|
12 |
0xD 0xA |
|
13 |
|
src/out/production/SIDS/.idea/workspace.xml | ||
---|---|---|
1 |
<?xml version="1.0" encoding="UTF-8"?> |
|
2 |
<project version="4"> |
|
3 |
<component name="ChangeListManager"> |
|
4 |
<list default="true" id="d51d95c7-4d11-4ecd-934b-ad1c8b084f2c" name="Default Changelist" comment=""> |
|
5 |
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" /> |
|
6 |
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> |
|
7 |
<change beforePath="$PROJECT_DIR$/channels/McChannel.java" beforeDir="false" afterPath="$PROJECT_DIR$/channels/McChannel.java" afterDir="false" /> |
|
8 |
<change beforePath="$PROJECT_DIR$/channels/MdbChannel.java" beforeDir="false" afterPath="$PROJECT_DIR$/channels/MdbChannel.java" afterDir="false" /> |
|
9 |
<change beforePath="$PROJECT_DIR$/channels/MdrChannel.java" beforeDir="false" afterPath="$PROJECT_DIR$/channels/MdrChannel.java" afterDir="false" /> |
|
10 |
<change beforePath="$PROJECT_DIR$/out/production/SIDS/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/SIDS/.idea/misc.xml" afterDir="false" /> |
|
11 |
<change beforePath="$PROJECT_DIR$/out/production/SIDS/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/SIDS/.idea/workspace.xml" afterDir="false" /> |
|
12 |
<change beforePath="$PROJECT_DIR$/out/production/SIDS/TestApp.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/SIDS/TestApp.class" afterDir="false" /> |
|
13 |
<change beforePath="$PROJECT_DIR$/out/production/SIDS/channels/McChannel.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/SIDS/channels/McChannel.class" afterDir="false" /> |
|
14 |
<change beforePath="$PROJECT_DIR$/out/production/SIDS/channels/MdbChannel.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/SIDS/channels/MdbChannel.class" afterDir="false" /> |
|
15 |
<change beforePath="$PROJECT_DIR$/out/production/SIDS/channels/MdrChannel.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/SIDS/channels/MdrChannel.class" afterDir="false" /> |
|
16 |
<change beforePath="$PROJECT_DIR$/out/production/SIDS/peer/Peer.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/SIDS/peer/Peer.class" afterDir="false" /> |
|
17 |
<change beforePath="$PROJECT_DIR$/out/production/SIDS/protocols/BackupProtocol.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/SIDS/protocols/BackupProtocol.class" afterDir="false" /> |
|
18 |
<change beforePath="$PROJECT_DIR$/out/production/SIDS/protocols/RestoreProtocol.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/SIDS/protocols/RestoreProtocol.class" afterDir="false" /> |
|
19 |
<change beforePath="$PROJECT_DIR$/out/production/SIDS/utils/Chunk.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/SIDS/utils/Chunk.class" afterDir="false" /> |
|
20 |
<change beforePath="$PROJECT_DIR$/out/production/SIDS/utils/FileController.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/SIDS/utils/FileController.class" afterDir="false" /> |
|
21 |
<change beforePath="$PROJECT_DIR$/out/production/SIDS/utils/Utils.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/SIDS/utils/Utils.class" afterDir="false" /> |
|
22 |
<change beforePath="$PROJECT_DIR$/protocols/BackupProtocol.java" beforeDir="false" afterPath="$PROJECT_DIR$/protocols/BackupProtocol.java" afterDir="false" /> |
|
23 |
<change beforePath="$PROJECT_DIR$/protocols/RestoreProtocol.java" beforeDir="false" afterPath="$PROJECT_DIR$/protocols/RestoreProtocol.java" afterDir="false" /> |
|
24 |
<change beforePath="$PROJECT_DIR$/storage/Backup/Peer11/35c42a2c5b603a69a045174897a3b25a54920b06b0f2ecf24610f4cc0379a5c4/0" beforeDir="false" /> |
|
25 |
<change beforePath="$PROJECT_DIR$/storage/Backup/Peer11/35c42a2c5b603a69a045174897a3b25a54920b06b0f2ecf24610f4cc0379a5c4/1" beforeDir="false" /> |
|
26 |
<change beforePath="$PROJECT_DIR$/utils/Chunk.java" beforeDir="false" afterPath="$PROJECT_DIR$/utils/Chunk.java" afterDir="false" /> |
|
27 |
<change beforePath="$PROJECT_DIR$/utils/FileController.java" beforeDir="false" afterPath="$PROJECT_DIR$/utils/FileController.java" afterDir="false" /> |
|
28 |
<change beforePath="$PROJECT_DIR$/utils/Utils.java" beforeDir="false" afterPath="$PROJECT_DIR$/utils/Utils.java" afterDir="false" /> |
|
29 |
</list> |
|
30 |
<ignored path="$PROJECT_DIR$/out/" /> |
|
31 |
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" /> |
|
32 |
<option name="SHOW_DIALOG" value="false" /> |
|
33 |
<option name="HIGHLIGHT_CONFLICTS" value="true" /> |
|
34 |
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" /> |
|
35 |
<option name="LAST_RESOLUTION" value="IGNORE" /> |
|
36 |
</component> |
|
37 |
<component name="FileEditorManager"> |
|
38 |
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300"> |
|
39 |
<file pinned="false" current-in-tab="false"> |
|
40 |
<entry file="file://$PROJECT_DIR$/protocols/RestoreProtocol.java"> |
|
41 |
<provider selected="true" editor-type-id="text-editor"> |
|
42 |
<state relative-caret-position="151"> |
|
43 |
<caret line="79" column="61" selection-start-line="79" selection-start-column="61" selection-end-line="79" selection-end-column="61" /> |
|
44 |
</state> |
|
45 |
</provider> |
|
46 |
</entry> |
|
47 |
</file> |
|
48 |
<file pinned="false" current-in-tab="false"> |
|
49 |
<entry file="file://$PROJECT_DIR$/channels/McChannel.java"> |
|
50 |
<provider selected="true" editor-type-id="text-editor"> |
|
51 |
<state relative-caret-position="601"> |
|
52 |
<caret line="68" column="36" selection-start-line="68" selection-start-column="36" selection-end-line="68" selection-end-column="36" /> |
|
53 |
</state> |
|
54 |
</provider> |
|
55 |
</entry> |
|
56 |
</file> |
|
57 |
<file pinned="false" current-in-tab="true"> |
|
58 |
<entry file="file://$PROJECT_DIR$/utils/Chunk.java"> |
|
59 |
<provider selected="true" editor-type-id="text-editor"> |
|
60 |
<state relative-caret-position="361"> |
|
61 |
<caret line="29" column="141" selection-start-line="29" selection-start-column="141" selection-end-line="29" selection-end-column="141" /> |
|
62 |
</state> |
|
63 |
</provider> |
|
64 |
</entry> |
|
65 |
</file> |
|
66 |
<file pinned="false" current-in-tab="false"> |
|
67 |
<entry file="file://$PROJECT_DIR$/channels/MdrChannel.java"> |
|
68 |
<provider selected="true" editor-type-id="text-editor"> |
|
69 |
<state relative-caret-position="240"> |
|
70 |
<caret line="21" column="63" selection-start-line="21" selection-start-column="63" selection-end-line="21" selection-end-column="63" /> |
|
71 |
</state> |
|
72 |
</provider> |
|
73 |
</entry> |
|
74 |
</file> |
|
75 |
<file pinned="false" current-in-tab="false"> |
|
76 |
<entry file="file://$PROJECT_DIR$/utils/Utils.java"> |
|
77 |
<provider selected="true" editor-type-id="text-editor"> |
|
78 |
<state relative-caret-position="330"> |
|
79 |
<caret line="28" column="85" selection-start-line="28" selection-start-column="85" selection-end-line="28" selection-end-column="85" /> |
|
80 |
</state> |
|
81 |
</provider> |
|
82 |
</entry> |
|
83 |
</file> |
|
84 |
<file pinned="false" current-in-tab="false"> |
|
85 |
<entry file="file://$PROJECT_DIR$/peer/Peer.java"> |
|
86 |
<provider selected="true" editor-type-id="text-editor"> |
|
87 |
<state relative-caret-position="60"> |
|
88 |
<caret line="15" column="13" selection-start-line="15" selection-start-column="13" selection-end-line="15" selection-end-column="13" /> |
|
89 |
</state> |
|
90 |
</provider> |
|
91 |
</entry> |
|
92 |
</file> |
|
93 |
<file pinned="false" current-in-tab="false"> |
|
94 |
<entry file="file://$PROJECT_DIR$/TestApp.java"> |
|
95 |
<provider selected="true" editor-type-id="text-editor"> |
|
96 |
<state relative-caret-position="90"> |
|
97 |
<caret line="10" selection-start-line="10" selection-end-line="10" /> |
|
98 |
</state> |
|
99 |
</provider> |
Also available in: Unified diff