-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerminalClient.java
More file actions
718 lines (630 loc) · 25.6 KB
/
TerminalClient.java
File metadata and controls
718 lines (630 loc) · 25.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import java.util.Date;
import java.util.Map;
import java.util.HashMap;
import java.awt.Color;
import java.awt.Font;
/**
* Represents the connection state of a host
*/
enum ConnectionState {
DISCONNECTED,
CONNECTING,
CONNECTED,
FAILED
}
/**
* Represents the execution mode of the terminal client
*/
enum ExecutionMode {
MANUAL,
AUTOMATIC
}
/**
* Configuration for a remote host connection
*/
class HostConfig {
private static final int RETRY_COOLDOWN_MS = 5000;
final String hostname;
final int port;
final String clientName;
final String autoCommand;
ConnectionState state;
long lastAttempt;
int retryCount;
public HostConfig(String hostname, int port, String clientName, String autoCommand) {
this.hostname = hostname;
this.port = port;
this.clientName = clientName;
this.autoCommand = autoCommand;
this.state = ConnectionState.DISCONNECTED;
this.lastAttempt = 0;
this.retryCount = 0;
}
public boolean canRetry() {
return System.currentTimeMillis() - lastAttempt >= RETRY_COOLDOWN_MS;
}
@Override
public String toString() {
return String.format("Host[%s:%d, client=%s]", hostname, port, clientName);
}
}
/**
* Manages network connections to remote hosts
*/
class ConnectionManager {
private static final int TIMEOUT_MS = 30000;
private static LogCallback logCallback;
private Socket socket;
private PrintWriter writer;
private BufferedReader reader;
private HostConfig config;
public static void setLogCallback(LogCallback callback) {
logCallback = callback;
}
interface LogCallback {
void log(String message);
}
public ConnectionManager(HostConfig config) {
this.config = config;
}
public boolean connect() throws IOException {
try {
socket = new Socket(config.hostname, config.port);
socket.setSoTimeout(TIMEOUT_MS);
socket.setKeepAlive(true);
writer = new PrintWriter(socket.getOutputStream(), true);
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
config.state = ConnectionState.CONNECTED;
return true;
} catch (IOException e) {
config.state = ConnectionState.FAILED;
close(); // Ensure cleanup on connection failure
throw e;
}
}
public void sendCommand(String command) throws IOException {
if (!config.state.equals(ConnectionState.CONNECTED) || socket == null || socket.isClosed()) {
throw new IOException("Not connected to host: " + config.hostname);
}
try {
writer.println(command);
if (writer.checkError()) { // Check for write errors
throw new IOException("Write error occurred");
}
} catch (Exception e) {
close(); // Cleanup on error
throw new IOException("Failed to send command: " + e.getMessage());
}
}
public String readResponse() throws IOException {
if (!config.state.equals(ConnectionState.CONNECTED) || reader == null) {
throw new IOException("Not connected to host: " + config.hostname);
}
try {
String response = reader.readLine();
if (response == null) {
throw new IOException("Connection closed by server");
}
return response;
} catch (IOException e) {
close(); // Cleanup on error
throw e;
}
}
public void close() {
try {
if (writer != null) {
writer.close();
}
if (reader != null) {
reader.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException e) {
// Log close errors but don't throw
if (logCallback != null) {
logCallback.log("Error closing connection: " + e.getMessage());
}
} finally {
config.state = ConnectionState.DISCONNECTED;
reader = null;
writer = null;
socket = null;
}
}
}
/**
* Terminal client application for managing remote host connections
*/
public class TerminalClient {
private static final int MAX_RETRIES = 3;
private static final String CONFIG_FILE = "client_config.properties";
private static final String LOCK_FILE = "terminal_client.lock";
private static final int LOG_MAX_LINES = 100;
private static BufferedReader consoleReader;
private static Properties config = new Properties();
private static boolean isSilent;
private static ExecutionMode executionMode;
private static JDialog statusDialog;
private static JLabel statusLabel;
private static File lockFile;
private static List<HostConfig> hostConfigs = new ArrayList<HostConfig>();
private static Map<String, ConnectionManager> connections = new HashMap<String, ConnectionManager>();
private static boolean isAlreadyRunning() {
try {
return !lockFile.createNewFile();
} catch (IOException e) {
return true;
}
}
public static void main(String[] args) {
ConnectionManager.setLogCallback(new ConnectionManager.LogCallback() {
public void log(String message) {
logError(message);
}
});
lockFile = new File(LOCK_FILE);
if (isAlreadyRunning()) {
if (isRunningInCommandPrompt()) {
System.err.println("Another instance is already running.");
} else {
showError("Another instance is already running.");
}
System.exit(1);
}
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
if (lockFile.exists()) {
lockFile.delete();
}
}
});
loadConfig();
isSilent = Boolean.parseBoolean(getConfigString("silentMode", ""));
executionMode = ExecutionMode.valueOf(getConfigString("executionMode", "MANUAL"));
consoleReader = new BufferedReader(new InputStreamReader(System.in));
// For automatic mode
if (executionMode.equals(ExecutionMode.AUTOMATIC)) {
if (!isSilent) {
createAndShowStatusMessage();
} else {
System.out.println("Terminal client is running in silent mode...");
}
}
// For manual mode
if (executionMode.equals(ExecutionMode.MANUAL) && !isRunningInCommandPrompt()) {
showError("Manual mode must be run in command prompt environment.");
System.exit(1);
}
if (executionMode.equals(ExecutionMode.AUTOMATIC)) {
if (!isSilent) {
createAndShowStatusMessage();
}
// Single attempt to connect to all hosts
connectToHosts();
// Execute commands on any successfully connected hosts
for (Map.Entry<String, ConnectionManager> entry : connections.entrySet()) {
HostConfig hostConfig = null;
for (HostConfig config : hostConfigs) {
if (config.hostname.equals(entry.getKey())) {
hostConfig = config;
break;
}
}
if (hostConfig != null && !hostConfig.autoCommand.isEmpty()) {
try {
// If the command contains multiple parts, add delays
if (hostConfig.autoCommand.contains(";")) {
String[] parts = hostConfig.autoCommand.split(";");
StringBuilder commandWithDelays = new StringBuilder();
for (int i = 0; i < parts.length; i++) {
commandWithDelays.append(parts[i].trim());
if (i < parts.length - 1) {
commandWithDelays.append("; sleep 2; ");
}
}
entry.getValue().sendCommand(commandWithDelays.toString());
} else {
entry.getValue().sendCommand(hostConfig.autoCommand);
}
if (!isSilent) {
updateStatusLabel("Sending command to " + hostConfig.hostname);
Thread.sleep(1500);
}
} catch (Exception e) {
logError("Error executing commands on " + hostConfig.hostname + ": " + e.getMessage());
}
}
}
// Close all connections
for (ConnectionManager connection : connections.values()) {
connection.close();
}
connections.clear();
if (!isSilent) {
disposeStatusMessage();
}
System.exit(0);
} else {
// Manual mode
System.out.println("Available hosts:");
for (int i = 0; i < hostConfigs.size(); i++) {
System.out.println((i + 1) + ". " + hostConfigs.get(i));
}
try {
System.out.print("Select host number (or 'all' for all hosts): ");
String selection = consoleReader.readLine();
List<HostConfig> selectedHosts = new ArrayList<HostConfig>();
if ("all".equalsIgnoreCase(selection)) {
selectedHosts.addAll(hostConfigs);
} else {
try {
int index = Integer.parseInt(selection) - 1;
if (index >= 0 && index < hostConfigs.size()) {
selectedHosts.add(hostConfigs.get(index));
} else {
showError("Invalid host number");
System.exit(1);
}
} catch (NumberFormatException e) {
showError("Invalid input. Please enter a number or 'all'");
System.exit(1);
}
}
// Connect to selected hosts
for (HostConfig hostConfig : selectedHosts) {
try {
logInfo("Attempting to connect to " + hostConfig);
ConnectionManager connection = new ConnectionManager(hostConfig);
connection.connect();
connections.put(hostConfig.hostname, connection);
connection.sendCommand(hostConfig.clientName);
System.out.println("Connected to " + hostConfig.hostname);
Thread.sleep(1000);
} catch (Exception e) {
String errorMsg = "Failed to connect to " + hostConfig + ": " + e.getMessage();
logError(errorMsg);
System.err.println(errorMsg);
}
}
if (connections.isEmpty()) {
showError("No connections established");
System.exit(1);
}
// Create response handlers for each connection
for (final Map.Entry<String, ConnectionManager> entry : connections.entrySet()) {
Thread responseHandler = new Thread(new Runnable() {
public void run() {
try {
String response;
while ((response = entry.getValue().readResponse()) != null) {
System.out.println("[" + entry.getKey() + "] " + response);
if ("Goodbye!".equalsIgnoreCase(response.trim())) {
break;
}
}
} catch (IOException e) {
logError("Connection lost to " + entry.getKey() + ": " + e.getMessage());
}
}
});
responseHandler.start();
}
// Main command loop
String command;
while (true) {
System.out.print(">>> ");
command = consoleReader.readLine();
if ("exit".equalsIgnoreCase(command.trim())) {
for (ConnectionManager connection : connections.values()) {
try {
connection.sendCommand("exit");
} catch (IOException e) {
// Ignore send errors during exit
}
}
break;
}
// Send command to all connected hosts
for (Map.Entry<String, ConnectionManager> entry : connections.entrySet()) {
try {
entry.getValue().sendCommand(command);
} catch (IOException e) {
System.err.println("Failed to send command to " + entry.getKey() + ": " + e.getMessage());
}
}
Thread.sleep(1000);
}
} catch (Exception e) {
showError("Error in manual mode: " + e.getMessage());
} finally {
// Close all connections
for (ConnectionManager connection : connections.values()) {
connection.close();
}
connections.clear();
}
}
}
private static boolean isRunningInCommandPrompt() {
return System.console() != null;
}
private static void createAndShowStatusMessage() {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame();
statusDialog = new JDialog(frame, "Terminal Client Status", true);
statusDialog.setSize(400, 100);
statusDialog.setLocationRelativeTo(null);
statusDialog.setLayout(new BorderLayout());
statusDialog.setUndecorated(true);
JPanel panel = new JPanel(new BorderLayout(5, 5));
panel.setBackground(new Color(44, 62, 80));
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
statusLabel = new JLabel("Initializing...");
statusLabel.setForeground(Color.WHITE);
statusLabel.setFont(new Font("Arial", Font.BOLD, 12));
JProgressBar progressBar = new JProgressBar();
progressBar.setIndeterminate(true);
progressBar.setStringPainted(false);
progressBar.setPreferredSize(new Dimension(progressBar.getPreferredSize().width, 5));
panel.add(statusLabel, BorderLayout.CENTER);
panel.add(progressBar, BorderLayout.SOUTH);
statusDialog.add(panel);
statusDialog.setVisible(true);
});
}
private static void updateStatusLabel(final String message) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
if (statusLabel != null) {
statusLabel.setText(message);
}
}
});
}
private static void disposeStatusMessage() {
if (statusDialog != null) {
updateStatusLabel("Operation completed. closing.");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
statusDialog.dispose();
}
}
private static void loadConfig() {
File configFile = new File(CONFIG_FILE);
if (!configFile.exists()) {
createDefaultConfig();
}
FileInputStream in = null;
try {
in = new FileInputStream(configFile);
config.load(in);
// Check if this is an old format config
if (!config.containsKey("hostCount")) {
String oldIp = config.getProperty("serverIP", "");
String oldPort = config.getProperty("port", "");
String oldClientName = config.getProperty("clientName", "");
String oldAutoCommand = config.getProperty("autoCommand", "");
if (!oldIp.isEmpty() && !oldPort.isEmpty()) {
// Set up new format
config.setProperty("hostCount", "1");
config.setProperty("host.1.ip", oldIp);
config.setProperty("host.1.port", oldPort);
config.setProperty("host.1.clientName", oldClientName);
config.setProperty("host.1.autoCommand", oldAutoCommand);
// Remove old properties
config.remove("serverIP");
config.remove("port");
config.remove("clientName");
config.remove("autoCommand");
// Save the new format
saveConfig();
}
}
// Load host configurations
hostConfigs.clear();
int hostCount = getConfigInt("hostCount", 1);
for (int i = 1; i <= hostCount; i++) {
String hostname = getConfigString("host." + i + ".ip", "");
int port = getConfigInt("host." + i + ".port", -1);
String clientName = getConfigString("host." + i + ".clientName", "");
String autoCommand = getConfigString("host." + i + ".autoCommand", "");
if (!hostname.isEmpty() && port > 0) {
hostConfigs.add(new HostConfig(hostname, port, clientName, autoCommand));
}
}
if (hostConfigs.isEmpty()) {
loadConfig(); // Reload with default config
}
} catch (IOException e) {
showError("Error loading config file: " + e.getMessage());
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
showError("Error closing config file input stream: " + e.getMessage());
}
}
}
}
private static void createDefaultConfig() {
// Clear existing properties
config.clear();
config.setProperty("hostCount", "2");
// First host
config.setProperty("host.1.ip", "192.168.0.103");
config.setProperty("host.1.port", "8887");
config.setProperty("host.1.clientName", "Ryu 103");
config.setProperty("host.1.autoCommand", "ps aux | grep '[j]ava' | grep -v 'TerminalServer.jar' | awk '{print $2}' | xargs -r kill; sleep 2; nohup /home/user1/AiJPOS_TouchScreen/bin/linux/AiJPOS.sh > /dev/null 2>&1 & sleep 2; exit");
// Second host
config.setProperty("host.2.ip", "192.168.0.105");
config.setProperty("host.2.port", "8887");
config.setProperty("host.2.clientName", "Ryu 105");
config.setProperty("host.2.autoCommand", "ps aux | grep '[j]ava' | grep -v 'TerminalServer.jar' | awk '{print $2}' | xargs -r kill; /home/user1/AiJPOS_TouchScreen_Other/bin/linux/PsJPOS.sh; exit");
// Global settings
config.setProperty("silentMode", "false");
config.setProperty("executionMode", "AUTOMATIC");
saveConfig();
// Log the creation of default config
logInfo("Created default configuration with " + config.getProperty("hostCount") + " hosts");
}
private static void saveConfig() {
FileOutputStream out = null;
try {
out = new FileOutputStream(CONFIG_FILE);
config.store(out, "Client Configuration");
} catch (IOException e) {
showError("Error saving config file: " + e.getMessage());
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
showError("Error closing config file output stream: " + e.getMessage());
}
}
}
}
private static String getConfigString(String key, String defaultValue) {
return config.getProperty(key, defaultValue);
}
private static int getConfigInt(String key, int defaultValue) {
try {
return Integer.parseInt(config.getProperty(key, String.valueOf(defaultValue)));
} catch (NumberFormatException e) {
return defaultValue;
}
}
private static void showError(String message) {
if (!isSilent) {
JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);
} else {
logError(message);
}
System.err.println("Error: " + message);
}
private static void logError(String e) {
try {
File logDir = new File("logs");
if (!logDir.exists()) {
logDir.mkdirs();
}
File logFile = new File(logDir, "terminal_client.log");
BufferedWriter writer = new BufferedWriter(new FileWriter(logFile, true));
writer.write(e);
writer.newLine();
writer.close();
manageLogFileSize(logFile);
} catch (IOException ioException) {
System.err.println("Logging error: " + ioException.getMessage());
}
}
private static void manageLogFileSize(File logFile) {
try {
BufferedReader reader = new BufferedReader(new FileReader(logFile));
List<String> lines = new ArrayList<String>();
String line;
while ((line = reader.readLine()) != null) {
lines.add(line);
}
reader.close();
if (lines.size() > LOG_MAX_LINES) {
lines = lines.subList(lines.size() - LOG_MAX_LINES, lines.size());
BufferedWriter writer = new BufferedWriter(new FileWriter(logFile));
for (String l : lines) {
writer.write(l);
writer.newLine();
}
writer.close();
}
} catch (IOException e) {
System.err.println("Error managing log file size: " + e.getMessage());
}
}
private static void connectToHosts() {
for (HostConfig hostConfig : hostConfigs) {
if (hostConfig.retryCount >= MAX_RETRIES) continue;
if (!hostConfig.canRetry()) continue;
try {
updateStatusLabel("[*] Connecting to " + hostConfig.hostname);
ConnectionManager connection = new ConnectionManager(hostConfig);
connection.connect();
connections.put(hostConfig.hostname, connection);
updateStatusLabel("[>] Sending client name: " + hostConfig.clientName);
connection.sendCommand(hostConfig.clientName);
hostConfig.state = ConnectionState.CONNECTED;
hostConfig.retryCount = 0;
} catch (Exception e) {
logError("[!] " + hostConfig.hostname + ": " + e.getMessage());
ConnectionManager connection = connections.remove(hostConfig.hostname);
if (connection != null) {
connection.close();
}
hostConfig.state = ConnectionState.FAILED;
hostConfig.retryCount++;
}
}
if (connections.isEmpty() && allHostsMaxedRetries()) {
updateStatusLabel("[X] All connections failed");
try { Thread.sleep(1000); } catch (InterruptedException e) {}
System.exit(1);
}
}
private static boolean allHostsMaxedRetries() {
for (HostConfig hostConfig : hostConfigs) {
if (hostConfig.retryCount < MAX_RETRIES) {
return false;
}
}
return true;
}
private static void logInfo(String message) {
try {
File logDir = new File("logs");
if (!logDir.exists()) {
logDir.mkdirs();
}
File logFile = new File(logDir, "terminal_client.log");
BufferedWriter writer = new BufferedWriter(new FileWriter(logFile, true));
writer.write("[INFO] " + new Date() + " - " + message);
writer.newLine();
writer.close();
manageLogFileSize(logFile);
} catch (IOException ioException) {
System.err.println("Logging error: " + ioException.getMessage());
}
}
}