-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAguardaMensagem.java
More file actions
148 lines (102 loc) · 3.21 KB
/
AguardaMensagem.java
File metadata and controls
148 lines (102 loc) · 3.21 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
import java.net.InetAddress;
import java.net.UnknownHostException;
import spread.BasicMessageListener;
import spread.SpreadConnection;
import spread.SpreadException;
import spread.SpreadGroup;
import spread.SpreadMessage;
public class AguardaMensagem implements Runnable, BasicMessageListener {
SpreadConnection connection;
public void setConnection(SpreadConnection c){
connection = c;
}
@Override
public void run() {
SpreadMessage msgreceived = new SpreadMessage();
/* String address = "127.0.0.1";
int port = 0;
String user = "Cliente01";
startSpreadConnection(user, address, port);
*/
boolean aguardando = true;
while(aguardando){
try{
// Receive a message.
/////////////////////
msgreceived = connection.receive();
messageReceived(msgreceived);
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
}
private 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 + ">");
char letra = mensagemRecebida.charAt(0);
//if (letra != 'l')
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);
}
}
}