Search in sources :

Example 1 with WebSocketMessageModel

use of io.jpom.model.WebSocketMessageModel in project Jpom by dromara.

the class AgentWebSocketUpdateHandle method onMessage.

@OnMessage
public void onMessage(String message, Session session) throws Exception {
    WebSocketMessageModel model = WebSocketMessageModel.getInstance(message);
    switch(model.getCommand()) {
        case "getVersion":
            model.setData(JSONObject.toJSONString(JpomManifest.getInstance()));
            break;
        case "upload":
            AgentFileModel agentFileModel = ((JSONObject) model.getParams()).toJavaObject(AgentFileModel.class);
            UploadFileModel uploadFileModel = new UploadFileModel();
            uploadFileModel.setId(model.getNodeId());
            uploadFileModel.setName(agentFileModel.getName());
            uploadFileModel.setSize(agentFileModel.getSize());
            uploadFileModel.setVersion(agentFileModel.getVersion());
            uploadFileModel.setSavePath(AgentConfigBean.getInstance().getTempPath().getAbsolutePath());
            uploadFileModel.remove();
            UPLOAD_FILE_INFO.put(session.getId(), uploadFileModel);
            break;
        case "restart":
            model.setData(restart(session));
            break;
        default:
            break;
    }
    SocketSessionUtil.send(session, model.toString());
// session.sendMessage(new TextMessage(model.toString()));
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) UploadFileModel(io.jpom.model.data.UploadFileModel) WebSocketMessageModel(io.jpom.model.WebSocketMessageModel) AgentFileModel(io.jpom.model.AgentFileModel)

Example 2 with WebSocketMessageModel

use of io.jpom.model.WebSocketMessageModel in project Jpom by dromara.

the class AgentWebSocketUpdateHandle method onMessage.

@OnMessage
public void onMessage(byte[] message, Session session) throws Exception {
    UploadFileModel uploadFileModel = UPLOAD_FILE_INFO.get(session.getId());
    uploadFileModel.save(message);
    // 更新进度
    WebSocketMessageModel model = new WebSocketMessageModel("updateNode", uploadFileModel.getId());
    model.setData(uploadFileModel);
    SocketSessionUtil.send(session, model.toString());
// session.sendMessage(new TextMessage(model.toString()));
}
Also used : UploadFileModel(io.jpom.model.data.UploadFileModel) WebSocketMessageModel(io.jpom.model.WebSocketMessageModel)

Example 3 with WebSocketMessageModel

use of io.jpom.model.WebSocketMessageModel in project Jpom by dromara.

the class NodeUpdateHandler method updateNodeItemHttp.

private void updateNodeItemHttp(NodeModel nodeModel, WebSocketSession session, AgentFileModel agentFileModel) {
    File file = FileUtil.file(agentFileModel.getSavePath());
    JsonMessage<String> message = NodeForward.requestMultipart(nodeModel, "file", file, NodeUrl.SystemUploadJar);
    String id = nodeModel.getId();
    WebSocketMessageModel callbackRestartMessage = new WebSocketMessageModel("restart", id);
    callbackRestartMessage.setData(message.getMsg());
    this.sendMsg(callbackRestartMessage, session);
    // 先等待一会,太快可能还没重启
    ThreadUtil.sleep(5000L);
    int retryCount = 0;
    try {
        while (retryCount <= CHECK_COUNT) {
            ++retryCount;
            try {
                ThreadUtil.sleep(1000L);
                JsonMessage<Object> jsonMessage = NodeForward.requestBySys(nodeModel, NodeUrl.Info, "nodeId", id);
                if (jsonMessage.getCode() == HttpStatus.HTTP_OK) {
                    this.sendMsg(callbackRestartMessage.setData("重启完成"), session);
                    return;
                }
            } catch (Exception ignored) {
            }
        }
        this.sendMsg(callbackRestartMessage.setData("重连失败"), session);
    } catch (Exception e) {
        DefaultSystemLog.getLog().error("升级后重连插件端失败:" + id, e);
        this.sendMsg(callbackRestartMessage.setData("重连插件端失败"), session);
    }
}
Also used : WebSocketMessageModel(io.jpom.model.WebSocketMessageModel) JSONObject(com.alibaba.fastjson.JSONObject) File(java.io.File) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException)

Example 4 with WebSocketMessageModel

use of io.jpom.model.WebSocketMessageModel in project Jpom by dromara.

the class NodeUpdateHandler method handleTextMessage.

@Override
protected String handleTextMessage(Map<String, Object> attributes, WebSocketSession session, JSONObject json, ConsoleCommandOp consoleCommandOp) throws IOException {
    WebSocketMessageModel model = WebSocketMessageModel.getInstance(json.toString());
    String ids = null;
    String command = model.getCommand();
    switch(command) {
        case "getAgentVersion":
            model.setData(getAgentVersion());
            break;
        case "updateNode":
            super.logOpt(this.getClass(), attributes, json);
            updateNode(model, session);
            break;
        case "heart":
            break;
        default:
            {
                if (StrUtil.startWith(command, "getNodeList:")) {
                    ids = StrUtil.removePrefix(command, "getNodeList:");
                }
            }
            break;
    }
    if (StrUtil.isNotEmpty(ids)) {
        pullNodeList(session, ids);
    }
    if (model.getData() != null) {
        return model.toString();
    }
    return null;
}
Also used : WebSocketMessageModel(io.jpom.model.WebSocketMessageModel)

Example 5 with WebSocketMessageModel

use of io.jpom.model.WebSocketMessageModel in project Jpom by dromara.

the class NodeUpdateHandler method onError.

private void onError(WebSocketSession session, String msg) {
    WebSocketMessageModel error = new WebSocketMessageModel("onError", "");
    error.setData(msg);
    this.sendMsg(error, session);
}
Also used : WebSocketMessageModel(io.jpom.model.WebSocketMessageModel)

Aggregations

WebSocketMessageModel (io.jpom.model.WebSocketMessageModel)7 JSONObject (com.alibaba.fastjson.JSONObject)2 UploadFileModel (io.jpom.model.data.UploadFileModel)2 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 AgentFileModel (io.jpom.model.AgentFileModel)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1