Commit 63dfbc47 authored by Elf's avatar Elf

中间节点进行计算

parent 5e506afb
...@@ -165,9 +165,10 @@ public class FileService{ ...@@ -165,9 +165,10 @@ public class FileService{
List<FileSlice> list = new ArrayList<>(result); List<FileSlice> list = new ArrayList<>(result);
SplitResult splitResult = new SplitResult(); SplitResult splitResult = new SplitResult();
splitResult.setFileSliceList(sortList(list)); splitResult.setFileSliceList(sortList(list));
//merge
ComputingResult computingResult = distributedComService.sliceComputing(splitResult); ComputingResult computingResult = distributedComService.sliceComputing(splitResult);
MergeResult mergeResult = distributedComService.sliceMerge(computingResult,sliceNum); MergeResult mergeResult = distributedComService.sliceMerge(computingResult,sliceNum);
//save
try { try {
saveFile(fileName, mergeResult); saveFile(fileName, mergeResult);
return "success"; return "success";
......
package top.ninwoo.dbc.server;
import top.ninwoo.dbc.api.po.ComputingResult;
import top.ninwoo.dbc.api.po.FileSlice;
import java.io.IOException;
public interface FileService {
String fileComputing(FileSlice fileSlice) throws IOException;
void fileSave(ComputingResult computingResult);
}
package top.ninwoo.dbc.server.controller; package top.ninwoo.dbc.server.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import top.ninwoo.dbc.api.po.FileSlice; import top.ninwoo.dbc.api.po.FileSlice;
import top.ninwoo.dbc.server.FileService;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
...@@ -16,6 +19,8 @@ public class ServerController { ...@@ -16,6 +19,8 @@ public class ServerController {
public String hello(){ public String hello(){
return "hello,springboot"; return "hello,springboot";
}*/ }*/
@Autowired
private FileService fileService;
private static final Map<Long, Map<Integer, FileSlice>> fileSliceMap = new HashMap<>(); private static final Map<Long, Map<Integer, FileSlice>> fileSliceMap = new HashMap<>();
...@@ -25,7 +30,7 @@ public class ServerController { ...@@ -25,7 +30,7 @@ public class ServerController {
} }
@PostMapping("/data/put/") @PostMapping("/data/put/")
public String putData(@RequestBody FileSlice fileSlice) { public String putData(@RequestBody FileSlice fileSlice) throws IOException {
if(fileSlice == null) { if(fileSlice == null) {
return "failed"; return "failed";
} }
...@@ -35,6 +40,8 @@ public class ServerController { ...@@ -35,6 +40,8 @@ public class ServerController {
Map<Integer, FileSlice> sliceMap = fileSliceMap.get(fileSlice.getFileId()); Map<Integer, FileSlice> sliceMap = fileSliceMap.get(fileSlice.getFileId());
sliceMap.put(fileSlice.getSliceId(), fileSlice); sliceMap.put(fileSlice.getSliceId(), fileSlice);
//完成分散计算
fileService.fileComputing(fileSlice);
return "success"; return "success";
} }
......
package top.ninwoo.dbc.server.serviceimpl;
import org.springframework.stereotype.Service;
import top.ninwoo.dbc.api.po.ComputingResult;
import top.ninwoo.dbc.api.po.FileSlice;
import top.ninwoo.dbc.server.FileService;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
@Service
public class FileServiceImpl implements FileService {
//分散计算
@Override
public String fileComputing(FileSlice fileSlice) throws IOException {
if(this.greySlice(fileSlice)){
return "sucess";
}
else{
return "failed";
}
}
//存储模块
//todo
//目前仅是灰度处理,如果为多个计算,则需要添加
@Override
public void fileSave(ComputingResult computingResult){}
/**
* 具体任务
* 灰度
* todo 以后继续添加
*/
private boolean greySlice(FileSlice fileSlice) throws IOException {
//读图片
//npe,可能会出问题
if(fileSlice.getFileBytes().equals(null)){
return false;
}else{
ByteArrayInputStream bis = new ByteArrayInputStream(fileSlice.getFileBytes());
BufferedImage image = ImageIO.read(bis);
int width = image.getWidth();
int height = image.getHeight();
BufferedImage grayImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
for(int j= 0 ; j < width ; j++){
for(int k = 0 ; k < height; k++){
int rgb = image.getRGB(j, k);
grayImage.setRGB(j, k, rgb);
}
}
//这里需提供新的文件输出
ImageIO.write(grayImage, "jpg", new File("/out.jpg"));
return true;
}
}
}
...@@ -37,6 +37,7 @@ public class FileController { ...@@ -37,6 +37,7 @@ public class FileController {
//BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(file.getOriginalFilename()))); //BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(file.getOriginalFilename())));
FileOutputStream out = new FileOutputStream(new File(file.getOriginalFilename())); FileOutputStream out = new FileOutputStream(new File(file.getOriginalFilename()));
byte[] bytes = file.getBytes(); byte[] bytes = file.getBytes();
//发送切片
fileService.sendFile(fileId,bytes); fileService.sendFile(fileId,bytes);
//out.write(file.getBytes()); //out.write(file.getBytes());
out.flush(); out.flush();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment