forked from fanruan/design
Browse Source
* commit '3838b06108bc132d2508c5bfb8023872cc9f7e40': check merge merge merge merge start designer socket client merge merge env..master
superman
7 years ago
15 changed files with 411 additions and 1573 deletions
@ -0,0 +1,88 @@
|
||||
package com.fr.design.mainframe.loghandler.socketio; |
||||
|
||||
import com.fr.base.env.EnvContext; |
||||
import com.fr.base.env.resource.LocalEnvConfig; |
||||
import com.fr.core.env.EnvConfig; |
||||
import com.fr.core.env.EnvConstants; |
||||
import com.fr.core.env.EnvEvents; |
||||
import com.fr.decision.webservice.utils.DecisionServiceConstants; |
||||
import com.fr.design.mainframe.loghandler.DesignerLogHandler; |
||||
import com.fr.event.Event; |
||||
import com.fr.event.EventDispatcher; |
||||
import com.fr.event.Listener; |
||||
import com.fr.event.Null; |
||||
import com.fr.general.LogRecordTime; |
||||
import com.fr.general.LogUtils; |
||||
import com.fr.log.FineLoggerFactory; |
||||
import com.fr.third.guava.base.Optional; |
||||
import com.fr.web.WebSocketConfig; |
||||
import io.socket.client.IO; |
||||
import io.socket.client.Socket; |
||||
import io.socket.emitter.Emitter; |
||||
|
||||
import java.io.ByteArrayInputStream; |
||||
import java.net.URI; |
||||
import java.net.URL; |
||||
|
||||
public class DesignerSocketIO { |
||||
|
||||
private static Optional<Socket> socketIO = Optional.absent(); |
||||
|
||||
private static final Emitter.Listener printLog = new Emitter.Listener() { |
||||
@Override |
||||
public void call(Object... objects) { |
||||
try { |
||||
LogRecordTime[] logRecordTimes = LogUtils.readXMLLogRecords(new ByteArrayInputStream((byte[]) objects[0])); |
||||
for (LogRecordTime logRecordTime : logRecordTimes) { |
||||
DesignerLogHandler.getInstance().printRemoteLog(logRecordTime); |
||||
} |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
} |
||||
}; |
||||
|
||||
static { |
||||
EventDispatcher.listen(EnvEvents.AFTER_SIGN_OUT, new Listener<Null>() { |
||||
@Override |
||||
public void on(Event event, Null param) { |
||||
if (socketIO.isPresent()) { |
||||
socketIO.get().close(); |
||||
socketIO = Optional.absent(); |
||||
} |
||||
} |
||||
}); |
||||
EventDispatcher.listen(EnvEvents.AFTER_SIGN_IN, new Listener<Null>() { |
||||
@Override |
||||
public void on(Event event, Null param) { |
||||
updateSocket(); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
public static void init() { |
||||
updateSocket(); |
||||
} |
||||
|
||||
private static void updateSocket() { |
||||
EnvConfig env = EnvContext.currentEnv(); |
||||
if (env instanceof LocalEnvConfig) { |
||||
return; |
||||
} |
||||
|
||||
try { |
||||
String uri = String.format("http://%s:%s%s?%s=%s", |
||||
new URL(env.getPath()).getHost(), |
||||
WebSocketConfig.getInstance().getPort(), |
||||
EnvConstants.WS_NAMESPACE, |
||||
DecisionServiceConstants.WEB_SOCKET_TOKEN_NAME, |
||||
EnvContext.currentToken()); |
||||
|
||||
socketIO = Optional.of(IO.socket(new URI(uri))); |
||||
socketIO.get().on(EnvConstants.WS_LOGRECORD, printLog); |
||||
socketIO.get().connect(); |
||||
} catch (Exception e) { |
||||
FineLoggerFactory.getLogger().error(e.getMessage(), e); |
||||
} |
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
@ -1,43 +1,61 @@
|
||||
package com.fr.env; |
||||
|
||||
|
||||
import com.fr.base.Env; |
||||
import com.fr.base.FRContext; |
||||
import com.fr.base.env.EnvContext; |
||||
import com.fr.base.env.resource.EnvConfigUtils; |
||||
import com.fr.base.env.resource.RemoteEnvConfig; |
||||
import com.fr.core.env.EnvConfig; |
||||
import com.fr.core.env.EnvEvents; |
||||
import com.fr.dav.LocalEnv; |
||||
import com.fr.design.utils.DesignUtils; |
||||
import com.fr.event.Event; |
||||
import com.fr.event.EventDispatcher; |
||||
import com.fr.event.Listener; |
||||
import com.fr.event.Null; |
||||
import com.fr.general.ComparatorUtils; |
||||
import com.fr.general.env.EnvContext; |
||||
import com.fr.general.Inter; |
||||
|
||||
import javax.swing.JOptionPane; |
||||
import javax.swing.UIManager; |
||||
|
||||
|
||||
public class SignIn { |
||||
public static Env lastSelectedEnv;// 记录最后登录的Env
|
||||
|
||||
static { |
||||
EventDispatcher.listen(EnvEvents.CONNECTION_ERROR, new Listener<Null>() { |
||||
@Override |
||||
public void on(Event event, Null param) { |
||||
if (JOptionPane.showConfirmDialog(null, Inter.getLocText("FR-Remote_Connect2Server_Again"), UIManager.getString("OptionPane.titleText"), JOptionPane.YES_NO_OPTION) |
||||
== JOptionPane.OK_OPTION) { |
||||
try { |
||||
EnvContext.signIn(EnvContext.currentEnv()); |
||||
} catch (Exception e) { |
||||
FRContext.getLogger().error(e.getMessage()); |
||||
} |
||||
} |
||||
} |
||||
}); |
||||
} |
||||
|
||||
/** |
||||
* 注册入环境 |
||||
* @param selectedEnv 选择的环境 |
||||
* @throws Exception 异常 |
||||
*/ |
||||
public static void signIn(Env selectedEnv) throws Exception { |
||||
boolean validServer; |
||||
signOutOldEnv(selectedEnv); |
||||
selectedEnv.signIn(); |
||||
validServer = true; |
||||
if (validServer) { |
||||
DesignUtils.switchToEnv(selectedEnv); |
||||
lastSelectedEnv = selectedEnv; |
||||
public static void signIn(EnvConfig selectedEnv) throws Exception { |
||||
if (EnvContext.currentEnv() != null && !ComparatorUtils.equals(EnvContext.currentEnv(), selectedEnv)) { |
||||
EnvContext.signOut(); |
||||
} |
||||
EnvContext.signIn(selectedEnv); |
||||
DesignUtils.switchToEnv(trans(selectedEnv)); |
||||
} |
||||
|
||||
private static void signOutOldEnv(Env newEnv) { |
||||
// 环境相同直接返回,避免浪费过多时间
|
||||
if (lastSelectedEnv == null || ComparatorUtils.equals(lastSelectedEnv, newEnv)) { |
||||
return; |
||||
} |
||||
try { |
||||
EnvContext.fireBeforeSignOut(); |
||||
lastSelectedEnv.signOut(); |
||||
EnvContext.fireAfterSignOut(); |
||||
} catch (Exception e) { |
||||
FRContext.getLogger().error(e.getMessage(), e); |
||||
private static com.fr.base.Env trans(EnvConfig env) { |
||||
if (env instanceof RemoteEnvConfig) { |
||||
return new RemoteEnv(env.getPath(), EnvConfigUtils.getUsername(env), EnvConfigUtils.getPassword(env)); |
||||
} else { |
||||
return new LocalEnv(); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue