Merge branch 'Develop_Branch' of
https://gitee.com/fjcode/LuckyFrameClient into Develop_Branch # Conflicts: # src/main/java/luckyclient/netty/ClientHandler.java
This commit is contained in:
commit
f82667d909
|
@ -1,68 +1,68 @@
|
|||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<style type="text/css">
|
||||
body {
|
||||
margin:0;
|
||||
padding:0;
|
||||
font-family:Arial, Helvetica, sans-serif;
|
||||
font-size:12px;
|
||||
color:#6a6a6a;
|
||||
}
|
||||
a {
|
||||
font-size:13px
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="780" border="0" align="center" cellpadding="0" cellspacing="0" >
|
||||
<tr>
|
||||
<td height="10"> </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td valign="top" style="border-left:1px solid #CCC; border-right:1px solid #CCC;border-top:1px solid #CCC;">
|
||||
<table width="620" border="0" align="center" cellpadding="0" cellspacing="0">
|
||||
<tr>
|
||||
<td height="92" style="background-color:rgba(51,204,255,0.6);">
|
||||
<a href="http://www.luckyframe.cn" title="LuckyFrame" target="_blank"><img src="http://${webip}:${webport}/img/maillogo.png" alt="LuckyFrame" width="300" height="92" border="0" /></a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td height="1" colspan="2"><hr style="border-bottom:5px solid #f1f1f1; display:block;" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td height="20" colspan="2"> </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td height="40" colspan="2" style="font-size:12px; text-indent:25px;"><div style="margin: 0px auto; padding: 0px 10px; width: 680px;">
|
||||
<div style="color: rgb(77, 77, 77); line-height: 1.5; font-size: 14px; margin-bottom: 25px;">
|
||||
<strong style="margin-bottom: 15px; display: block;">亲爱的Tester: 您好!以下是自动化任务【${jobname}】执行情况。</strong>
|
||||
<p><b>自动构建状态: </b> 【<font color='#5CACEE'>${buildstatus}</font>】</p>
|
||||
<p><b>自动重启TOMCAT状态: </b> 【<font color='#5CACEE'>${restartstatus}</font>】</p>
|
||||
<br>
|
||||
<p><b>本次任务预期执行用例共【<font color='#2828FF'>${casecount}</font>】条,耗時【${time}】</b></p>
|
||||
<p><b>用例执行成功: </b> 【<font color='#28FF28'>${casesuc}</font>】</p>
|
||||
<p><b>用例执行失败: </b> 【<font color='#FF0000'>${casefail}</font>】</p>
|
||||
<p><b>用例有可能由于脚本原因未成功解析被锁定:</b> 【<font color='#AE57A4'>${caselock}</font>】</p>
|
||||
<p><b>用例由于长时间未收到接口Response未执行完成:</b> 【<font color='#FFAD86'>${caseunex}</font>】</p>
|
||||
<p> </p>
|
||||
<p> 此为自动化平台LuckyFrame的系统邮件,请勿回复</p>
|
||||
<p> 请及时前往<a href='http://${webip}:${webport}'>LuckyFrame平台</a>查看您的任务执行的更多细节</p>
|
||||
<p>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td height="10" colspan="2"> </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td height="40" colspan="2"> </td>
|
||||
</tr>
|
||||
</table></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td height="30" align="center" bgcolor="#E3E3E3" style="font-size:12px; color:#666;">Copyright © <a href='http://www.luckyframe.cn'>LuckyFrame</a>
|
||||
2017-2099 All Right Reserved </td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<style type="text/css">
|
||||
body {
|
||||
margin:0;
|
||||
padding:0;
|
||||
font-family:Arial, Helvetica, sans-serif;
|
||||
font-size:12px;
|
||||
color:#6a6a6a;
|
||||
}
|
||||
a {
|
||||
font-size:13px
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<table width="780" border="0" align="center" cellpadding="0" cellspacing="0" >
|
||||
<tr>
|
||||
<td height="10"> </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td valign="top" style="border-left:1px solid #CCC; border-right:1px solid #CCC;border-top:1px solid #CCC;">
|
||||
<table width="620" border="0" align="center" cellpadding="0" cellspacing="0">
|
||||
<tr>
|
||||
<td height="92" style="background-color:rgba(51,204,255,0.6);">
|
||||
<a href="http://www.luckyframe.cn" title="LuckyFrame" target="_blank"><img src="http://${webip}:${webport}/img/maillogo.png" alt="LuckyFrame" width="300" height="92" border="0" /></a></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td height="1" colspan="2"><hr style="border-bottom:5px solid #f1f1f1; display:block;" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td height="20" colspan="2"> </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td height="40" colspan="2" style="font-size:12px; text-indent:25px;"><div style="margin: 0px auto; padding: 0px 10px; width: 680px;">
|
||||
<div style="color: rgb(77, 77, 77); line-height: 1.5; font-size: 14px; margin-bottom: 25px;">
|
||||
<strong style="margin-bottom: 15px; display: block;">亲爱的Tester: 您好!以下是自动化任务【${jobname}】执行情况。</strong>
|
||||
<p><b>自动构建状态: </b> 【<font color='#5CACEE'>${buildstatus}</font>】</p>
|
||||
<p><b>自动重启TOMCAT状态: </b> 【<font color='#5CACEE'>${restartstatus}</font>】</p>
|
||||
<br>
|
||||
<p><b>本次任务预期执行用例共【<font color='#2828FF'>${casecount}</font>】条,耗時【${time}】</b></p>
|
||||
<p><b>用例执行成功: </b> 【<font color='#28FF28'>${casesuc}</font>】</p>
|
||||
<p><b>用例执行失败: </b> 【<font color='#FF0000'>${casefail}</font>】</p>
|
||||
<p><b>用例有可能由于脚本原因未成功解析被锁定:</b> 【<font color='#AE57A4'>${caselock}</font>】</p>
|
||||
<p><b>用例由于长时间未收到接口Response未执行完成:</b> 【<font color='#FFAD86'>${caseunex}</font>】</p>
|
||||
<p> </p>
|
||||
<p> 此为自动化平台LuckyFrame的系统邮件,请勿回复</p>
|
||||
<p> 请及时前往<a href='http://${webip}:${webport}'>LuckyFrame平台</a>查看您的任务执行的更多细节</p>
|
||||
<p>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td height="10" colspan="2"> </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td height="40" colspan="2"> </td>
|
||||
</tr>
|
||||
</table></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td height="30" align="center" bgcolor="#E3E3E3" style="font-size:12px; color:#666;">Copyright © <a href='http://www.luckyframe.cn'>LuckyFrame</a>
|
||||
2017-2099 All Right Reserved </td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -1,268 +1,264 @@
|
|||
package luckyclient.netty;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.support.PropertiesLoaderUtils;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.ChannelHandlerAdapter;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.EventLoop;
|
||||
import io.netty.handler.timeout.IdleState;
|
||||
import io.netty.handler.timeout.IdleStateEvent;
|
||||
import luckyclient.utils.config.SysConfig;
|
||||
|
||||
|
||||
public class ClientHandler extends ChannelHandlerAdapter {
|
||||
|
||||
//从application.properties文件中获取用到的参数;
|
||||
private static Resource resource = new ClassPathResource("application.properties");
|
||||
private static Properties props;
|
||||
|
||||
static {
|
||||
try {
|
||||
props = PropertiesLoaderUtils.loadProperties(resource);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static String port = props.getProperty("server.port");
|
||||
|
||||
static final String ECHO_REQ = "$_";
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ClientHandler.class);
|
||||
|
||||
private static final String HOST_NAME= SysConfig.getConfiguration().getProperty("host.name");
|
||||
|
||||
private static final String CLIENT_VERSION= SysConfig.getConfiguration().getProperty("client.verison");
|
||||
|
||||
private static final String SERVER_IP= SysConfig.getConfiguration().getProperty("server.web.ip");
|
||||
|
||||
private static final String SERVER_PORT= SysConfig.getConfiguration().getProperty("server.web.port");
|
||||
|
||||
private int byteRead;
|
||||
|
||||
private volatile int start = 0;
|
||||
|
||||
private volatile int lastLength = 0;
|
||||
|
||||
public RandomAccessFile randomAccessFile;
|
||||
|
||||
|
||||
private static ChannelHandlerContext ctx;
|
||||
|
||||
public ClientHandler() throws IOException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws UnsupportedEncodingException, InterruptedException {
|
||||
//服务端消息处理,如果接收到测试任务方法,则直接产生一个http请求并发送请求到本地
|
||||
String jsonStr=new String(msg.toString().getBytes(),"UTF-8");
|
||||
JSONObject json=new JSONObject();
|
||||
try
|
||||
{
|
||||
json= JSON.parseObject(jsonStr);
|
||||
|
||||
}catch (Exception e)
|
||||
{
|
||||
System.out.println("收到服务端非Json消息:"+jsonStr);
|
||||
return;
|
||||
}
|
||||
System.out.println("收到服务端消息:"+json.toString());
|
||||
//解析消息
|
||||
if("run".equals(json.get("method"))){
|
||||
try {
|
||||
//返回请求
|
||||
JSONObject re=new JSONObject();
|
||||
re.put("method","return");
|
||||
Result result=new Result(1,"同步等待消息返回",json.get("uuid").toString(),null);
|
||||
//如果是调度请求,则发起一个HTTP请求
|
||||
//获取是get方法还是post方法
|
||||
String getOrPost=json.get("getOrPost").toString();
|
||||
String urlParam="http://127.0.0.1:"+port+"/"+json.get("url").toString();
|
||||
Integer socketTimeout=Integer.valueOf(json.get("socketTimeout").toString());
|
||||
String tmpResult="";
|
||||
if("get".equals(getOrPost))
|
||||
{
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String,Object> jsonparams = (Map<String,Object>)json.get("data");
|
||||
//get方法
|
||||
try {
|
||||
tmpResult=HttpRequest.httpClientGet(urlParam,jsonparams,socketTimeout);
|
||||
} catch (Exception e) {
|
||||
System.out.println("转发服务端GET请求出错");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
String jsonparams=json.get("data").toString();
|
||||
//post方法
|
||||
try {
|
||||
tmpResult=HttpRequest.httpClientPost(urlParam,jsonparams,socketTimeout);
|
||||
} catch (Exception e) {
|
||||
System.out.println("转发服务端POST请求出错");
|
||||
}
|
||||
}
|
||||
result.setMessage(tmpResult);
|
||||
re.put("data",result);
|
||||
sendMessage(re.toString());
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
else if("download".equals(json.get("method"))){
|
||||
String loadpath=json.get("path").toString();
|
||||
String path = System.getProperty("user.dir")+loadpath;
|
||||
String fileName=json.get("fileName").toString();
|
||||
//发出http请求下载文件
|
||||
try {
|
||||
HttpRequest.downLoadFromUrl("http://"+SERVER_IP+":"+SERVER_PORT+"/common/download?fileName="+fileName,fileName,path);
|
||||
//返回请求
|
||||
JSONObject re=new JSONObject();
|
||||
Result result=new Result(1,"上传成功",json.get("uuid").toString(),null);
|
||||
re.put("method","return");
|
||||
re.put("data",result);
|
||||
sendMessage(re.toString());
|
||||
System.out.println("下载驱动包成功,路径为:"+path+";文件名为:"+fileName);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
//返回请求
|
||||
JSONObject re=new JSONObject();
|
||||
Result result=new Result(0,"上传失败",json.get("uuid").toString(),null);
|
||||
re.put("method","return");
|
||||
re.put("data",result);
|
||||
sendMessage(re.toString());
|
||||
System.out.println("下载驱动包失败,路径为:"+path+";文件名为:"+fileName);
|
||||
}
|
||||
}
|
||||
else if("upload".equals(json.get("method")))
|
||||
{
|
||||
//上传截图到服务器
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String,Object> jsonparams = (Map<String,Object>)json.get("data");
|
||||
String fileName=jsonparams.get("imgName").toString();
|
||||
String ctxPath = System.getProperty("user.dir")+File.separator+"log"+File.separator+"ScreenShot"+File.separator+fileName;
|
||||
File file=new File(ctxPath);
|
||||
start=Integer.valueOf(json.get("start").toString());
|
||||
FileUploadFile fileUploadFile=new FileUploadFile();
|
||||
fileUploadFile.setFile(file);
|
||||
if (start != -1) {
|
||||
try {
|
||||
if(start==0)
|
||||
lastLength = 1024 * 10;
|
||||
randomAccessFile = new RandomAccessFile(fileUploadFile.getFile(), "r");
|
||||
randomAccessFile.seek(start); //将文件定位到start
|
||||
System.out.println("长度:" + (randomAccessFile.length() - start));
|
||||
int a = (int) (randomAccessFile.length() - start);
|
||||
int b = (int) (randomAccessFile.length() / 1024 * 2);
|
||||
if (a < lastLength) {
|
||||
lastLength = a;
|
||||
}
|
||||
System.out.println("文件长度:" + (randomAccessFile.length()) + ",start:" + start + ",a:" + a + ",b:" + b + ",lastLength:" + lastLength);
|
||||
byte[] bytes = new byte[lastLength];
|
||||
System.out.println("bytes的长度是="+bytes.length);
|
||||
if ((byteRead = randomAccessFile.read(bytes)) != -1 && (randomAccessFile.length() - start) > 0) {
|
||||
System.out.println("byteRead = " + byteRead);
|
||||
fileUploadFile.setEndPos(byteRead);
|
||||
fileUploadFile.setBytes(bytes);
|
||||
//返回请求
|
||||
JSONObject re=new JSONObject();
|
||||
Result result=new Result(1,"上传成功",json.get("uuid").toString(),fileUploadFile);
|
||||
re.put("method","upload");
|
||||
re.put("data",result);
|
||||
re.put("uuid",json.get("uuid").toString());
|
||||
re.put("imgName",fileName);
|
||||
sendMessage(re.toString());
|
||||
try {
|
||||
ctx.writeAndFlush(fileUploadFile);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
randomAccessFile.close();
|
||||
System.out.println("文件已经读完channelRead()--------" + byteRead);
|
||||
//返回请求
|
||||
JSONObject re=new JSONObject();
|
||||
Result result=new Result(1,"上传成功",json.get("uuid").toString(),null);
|
||||
re.put("method","return");
|
||||
re.put("data",result);
|
||||
sendMessage(re.toString());
|
||||
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
//发送客户端登录消息
|
||||
JSONObject json=new JSONObject();
|
||||
json.put("hostName",HOST_NAME);
|
||||
json.put("version",CLIENT_VERSION);
|
||||
json.put("method","clientUp");
|
||||
ClientHandler.ctx=ctx;
|
||||
sendMessage(json.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx)
|
||||
{
|
||||
System.out.println("连接已断开,正在尝试重连...");
|
||||
//使用过程中断线重连
|
||||
final EventLoop eventLoop = ctx.channel().eventLoop();
|
||||
eventLoop.schedule(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
NettyClient.start();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}, 1, TimeUnit.SECONDS);
|
||||
|
||||
ctx.fireChannelInactive();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
||||
cause.printStackTrace();
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void userEventTriggered(ChannelHandlerContext ctx, Object evt)
|
||||
throws Exception {
|
||||
if (evt instanceof IdleStateEvent) {
|
||||
IdleStateEvent event = (IdleStateEvent) evt;
|
||||
if (event.state().equals(IdleState.READER_IDLE)) {
|
||||
/**发送心跳,保持长连接*/
|
||||
JSONObject json=new JSONObject();
|
||||
json.put("method","ping");
|
||||
json.put("hostName",HOST_NAME);
|
||||
ctx.channel().writeAndFlush(json.toString()+"$_").sync();
|
||||
//log.info("心跳发送成功!");
|
||||
}
|
||||
}
|
||||
super.userEventTriggered(ctx, evt);
|
||||
}
|
||||
|
||||
|
||||
public static void sendMessage(String json) throws InterruptedException {
|
||||
ctx.channel().writeAndFlush(Unpooled.copiedBuffer((json+"$_").getBytes()));
|
||||
}
|
||||
package luckyclient.netty;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.support.PropertiesLoaderUtils;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.ChannelHandlerAdapter;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.EventLoop;
|
||||
import io.netty.handler.timeout.IdleState;
|
||||
import io.netty.handler.timeout.IdleStateEvent;
|
||||
import luckyclient.utils.config.SysConfig;
|
||||
|
||||
|
||||
public class ClientHandler extends ChannelHandlerAdapter {
|
||||
|
||||
//从application.properties文件中获取用到的参数;
|
||||
private static Resource resource = new ClassPathResource("application.properties");
|
||||
private static Properties props;
|
||||
|
||||
static {
|
||||
try {
|
||||
props = PropertiesLoaderUtils.loadProperties(resource);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static String port = props.getProperty("server.port");
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ClientHandler.class);
|
||||
|
||||
private static final String HOST_NAME = SysConfig.getConfiguration().getProperty("host.name");
|
||||
|
||||
private static final String CLIENT_VERSION = SysConfig.getConfiguration().getProperty("client.verison");
|
||||
|
||||
private static final String SERVER_IP = SysConfig.getConfiguration().getProperty("server.web.ip");
|
||||
|
||||
private static final String SERVER_PORT = SysConfig.getConfiguration().getProperty("server.web.port");
|
||||
|
||||
private int byteRead;
|
||||
|
||||
|
||||
private volatile int lastLength = 0;
|
||||
|
||||
public RandomAccessFile randomAccessFile;
|
||||
|
||||
|
||||
private static ChannelHandlerContext ctx;
|
||||
|
||||
public ClientHandler() throws IOException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws UnsupportedEncodingException, InterruptedException {
|
||||
//服务端消息处理,如果接收到测试任务方法,则直接产生一个http请求并发送请求到本地
|
||||
String jsonStr = new String(msg.toString().getBytes(), "UTF-8");
|
||||
JSONObject json = new JSONObject();
|
||||
try {
|
||||
json = JSON.parseObject(jsonStr);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("收到服务端非Json消息,但是异常:" + jsonStr);
|
||||
return;
|
||||
}
|
||||
log.info("收到服务端消息:" + json.toString());
|
||||
//解析消息
|
||||
if ("run".equals(json.get("method"))) {
|
||||
try {
|
||||
//返回请求
|
||||
JSONObject re = new JSONObject();
|
||||
re.put("method", "return");
|
||||
Result result = new Result(1, "同步等待消息返回", json.get("uuid").toString(), null);
|
||||
//如果是调度请求,则发起一个HTTP请求
|
||||
//获取是get方法还是post方法
|
||||
String getOrPost = json.get("getOrPost").toString();
|
||||
String urlParam = "http://127.0.0.1:" + port + "/" + json.get("url").toString();
|
||||
Integer socketTimeout = Integer.valueOf(json.get("socketTimeout").toString());
|
||||
String tmpResult = "";
|
||||
if ("get".equals(getOrPost)) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> jsonparams = (Map<String, Object>) json.get("data");
|
||||
//get方法
|
||||
try {
|
||||
tmpResult = HttpRequest.httpClientGet(urlParam, jsonparams, socketTimeout);
|
||||
} catch (Exception e) {
|
||||
log.error("转发服务端GET请求出错");
|
||||
}
|
||||
} else {
|
||||
String jsonparams = json.get("data").toString();
|
||||
//post方法
|
||||
try {
|
||||
tmpResult = HttpRequest.httpClientPost(urlParam, jsonparams, socketTimeout);
|
||||
} catch (Exception e) {
|
||||
log.error("转发服务端POST请求出错");
|
||||
}
|
||||
}
|
||||
result.setMessage(tmpResult);
|
||||
re.put("data", result);
|
||||
sendMessage(re.toString());
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else if ("download".equals(json.get("method"))) {
|
||||
String loadpath = json.get("path").toString();
|
||||
String path = System.getProperty("user.dir") + loadpath;
|
||||
String fileName = json.get("fileName").toString();
|
||||
//发出http请求下载文件
|
||||
try {
|
||||
HttpRequest.downLoadFromUrl("http://" + SERVER_IP + ":" + SERVER_PORT + "/common/download?fileName=" + fileName, fileName, path);
|
||||
//返回请求
|
||||
JSONObject re = new JSONObject();
|
||||
Result result = new Result(1, "上传成功", json.get("uuid").toString(), null);
|
||||
re.put("method", "return");
|
||||
re.put("data", result);
|
||||
sendMessage(re.toString());
|
||||
log.info("下载驱动包成功,路径为:" + path + ";文件名为:" + fileName);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
//返回请求
|
||||
JSONObject re = new JSONObject();
|
||||
Result result = new Result(0, "上传失败", json.get("uuid").toString(), null);
|
||||
re.put("method", "return");
|
||||
re.put("data", result);
|
||||
sendMessage(re.toString());
|
||||
log.error("下载驱动包失败,路径为:" + path + ";文件名为:" + fileName);
|
||||
}
|
||||
} else if ("upload".equals(json.get("method"))) {
|
||||
try {
|
||||
//上传截图到服务器
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> jsonparams = (Map<String, Object>) json.get("data");
|
||||
String fileName = jsonparams.get("imgName").toString();
|
||||
String ctxPath = System.getProperty("user.dir") + File.separator + "log" + File.separator + "ScreenShot" + File.separator + fileName;
|
||||
File file = new File(ctxPath);
|
||||
int start = Integer.valueOf(json.get("start").toString());
|
||||
FileUploadFile fileUploadFile = new FileUploadFile();
|
||||
fileUploadFile.setFile(file);
|
||||
if (start != -1) {
|
||||
if (start == 0)
|
||||
lastLength = 1024 * 10;
|
||||
randomAccessFile = new RandomAccessFile(fileUploadFile.getFile(), "r");
|
||||
randomAccessFile.seek(start); //将文件定位到start
|
||||
log.info("长度:" + (randomAccessFile.length() - start));
|
||||
int a = (int) (randomAccessFile.length() - start);
|
||||
int b = (int) (randomAccessFile.length() / 1024 * 2);
|
||||
if (a < lastLength) {
|
||||
lastLength = a;
|
||||
}
|
||||
log.info("文件长度:" + (randomAccessFile.length()) + ",start:" + start + ",a:" + a + ",b:" + b + ",lastLength:" + lastLength);
|
||||
byte[] bytes = new byte[lastLength];
|
||||
log.info("bytes的长度是=" + bytes.length);
|
||||
if ((byteRead = randomAccessFile.read(bytes)) != -1 && (randomAccessFile.length() - start) > 0) {
|
||||
log.info("byteRead = " + byteRead);
|
||||
fileUploadFile.setEndPos(byteRead);
|
||||
fileUploadFile.setBytes(bytes);
|
||||
//返回请求
|
||||
JSONObject re = new JSONObject();
|
||||
Result result = new Result(1, "上传成功", json.get("uuid").toString(), fileUploadFile);
|
||||
re.put("method", "upload");
|
||||
re.put("data", result);
|
||||
re.put("uuid", json.get("uuid").toString());
|
||||
re.put("imgName", fileName);
|
||||
re.put("start", start);
|
||||
sendMessage(re.toString());
|
||||
try {
|
||||
ctx.writeAndFlush(fileUploadFile);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
randomAccessFile.close();
|
||||
log.info("文件已经读完channelRead()--------" + byteRead);
|
||||
//返回请求
|
||||
JSONObject re = new JSONObject();
|
||||
Result result = new Result(1, "上传成功", json.get("uuid").toString(), null);
|
||||
re.put("method", "return");
|
||||
re.put("data", result);
|
||||
sendMessage(re.toString());
|
||||
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
//返回请求
|
||||
JSONObject re = new JSONObject();
|
||||
Result result = new Result(0, "异常错误", json.get("uuid").toString(), null);
|
||||
re.put("method", "return");
|
||||
re.put("data", result);
|
||||
sendMessage(re.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||
//发送客户端登录消息
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("hostName", HOST_NAME);
|
||||
json.put("version", CLIENT_VERSION);
|
||||
json.put("method", "clientUp");
|
||||
ClientHandler.ctx = ctx;
|
||||
sendMessage(json.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) {
|
||||
log.info("连接已断开,正在尝试重连...");
|
||||
//使用过程中断线重连
|
||||
final EventLoop eventLoop = ctx.channel().eventLoop();
|
||||
eventLoop.schedule(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
NettyClient.start();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}, 1, TimeUnit.SECONDS);
|
||||
|
||||
ctx.fireChannelInactive();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
|
||||
cause.printStackTrace();
|
||||
ctx.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void userEventTriggered(ChannelHandlerContext ctx, Object evt)
|
||||
throws Exception {
|
||||
if (evt instanceof IdleStateEvent) {
|
||||
IdleStateEvent event = (IdleStateEvent) evt;
|
||||
if (event.state().equals(IdleState.READER_IDLE)) {
|
||||
/**发送心跳,保持长连接*/
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("method", "ping");
|
||||
json.put("hostName", HOST_NAME);
|
||||
//ctx.channel().writeAndFlush(json.toString() + "$_").sync();
|
||||
sendMessage(json.toString());
|
||||
//log.info("心跳发送成功!");
|
||||
}
|
||||
}
|
||||
super.userEventTriggered(ctx, evt);
|
||||
}
|
||||
|
||||
|
||||
public static void sendMessage(String json) throws InterruptedException {
|
||||
ctx.channel().writeAndFlush(Unpooled.copiedBuffer((json + "$_").getBytes()));
|
||||
}
|
||||
}
|
|
@ -1,93 +1,93 @@
|
|||
package luckyclient.netty;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelFuture;
|
||||
import io.netty.channel.ChannelFutureListener;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.ChannelOption;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import io.netty.channel.EventLoop;
|
||||
import io.netty.channel.EventLoopGroup;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
|
||||
import io.netty.handler.codec.string.StringDecoder;
|
||||
import io.netty.handler.codec.string.StringEncoder;
|
||||
import io.netty.handler.timeout.IdleStateHandler;
|
||||
import luckyclient.utils.config.SysConfig;
|
||||
|
||||
public class NettyClient {
|
||||
private static final String NETTY_SERVER_IP= SysConfig.getConfiguration().getProperty("server.web.ip");
|
||||
|
||||
private static final int NETTY_SERVER_PORT=Integer.parseInt(SysConfig.getConfiguration().getProperty("netty.server.port"));
|
||||
|
||||
protected static Channel channel;
|
||||
|
||||
private static ChannelFuture connect;
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(NettyClient.class);
|
||||
|
||||
private static ClientHandler clientHandler;
|
||||
public static void start() throws InterruptedException{
|
||||
EventLoopGroup group = new NioEventLoopGroup();
|
||||
try {
|
||||
Bootstrap b = new Bootstrap();
|
||||
clientHandler=new ClientHandler();
|
||||
b.group(group)
|
||||
.channel(NioSocketChannel.class)
|
||||
.option(ChannelOption.SO_KEEPALIVE,true)
|
||||
.handler(new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
public void initChannel(SocketChannel ch) throws Exception {
|
||||
ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
|
||||
ChannelPipeline p = ch.pipeline();
|
||||
p.addLast("decoder", new StringDecoder());
|
||||
p.addLast("encoder", new StringEncoder());
|
||||
p.addLast(new DelimiterBasedFrameDecoder(1024, delimiter));
|
||||
p.addLast(new IdleStateHandler(1,0,0,TimeUnit.SECONDS));
|
||||
p.addLast(clientHandler);
|
||||
}
|
||||
});
|
||||
//连接服务端
|
||||
connect = b.connect(NETTY_SERVER_IP, NETTY_SERVER_PORT);
|
||||
//断线重连
|
||||
connect.addListener(new ChannelFutureListener() {
|
||||
@Override
|
||||
public void operationComplete(ChannelFuture channelFuture) throws Exception {
|
||||
if (!channelFuture.isSuccess()) {
|
||||
final EventLoop loop = channelFuture.channel().eventLoop();
|
||||
loop.schedule(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
log.error("服务端链接不上,开始重连操作...");
|
||||
start();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}, 1L, TimeUnit.SECONDS);
|
||||
} else {
|
||||
channel = channelFuture.channel();
|
||||
log.info("服务端链接成功...");
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
//不关闭通道
|
||||
//group.shutdownGracefully();
|
||||
}
|
||||
}
|
||||
}
|
||||
package luckyclient.netty;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import io.netty.bootstrap.Bootstrap;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelFuture;
|
||||
import io.netty.channel.ChannelFutureListener;
|
||||
import io.netty.channel.ChannelInitializer;
|
||||
import io.netty.channel.ChannelOption;
|
||||
import io.netty.channel.ChannelPipeline;
|
||||
import io.netty.channel.EventLoop;
|
||||
import io.netty.channel.EventLoopGroup;
|
||||
import io.netty.channel.nio.NioEventLoopGroup;
|
||||
import io.netty.channel.socket.SocketChannel;
|
||||
import io.netty.channel.socket.nio.NioSocketChannel;
|
||||
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
|
||||
import io.netty.handler.codec.string.StringDecoder;
|
||||
import io.netty.handler.codec.string.StringEncoder;
|
||||
import io.netty.handler.timeout.IdleStateHandler;
|
||||
import luckyclient.utils.config.SysConfig;
|
||||
|
||||
public class NettyClient {
|
||||
private static final String NETTY_SERVER_IP= SysConfig.getConfiguration().getProperty("server.web.ip");
|
||||
|
||||
private static final int NETTY_SERVER_PORT=Integer.parseInt(SysConfig.getConfiguration().getProperty("netty.server.port"));
|
||||
|
||||
protected static Channel channel;
|
||||
|
||||
private static ChannelFuture connect;
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(NettyClient.class);
|
||||
|
||||
private static ClientHandler clientHandler;
|
||||
public static void start() throws InterruptedException{
|
||||
EventLoopGroup group = new NioEventLoopGroup();
|
||||
try {
|
||||
Bootstrap b = new Bootstrap();
|
||||
clientHandler=new ClientHandler();
|
||||
b.group(group)
|
||||
.channel(NioSocketChannel.class)
|
||||
.option(ChannelOption.SO_KEEPALIVE,true)
|
||||
.handler(new ChannelInitializer<SocketChannel>() {
|
||||
@Override
|
||||
public void initChannel(SocketChannel ch) throws Exception {
|
||||
ByteBuf delimiter = Unpooled.copiedBuffer("$_".getBytes());
|
||||
ChannelPipeline p = ch.pipeline();
|
||||
p.addLast(new DelimiterBasedFrameDecoder(1024, delimiter));
|
||||
p.addLast("decoder", new StringDecoder());
|
||||
p.addLast("encoder", new StringEncoder());
|
||||
p.addLast(new IdleStateHandler(1,0,0,TimeUnit.SECONDS));
|
||||
p.addLast(clientHandler);
|
||||
}
|
||||
});
|
||||
//连接服务端
|
||||
connect = b.connect(NETTY_SERVER_IP, NETTY_SERVER_PORT);
|
||||
//断线重连
|
||||
connect.addListener(new ChannelFutureListener() {
|
||||
@Override
|
||||
public void operationComplete(ChannelFuture channelFuture) throws Exception {
|
||||
if (!channelFuture.isSuccess()) {
|
||||
final EventLoop loop = channelFuture.channel().eventLoop();
|
||||
loop.schedule(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
log.error("服务端链接不上,开始重连操作...");
|
||||
start();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}, 1L, TimeUnit.SECONDS);
|
||||
} else {
|
||||
channel = channelFuture.channel();
|
||||
log.info("服务端链接成功...");
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
//不关闭通道
|
||||
//group.shutdownGracefully();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue