| @@ -8,5 +8,10 @@ | |||||
| "outputFileName": "cloud-agent", | "outputFileName": "cloud-agent", | ||||
| "outputDirectory": "log", | "outputDirectory": "log", | ||||
| "level": "debug" | "level": "debug" | ||||
| }, | |||||
| "rabbitMQ": { | |||||
| "address": "127.0.0.1:5672", | |||||
| "account": "guest", | |||||
| "password": "guest" | |||||
| } | } | ||||
| } | } | ||||
| @@ -9,7 +9,7 @@ import ( | |||||
| "gitlink.org.cn/cloudream/agent/internal/config" | "gitlink.org.cn/cloudream/agent/internal/config" | ||||
| "gitlink.org.cn/cloudream/utils" | "gitlink.org.cn/cloudream/utils" | ||||
| racli "gitlink.org.cn/cloudream/rabbitmq/client" | |||||
| coorcli "gitlink.org.cn/cloudream/rabbitmq/client/coordinator" | |||||
| ramsg "gitlink.org.cn/cloudream/rabbitmq/message" | ramsg "gitlink.org.cn/cloudream/rabbitmq/message" | ||||
| agtmsg "gitlink.org.cn/cloudream/rabbitmq/message/agent" | agtmsg "gitlink.org.cn/cloudream/rabbitmq/message/agent" | ||||
| coormsg "gitlink.org.cn/cloudream/rabbitmq/message/coordinator" | coormsg "gitlink.org.cn/cloudream/rabbitmq/message/coordinator" | ||||
| @@ -59,6 +59,14 @@ func (service *CommandService) RepMove(msg *agtmsg.RepMoveCommand) *agtmsg.Agent | |||||
| for { | for { | ||||
| readCnt, err := ipfsRd.Read(buf) | readCnt, err := ipfsRd.Read(buf) | ||||
| if readCnt > 0 { | |||||
| err = myio.WriteAll(outFile, buf[:readCnt]) | |||||
| if err != nil { | |||||
| log.Warnf("write data to file %s failed, err: %s", outFilePath, err.Error()) | |||||
| return ramsg.ReplyFailed[agtmsg.AgentMoveResp](errorcode.OPERATION_FAILED, "write data to file failed") | |||||
| } | |||||
| } | |||||
| // 文件读取完毕 | // 文件读取完毕 | ||||
| if err == io.EOF { | if err == io.EOF { | ||||
| break | break | ||||
| @@ -68,16 +76,10 @@ func (service *CommandService) RepMove(msg *agtmsg.RepMoveCommand) *agtmsg.Agent | |||||
| log.Warnf("read ipfs file %s data failed, err: %s", fileHash, err.Error()) | log.Warnf("read ipfs file %s data failed, err: %s", fileHash, err.Error()) | ||||
| return ramsg.ReplyFailed[agtmsg.AgentMoveResp](errorcode.OPERATION_FAILED, "read ipfs file data failed") | return ramsg.ReplyFailed[agtmsg.AgentMoveResp](errorcode.OPERATION_FAILED, "read ipfs file data failed") | ||||
| } | } | ||||
| err = myio.WriteAll(outFile, buf[:readCnt]) | |||||
| if err != nil { | |||||
| log.Warnf("write data to file %s failed, err: %s", outFilePath, err.Error()) | |||||
| return ramsg.ReplyFailed[agtmsg.AgentMoveResp](errorcode.OPERATION_FAILED, "write data to file failed") | |||||
| } | |||||
| } | } | ||||
| //向coor报告临时缓存hash | //向coor报告临时缓存hash | ||||
| coorClient, err := racli.NewCoordinatorClient() | |||||
| coorClient, err := coorcli.NewCoordinatorClient(&config.Cfg().RabbitMQ) | |||||
| if err != nil { | if err != nil { | ||||
| log.Warnf("new coordinator client failed, err: %s", err.Error()) | log.Warnf("new coordinator client failed, err: %s", err.Error()) | ||||
| return ramsg.ReplyFailed[agtmsg.AgentMoveResp](errorcode.OPERATION_FAILED, "new coordinator client failed") | return ramsg.ReplyFailed[agtmsg.AgentMoveResp](errorcode.OPERATION_FAILED, "new coordinator client failed") | ||||
| @@ -91,11 +91,26 @@ func (s *GRPCService) GetFile(req *agentserver.GetReq, server agentserver.FileTr | |||||
| defer reader.Close() | defer reader.Close() | ||||
| buf := make([]byte, 1024) | buf := make([]byte, 1024) | ||||
| readAllCnt := 0 | |||||
| for { | for { | ||||
| readCnt, err := reader.Read(buf) | readCnt, err := reader.Read(buf) | ||||
| if readCnt > 0 { | |||||
| readAllCnt += readCnt | |||||
| err = server.Send(&agentserver.FileDataPacket{ | |||||
| Type: agentserver.FileDataPacketType_Data, | |||||
| Data: buf[:readCnt], | |||||
| }) | |||||
| if err != nil { | |||||
| log.WithField("FileHash", req.FileHash). | |||||
| Warnf("send file data failed, err: %s", err.Error()) | |||||
| return fmt.Errorf("send file data failed, err: %w", err) | |||||
| } | |||||
| } | |||||
| // 文件读取完毕 | // 文件读取完毕 | ||||
| if err == io.EOF { | if err == io.EOF { | ||||
| log.WithField("FileHash", req.FileHash).Debugf("send data size %d", readAllCnt) | |||||
| // 发送EOF消息 | // 发送EOF消息 | ||||
| server.Send(&agentserver.FileDataPacket{ | server.Send(&agentserver.FileDataPacket{ | ||||
| Type: agentserver.FileDataPacketType_EOF, | Type: agentserver.FileDataPacketType_EOF, | ||||
| @@ -108,15 +123,5 @@ func (s *GRPCService) GetFile(req *agentserver.GetReq, server agentserver.FileTr | |||||
| log.Warnf("read file %s data failed, err: %s", req.FileHash, err.Error()) | log.Warnf("read file %s data failed, err: %s", req.FileHash, err.Error()) | ||||
| return fmt.Errorf("read file data failed, err: %w", err) | return fmt.Errorf("read file data failed, err: %w", err) | ||||
| } | } | ||||
| err = server.Send(&agentserver.FileDataPacket{ | |||||
| Type: agentserver.FileDataPacketType_Data, | |||||
| Data: buf[:readCnt], | |||||
| }) | |||||
| if err != nil { | |||||
| log.WithField("FileHash", req.FileHash). | |||||
| Warnf("send file data failed, err: %s", err.Error()) | |||||
| return fmt.Errorf("send file data failed, err: %w", err) | |||||
| } | |||||
| } | } | ||||
| } | } | ||||
| @@ -1,17 +1,19 @@ | |||||
| package config | package config | ||||
| import ( | import ( | ||||
| racfg "gitlink.org.cn/cloudream/rabbitmq/config" | |||||
| c "gitlink.org.cn/cloudream/utils/config" | c "gitlink.org.cn/cloudream/utils/config" | ||||
| log "gitlink.org.cn/cloudream/utils/logger" | log "gitlink.org.cn/cloudream/utils/logger" | ||||
| ) | ) | ||||
| type Config struct { | type Config struct { | ||||
| ID int `json:"id"` | |||||
| GRPCListenAddress string `json:"grpcListenAddress"` | |||||
| LocalIP string `json:"localIP"` | |||||
| IPFSPort int `json:"ipfsPort"` | |||||
| StorageBaseDir string `json:"storageBaseDir"` | |||||
| Logger log.Config `json:"logger"` | |||||
| ID int `json:"id"` | |||||
| GRPCListenAddress string `json:"grpcListenAddress"` | |||||
| LocalIP string `json:"localIP"` | |||||
| IPFSPort int `json:"ipfsPort"` | |||||
| StorageBaseDir string `json:"storageBaseDir"` | |||||
| Logger log.Config `json:"logger"` | |||||
| RabbitMQ racfg.Config `json:"rabbitMQ"` | |||||
| } | } | ||||
| var cfg Config | var cfg Config | ||||
| @@ -47,7 +47,7 @@ func main() { | |||||
| // 启动命令服务器 | // 启动命令服务器 | ||||
| // TODO 需要设计AgentID持久化机制 | // TODO 需要设计AgentID持久化机制 | ||||
| agtSvr, err := rasvr.NewAgentServer(NewCommandService(ipfs), config.Cfg().ID) | |||||
| agtSvr, err := rasvr.NewAgentServer(NewCommandService(ipfs), config.Cfg().ID, &config.Cfg().RabbitMQ) | |||||
| if err != nil { | if err != nil { | ||||
| log.Fatalf("new agent server failed, err: %s", err.Error()) | log.Fatalf("new agent server failed, err: %s", err.Error()) | ||||
| } | } | ||||
| @@ -6,14 +6,14 @@ import ( | |||||
| log "github.com/sirupsen/logrus" | log "github.com/sirupsen/logrus" | ||||
| "gitlink.org.cn/cloudream/agent/internal/config" | "gitlink.org.cn/cloudream/agent/internal/config" | ||||
| racli "gitlink.org.cn/cloudream/rabbitmq/client" | |||||
| coorcli "gitlink.org.cn/cloudream/rabbitmq/client/coordinator" | |||||
| coormsg "gitlink.org.cn/cloudream/rabbitmq/message/coordinator" | coormsg "gitlink.org.cn/cloudream/rabbitmq/message/coordinator" | ||||
| "gitlink.org.cn/cloudream/utils" | "gitlink.org.cn/cloudream/utils" | ||||
| "gitlink.org.cn/cloudream/utils/consts" | "gitlink.org.cn/cloudream/utils/consts" | ||||
| ) | ) | ||||
| func reportStatus(wg *sync.WaitGroup) { | func reportStatus(wg *sync.WaitGroup) { | ||||
| coorCli, err := racli.NewCoordinatorClient() | |||||
| coorCli, err := coorcli.NewCoordinatorClient(&config.Cfg().RabbitMQ) | |||||
| if err != nil { | if err != nil { | ||||
| wg.Done() | wg.Done() | ||||
| log.Error("new coordinator client failed, err: %w", err) | log.Error("new coordinator client failed, err: %w", err) | ||||