-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathCaller.java
More file actions
63 lines (51 loc) · 1.91 KB
/
Caller.java
File metadata and controls
63 lines (51 loc) · 1.91 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
import java.io.IOException;
import java.net.Socket;
public class Caller {
private String localNick;
private String remoteIP;
private String remoteNick;
private Command lastCommand;
private NickCommand nickCommand;
private String lastError;
public Caller(String localNick,String remoteIP){
this.localNick=localNick;
this.remoteIP=remoteIP;
}
public Connection call() throws IOException {
Connection connection = new Connection(new Socket(remoteIP,Protocol.PORT_NUMBER));
//Проверка, к тому ли мы подключились.
lastCommand = connection.recieve();
if (lastCommand.type==CommandType.NICK) {
nickCommand = (NickCommand) lastCommand;
}
else{
connection.disconnect();
lastError="Wrong IP";
UltimateGUI ultimateGUI = new UltimateGUI("Failed to connect");
return null;
}
//Проверка, занят ли тот пользователь
remoteNick = nickCommand.getNick();
if (nickCommand.isBusy()){
lastCommand = connection.recieve();
connection.disconnect();
UltimateGUI busyGUI = new UltimateGUI(remoteNick+" is busy");
lastError="User is busy";
remoteNick=null;
return null;
}
//Если не занят, то отсылаем ник и ждём подтверждения
lastCommand = connection.recieve();
//Если accept - вернуть connection
if (lastCommand.type==CommandType.ACCEPT) return connection;
else{
UltimateGUI ultimateGUI = new UltimateGUI(remoteNick+" rejected your call");
lastError="User rejected your call";
connection.disconnect();
return null;
}
}
public String getRemoteNick(){
return remoteNick;
}
}