Browse Source

回滚

feature/big-screen
Maksim 5 years ago
parent
commit
feeb948ff3
  1. 167
      designer-realize/src/main/java/com/fr/design/mainframe/socketio/DesignerSocketIO.java

167
designer-realize/src/main/java/com/fr/design/mainframe/socketio/DesignerSocketIO.java

@ -13,7 +13,6 @@ import com.fr.report.RemoteDesignConstants;
import com.fr.serialization.SerializerHelper; import com.fr.serialization.SerializerHelper;
import com.fr.stable.ArrayUtils; import com.fr.stable.ArrayUtils;
import com.fr.third.apache.log4j.spi.LoggingEvent; import com.fr.third.apache.log4j.spi.LoggingEvent;
import com.fr.third.guava.base.Optional;
import com.fr.workspace.WorkContext; import com.fr.workspace.WorkContext;
import com.fr.workspace.Workspace; import com.fr.workspace.Workspace;
import com.fr.workspace.base.WorkspaceConstants; import com.fr.workspace.base.WorkspaceConstants;
@ -36,28 +35,19 @@ public class DesignerSocketIO {
Disconnecting Disconnecting
} }
private static Optional<Socket> socketIO = Optional.absent(); private static Socket socket = null;
private static Status status = Status.Disconnected; private static Status status = Status.Disconnected;
//维护一个当前工作环境的uri列表
private static String[] uri;
//维护一个关于uri列表的计数器
private static int count;
private static final Emitter.Listener printLog = new Emitter.Listener() {
@Override
public void call(Object... objects) {
if (ArrayUtils.isNotEmpty(objects)) {
try {
LoggingEvent event = SerializerHelper.deserialize((byte[]) objects[0]);
DesignerLogger.log(event);
} catch (Exception e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
}
}
}
};
public static void close() { public static void close() {
if (socketIO.isPresent()) { if (socket != null) {
status = Status.Disconnecting; status = Status.Disconnecting;
socketIO.get().close(); socket.close();
socketIO = Optional.absent(); socket = null;
} }
} }
@ -66,57 +56,42 @@ public class DesignerSocketIO {
if (current.isLocal()) { if (current.isLocal()) {
return; return;
} }
//每当更换工作环境,更新uri列表,同时更新计数器count
try { try {
String[] uri = getSocketUri(current); uri = getSocketUri();
socketIO = Optional.of(IO.socket(new URI(uri[0]))); } catch (IOException e) {
socketIO.get().on(WorkspaceConstants.WS_LOGRECORD, printLog); FineLoggerFactory.getLogger().error(e.getMessage(), e);
socketIO.get().on(WorkspaceConstants.CONFIG_MODIFY, new Emitter.Listener() { }
@Override count = 0;
public void call(Object... objects) { //建立socket并注册监听
assert objects != null && objects.length == 1; createSocket();
String param = (String) objects[0]; }
EventDispatcher.fire(RemoteConfigEvent.EDIT, param);
} private static void createSocket(){
}); //根据uri和计数器建立连接,并注册监听
socketIO.get().on(Socket.EVENT_DISCONNECT, new Emitter.Listener() { try {
@Override if(count<uri.length) {
public void call(Object... objects) { socket = IO.socket(new URI(uri[count]));
/* socket.on(WorkspaceConstants.WS_LOGRECORD, printLog);
* todo 远程心跳断开不一定 socketio 断开 和远程紧密相关的业务都绑定在心跳上切换成心跳断开之后进行提醒 socket.on(WorkspaceConstants.CONFIG_MODIFY, modifyConfig);
* socketio 只用推日志和通知配置变更 socket.on(Socket.EVENT_CONNECT_ERROR, failRetry);
*/ socket.on(Socket.EVENT_DISCONNECT, disConnectHint);
if (status != Status.Disconnecting) { socket.connect();
try { status = Status.Connected;
UIUtil.invokeAndWaitIfNeeded(new Runnable() { }else {
@Override //表示所有的uri都连接不成功
public void run() { FineLoggerFactory.getLogger().warn("All uris failed to connect");
JOptionPane.showMessageDialog( }
DesignerContext.getDesignerFrame(),
Toolkit.i18nText("Fine-Design_Basic_Remote_Disconnected"),
UIManager.getString("OptionPane.messageDialogTitle"),
JOptionPane.ERROR_MESSAGE,
UIManager.getIcon("OptionPane.errorIcon"));
EnvChangeEntrance.getInstance().chooseEnv();
}
});
} catch (Exception e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
}
}
status = Status.Disconnected;
}
});
socketIO.get().connect();
status = Status.Connected;
} catch (Exception e) { } catch (Exception e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e); FineLoggerFactory.getLogger().error(e.getMessage(), e);
} }
} }
private static String[] getSocketUri(Workspace current) throws IOException { private static String[] getSocketUri() throws IOException {
Workspace current = WorkContext.getCurrent();
URL url = new URL(current.getPath()); URL url = new URL(current.getPath());
Integer[] ports = WorkContext.getCurrent().get(SocketInfoOperator.class).getPort(); Integer[] ports = current.get(SocketInfoOperator.class).getPort();
WorkspaceConnection connection = WorkContext.getCurrent().getConnection(); WorkspaceConnection connection = current.getConnection();
String[] result = new String[ports.length]; String[] result = new String[ports.length];
for (int i = 0; i < ports.length; i++) { for (int i = 0; i < ports.length; i++) {
result[i] = String.format("%s://%s:%s%s?%s=%s&%s=%s", result[i] = String.format("%s://%s:%s%s?%s=%s&%s=%s",
@ -130,6 +105,72 @@ public class DesignerSocketIO {
connection.getId()); connection.getId());
} }
return result; return result;
} }
//失败重试监听器:1、关闭失败的socket 2、计数器加1 3、调用创建socket方法
private static final Emitter.Listener failRetry = new Emitter.Listener() {
@Override
public void call(Object... args) {
status = Status.Disconnecting;
socket.close();
count++;
createSocket();
}
};
//日志输出监听器
private static final Emitter.Listener printLog = new Emitter.Listener() {
@Override
public void call(Object... objects) {
if (ArrayUtils.isNotEmpty(objects)) {
try {
LoggingEvent event = SerializerHelper.deserialize((byte[]) objects[0]);
DesignerLogger.log(event);
} catch (Exception e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
}
}
}
};
//断开连接提醒监听器
private static final Emitter.Listener disConnectHint = new Emitter.Listener() {
@Override
public void call(Object... objects) {
/*
* todo 远程心跳断开不一定 socket 断开 和远程紧密相关的业务都绑定在心跳上切换成心跳断开之后进行提醒
* socket 只用推日志和通知配置变更
*/
if (status != Status.Disconnecting) {
try {
UIUtil.invokeAndWaitIfNeeded(new Runnable() {
@Override
public void run() {
JOptionPane.showMessageDialog(
DesignerContext.getDesignerFrame(),
Toolkit.i18nText("Fine-Design_Basic_Remote_Disconnected"),
UIManager.getString("OptionPane.messageDialogTitle"),
JOptionPane.ERROR_MESSAGE,
UIManager.getIcon("OptionPane.errorIcon"));
EnvChangeEntrance.getInstance().chooseEnv();
}
});
} catch (Exception e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
}
}
status = Status.Disconnected;
}
};
//配置变更监听器
private static final Emitter.Listener modifyConfig = new Emitter.Listener() {
@Override
public void call(Object... objects) {
assert objects != null && objects.length == 1;
String param = (String) objects[0];
EventDispatcher.fire(RemoteConfigEvent.EDIT, param);
}
};
} }

Loading…
Cancel
Save