forked from ADarko22/AkkaGuiChatSourceCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommunicator.java
More file actions
145 lines (101 loc) · 4.78 KB
/
Communicator.java
File metadata and controls
145 lines (101 loc) · 4.78 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
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;
public Communicator(String path, GuiChat chat) {
this.chat = chat;
this.path = path;
sendIdentifyRequest();
//chat.getTextAreaMessages().setText("ok\n");
}
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());
}
private void writeToRoom(String logMessage) {
this.chat.getRoom().setText(this.chat.getRoom().getText()+logMessage);
}
@Override
public void onReceive(Object message) throws Exception {
if (message instanceof ActorIdentity) {
remoteActor = ((ActorIdentity) message).getRef();
if (remoteActor == null) {
System.out.println("Remote actor not available: " + path);
} else {
writeToRoom("SUCCESSFULLY CONNECTED TO THE SERVER!!\n");
getContext().watch(remoteActor);
getContext().become(active, true);
}
} else if (message instanceof ReceiveTimeout) {
sendIdentifyRequest();
} else {
writeToRoom("Not ready yet");
}
}
Procedure<Object> active = new Procedure<Object>() {
@Override
public void apply(Object message) {
switch(message.getClass().getSimpleName()) {
case "LoginMessage":
remoteActor.tell(message,getSelf());
break;
case "RejectLogin":
writeToRoom(((Messages.RejectLogin)message).getCause());
chat.setBtnLogin(true);
chat.setUsername(true);
break;
case "AckLogin":
writeToRoom("Login Riuscito \n");
chat.setClientInput(true);
chat.setBtnSend(true);
//!! aggiornare la userlist con quella che ricevo nel campo di acklogin
break;
case "Terminated":
writeToRoom("Calculator terminated");
sendIdentifyRequest();
getContext().unbecome();
break;
case "ReceiveTimeout":
//ignore
writeToRoom("Ricevuto Receive timeout");
break;
case "Reply":
break;
case "ChatMessage":
break;
case "ToPrintMessage": String msgToPrint = ((Messages.ToPrintMessage)message).getContent();
writeToRoom(msgToPrint);
break;
default: unhandled(message);
//writeToRoom("Messaggio Sconosciuto: ");
//writeToRoom(message.getClass().getSimpleName());
//writeToRoom("\n");
}
}
};
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://ChatSystem@127.0.0.1:2552/user/remoteActor";
final ActorRef communicator = system.actorOf(Props.create(Communicator.class, path, frame), "communicator");
frame.setActorReference(communicator);
}
}