-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerGUI.java
More file actions
398 lines (336 loc) · 14.6 KB
/
ServerGUI.java
File metadata and controls
398 lines (336 loc) · 14.6 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import java.awt.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
import javax.swing.*;
public class ServerGUI extends JFrame {
private JTextArea chatArea;
private JTextField inputField;
private JButton sendButton, fileButton, voiceCallButton, videoCallButton;
private JButton receiveCallButton, endCallButton, cancelCallButton;
private JLabel statusLabel;
private ServerSocket serverSocket;
private Socket clientSocket;
private BufferedReader br;
private PrintStream ps;
private String connectionId, connectionPassword;
private JFrame videoCallFrame;
private CameraPanel cameraPanel;
public ServerGUI() {
initializeGUI();
startServer();
}
private void initializeGUI() {
setTitle("Server Control Panel");
setSize(800, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
JPanel statusPanel = new JPanel(new BorderLayout());
statusPanel.setBackground(new Color(230, 230, 250));
statusPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
statusLabel = new JLabel("Server starting...");
statusLabel.setFont(new Font("Segoe UI", Font.BOLD, 16));
statusLabel.setForeground(new Color(25, 25, 112));
statusPanel.add(statusLabel, BorderLayout.WEST);
voiceCallButton = createButton("Voice Call", new Color(30, 144, 255));
videoCallButton = createButton("Video Call", new Color(138, 43, 226));
videoCallButton.addActionListener(e -> initiateVideoCall());
JPanel callPanel = new JPanel();
callPanel.setBackground(new Color(230, 230, 250));
callPanel.add(voiceCallButton);
callPanel.add(videoCallButton);
statusPanel.add(callPanel, BorderLayout.EAST);
add(statusPanel, BorderLayout.NORTH);
chatArea = new JTextArea();
chatArea.setEditable(false);
chatArea.setLineWrap(true);
chatArea.setWrapStyleWord(true);
chatArea.setFont(new Font("Segoe UI", Font.PLAIN, 14));
chatArea.setForeground(new Color(0, 51, 102));
chatArea.setBackground(Color.WHITE);
JScrollPane chatScroll = new JScrollPane(chatArea);
add(chatScroll, BorderLayout.CENTER);
inputField = new JTextField();
inputField.setFont(new Font("Segoe UI", Font.PLAIN, 14));
inputField.addActionListener(e -> sendMessage());
sendButton = createButton("Send", new Color(34, 139, 34));
sendButton.addActionListener(e -> sendMessage());
fileButton = createButton("Send File", new Color(255, 165, 0));
fileButton.addActionListener(e -> sendFile());
JPanel controlPanel = new JPanel(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.setBackground(new Color(230, 230, 250));
buttonPanel.add(sendButton);
buttonPanel.add(fileButton);
controlPanel.add(inputField, BorderLayout.CENTER);
controlPanel.add(buttonPanel, BorderLayout.EAST);
add(controlPanel, BorderLayout.SOUTH);
JPanel videoCallPanel = new JPanel();
videoCallPanel.setBackground(new Color(245, 245, 245));
receiveCallButton = createButton("Receive Call", new Color(34, 139, 34));
receiveCallButton.addActionListener(e -> receiveVideoCall());
endCallButton = createButton("End Call", new Color(244, 67, 54));
endCallButton.addActionListener(e -> endVideoCall());
endCallButton.setEnabled(false);
cancelCallButton = createButton("Cancel Call", new Color(255, 69, 0));
cancelCallButton.addActionListener(e -> cancelVideoCall());
videoCallPanel.add(receiveCallButton);
videoCallPanel.add(endCallButton);
videoCallPanel.add(cancelCallButton);
add(videoCallPanel, BorderLayout.EAST);
setVisible(true);
}
private JButton createButton(String text, Color color) {
JButton button = new JButton(text);
button.setBackground(color);
button.setForeground(Color.WHITE);
button.setFont(new Font("Segoe UI", Font.BOLD, 14));
return button;
}
private void startServer() {
new Thread(() -> {
try {
generateCredentials();
serverSocket = new ServerSocket(2100);
updateStatus("Server running on port 2100", new Color(34, 139, 34));
displayMessage("Server started successfully\nConnection ID: " + connectionId + "\nPassword: " + connectionPassword);
while (true) {
displayMessage("Waiting for client connection...");
clientSocket = serverSocket.accept();
String clientIP = clientSocket.getInetAddress().getHostAddress();
displayMessage("Client connected from IP: " + clientIP);
updateStatus("Client connected from IP: " + clientIP, new Color(76, 175, 80));
displayMessage("Client started successfully");
displayMessage("Client connected from IP: " + clientIP);
br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
ps = new PrintStream(clientSocket.getOutputStream(), true);
handleClientConnection(clientIP);
}
} catch (IOException e) {
displayMessage("Error: " + e.getMessage());
updateStatus("Server error", Color.RED);
}
}).start();
}
private void generateCredentials() {
Random random = new Random();
connectionId = "ID" + (random.nextInt(9000) + 1000);
connectionPassword = "PASS" + (random.nextInt(9000) + 1000);
}
private void handleClientConnection(String clientIP) {
try {
String id = br.readLine();
String password = br.readLine();
if (validateCredentials(id, password)) {
ps.println("SUCCESS");
displayMessage("Client authenticated from IP: " + clientIP + "\nWaiting for messages...");
displayMessage("Client authenticated from IP: " + clientIP);
displayMessage("Waiting for messages...");
String message;
while ((message = br.readLine()) != null) {
if ("[VIDEO_CALL_REQUEST]".equals(message)) {
displayMessage("Incoming video call from " + clientIP);
} else if ("[FILE_TRANSFER]".equals(message)) {
receiveFile();
} else {
String finalMessage = message;
SwingUtilities.invokeLater(() -> chatArea.append("Client: " + finalMessage + "\n"));
}
}
} else {
ps.println("INVALID_CREDENTIALS");
clientSocket.close();
updateStatus("Authentication failed for IP: " + clientIP, new Color(244, 67, 54));
displayMessage("Invalid credentials from IP: " + clientIP + "\nConnection closed");
}
} catch (IOException e) {
displayMessage("Client disconnected");
}
}
private boolean validateCredentials(String id, String password) {
return id.equals(connectionId) && password.equals(connectionPassword);
}
private void sendMessage() {
String message = inputField.getText().trim();
if (!message.isEmpty()) {
ps.println(message);
displayMessage("Server: " + message);
inputField.setText("");
}
}
private void initiateVideoCall() {
chatArea.append("Starting video call...\n");
SwingUtilities.invokeLater(() -> {
videoCallFrame = new JFrame("Video Call");
videoCallFrame.setSize(800, 600);
videoCallFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
videoCallFrame.setLayout(new BorderLayout());
JLabel callStatus = new JLabel("Connecting...");
callStatus.setHorizontalAlignment(SwingConstants.CENTER);
callStatus.setFont(new Font("Segoe UI", Font.BOLD, 16));
videoCallFrame.add(callStatus, BorderLayout.CENTER);
JPanel callControlPanel = new JPanel(new FlowLayout());
receiveCallButton.addActionListener(e -> {
callStatus.setText("Call Connected");
startCamera();
});
endCallButton = createButton("End Call", new Color(255, 69, 0));
endCallButton.addActionListener(e -> {
callStatus.setText("Call Ended");
videoCallFrame.dispose();
});
cancelCallButton = createButton("Cancel Call", new Color(255, 165, 0));
cancelCallButton.addActionListener(e -> {
callStatus.setText("Call Canceled");
videoCallFrame.dispose();
});
callControlPanel.add(receiveCallButton);
callControlPanel.add(endCallButton);
callControlPanel.add(cancelCallButton);
videoCallFrame.add(callControlPanel, BorderLayout.SOUTH);
videoCallFrame.setVisible(true);
});
}
private void startCamera() {
cameraPanel = new CameraPanel();
videoCallFrame.getContentPane().add(cameraPanel, BorderLayout.CENTER);
cameraPanel.startCamera();
}
private class CameraPanel extends JPanel {
private Dimension cameraResolution = new Dimension(640, 480);
private boolean isRunning = false;
public void startCamera() {
isRunning = true;
new Thread(() -> {
while (isRunning) {
repaint();
try {
Thread.sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (isRunning) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
}
}
}
private void receiveVideoCall() {
displayMessage("Video call accepted");
endCallButton.setEnabled(true);
receiveCallButton.setEnabled(false);
}
private void endVideoCall() {
displayMessage("Video call ended");
endCallButton.setEnabled(false);
receiveCallButton.setEnabled(true);
}
private void cancelVideoCall() {
ps.println("[VIDEO_CALL_CANCEL]");
displayMessage("Video call canceled");
}
private void sendFile() {
JFileChooser chooser = new JFileChooser();
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
try {
ps.println("[FILE_TRANSFER]");
ps.println(file.getName());
FileInputStream fis = new FileInputStream(file);
OutputStream os = clientSocket.getOutputStream();
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.flush();
fis.close();
displayMessage("File sent: " + file.getName());
} catch (IOException e) {
displayMessage("Failed to send file: " + e.getMessage());
}
}
}
private void receiveFile() {
try {
String fileName = br.readLine();
File receivedFile = new File(fileName);
FileOutputStream fos = new FileOutputStream(receivedFile);
InputStream is = clientSocket.getInputStream();
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
fos.close();
displayMessage("File received: " + fileName);
} catch (IOException e) {
displayMessage("Failed to receive file: " + e.getMessage());
}
}
private void displayMessage(String message) {
SwingUtilities.invokeLater(() -> chatArea.append(message + "\n"));
}
private void updateStatus(String status, Color color) {
SwingUtilities.invokeLater(() -> {
statusLabel.setText("Server Status: " + status);
statusLabel.setForeground(color);
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new ServerLoginGUI());
}
}
class ServerLoginGUI extends JFrame {
private JTextField idField;
private JPasswordField passwordField;
private JButton loginButton;
private JLabel statusLabel;
private static final String SERVER_ID = "admin";
private static final String SERVER_PASSWORD = "password";
public ServerLoginGUI() {
setTitle("Server Login");
setSize(400, 250);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
JPanel panel = new JPanel(new GridLayout(3, 2, 10, 10));
panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
JLabel idLabel = new JLabel("Server ID:");
idField = new JTextField();
JLabel passwordLabel = new JLabel("Password:");
passwordField = new JPasswordField();
loginButton = new JButton("Login");
loginButton.addActionListener(e -> verifyLogin());
statusLabel = new JLabel("", SwingConstants.CENTER);
statusLabel.setForeground(Color.RED);
panel.add(idLabel);
panel.add(idField);
panel.add(passwordLabel);
panel.add(passwordField);
panel.add(new JLabel());
panel.add(loginButton);
add(panel, BorderLayout.CENTER);
add(statusLabel, BorderLayout.SOUTH);
setVisible(true);
}
private void verifyLogin() {
String enteredId = idField.getText().trim();
String enteredPassword = new String(passwordField.getPassword()).trim();
if (enteredId.equals(SERVER_ID) && enteredPassword.equals(SERVER_PASSWORD)) {
JOptionPane.showMessageDialog(this, "Server Login Successful", "Success", JOptionPane.INFORMATION_MESSAGE);
dispose();
new ServerGUI();
} else {
statusLabel.setText("Invalid Credentials. Try Again!");
}
}
}