-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPut.java
More file actions
98 lines (81 loc) · 2.77 KB
/
Put.java
File metadata and controls
98 lines (81 loc) · 2.77 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.rmi.registry.*;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.io.File;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.Inet4Address;
import java.lang.Exception;
public class Put {
private static ServerSocket ssock ;
private static Socket socket;
private Registry namingserver; ////
private Service service_stub;
private String IP_NAMING_SERVER;
private int PORT_NAMING_SERVER;
public Put(String IP , String PORT, int Server_Port ) throws Exception {
this.IP_NAMING_SERVER = IP ;
this.PORT_NAMING_SERVER = Integer.parseInt(PORT);
ssock = new ServerSocket(Server_Port);
Registry namingserver = LocateRegistry.getRegistry(IP_NAMING_SERVER, PORT_NAMING_SERVER);
service_stub = (Service)namingserver.lookup("NamingServer");
}
private void transfer(String args[] ) throws Exception {
//if ( service_stub.put(args[4], args[3] , args[0]) == false ){ ///write file
// System.out.println("File exist ");
// System.exit(1);
//}
String path = args[0];
new Thread (new Runnable (){
public void run(){
System.out.println(" listening ...");
try{
Socket socket = ssock.accept();
String anim = "|/-\\";
File file = new File(path);
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
OutputStream os = socket.getOutputStream();
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);
int x = (int)((current * 100)/fileLength) ;
String data = "\r" + anim.charAt(x % anim.length()) + " " + x + "%" + "Sent" ;
System.out.write(data.getBytes());
}
os.flush();
System.out.println("File sent succesfully!");
}catch(Exception e){ e.printStackTrace();}
}
}).start();
service_stub.put(args[4], args[3] , args[0]);
//run(args[0]);
}
public static void main(String[] args) throws Exception {
if (args.length < 5){
System.err.println( "Bad usage " + "file | IP address of naming server | port of naming server | port for tcp |its own ip "); //contact naming serv to put file
System.exit(1);
}
Put object = new Put(args[1], args[2], Integer.parseInt(args[3])); //2nd arg ip of naming server
object.transfer(args);
}
}