上传量下载量计算
Change-Id: I539eb93ad3fa457ab0e7966e3406e7c9c2e00bc5
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 188fe38..1f20988 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -6,6 +6,17 @@
<component name="ChangeListManager">
<list default="true" id="f008fe30-0711-42e2-bb33-17dcfdbad387" name="Changes" comment="pom">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
+ <change beforePath="$PROJECT_DIR$/src/main/java/com/example/myproject/MyProjectApplication.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/myproject/MyProjectApplication.java" afterDir="false" />
+ <change beforePath="$PROJECT_DIR$/src/main/java/com/example/myproject/controller/TorrentController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/myproject/controller/TorrentController.java" afterDir="false" />
+ <change beforePath="$PROJECT_DIR$/src/main/java/com/example/myproject/entity/TorrentReport.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/myproject/entity/TorrentReport.java" afterDir="false" />
+ <change beforePath="$PROJECT_DIR$/src/main/java/com/example/myproject/service/TorrentService.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/myproject/service/TorrentService.java" afterDir="false" />
+ <change beforePath="$PROJECT_DIR$/src/main/java/com/example/myproject/service/serviceImpl/TorrentServiceImpl.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/myproject/service/serviceImpl/TorrentServiceImpl.java" afterDir="false" />
+ <change beforePath="$PROJECT_DIR$/target/classes/com/example/myproject/MyProjectApplication.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/com/example/myproject/MyProjectApplication.class" afterDir="false" />
+ <change beforePath="$PROJECT_DIR$/target/classes/com/example/myproject/controller/TorrentController.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/com/example/myproject/controller/TorrentController.class" afterDir="false" />
+ <change beforePath="$PROJECT_DIR$/target/classes/com/example/myproject/entity/TorrentReport$TorrentReportBuilder.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/com/example/myproject/entity/TorrentReport$TorrentReportBuilder.class" afterDir="false" />
+ <change beforePath="$PROJECT_DIR$/target/classes/com/example/myproject/entity/TorrentReport.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/com/example/myproject/entity/TorrentReport.class" afterDir="false" />
+ <change beforePath="$PROJECT_DIR$/target/classes/com/example/myproject/service/TorrentService.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/com/example/myproject/service/TorrentService.class" afterDir="false" />
+ <change beforePath="$PROJECT_DIR$/target/classes/com/example/myproject/service/serviceImpl/TorrentServiceImpl.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/com/example/myproject/service/serviceImpl/TorrentServiceImpl.class" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -102,7 +113,7 @@
<option name="Make" enabled="true" />
</method>
</configuration>
- <configuration name="MyProjectApplication" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
+ <configuration name="MyProjectApplication" type="Application" factoryName="Application" temporary="true">
<option name="MAIN_CLASS_NAME" value="com.example.myproject.MyProjectApplication" />
<module name="echo-backend" />
<extension name="coverage">
@@ -115,17 +126,41 @@
<option name="Make" enabled="true" />
</method>
</configuration>
- <configuration name="TorrentControllerTest" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
+ <configuration name="MyProjectApplication" type="Application" factoryName="Application" temporary="true">
+ <option name="MAIN_CLASS_NAME" value="com.example.myproject.MyProjectApplication" />
<module name="echo-backend" />
<extension name="coverage">
<pattern>
- <option name="PATTERN" value="com.example.myproject.controller.*" />
+ <option name="PATTERN" value="com.example.myproject.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
- <option name="PACKAGE_NAME" value="com.example.myproject.controller" />
- <option name="MAIN_CLASS_NAME" value="com.example.myproject.controller.TorrentControllerTest" />
- <option name="TEST_OBJECT" value="class" />
+ <method v="2">
+ <option name="Make" enabled="true" />
+ </method>
+ </configuration>
+ <configuration name="MyProjectApplication" type="Application" factoryName="Application" temporary="true">
+ <option name="MAIN_CLASS_NAME" value="com.example.myproject.MyProjectApplication" />
+ <module name="echo-backend" />
+ <extension name="coverage">
+ <pattern>
+ <option name="PATTERN" value="com.example.myproject.*" />
+ <option name="ENABLED" value="true" />
+ </pattern>
+ </extension>
+ <method v="2">
+ <option name="Make" enabled="true" />
+ </method>
+ </configuration>
+ <configuration name="MyProjectApplication" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
+ <option name="MAIN_CLASS_NAME" value="com.example.myproject.MyProjectApplication" />
+ <module name="echo-backend" />
+ <extension name="coverage">
+ <pattern>
+ <option name="PATTERN" value="com.example.myproject.*" />
+ <option name="ENABLED" value="true" />
+ </pattern>
+ </extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
@@ -187,10 +222,10 @@
<recent_temporary>
<list>
<item itemvalue="应用程序.MyProjectApplication" />
+ <item itemvalue="应用程序.MyProjectApplication" />
+ <item itemvalue="应用程序.MyProjectApplication" />
+ <item itemvalue="应用程序.MyProjectApplication" />
<item itemvalue="JUnit.UserControllerTest.testUploadUserAvatar" />
- <item itemvalue="JUnit.UserControllerTest.testUpdateUserAvatar" />
- <item itemvalue="JUnit.UserControllerTest" />
- <item itemvalue="JUnit.TorrentControllerTest" />
</list>
</recent_temporary>
</component>
diff --git a/src/main/java/com/example/myproject/MyProjectApplication.java b/src/main/java/com/example/myproject/MyProjectApplication.java
index 417fa6d..2c5d86e 100644
--- a/src/main/java/com/example/myproject/MyProjectApplication.java
+++ b/src/main/java/com/example/myproject/MyProjectApplication.java
@@ -4,11 +4,12 @@
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
-
+@EnableAsync
@MapperScan("com.example.myproject.mapper") // 扫描 Mapper 接口
public class MyProjectApplication {
public static void main(String[] args) {
diff --git a/src/main/java/com/example/myproject/controller/TorrentController.java b/src/main/java/com/example/myproject/controller/TorrentController.java
index b0abc9c..4ce3eca 100644
--- a/src/main/java/com/example/myproject/controller/TorrentController.java
+++ b/src/main/java/com/example/myproject/controller/TorrentController.java
@@ -149,12 +149,9 @@
File dest = new File(uploadDir + fileName);
coverImage.transferTo(dest);
-
String imageUrl = request.getScheme() + "://" + request.getServerName() + ":" +
request.getServerPort() + "/uploads/torrents/" + fileName;
// String imageUrl ="/uploads/torrents/" + fileName;
-
-
param.setImageUrl(imageUrl);
}
@@ -266,7 +263,7 @@
* 用户的上传下载量是“促销”后的
* 作弊检测——下载速度检测
*/
- torrentService.processUploadDownload(Long.valueOf(userId),peerId, infoHash,torrentId,uploaded,downloaded);
+
TorrentReport report = TorrentReport.builder()
.userId(userId)
@@ -277,7 +274,7 @@
BeanUtils.copyProperties(trackerProtocol, report);
torrentReportRepository.save(report);
synchronized (this){
-
+ torrentService.processUploadDownload(Long.valueOf(userId),peerId, infoHash,torrentId,uploaded,downloaded);
boolean isAdded = peers.stream().anyMatch(m -> {
try{
Map<String, BEValue> map1 = m.getMap();
@@ -324,5 +321,73 @@
}
}
}
+//@GetMapping("/announce")
+//public ResponseEntity<byte[]> announce(TrackerProtocol trackerProtocol, HttpServletRequest request, @RequestParam(value = "info_hash") String encodedInfoHash){
+// HashMap<String, BEValue> map = new HashMap<>();
+// if (StrUtil.isBlank(trackerProtocol.getIp())) {
+// trackerProtocol.setIp(request.getRemoteAddr());
+// }
+// try {
+// byte[] bytes = URLDecoder.decode(request.getQueryString().split("info_hash=")[1].split("&")[0], StandardCharsets.ISO_8859_1).getBytes(StandardCharsets.ISO_8859_1);
+// String infoHash = TorrentUtils.byteArrayToHexString(bytes);
+// trackerProtocol.setInfo_hash(infoHash);
+// TorrentEntity torrent = torrentService.selectByInfoHash(infoHash);
+// if (torrent == null) {
+// throw new RuntimeException("种子不存在");
+// }
+// Integer userId = trackerProtocol.getUserId();
+// TorrentReport report = TorrentReport.builder()
+// .userId(userId )
+// .torrentId(torrent.getId())
+// .peerId(trackerProtocol.getPeer_id())
+// .infoHash(infoHash)
+// .build();
+// BeanUtils.copyProperties(trackerProtocol, report);
+// torrentReportRepository.save(report);
+// synchronized (this){
+// boolean isAdded = peers.stream().anyMatch(m -> {
+// try{
+// Map<String, BEValue> map1 = m.getMap();
+// return map1.get("ip").getString().equals(trackerProtocol.getIp()) && map1.get("port").getInt() == trackerProtocol.getPort();
+// }catch (Exception e){
+// return false;
+// }
+// });
+// log.info("client report: {}", trackerProtocol);
+// map.put("interval", new BEValue(30));
+// map.put("peers", new BEValue(peers));
+// peers.forEach(p -> {
+// try {
+// Map<String, BEValue> map1 = p.getMap();
+// log.info("Peer info: ip={}, port={}, id={}", map1.get("ip").getString(), map1.get("port").getInt(), map1.get("peer id").getString());
+// }catch (Exception e){
+// log.error("Print Peer info error", e);
+// }
+// });
+// ByteBuffer bencode = BEncoder.bencode(map);
+// if(!isAdded){
+// log.info("New peer added.");
+// Map<String, BEValue> p = new HashMap<>(3);
+// p.put("ip", new BEValue(trackerProtocol.getIp()));
+// p.put("peer id", new BEValue(trackerProtocol.getPeer_id()));
+// p.put("port", new BEValue(trackerProtocol.getPort()));
+// peers.add(new BEValue(p));
+// }
+// return ResponseEntity.ok(bencode.array());
+// }
+// }catch (Exception e){
+// try{
+// String message = e.getMessage();
+// message = message == null ? "" : message;
+// log.error("announce error", e);
+// map.put("failure reason", new BEValue(message , "UTF-8"));
+// return ResponseEntity.ok(BEncoder.bencode(map).array());
+// }catch (Exception ex){
+// log.error("announce error ", ex);
+// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Internal Server Error".getBytes());
+// }
+// }
+//}
+
}
\ No newline at end of file
diff --git a/src/main/java/com/example/myproject/entity/TorrentReport.java b/src/main/java/com/example/myproject/entity/TorrentReport.java
index 0d6de00..80cda28 100644
--- a/src/main/java/com/example/myproject/entity/TorrentReport.java
+++ b/src/main/java/com/example/myproject/entity/TorrentReport.java
@@ -2,8 +2,10 @@
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
+import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
+import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.time.LocalDateTime;
@@ -11,6 +13,8 @@
@Data
@Builder
@Entity
+@NoArgsConstructor
+@AllArgsConstructor
public class TorrentReport {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
diff --git a/src/main/java/com/example/myproject/service/TorrentService.java b/src/main/java/com/example/myproject/service/TorrentService.java
index e0a4f41..14b96a2 100644
--- a/src/main/java/com/example/myproject/service/TorrentService.java
+++ b/src/main/java/com/example/myproject/service/TorrentService.java
@@ -42,6 +42,7 @@
import com.example.myproject.dto.param.TorrentParam;
import com.example.myproject.dto.param.TorrentUploadParam;
import com.example.myproject.dto.TorrentUpdateDTO;
+import org.springframework.scheduling.annotation.Async;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
@@ -75,7 +76,7 @@
TorrentEntity selectByInfoHash(String infoHash);
-
+ @Async
void processUploadDownload(Long userId, String peerId, String infoHash, Long torrentId, double uploaded, double downloaded);
}
diff --git a/src/main/java/com/example/myproject/service/serviceImpl/TorrentServiceImpl.java b/src/main/java/com/example/myproject/service/serviceImpl/TorrentServiceImpl.java
index 210db93..159c347 100644
--- a/src/main/java/com/example/myproject/service/serviceImpl/TorrentServiceImpl.java
+++ b/src/main/java/com/example/myproject/service/serviceImpl/TorrentServiceImpl.java
@@ -435,8 +435,8 @@
// 2. 获取原始 announce 字段
// TODO: 对应本机应用地址
// String announce = "http://127.0.0.1:5011/seeds/announce?passkey="+passkey +"&userId=" + StpUtil.getLoginIdAsString();
- String announce = "http://192.168.5.149:5011/seeds/announce?passkey=" + passkey + "&userId=" + StpUtil.getLoginIdAsString();
-// String announce = "http://172.23.80.1:5011/seeds/announce?passkey=" + passkey + "&userId=" + 1;
+ // String announce = "http://192.168.5.184:5011/seeds/announce?passkey=" + passkey + "&userId=" + StpUtil.getLoginIdAsString();
+ String announce = "http://192.168.5.184:5011/seeds/announce?passkey=" + passkey + "&userId=" + 2;
decoded.put("announce", new BEValue(announce));
// 4. 编码成新的 .torrent 文件字节数组
@@ -531,6 +531,7 @@
@Override
+ @Transactional
public TorrentEntity selectByInfoHash(String infoHash) {
return torrentMapper.selectByInfoHash(infoHash);
}
@@ -541,7 +542,6 @@
log.info("开始处理上传和下载");
Optional<TorrentReport> lastReportOpt = torrentReportRepository.findLatestByPeerIdAndInfoHash(peerId, infoHash);
-
double lastUploaded = 0;
double lastDownloaded = 0;
LocalDateTime lastTime = null;
diff --git a/target/classes/com/example/myproject/MyProjectApplication.class b/target/classes/com/example/myproject/MyProjectApplication.class
index 93fb1c1..e1b6966 100644
--- a/target/classes/com/example/myproject/MyProjectApplication.class
+++ b/target/classes/com/example/myproject/MyProjectApplication.class
Binary files differ
diff --git a/target/classes/com/example/myproject/controller/TorrentController.class b/target/classes/com/example/myproject/controller/TorrentController.class
index e05b344..1fd4786 100644
--- a/target/classes/com/example/myproject/controller/TorrentController.class
+++ b/target/classes/com/example/myproject/controller/TorrentController.class
Binary files differ
diff --git a/target/classes/com/example/myproject/entity/TorrentReport$TorrentReportBuilder.class b/target/classes/com/example/myproject/entity/TorrentReport$TorrentReportBuilder.class
index 17b87a3..9fd9b73 100644
--- a/target/classes/com/example/myproject/entity/TorrentReport$TorrentReportBuilder.class
+++ b/target/classes/com/example/myproject/entity/TorrentReport$TorrentReportBuilder.class
Binary files differ
diff --git a/target/classes/com/example/myproject/entity/TorrentReport.class b/target/classes/com/example/myproject/entity/TorrentReport.class
index 260b34e..4f9c6a4 100644
--- a/target/classes/com/example/myproject/entity/TorrentReport.class
+++ b/target/classes/com/example/myproject/entity/TorrentReport.class
Binary files differ
diff --git a/target/classes/com/example/myproject/service/TorrentService.class b/target/classes/com/example/myproject/service/TorrentService.class
index 9fdf389..4927d64 100644
--- a/target/classes/com/example/myproject/service/TorrentService.class
+++ b/target/classes/com/example/myproject/service/TorrentService.class
Binary files differ
diff --git a/target/classes/com/example/myproject/service/serviceImpl/TorrentServiceImpl.class b/target/classes/com/example/myproject/service/serviceImpl/TorrentServiceImpl.class
index aa1401d..f1b6465 100644
--- a/target/classes/com/example/myproject/service/serviceImpl/TorrentServiceImpl.class
+++ b/target/classes/com/example/myproject/service/serviceImpl/TorrentServiceImpl.class
Binary files differ