-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStartClient.java
More file actions
149 lines (131 loc) · 4.89 KB
/
StartClient.java
File metadata and controls
149 lines (131 loc) · 4.89 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import FS.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import java.io.*;
import java.util.*;
public class StartClient {
Scanner input = new Scanner(System.in);
/*
* Main
*/
public static void main(String[] args) {
StartClient c = new StartClient();
try{
ORB orb = ORB.init(args, null);
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
FileSystem fileSystem = (FileSystem) FileSystemHelper.narrow(ncRef.resolve_str("FileSystem"));
c.menu(fileSystem);
}
catch(Exception e){
System.out.println("Error on client : " + e.toString());
e.printStackTrace();
}
}
public void menu(FileSystem fileSystem) {
//** MENU **//
String opt = "";
while(!opt.contains("QUIT")){
System.out.print("••••••••••••••••••\n");
System.out.print("• OPTIONS: \n");
System.out.print("•\n");
System.out.print("• create \n");
System.out.print("• delete \n");
System.out.print("• copy \n");
System.out.print("• write \n");
System.out.print("• read \n");
System.out.print("•\n");
System.out.print("• >>> ");
opt = input.nextLine();
switch(opt){
case "create":
this.create(fileSystem);
break;
case "delete":
this.delete(fileSystem);
break;
case "copy":
this.copy(fileSystem);
break;
case "write":
this.write(fileSystem);
break;
case "read":
this.read(fileSystem);
break;
case "EXIT":
input.close();
System.exit(0);
default:
System.out.println("This command doesnt exist. Try Again.");
break;
}
}
}
public void create(FileSystem fileSystem) {
System.out.print("\n - Enter the file name: ");
int result = fileSystem.create(input.nextLine());
if(result == 0)
System.out.println("\n • File created successfully \n");
else if(result == 1)
System.out.println("\n • File already exist \n");
else if(result == 3)
System.out.println("\n • Error creating file \n");
}
public void delete(FileSystem fileSystem) {
System.out.print("\n - Enter the file name: ");
int result = fileSystem.delete(input.nextLine());
if(result == 0)
System.out.println("\n • File deleted successfully \n");
else if(result == 4)
System.out.println("\n • Failed to delete the file \n");
else if (result == 2)
System.out.println("\n • File doesn't exist \n");
}
public void copy(FileSystem fileSystem) {
System.out.print("\n - Enter the fileName and newFileName, separated by ',' : ");
String textInput = input.nextLine();
String[] data = textInput.split(",");
if (data.length < 2) {
System.out.print("\n • ERRO - You must enter the fileName and the newFileName, separated by ','\n\n ");
return;
}
int result = fileSystem.copy(data[0], data[1]);
if(result == 0)
System.out.println("\n • File copied successfully \n");
else if(result == 2)
System.out.println("\n • File doesn't exist \n");
else if (result == 4)
System.out.println("\n • Failed to copy the file \n");
else if (result == 5)
System.out.println("\n • Already exist in file with the copy suggested name \n");
}
public void write(FileSystem fileSystem) {
System.out.print("\n - Enter the fileName and the text, separated by ',' : ");
String textInput = input.nextLine();
String[] data = textInput.split(",");
if (data.length < 2) {
System.out.print("\n • ERRO - You must enter the fileName and the text, separated by ','\n\n ");
return;
}
int result = fileSystem.write(data[0], data[1]);
if(result == 0)
System.out.println("\n • File written successfully \n");
else if(result == 2)
System.out.println("\n • File doesn't exist \n");
else if (result == 3)
System.out.println("\n • Failed to write at the file \n");
}
public void read(FileSystem fileSystem) {
System.out.print("\n - Enter the file name: ");
org.omg.CORBA.StringHolder readText = new org.omg.CORBA.StringHolder();
int result = fileSystem.read(input.nextLine(), readText);
if(result == 0){
System.out.println("\n • Text: "+ readText.value +" \n");
} else if(result == 2)
System.out.println("\n • File doesn't exist \n");
else if (result == 6)
System.out.println("\n • Error trying to read the file \n");
}
}