-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUIClient.java
More file actions
281 lines (234 loc) · 9.83 KB
/
Copy pathGUIClient.java
File metadata and controls
281 lines (234 loc) · 9.83 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// GUIClient.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.rmi.registry.*;
import java.util.*;
import java.util.List;
public class GUIClient extends JFrame {
private String method;
private JTextArea messageArea;
private JTextField inputField;
private JButton sendButton;
private JButton getAllButton;
private JTextArea logArea;
// Connection objects
private HelloInterface rmiStub;
private Socket socket;
private PrintWriter socketOut;
private BufferedReader socketIn;
private OutputStream wsOut;
private InputStream wsIn;
public GUIClient(String method) {
this.method = method;
setTitle(method + " Client");
setSize(600, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout(10, 10));
// Top panel - Connection info
JPanel topPanel = new JPanel();
topPanel.setBackground(new Color(70, 130, 180));
JLabel titleLabel = new JLabel(method + " Client Connected");
titleLabel.setFont(new Font("Arial", Font.BOLD, 16));
titleLabel.setForeground(Color.WHITE);
topPanel.add(titleLabel);
add(topPanel, BorderLayout.NORTH);
// Center panel - Messages
JPanel centerPanel = new JPanel(new BorderLayout(5, 5));
centerPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JLabel msgLabel = new JLabel("Server Messages:");
centerPanel.add(msgLabel, BorderLayout.NORTH);
messageArea = new JTextArea();
messageArea.setEditable(false);
messageArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
JScrollPane msgScroll = new JScrollPane(messageArea);
centerPanel.add(msgScroll, BorderLayout.CENTER);
add(centerPanel, BorderLayout.CENTER);
// Bottom panel - Input
JPanel bottomPanel = new JPanel(new BorderLayout(5, 5));
bottomPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JPanel inputPanel = new JPanel(new BorderLayout(5, 5));
inputField = new JTextField();
inputField.setFont(new Font("Arial", Font.PLAIN, 14));
inputPanel.add(new JLabel("Message:"), BorderLayout.WEST);
inputPanel.add(inputField, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel(new FlowLayout());
sendButton = new JButton("Send");
sendButton.setBackground(new Color(34, 139, 34));
sendButton.setForeground(Color.WHITE);
sendButton.addActionListener(e -> sendMessage());
getAllButton = new JButton("Get All Messages");
getAllButton.setBackground(new Color(70, 130, 180));
getAllButton.setForeground(Color.WHITE);
getAllButton.addActionListener(e -> getAllMessages());
buttonPanel.add(sendButton);
buttonPanel.add(getAllButton);
bottomPanel.add(inputPanel, BorderLayout.NORTH);
bottomPanel.add(buttonPanel, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.SOUTH);
// Connect to server
connectToServer();
// Test initial methods
testInitialMethods();
setVisible(true);
}
private void connectToServer() {
try {
switch (method) {
case "RMI":
Registry registry = LocateRegistry.getRegistry("localhost", 1099);
rmiStub = (HelloInterface) registry.lookup("HelloService");
break;
case "RPC":
socket = new Socket("localhost", 5000);
socketOut = new PrintWriter(socket.getOutputStream(), true);
socketIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
break;
case "WebSocket":
connectWebSocket();
break;
}
appendMessage("Connected to " + method + " server successfully!\n");
} catch (Exception e) {
JOptionPane.showMessageDialog(this,
"Could not connect to server: " + e.getMessage(),
"Connection Error",
JOptionPane.ERROR_MESSAGE);
}
}
private void connectWebSocket() throws Exception {
socket = new Socket("localhost", 8080);
wsOut = socket.getOutputStream();
wsIn = socket.getInputStream();
String key = Base64.getEncoder().encodeToString(
("client" + System.currentTimeMillis()).getBytes()
);
String handshake = "GET / HTTP/1.1\r\n" +
"Host: localhost:8080\r\n" +
"Upgrade: websocket\r\n" +
"Connection: Upgrade\r\n" +
"Sec-WebSocket-Key: " + key + "\r\n" +
"Sec-WebSocket-Version: 13\r\n\r\n";
wsOut.write(handshake.getBytes());
wsOut.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(wsIn));
String line;
while (!(line = reader.readLine()).isEmpty()) {
// Skip headers
}
}
private void testInitialMethods() {
try {
String helloResponse = callRemote("HELLO", "Alice");
appendMessage("Test 1 - " + helloResponse + "\n");
String addResponse = callRemote("ADD", "5,3");
appendMessage("Test 2 - 5 + 3 = " + addResponse + "\n\n");
appendMessage("You can now send messages to the server!\n");
} catch (Exception e) {
appendMessage("Error testing methods: " + e.getMessage() + "\n");
}
}
private void sendMessage() {
String message = inputField.getText().trim();
if (message.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter a message!", "Empty Message", JOptionPane.WARNING_MESSAGE);
return;
}
try {
String response = callRemote("SEND", message);
appendMessage("You: " + message + "\n");
appendMessage("Server: " + response + "\n\n");
inputField.setText("");
} catch (Exception e) {
appendMessage("Error sending message: " + e.getMessage() + "\n");
}
}
private void getAllMessages() {
try {
String response = callRemote("GETALL", "");
appendMessage("=== All Messages on Server ===\n");
if (response.equals("No messages")) {
appendMessage("No messages stored on server\n");
} else {
String[] messages = response.split(";;");
for (int i = 0; i < messages.length; i++) {
appendMessage((i + 1) + ". " + messages[i] + "\n");
}
}
appendMessage("\n");
} catch (Exception e) {
appendMessage("Error getting messages: " + e.getMessage() + "\n");
}
}
private String callRemote(String methodName, String params) throws Exception {
switch (method) {
case "RMI":
return callRMI(methodName, params);
case "RPC":
return callRPC(methodName, params);
case "WebSocket":
return callWebSocket(methodName, params);
default:
return "Unknown method";
}
}
private String callRMI(String methodName, String params) throws Exception {
switch (methodName) {
case "HELLO":
return rmiStub.sayHello(params);
case "ADD":
String[] nums = params.split(",");
return String.valueOf(rmiStub.add(Integer.parseInt(nums[0]), Integer.parseInt(nums[1])));
case "SEND":
rmiStub.sendMessage(params);
return "Message received";
case "GETALL":
List<String> msgs = rmiStub.getAllMessages();
return msgs.isEmpty() ? "No messages" : String.join(";;", msgs);
default:
return "Unknown";
}
}
private String callRPC(String methodName, String params) throws Exception {
String request = params.isEmpty() ? methodName : methodName + "|" + params;
socketOut.println(request);
return socketIn.readLine();
}
private String callWebSocket(String methodName, String params) throws Exception {
String request = params.isEmpty() ? methodName : methodName + "|" + params;
sendWSMessage(request);
return receiveWSMessage();
}
private void sendWSMessage(String message) throws IOException {
byte[] data = message.getBytes();
Random random = new Random();
byte[] mask = new byte[4];
random.nextBytes(mask);
wsOut.write(0x81);
wsOut.write(0x80 | data.length);
wsOut.write(mask);
for (int i = 0; i < data.length; i++) {
wsOut.write(data[i] ^ mask[i % 4]);
}
wsOut.flush();
}
private String receiveWSMessage() throws IOException {
int b = wsIn.read();
if (b == -1) return null;
b = wsIn.read();
int length = b & 0x7F;
byte[] data = new byte[length];
wsIn.read(data);
return new String(data);
}
private void appendMessage(String msg) {
messageArea.append(msg);
messageArea.setCaretPosition(messageArea.getDocument().getLength());
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new GUIClient("RMI"));
}
}