package com.inspect.tcpserver.controller;
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.inspect.tcpserver.domain.AjaxResult;
|
|
import com.inspect.tcpserver.tcp.NettyClient;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.MDC;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import javax.annotation.Resource;
|
|
import java.util.UUID;
|
|
|
|
@RestController
|
|
@RequestMapping("/client")
|
|
public class ClientController {
|
|
|
|
private Logger logger = LoggerFactory.getLogger(ClientController.class);
|
|
|
|
@Resource
|
|
NettyClient nettyClient;
|
|
|
|
@GetMapping("gray")
|
|
public String gray() {
|
|
|
|
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
|
|
MDC.put("traceId", uuid);
|
|
|
|
logger.info("这是一个来自springboot,logback的info级别的日志");
|
|
logger.warn("这是一条来自springboot,logback的warn级别的日志");
|
|
logger.warn("这是一条来自springboot,logback的warn级别的日志2");
|
|
logger.error("这是一条来自springboot,logback的error级别的日志", new Exception("系统错误"));
|
|
return "success";
|
|
}
|
|
|
|
/**
|
|
* 调用客户端发送消息
|
|
*/
|
|
@PostMapping("sendMsg")
|
|
public AjaxResult sendMsg(@RequestBody String msg) {
|
|
try {
|
|
|
|
if (StringUtils.isBlank(msg)) {
|
|
return AjaxResult.fail("500", "发送消息msg为空");
|
|
}
|
|
|
|
// 此处只是为了判断传入的格式是否正确
|
|
JSONObject jsonObject = JSONObject.parseObject(msg);
|
|
|
|
if (null == jsonObject) {
|
|
return AjaxResult.fail("500", "发送消息json对象为空");
|
|
}
|
|
|
|
logger.info("巡视主机客户端接收到消息,发送到上级。{}", msg);
|
|
msg = msg.replaceAll("sendCode", "SendCode");
|
|
msg = msg.replaceAll("receiveCode", "ReceiveCode");
|
|
msg = msg.replaceAll("type", "Type");
|
|
nettyClient.sendJsonMessage(msg);
|
|
|
|
return AjaxResult.success();
|
|
} catch (Exception e) {
|
|
logger.error("客户端发送消息捕获异常", e);
|
|
return AjaxResult.fail(500, "数据格式不正确");
|
|
}
|
|
}
|
|
|
|
}
|