1 /*
2 * #%L
3 * This file is part of jFold.
4 * %%
5 * Copyright (C) 2012 - 2024 Mike Thomas <mikepthomas@outlook.com>
6 * %%
7 * jFold is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 * %
12 * jFold is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * %
17 * You should have received a copy of the GNU General Public License
18 * along with jFold. If not, see <http://www.gnu.org/licenses/>.
19 * #L%
20 */
21 package info.mikethomas.jfold;
22
23 import info.mikethomas.jfold.util.Command;
24 import info.mikethomas.jfold.util.PyonParser;
25
26 import java.io.IOException;
27 import java.io.PrintStream;
28 import java.net.Socket;
29 import java.util.ArrayList;
30 import java.util.List;
31 import java.util.Scanner;
32
33 import lombok.extern.slf4j.XSlf4j;
34
35 import org.slf4j.profiler.Profiler;
36
37 /**
38 * <p>SocketConnection class.</p>
39 *
40 * @author Michael Thomas (mikepthomas@outlook.com)
41 * @version 7.6.21
42 */
43 @XSlf4j
44 public class SocketConnection {
45
46 /** Clear Screen. */
47 public static final String CLRSCR = "\033[H\033[2J";
48 /** Command Prompt. */
49 public static final String COMMAND_PROMPT = "> ";
50 /** Encoding. */
51 public static final String ENCODING = "UTF-8";
52 /** Welcome Message. */
53 public static final String WELCOME_MSG
54 = "Welcome to the FAHClient command server.";
55
56 /** Socket to connect to the Folding@Home client. */
57 private Socket socket;
58 /** OutputStream to send commands to. */
59 private PrintStream out;
60 /** InputStream to receive data from. */
61 private Scanner in;
62
63 /**
64 * <p>Constructor for SocketConnection.</p>
65 *
66 * @param address a {@link java.lang.String} object.
67 * @param port a int.
68 * @throws java.io.IOException a int.
69 */
70 public SocketConnection(
71 final String address, final int port) throws IOException {
72 super();
73
74 if (address != null) {
75 socket = new Socket(address, port);
76
77 out = new PrintStream(socket.getOutputStream(), true, ENCODING);
78 in = new Scanner(socket.getInputStream(), ENCODING);
79 in.useDelimiter(COMMAND_PROMPT);
80
81 var welcome = in.nextLine();
82 log.info(welcome);
83 if (!(CLRSCR + WELCOME_MSG).equals(welcome)) {
84 throw new IOException(
85 "Unexpected welcome message, check client version");
86 }
87 }
88 }
89
90 /**
91 * Send a command to the Folding@home Client.
92 *
93 * @param command command to send to the client
94 * @return String response from client
95 */
96 protected String sendCommand(final Command command) {
97 return sendCommand(command, new ArrayList<>());
98 }
99
100 /**
101 * Send a command to the Folding@home Client with arguments.
102 *
103 * @param command command to send to the client
104 * @param args command arguments
105 * @return String response from client
106 */
107 protected String sendCommand(final Command command,
108 final List<String> args) {
109 log.entry(command, args);
110
111 var profiler = new Profiler(command.toString());
112 profiler.setLogger(log);
113
114 var arguments = new StringBuilder();
115 args.forEach(arg -> arguments.append(" '").append(arg).append("'"));
116
117 // Send the command
118 profiler.start("Send Command");
119 out.println(command + arguments.toString());
120 log.info("Sent Command: {}{}", command, arguments);
121
122 // Get the output
123 var response = "";
124 switch (command.getResponseType()) {
125 case PYON:
126 case STRING:
127 profiler.start("Get Response");
128 response = in.next().replaceFirst(COMMAND_PROMPT, "");
129 if (response.startsWith(PyonParser.PYON_1)) {
130 profiler.start("Convert to JSON");
131 response = PyonParser.convert(response);
132 } else if (response.equals("\n")) {
133 // TODO: remove dirty hack to stop ObjectMapper breaking
134 switch (command) {
135 case SIMULATION_INFO:
136 response = "{}";
137 break;
138 case SLOT_INFO:
139 response = "[]";
140 break;
141 default:
142 break;
143 }
144 }
145
146 default:
147 log.info("Recieved Response: {}", response);
148 profiler.stop().log();
149 }
150 return log.exit(response);
151 }
152 }