-
Notifications
You must be signed in to change notification settings - Fork 0
/
Communicator.java
127 lines (98 loc) · 4.14 KB
/
Communicator.java
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
package remote;
//....//
import akka.actor.ActorRef;
import akka.actor.UntypedActor;
import akka.actor.ActorSystem;
import akka.actor.Props;
import java.io.Serializable;
import akka.actor.ActorIdentity;
import akka.actor.Identify;
import akka.actor.Terminated;
import akka.actor.ReceiveTimeout;
import akka.japi.Procedure;
import static java.util.concurrent.TimeUnit.SECONDS;
import scala.concurrent.duration.Duration;
import com.typesafe.config.ConfigFactory;
public class Communicator extends UntypedActor {
private GuiChat chat;
private final String path;
private ActorRef remoteActor = null;
/*constructor*/
public Communicator(String path, GuiChat chat) {
this.chat = chat;
this.path = path;
sendIdentifyRequest();
}
/*send an Identify request to the server address*/
private void sendIdentifyRequest() {
getContext().actorSelection(path).tell(new Identify(path), getSelf());
getContext().system().scheduler().scheduleOnce(Duration.create(3, SECONDS), getSelf(),ReceiveTimeout.getInstance(), getContext().dispatcher(), getSelf());
}
/*try to establilish te connection with the server*/
@Override
public void onReceive(Object message) throws Exception {
/*when server reply with its own ActorRef*/
if (message instanceof ActorIdentity) {
remoteActor = ((ActorIdentity) message).getRef();
if (remoteActor == null) {
System.out.println("Remote actor not available: " + path);
}
else {
this.chat.getRoom().setText("SUCCESSFULLY CONNECTED TO THE SERVER!!");
getContext().watch(remoteActor);
/*Communicator becomes to interacte with the chat service*/
getContext().become(active, true);
}
}
/*when timeout is elapsed without the server reply, retry the Identify*/
else if (message instanceof ReceiveTimeout) {
sendIdentifyRequest();
}
/**/
else {
writeToRoom("Not ready yet");
}
}
/*define the Communicator new behaviour after the connection with the server*/
Procedure<Object> active = new Procedure<Object>() {
@Override
public void apply(Object message) {
switch(message.getClass().getSimpleName()) {
case "Terminated":
writeToRoom("Calculator terminated");
sendIdentifyRequest();
getContext().unbecome();
break;
case "ReceiveTimeout":
//ignore
break;
case "Reply":
break;
case "ChatMessage":
remoteActor.tell(message, getSelf());
break;
case "ToPrintMessage": //print the message in the GUI (è meglio usare msgToPrint come private?)
String msgToPrint = ((Messages.ToPrintMessage)message).getContent();
writeToRoom(msgToPrint);
break;
default:
//writeToRoom("Messaggio non riconosciuto!...");
}
}
};
/*update the chatroom*/
private void writeToRoom(String logMessage) {
this.chat.getRoom().setText(chat.getRoom().getText()+"\n"+logMessage);
}
/*______START the CHAT CLIENT______*/
public static void main(String[] args) {
GuiChat frame = new GuiChat();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setTitle("Remote Chat with Akka");
final ActorSystem system = ActorSystem.create("ClientChatSystem", ConfigFactory.load("remotelookup"));
final String path = "akka.tcp://[email protected]:2552/user/remoteActor";
final ActorRef communicator = system.actorOf(Props.create(Communicator.class, path, frame), "communicator");
frame.setActorReference(communicator);
}
}