-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet.java
More file actions
67 lines (58 loc) · 2.34 KB
/
Get.java
File metadata and controls
67 lines (58 loc) · 2.34 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
/* Gets File From the Storage server hosting file ... and saves that file as the same name in current directory
*/
import java.rmi.*;
import java.rmi.RemoteException;
import java.rmi.Naming;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.registry.LocateRegistry;
import java.rmi.NotBoundException;
import java.rmi.registry.Registry;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.List;
public class Get extends NamingServer {
private Registry namingserver;
private Service service_stub;
private String IP_NAMING_SERVER;
private int PORT_NAMING_SERVER;
public Get(String IP , String PORT ) throws RemoteException , NotBoundException {
this.IP_NAMING_SERVER = IP ;
this.PORT_NAMING_SERVER = Integer.parseInt(PORT);
Registry namingserver = LocateRegistry.getRegistry(IP_NAMING_SERVER, PORT_NAMING_SERVER);
service_stub = (Service)namingserver.lookup("NamingServer");
}
public void run (String path) throws UnknownHostException, IOException
{
List <String> fileServer = service_stub.getStorage(path); // get storge server hosting "path" file
System.out.println(fileServer);
String addr = new String (fileServer.get(0)); // ip o
int port = Integer.parseInt(fileServer.get(1));// Tcp port listening on storageserver
Socket socket = new Socket(InetAddress.getByName(addr), port);// crate socket
byte[] contents = new byte[10000];
FileOutputStream fos = new FileOutputStream(path);
BufferedOutputStream bos = new BufferedOutputStream(fos);
InputStream is = socket.getInputStream();
int bytesRead = 0;
while((bytesRead=is.read(contents))!=-1)
bos.write(contents, 0, bytesRead);
bos.flush();
socket.close();
System.out.println("File saved successfully!");
}
public static void main (String args[] )throws RemoteException , NotBoundException , UnknownHostException , IOException{
//new Get().run(args[0]);
if (args.length < 3){
System.err.println( "Bad usage " + "file IP address of naming server port of naming server ");
System.exit(1);
}
Get object = new Get(args[1], args[2]); //2nd arg ip of naming server
object.run(args[0] ); // 1st arg file
}
}