Пример #1
0
        private async void OnStarted()
        {
            logger.LogInformation("Starting DataSpaceMicroservice (OnStarted)");

            // Connect to hub
            try
            {
                await hubConnectionDataSpace.StartAsync().ContinueWith(t =>
                {
                    if (t.IsFaulted)
                    {
                        logger.LogError("-- Couln't connect to signalR DataSpaceHub (OnStarted)");
                        return;
                    }
                    logger.LogInformation("DataSpaceMicroservice connected to DataSpaceHub successfully (OnStarted)");
                });

                await hubConnectionAuth.StartAsync().ContinueWith(t =>
                {
                    if (t.IsFaulted)
                    {
                        logger.LogError("-- Couln't connect to signalR AuthHub (OnStarted)");
                        return;
                    }
                    logger.LogInformation("DataSpaceMicroservice connected to AuthHub successfully (OnStarted)");
                });

                hubConnectionDataSpace.On <string, string, DirectoryDto>("SaveDirectoryMetadata", async(appId, phonenumber, dirDto) =>
                {
                    logger.LogInformation($"-- {appId} requesting SaveDirectoryMetadata for {appId}.");

                    // TODO: save dir
                    NodeDto directoryResponse = await dataSpaceService.NewDirectory(phonenumber, dirDto);
                    if (directoryResponse != null)
                    {
                        logger.LogInformation($"-- {directoryResponse.Name} metadata saved (Success). " +
                                              $"Returning response dto obj.");
                        await hubConnectionDataSpace.SendAsync("SaveDirectoryMetadataSuccess", appId, directoryResponse);
                        return;
                    }

                    logger.LogError($"-- {directoryResponse.Name} metadata not saved (Fail). " +
                                    $"Returning error message.");
                    await hubConnectionDataSpace.SendAsync("SaveDirectoryMetadataFail", appId,
                                                           $"Saving metadata failed, for file: {dirDto}, requested by: {appId}");
                });

                hubConnectionDataSpace.On <string, string, FileDto>("SaveFileMetadata", async(appId, phonenumber, fileDto) =>
                {
                    logger.LogInformation($"-- {appId} requesting SaveFileMetadata for {appId}.");

                    // TODO: save file
                    NodeDto fileResponse = dataSpaceService.FileUpload(phonenumber, fileDto);
                    if (fileResponse != null)
                    {
                        logger.LogInformation($"-- {fileResponse.Name} metadata saved (Success). " +
                                              $"Returning response dto obj.");
                        await hubConnectionDataSpace.SendAsync("SaveFileMetadataSuccess", appId, fileResponse);
                        return;
                    }

                    logger.LogError($"-- {fileResponse.Name} metadata not saved (Fail). " +
                                    $"Returning error message.");
                    await hubConnectionDataSpace.SendAsync("SaveFileMetadataFail", appId,
                                                           $"Saving metadata failed, for file: {fileDto}, requested by: {appId}");
                });

                hubConnectionDataSpace.On <string, string>("RequestFilesMetaData", async(appId, phoneNumber) =>
                {
                    logger.LogInformation($"-- {appId} requesting FilesMetaData for account: {phoneNumber}.");

                    // TODO: get list of all dirs and files (metadata)
                    DataSpaceMetadata dataSpaceMetadata = await dataSpaceService.GetAllByOwner(phoneNumber);
                    if (dataSpaceMetadata != null)
                    {
                        logger.LogInformation($"-- Returning metadata for all dirs/files.");
                        await hubConnectionDataSpace.SendAsync("RequestFilesMetaDataSuccess", appId, dataSpaceMetadata);
                        return;
                    }

                    logger.LogError($"-- Request couldn't be executed - returning error message.");
                    await hubConnectionDataSpace.SendAsync("SaveFileMetadataFail", appId,
                                                           $"Couldn't load directories and files, for account by number: {phoneNumber}, requested by: {appId}");
                });

                hubConnectionDataSpace.On <string, string, string>("DeleteFileMetadata", async(appId, phoneNumber, filePath) =>
                {
                    logger.LogInformation($"-- {appId} requesting DeleteFileMetadata ('{filePath}') by account: {phoneNumber}.");

                    // TODO: delete file and return appropriate result
                    if (await dataSpaceService.DeleteFile(phoneNumber, filePath))
                    {
                        logger.LogInformation($"-- File deleted successfully.");
                        await hubConnectionDataSpace.SendAsync("DeleteFileMetadataSuccess", appId, filePath);
                        return;
                    }

                    logger.LogError($"-- Request couldn't be executed- returning error message.");
                    await hubConnectionDataSpace.SendAsync("DeleteFileMetadataFail", appId, filePath,
                                                           $"Couldn't find {filePath} for owner: {phoneNumber}, requested by: {appId}");
                });

                hubConnectionDataSpace.On <string, string, string>("DeleteDirectoryMetadata", async(appId, phoneNumber, directoryPath) =>
                {
                    logger.LogInformation($"-- {appId} requesting DeleteDirectoryMetadata ('{directoryPath}') by account: {phoneNumber}.");

                    // TODO: delete directory and return appropriate result
                    if (await dataSpaceService.DeleteDirectory(phoneNumber, directoryPath))
                    {
                        logger.LogInformation($"-- Directory deleted successfully.");
                        await hubConnectionDataSpace.SendAsync("DeleteDirectoryMetadataSuccess", appId, directoryPath);
                        return;
                    }

                    logger.LogError($"-- Request couldn't be executed- returning error message.");
                    await hubConnectionDataSpace.SendAsync("DeleteDirectoryMetadataFail", appId, directoryPath,
                                                           $"Couldn't find directory:{directoryPath} for owner: {phoneNumber}, requested by: {appId}");
                });

                hubConnectionDataSpace.On <string, string, List <SimpleNodeDto> >("DeleteMultipleNodesMetadata", async(appId, phoneNumber, nodes) =>
                {
                    logger.LogInformation($"-- {appId} requesting DeleteMultipleNodesMetadata (total {nodes.Count}) by account: {phoneNumber}.");

                    // TODO: delete directory and return appropriate result
                    if (await dataSpaceService.BatchDeleteNodes(phoneNumber, nodes))
                    {
                        logger.LogInformation($"-- Nodes batch deleted successfully.");
                        await hubConnectionDataSpace.SendAsync("DeleteMultipleNodesMetadataSuccess", appId, nodes);
                        return;
                    }

                    logger.LogError($"-- Request couldn't be executed - returning error message.");
                    await hubConnectionDataSpace.SendAsync("DeleteMultipleNodesMetadataFail", appId, nodes,
                                                           $"Nodes batch delete failed (total {nodes.Count}) for owner: {phoneNumber}, requested by: {appId}");
                });
            }
            catch (Exception e)
            {
                logger.LogInformation("DataSpaceMicroservice couldn't be started (OnStarted)");
                return;
            }
            // Perform on-started activites here
        }
Пример #2
0
        public void RegisterHandlers()
        {
            hubConnection.On <string, DirectoryDto>("SaveDirectoryMetadata", async(phonenumber, dirDto) =>
            {
                logger.LogInformation($"-- {phonenumber} requesting SaveDirectoryMetadata for {phonenumber}.");

                // TODO: save dir
                NodeDto directoryResponse = await dataSpaceService.NewDirectory(phonenumber, dirDto);
                if (directoryResponse != null)
                {
                    logger.LogInformation($"-- {directoryResponse.Name} metadata saved (Success). " +
                                          $"Returning response dto obj.");
                    await hubConnection.SendAsync("SaveDirectoryMetadataSuccess", phonenumber, directoryResponse);
                    return;
                }

                logger.LogError($"-- {directoryResponse.Name} metadata not saved (Fail). " +
                                $"Returning error message.");
                await hubConnection.SendAsync("SaveDirectoryMetadataFail", phonenumber,
                                              $"Saving metadata failed, for file: {dirDto}, requested by: {phonenumber}");
            });

            hubConnection.On <string, FileDto>("SaveFileMetadata", async(phonenumber, fileDto) =>
            {
                logger.LogInformation($"-- {phonenumber} requesting SaveFileMetadata for {phonenumber}.");

                // TODO: save file
                NodeDto fileResponse = dataSpaceService.FileUpload(phonenumber, fileDto);
                if (fileResponse != null)
                {
                    logger.LogInformation($"-- {fileResponse.Name} metadata saved (Success). " +
                                          $"Returning response dto obj.");
                    await hubConnection.SendAsync("SaveFileMetadataSuccess", phonenumber, fileResponse);
                    return;
                }

                logger.LogError($"-- {fileResponse.Name} metadata not saved (Fail). " +
                                $"Returning error message.");
                await hubConnection.SendAsync("SaveFileMetadataFail", phonenumber,
                                              $"Saving metadata failed, for file: {fileDto}, requested by: {phonenumber}");
            });

            hubConnection.On <string>("RequestFilesMetaData", async(phoneNumber) =>
            {
                logger.LogInformation($"-- {phoneNumber} requesting FilesMetaData for account: {phoneNumber}.");

                // TODO: get list of all dirs and files (metadata)
                DataSpaceMetadata dataSpaceMetadata = await dataSpaceService.GetAllByOwner(phoneNumber);
                if (dataSpaceMetadata != null)
                {
                    logger.LogInformation($"-- Returning metadata for all dirs/files.");
                    await hubConnection.SendAsync("RequestFilesMetaDataSuccess", phoneNumber, dataSpaceMetadata);
                    return;
                }

                logger.LogError($"-- Request couldn't be executed - returning error message.");
                await hubConnection.SendAsync("RequestFilesMetaDataFail", phoneNumber,
                                              $"Couldn't load directories and files, for account by number: {phoneNumber}, requested by: {phoneNumber}");
            });

            hubConnection.On <string, string>("DeleteFileMetadata", async(phoneNumber, filePath) =>
            {
                logger.LogInformation($"-- {phoneNumber} requesting DeleteFileMetadata ('{filePath}') by account: {phoneNumber}.");

                // TODO: delete file and return appropriate result
                if (await dataSpaceService.DeleteFile(phoneNumber, filePath))
                {
                    logger.LogInformation($"-- File deleted successfully.");
                    await hubConnection.SendAsync("DeleteFileMetadataSuccess", phoneNumber, filePath);
                    return;
                }

                logger.LogError($"-- Request couldn't be executed- returning error message.");
                await hubConnection.SendAsync("DeleteFileMetadataFail", phoneNumber, filePath,
                                              $"Couldn't find {filePath} for owner: {phoneNumber}, requested by: {phoneNumber}");
            });

            hubConnection.On <string, string>("DeleteDirectoryMetadata", async(phoneNumber, directoryPath) =>
            {
                logger.LogInformation($"-- {phoneNumber} requesting DeleteDirectoryMetadata ('{directoryPath}') by account: {phoneNumber}.");

                // TODO: delete directory and return appropriate result
                if (await dataSpaceService.DeleteDirectory(phoneNumber, directoryPath))
                {
                    logger.LogInformation($"-- Directory deleted successfully.");
                    await hubConnection.SendAsync("DeleteDirectoryMetadataSuccess", phoneNumber, directoryPath);
                    return;
                }

                logger.LogError($"-- Request couldn't be executed- returning error message.");
                await hubConnection.SendAsync("DeleteDirectoryMetadataFail", phoneNumber, directoryPath,
                                              $"Couldn't find directory:{directoryPath} for owner: {phoneNumber}, requested by: {phoneNumber}");
            });

            hubConnection.On <string, List <SimpleNodeDto> >("DeleteMultipleNodesMetadata", async(phoneNumber, nodes) =>
            {
                logger.LogInformation($"-- {phoneNumber} requesting DeleteMultipleNodesMetadata (total {nodes.Count}) by account: {phoneNumber}.");

                // TODO: delete directory and return appropriate result
                if (await dataSpaceService.BatchDeleteNodes(phoneNumber, nodes))
                {
                    logger.LogInformation($"-- Nodes batch deleted successfully.");
                    await hubConnection.SendAsync("DeleteMultipleNodesMetadataSuccess", phoneNumber, nodes);
                    return;
                }

                logger.LogError($"-- Request couldn't be executed - returning error message.");
                await hubConnection.SendAsync("DeleteMultipleNodesMetadataFail", phoneNumber, nodes,
                                              $"Nodes batch delete failed (total {nodes.Count}) for owner: {phoneNumber}, requested by: {phoneNumber}");
            });
        }