-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
159 lines (125 loc) · 4.9 KB
/
Copy pathClient.java
File metadata and controls
159 lines (125 loc) · 4.9 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
import java.io.IOException;
import java.net.Socket;
public class Client extends Thread{
protected Connection connection;
private volatile boolean clientConnected = false;
public static void main(String[] args) {
new Client().run();
}
protected String getServerAddress(){
ConsoleHelper.writeMessage("input server address");
return ConsoleHelper.readString();
}
protected int getServerPort(){
ConsoleHelper.writeMessage("input server port");
return ConsoleHelper.readInt();
}
protected String getUserName(){
ConsoleHelper.writeMessage("input your name");
return ConsoleHelper.readString();
}
protected boolean shouldSendTextFromConsole(){
return true;
}
protected SocketThread getSocketThread(){
return new SocketThread();
}
protected void sendTextMessage(String text){
try {
Message message = new Message(MessageType.TEXT, text);
connection.send(message);
} catch (IOException e) {
ConsoleHelper.writeMessage("something went wrong while sending message");
clientConnected = false;
}
}
public class SocketThread extends Thread{
protected void processIncomingMessage(String message){
ConsoleHelper.writeMessage(message);
}
protected void informAboutAddingNewUser(String userName){
ConsoleHelper.writeMessage(String.format("%s joined our chat", userName));
}
protected void informAboutDeletingNewUser(String userName){
ConsoleHelper.writeMessage(String.format("%s left us", userName));
}
protected void notifyConnectionStatusChanged(boolean clientConnected){
Client.this.clientConnected = clientConnected;
synchronized (Client.this) {
Client.this.notify();
}
}
protected void clientHandshake() throws IOException, ClassNotFoundException{
while (true){
Message receivingMessage = connection.receive();
MessageType typeReceived = receivingMessage.getType();
if(typeReceived == MessageType.NAME_REQUEST){
String userName = getUserName();
Message sendUserName = new Message(MessageType.USER_NAME, userName);
connection.send(sendUserName);
}else if (typeReceived == MessageType.NAME_ACCEPTED){
notifyConnectionStatusChanged(true);
break;
}else{
throw new IOException("Unexpected MessageType");
}
}
}
protected void clientMainLoop() throws IOException, ClassNotFoundException{
Message receiveMessage;
while (true) {
receiveMessage = connection.receive();
if(receiveMessage.getType() == MessageType.TEXT){
processIncomingMessage(receiveMessage.getData());
}else if(receiveMessage.getType() == MessageType.USER_ADDED){
informAboutAddingNewUser(receiveMessage.getData());
}else if(receiveMessage.getType() == MessageType.USER_REMOVED){
informAboutDeletingNewUser(receiveMessage.getData());
}else{
throw new IOException("Unexpected MessageType");
}
}
}
@Override
public void run() {
super.run();
String serverAddress = getServerAddress();
int serverPort = getServerPort();
try{
Socket socket = new Socket(serverAddress, serverPort);
connection = new Connection(socket);
clientHandshake();
clientMainLoop();
}catch (Exception e){
notifyConnectionStatusChanged(false);
}
}
}
@Override
public void run(){
SocketThread socketThread = getSocketThread();
socketThread.setDaemon(true);
socketThread.start();
try {
synchronized (this) {
wait();
}
}catch (Exception e){
ConsoleHelper.writeMessage("thread exception...");
System.exit(1);
}
if(clientConnected){
ConsoleHelper.writeMessage("Соединение установлено. Для выхода наберите команду ‘exit’");
String input;
while (clientConnected){
input = ConsoleHelper.readString();
if (input.equals("exit"))
break;
if (shouldSendTextFromConsole())
sendTextMessage(input);
}
}else{
ConsoleHelper.writeMessage("Произошла ошибка во время работы клиента.");
}
}
}