-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMasterNodeFileTransfer_sending.java
More file actions
81 lines (70 loc) · 2.72 KB
/
Copy pathMasterNodeFileTransfer_sending.java
File metadata and controls
81 lines (70 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* Created and edited by: Sahil Pethe (ssp5329@g.rit.edu)
* Sukraat Ahluwalia (sxa4430@g.rit.edu)
*/
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.DigestInputStream;
import java.security.MessageDigest;
public class MasterNodeFileTransfer_sending extends MasterNode implements Runnable{
String fileName;
InetAddress ipClientPC;
int defaultPort;
MasterNodeFileTransfer_sending(String fName, InetAddress ip)
{
// we input the file name and ip address from the constructor
fileName=fName;
ipClientPC=ip;
}
@Override
public void run(){
try {
// the below code is used to transfer files from master node to the chunk server
//Initialize Sockets
Socket socket = new Socket(ipClientPC, 5000);
// initializing the data output stream
DataOutputStream dout = new DataOutputStream(socket.getOutputStream());
dout.writeUTF(fileName);
//Specify the file
File file = new File(Paths.get("").toAbsolutePath().toString()+"/files_to_transfer/"+fileName);
//File file = new File("files_to_transfer\\"+fileName);
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
//Get socket's output stream
OutputStream os = socket.getOutputStream();
//Read File Contents into contents array
byte[] contents;
long fileLength = file.length();
long current = 0;
long start = System.nanoTime();
while (current != fileLength) {
int size = 10000;
if (fileLength - current >= size)
current += size;
else {
size = (int) (fileLength - current);
current = fileLength;
}
contents = new byte[size];
bis.read(contents, 0, size);
os.write(contents);
}
os.flush();
// closing the socket connection
socket.close();
// we delete the files from the master node after the transfer is completed
file.delete();
// we remove the active file transfers filename from the arraylist
activeFileTransfers.remove(fileName);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}