Files
VisualSapfor/src/_VisualDVM/Repository/Component/Visualizer_2.java

134 lines
4.4 KiB
Java
Raw Normal View History

2024-10-09 22:21:57 +03:00
package _VisualDVM.Repository.Component;
2024-10-11 00:00:30 +03:00
import Common.Utils.Utils_;
import Common.Visual.UI_;
import _VisualDVM.Global;
2024-10-09 22:01:19 +03:00
import _VisualDVM.Utils;
2024-10-10 23:57:36 +03:00
import Common.Passes.PassException;
2023-09-17 22:13:42 +03:00
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.file.Paths;
public class Visualizer_2 extends OSDComponent {
//</editor-fold>
//-
//<editor-fold desc="функционал">
int port;
public String PID = "";
Socket client = null;
PrintWriter out = null;
BufferedReader in = null;
String request = "";
String response = "";
public Visualizer_2(int port_in) {
port = port_in;
}
public static void UnpackVersionInfo(Component component, String packed) {
String[] data = packed.split("\\|");
//лишний пробел.
String text = data[0].substring(0, data[0].length() - 1);
component.date_text = data[1] + data[2] + data[3];
component.version = Long.parseLong(text);
}
//<editor-fold desc="компонент">
@Override
public ComponentType getComponentType() {
return ComponentType.Visualizer_2;
}
@Override
public String getHome() {
2024-10-11 00:00:30 +03:00
return Utils_.getHomePath();
2023-09-17 22:13:42 +03:00
}
@Override
public void GetVersionInfo() {
try {
Command("get_version: ");
UnpackVersionInfo(this, response);
} catch (Exception e) {
2024-10-11 00:00:30 +03:00
Utils_.MainLog.PrintException(e);
2023-09-17 22:13:42 +03:00
}
}
public void refreshPid(){
try {
// UI.Info("Getting Server PID...");
Command("get_pid: ");
PID = response;
// UI.Info("SERVER PID = "+Utils.Brackets(PID));
} catch (Exception e) {
2024-10-11 00:00:30 +03:00
Utils_.MainLog.PrintException(e);
2023-09-17 22:13:42 +03:00
}
}
@Override
public void Update() throws Exception {
super.Update();
SendRequest("update_server: ");
ReplaceOldFile();
2024-10-11 00:00:30 +03:00
UI_.Info("Сервер успешно обновлен.\n" +
2023-09-17 22:13:42 +03:00
"Визуализатор завершает работу.\n" +
"Для продолжения перезапустите визуализатор вручную.");
System.exit(0);
}
@Override
public String getAssemblyCommand() {
return "cd Repo/sapfor/experts/Sapfor_2017/_src/Server\n" +
"g++ -O3 -std=c++17 checkUniq.cpp server.cpp -o Visualizer_2 -lpthread -lstdc++fs\n";
}
@Override
public File getAssemblyFile() {
return Paths.get(
Global.RepoDirectory.getAbsolutePath(),
"sapfor/experts/Sapfor_2017/_src/Server/Visualizer_2").toFile();
}
public void Connect() throws Exception {
ClearLog();
Print("соединение с Visualiser_2...");
client = Utils.createClientSocket(
InetAddress.getLoopbackAddress(),
port, 0
);
in = new BufferedReader(new InputStreamReader(client.getInputStream())); //то что нам приходит от сервера
out = new PrintWriter(client.getOutputStream(), true); //то что мы пишем серверу.
}
public void Interrupt(){
Utils.Kill(PID, false);
}
public void Shutdown() throws Exception {
Print("завершение сеанса с Visualiser_2...");
SendRequest("close: ");
if (client != null)
client.close();
if (in != null)
in.close();
if (out != null)
out.close();
}
//запрос.
public void SendRequest(String request_in) throws Exception {
Print("->" + request_in);
out.flush();
System.gc();
out.println(request = request_in);
}
//запрос-ответ. анализируются только общие ошибочные случаи.
public String Command(String request_in) throws Exception {
SendRequest(request_in);
response = in.readLine();
switch (response) {
case "NOT_FOUND":
case "WRONG":
case "SEG_FAULT":
2024-10-11 00:00:30 +03:00
throw new PassException("Команда серверу SAPFOR вернула " + Utils_.Brackets(response));
2023-09-17 22:13:42 +03:00
default:
break;
}
Print("<-" + response);
out.flush();
System.gc();
return response;
}
//</editor-fold>
}