新增延迟测试,允许测试指定服务器之间或指定服务器到自定义公网ip的延迟
This commit is contained in:
parent
46c26c94e0
commit
bcc123f8dc
@ -26,6 +26,7 @@ public class AbstractMessage {
|
||||
public static final int SHARE_FILE_QUERY_MESSAGE = 73;
|
||||
public static final int CONFIG_MESSAGE = 80;
|
||||
public static final int CONNECT_MESSAGE = 90;
|
||||
public static final int DELAY_TEST_MESSAGE = 91;
|
||||
|
||||
public int messageType;
|
||||
public int messageId;
|
||||
|
||||
11
src/main/java/com/lion/snss/message/DelayTestMessage.java
Normal file
11
src/main/java/com/lion/snss/message/DelayTestMessage.java
Normal file
@ -0,0 +1,11 @@
|
||||
package com.lion.snss.message;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class DelayTestMessage extends AbstractMessage{
|
||||
{
|
||||
messageType = DELAY_TEST_MESSAGE;
|
||||
}
|
||||
String ip;
|
||||
}
|
||||
@ -56,6 +56,7 @@ public class MessageCodec extends ByteToMessageCodec<AbstractMessage> {
|
||||
case AbstractMessage.SHARE_FILE_QUERY_MESSAGE -> objectMapper.readValue(bytes, ShareFileQueryMessage.class);
|
||||
case AbstractMessage.CONFIG_MESSAGE -> objectMapper.readValue(bytes, ConfigMessage.class);
|
||||
case AbstractMessage.CONNECT_MESSAGE -> objectMapper.readValue(bytes, ConnectMessage.class);
|
||||
case AbstractMessage.DELAY_TEST_MESSAGE -> objectMapper.readValue(bytes, DelayTestMessage.class);
|
||||
default -> throw new IllegalStateException("Unexpected value: " + type);
|
||||
};
|
||||
|
||||
|
||||
@ -13,7 +13,7 @@ public class ResponseMessage extends AbstractMessage{
|
||||
boolean result;
|
||||
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
HashMap<String, String> data;
|
||||
HashMap<String, Object> data;
|
||||
|
||||
public ResponseMessage(){
|
||||
data = new HashMap<>();
|
||||
@ -24,25 +24,28 @@ public class ResponseMessage extends AbstractMessage{
|
||||
data = new HashMap<>();
|
||||
}
|
||||
|
||||
public void setDataAndResult(String data, boolean result){
|
||||
public ResponseMessage setDataAndResult(String data, boolean result){
|
||||
this.result = result;
|
||||
if(result){
|
||||
this.data.put("data", data);
|
||||
}else{
|
||||
this.data.put("cause", data);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public void put(String key, String value){
|
||||
public ResponseMessage put(String key, String value){
|
||||
data.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void put(String key, Object o){
|
||||
public ResponseMessage put(String key, Object o){
|
||||
data.put(key, String.valueOf(o));
|
||||
return this;
|
||||
}
|
||||
|
||||
public String get(String key){
|
||||
return data.get(key);
|
||||
return data.get(key).toString();
|
||||
}
|
||||
|
||||
public ResponseMessage success(String data) {
|
||||
|
||||
@ -27,6 +27,7 @@ import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
@ -447,6 +448,10 @@ public class CommunicateToMainService {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
case AbstractMessage.DELAY_TEST_MESSAGE -> {
|
||||
DelayTestMessage delayTestMessage = (DelayTestMessage) abstractMessage;
|
||||
ctx.channel().writeAndFlush(delayTestToIp(delayTestMessage));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1244,5 +1249,33 @@ public class CommunicateToMainService {
|
||||
dynamicConfigMessage.updateUserStorage(user);
|
||||
channelFuture.channel().writeAndFlush(dynamicConfigMessage);
|
||||
}
|
||||
|
||||
private ResponseMessage delayTestToIp(DelayTestMessage delayTestMessage) {
|
||||
ResponseMessage response = new ResponseMessage(delayTestMessage.getMessageId());
|
||||
long sum = 0;
|
||||
long[] data = new long[6];
|
||||
for(int i=0; i<5; i++){
|
||||
try {
|
||||
long start = System.currentTimeMillis();
|
||||
if(!Inet4Address.getByName(ip).isReachable(5000)) {
|
||||
data[i] = -1;
|
||||
continue;
|
||||
}
|
||||
long duration = System.currentTimeMillis() - start;
|
||||
if(duration == 0)
|
||||
duration = 1;
|
||||
data[i] = duration;
|
||||
sum += duration;
|
||||
} catch (IOException e) {
|
||||
response.failure("该ip无法连接或连接超时");
|
||||
return response;
|
||||
}
|
||||
}
|
||||
if(sum == 0)
|
||||
return response.failure("该ip无法连接或连接超时");
|
||||
else
|
||||
data[5] = sum / 5;
|
||||
return response.put("data", Arrays.toString(data)).success();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user