上传量下载量计算
Change-Id: I539eb93ad3fa457ab0e7966e3406e7c9c2e00bc5
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;