Skip to content

Commit d91db09

Browse files
author
quaa
committed
Added client class
1 parent d0398f4 commit d91db09

File tree

2 files changed

+80
-60
lines changed

2 files changed

+80
-60
lines changed

src/Client.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import java.io.ObjectOutputStream;
2+
import java.net.Socket;
3+
4+
5+
public class Client {
6+
private String dirName;
7+
private String serverIP;
8+
private int PORT_NUMBER;
9+
10+
public Client(String dirName, String serverIP, int port) {
11+
this.dirName = dirName;
12+
this.serverIP = serverIP;
13+
PORT_NUMBER = port;
14+
15+
System.out.println("Client Selected!");
16+
System.out.println("Dir to sync: " + dirName);
17+
System.out.println("Server IP: " + serverIP);
18+
}
19+
20+
public void runClient() throws Exception {
21+
String localDirName = dirName; //cleaning up the users input
22+
if(dirName.contains("/")){
23+
if(dirName.lastIndexOf("/") != (dirName.length() - 1)) {
24+
localDirName = dirName.substring(dirName.lastIndexOf("/"));
25+
} else {
26+
localDirName = dirName.substring(0, (dirName.length() - 1));
27+
if(localDirName.contains("/"))
28+
localDirName = localDirName.substring(localDirName.lastIndexOf("/"));
29+
}
30+
}
31+
32+
if(localDirName.equals(".")){
33+
System.out.println("Please input a dir name instead of ./ or .");
34+
Thread.sleep(10);
35+
System.exit(0);
36+
}
37+
38+
if(!localDirName.startsWith("./")){ //still cleaning up their input
39+
if(localDirName.startsWith("/"))
40+
localDirName = "." + localDirName;
41+
else
42+
localDirName = "./" + localDirName;
43+
}
44+
45+
Socket sock = new Socket(serverIP, PORT_NUMBER);
46+
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
47+
oos.writeObject(localDirName);
48+
oos.flush();
49+
50+
/* //check to see if file exists on server
51+
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
52+
String fExists = (String) ois.readObject();
53+
54+
if(fExists.equals("1")){
55+
System.out.print("Receiving file: ");
56+
byte[] mybytearray = new byte[1024];
57+
InputStream is = sock.getInputStream();
58+
FileOutputStream fos = new FileOutputStream(fileName);
59+
BufferedOutputStream bos = new BufferedOutputStream(fos);
60+
int bytesRead = 0;
61+
int current = 0;
62+
while((bytesRead = is.read(mybytearray, 0, mybytearray.length)) != -1){
63+
bos.write(mybytearray, 0, bytesRead);
64+
current = current + bytesRead;
65+
System.out.print(".");
66+
}
67+
System.out.println();
68+
System.out.println("Done!");
69+
oos.close();
70+
ois.close();
71+
bos.close();
72+
}else{
73+
System.out.println("Server replied that " + fileName + " does not exist, closing connection.");
74+
}
75+
*/ oos.close();
76+
sock.close();
77+
}
78+
}

src/javaFileSync.java

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -66,66 +66,8 @@ public static void server() throws Exception {
6666
}
6767

6868
public static void client(String dirName, String serverIP) throws Exception {
69-
System.out.println("Client Selected!");
70-
System.out.println("Dir to sync: " + dirName);
71-
System.out.println("Server IP: " + serverIP);
72-
73-
String localDirName = dirName; //cleaning up the users input
74-
if(dirName.contains("/")){
75-
if(dirName.lastIndexOf("/") != (dirName.length() - 1)) {
76-
localDirName = dirName.substring(dirName.lastIndexOf("/"));
77-
} else {
78-
localDirName = dirName.substring(0, (dirName.length() - 1));
79-
if(localDirName.contains("/"))
80-
localDirName = localDirName.substring(localDirName.lastIndexOf("/"));
81-
}
82-
}
83-
84-
if(localDirName.equals(".")){
85-
System.out.println("Please input a dir name instead of ./ or .");
86-
Thread.sleep(10);
87-
System.exit(0);
88-
}
89-
90-
if(!localDirName.startsWith("./")){ //still cleaning up their input
91-
if(localDirName.startsWith("/"))
92-
localDirName = "." + localDirName;
93-
else
94-
localDirName = "./" + localDirName;
95-
}
96-
97-
Socket sock = new Socket(serverIP, PORT_NUMBER);
98-
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
99-
oos.writeObject(localDirName);
100-
oos.flush();
101-
102-
/* //check to see if file exists on server
103-
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
104-
String fExists = (String) ois.readObject();
105-
106-
if(fExists.equals("1")){
107-
System.out.print("Receiving file: ");
108-
byte[] mybytearray = new byte[1024];
109-
InputStream is = sock.getInputStream();
110-
FileOutputStream fos = new FileOutputStream(fileName);
111-
BufferedOutputStream bos = new BufferedOutputStream(fos);
112-
int bytesRead = 0;
113-
int current = 0;
114-
while((bytesRead = is.read(mybytearray, 0, mybytearray.length)) != -1){
115-
bos.write(mybytearray, 0, bytesRead);
116-
current = current + bytesRead;
117-
System.out.print(".");
118-
}
119-
System.out.println();
120-
System.out.println("Done!");
121-
oos.close();
122-
ois.close();
123-
bos.close();
124-
}else{
125-
System.out.println("Server replied that " + fileName + " does not exist, closing connection.");
126-
}
127-
*/ oos.close();
128-
sock.close();
69+
Client c = new Client(dirName, serverIP, PORT_NUMBER);
70+
c.runClient();
12971
}
13072

13173
// Process all files and directories under dir

0 commit comments

Comments
 (0)