-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
318 lines (211 loc) · 8.42 KB
/
Client.java
File metadata and controls
318 lines (211 loc) · 8.42 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Implementar um cliente que possa criar, excluir, ler e escrever um arquivo no serviço de arquivos
import java.io.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Scanner;
import spread.*;
public class Client implements BasicMessageListener {
static Thread escutadorDeMensagens;
public static String fileName;
// The Spread Connection.
private static SpreadConnection connection;
private static String userName;
boolean aguardando;
public Client(String user, String address, int port) {
userName = user;
//startSpreadConnection(userName, address, port);
int opcao;
Scanner entrada = new Scanner(System.in);
escutadorDeMensagens.start();
do{
menu();
opcao = entrada.nextInt();
switch(opcao){
case 1:
System.out.print("Digite o nome do arquivo que deseja criar: ");
readFileName();
System.out.print("Digite a quantidade de réplicas que deseja criar: ");
String numReplicas = "";
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
numReplicas = in.readLine();
} catch (IOException exception) {
exception.printStackTrace();
}
// Monta a mensagem de criação de arquivo
String info = new String();
info = "C#" + fileName + "#" + numReplicas;
sendMessage(info, "SERVER_GROUP");
System.out.println("Enviando mensagem <" + info + "> para o SERVER_GROUP para a criação do arquivo " + fileName);
// Aguarda a mensagem de resposta do servidor
// aguardaResposta();
break;
case 2:
System.out.println("Chamada do método para escrever em um arquivo");
//escrever arquivo
System.out.print("Digite o nome do arquivo que deseja editar: ");
readFileName();
System.out.print("Escreva o texto do arquivo e aperte enter\n>");
String texto = "";
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
texto = in.readLine();
} catch (IOException exception) {
exception.printStackTrace();
}
// Monta a mensagem de edição de arquivo
info = "W#" + fileName + "#" + texto;
sendMessage(info, "SERVER_GROUP");
System.out.println("Enviando mensagem <" + info + "> para o SERVER_GROUP para a edição do arquivo " + fileName);
// Aguarda a mensagem de resposta do servidor
// aguardaResposta();
break;
case 3:
System.out.println("Chamada do método para ler um arquivo");
System.out.print("Digite o nome do arquivo que deseja ler do Serviço de Arquivos: ");
readFileName();
// Monta a mensagem de leitura do arquivo
info = new String();
info = "L#" + fileName;
sendMessage(info, "SERVER_GROUP");
System.out.println("Enviando mensagem <" + info + "> para o SERVER_GROUP para a leitura do arquivo " + fileName);
//aguardaResposta();
break;
case 4:
System.out.println("Chamada do método para excluir um arquivo");
System.out.print("Digite o nome do arquivo que deseja excluir : ");
readFileName();
// Monta a mensagem de criação de arquivo
info = new String();
info = "D#" + fileName;
sendMessage(info, "SERVER_GROUP");
System.out.println("Enviando mensagem <" + info + "> para o SERVER_GROUP para a deleção do arquivo " + fileName);
// Aguarda a mensagem de resposta do servidor
//aguardaResposta();
break;
case 9:
System.out.println("Saindo");
//consulta();
break;
default:
System.out.println("Opção inválida.");
}
} while(opcao != 9);
}
public void menu(){
System.out.println("\n\tCliente do Serviço de Arquivos DistribuÃdo\n");
System.out.println("1. Criar arquivo");
System.out.println("2. Escrever arquivo");
System.out.println("3. Ler arquivo");
System.out.println("4. Excluir arquivo");
System.out.println("9. Sair\n");
//System.out.println("\nOpcao:");
}
private static void closeSpreadConnection() {
try {
connection.disconnect();
} catch (SpreadException e) {
e.printStackTrace();
}
}
private void readFileName() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
fileName = in.readLine();
} catch (IOException exception) {
exception.printStackTrace();
}
}
private void sendMessage(String info, String groupName) {
SpreadMessage msg = new SpreadMessage();
msg.setSafe();
msg.addGroup(groupName);
msg.setData(info.getBytes());
// Envia a mensagem
try {
connection.multicast(msg);
} catch (SpreadException e) {
e.printStackTrace();
}
}
private static void startSpreadConnection(String user, String address, int port) {
// Establish the spread connection.
///////////////////////////////////
try
{
connection = new SpreadConnection();
connection.connect(InetAddress.getByName(address), port, user, false, true);
// Join the group CLIENT_GROUP
SpreadGroup spreadGroup = new SpreadGroup();
spreadGroup.join(connection, "CLIENT_GROUP");
System.out.println("Juntou-se ao " + spreadGroup + ".");
System.out.println("Aberta a conexão de " + user + " com " + address + ":" + port);
}
catch(SpreadException e)
{
System.err.println("Houve um erro ao tentar conectar ao daemon.");
e.printStackTrace();
System.exit(1);
}
catch(UnknownHostException e)
{
System.err.println("O daemon nao foi encontrado. Tente executar o daemon spread na pasta java." + address);
System.exit(1);
}
}
@Override
public void messageReceived(SpreadMessage msg) {
try{
if (msg.isRegular()){
//System.out.print("\nRecebida uma mensagem [REGULAR] ");
//System.out.println(" enviada por " + msg.getSender() + ".");
byte data[] = msg.getData();
String mensagemRecebida = new String(data);
//System.out.println("Recebida a mensagem <" + dado + ">");
aguardando = false;
char letra = mensagemRecebida.charAt(0);
//if (letra != 'l')
//aguardaResposta();
String[] splitedMessage = mensagemRecebida.split("#");
switch(letra) {
case 'l':
if (splitedMessage.length == 1)
System.out.println("Arquivo Vazio");
else {
String texto = splitedMessage[1];
System.out.println("Este é o arquivo solicitado:\n");
System.out.println("-------\n");
System.out.println(texto);
System.out.println("-------\n");
}
break;
case 'E':
String erro = splitedMessage[1];
System.out.println(erro);
break;
case 'K':
String okMsg = splitedMessage[1];
System.out.println(okMsg);
break;
}
}
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args) {
fileName = "";
String user = "cliente01";
String address = "127.0.0.1";
int port = 0;
startSpreadConnection(user, address, port);
AguardaMensagem wait = new AguardaMensagem();
wait.setConnection(connection);
escutadorDeMensagens = new Thread(wait);
new Client(user, address, port);
closeSpreadConnection();
}
}