Ejemplo n.º 1
0
        //working for relative and absolute
        public static Response RemoveFile(string path)
        {
            var response = ResponseFormation.GetResponse();

            string dirPath  = PathHelpers.GetDirFromPath(path);
            string filename = PathHelpers.GetFilenameFromPath(path);

            var directory = FileTree.GetDirectory(dirPath);

            if (directory == null)
            {
                response.Message = string.Format(ResponseMessages.DirectoryNotFound, dirPath);
            }
            else
            {
                var file = FileTree.GetFile(directory, filename);
                if (file == null)
                {
                    response.Message = string.Format(ResponseMessages.FileNotFound, filename, dirPath);
                }
                else if (file.IPAddresses.Exists(x => x.Equals(State.LocalEndPoint.ToString())) &&
                         File.Exists(Path.Combine(State.GetRootDirectory().FullName, file.ImplicitName)))
                {
                    RemoveFileFromDisk(file.ImplicitName);
                    FileTree.RemoveFile(directory, file);
                    response.IsSuccess = true;
                    response.Message   = "Succesfully Deleted";
                    FileTreeService.UpdateFileTree(FileTree.GetRootDirectory().SerializeToByteArray());

                    if (file.IPAddresses.Count > 1)
                    {
                        var remoteIps = file.IPAddresses.Where(x => !x.Equals(State.LocalEndPoint.ToString())).ToList();
                        if (remoteIps != null)
                        {
                            foreach (var ip in remoteIps)
                            {
                                var socket = ServerList.GetServerList()
                                             .FirstOrDefault(x => x.IPPort.Equals(ip)).Socket;
                                var request = new Request
                                {
                                    Type       = "FileService",
                                    Method     = "RemoveFileFromDisk",
                                    Parameters = new object[] { file.ImplicitName }
                                };
                                ServerCommunication.Send(socket, request.SerializeToByteArray());
                            }
                        }
                    }
                }
                else
                {
                    // send request to remote server
                    response.IsSuccess = true;
                    response.Message   = "Forwarded";
                    response.Command   = Command.forwarded;
                }
            }
            return(response);
        }
Ejemplo n.º 2
0
        public static void UpdateFileTree(byte[] data)
        {
            Response response = new Response
            {
                Command = Command.updateFileTree,
                Bytes   = data
            };

            ServerCommunication.BroadcastToServers(response.SerializeToByteArray());
        }
Ejemplo n.º 3
0
        public void Accept()
        {
            Socket client = listener.Accept();

            var buff    = Network.Receive(client, 10000);
            var request = buff.Deserialize <Request>();

            if (request.Command == Command.clientConnect)
            {
                ServerCommunication.Send(client, ConfigurationHelper.Read(CommonFilePaths.ConfigFile)
                                         .ToList().SerializeToByteArray());
                ClientList.Add(client);
                Task.Run(() => ListenRequest(client));
            }
            else if (request.Command == Command.serverConnect)
            {
                ServerCommunication.AcceptConnection(request, client);
            }
            else
            {
                client.Shutdown(SocketShutdown.Both);
                client.Close();
            }
        }
Ejemplo n.º 4
0
        //working for relative and absolute
        public static Response UpdateFile(string path, string data)
        {
            var response = ResponseFormation.GetResponse();

            string dirPath  = PathHelpers.GetDirFromPath(path);
            string filename = PathHelpers.GetFilenameFromPath(path);

            var directory = FileTree.GetDirectory(dirPath);

            if (directory == null)
            {
                response.Message = string.Format(ResponseMessages.DirectoryNotFound, dirPath);
            }
            else
            {
                var file = FileTree.GetFile(directory, filename);
                var str  = State.LocalEndPoint.ToString();
                if (file == null)
                {
                    response.Message = string.Format(ResponseMessages.FileNotFound, filename, dirPath);
                }
                else if (file.IPAddresses.Exists(x => x.Equals(State.LocalEndPoint.ToString())))
                {
                    File.WriteAllText(Path.Combine(State.GetRootDirectory().FullName, file.ImplicitName), data);
                    response.IsSuccess = true;
                    response.Message   = "Successfully updated";
                    file.Size          = data.Length;

                    var  servers      = ServerList.GetServers();
                    bool isReplicated = false;
                    if (servers.Count > 0)
                    {
                        Request request = new Request();
                        if (file.IPAddresses.Count < 2)
                        {
                            request.Type       = "FileService";
                            request.Method     = "Replicate";
                            request.Parameters = new object[] { data, file };
                            int index = new Random().Next(servers.Count);
                            file.IPAddresses.Add(ServerList.GetServerList()
                                                 .FirstOrDefault(x => x.Socket.Equals(servers[index])).IPPort);
                            ServerCommunication.Send(servers[index], request.SerializeToByteArray());
                            FileTreeService.UpdateFileTree(FileTree.GetRootDirectory().SerializeToByteArray());
                            isReplicated = true;
                        }
                        else
                        {
                            request.Command    = Command.updateFile;
                            request.Method     = "UpdateFile";
                            request.Type       = "FileService";
                            request.Parameters = new object[] { path, data };
                            foreach (var ip in file.IPAddresses)
                            {
                                var s = ServerList.GetServerList().FirstOrDefault(x => x.IPPort.Equals(ip));
                                if (s != null)
                                {
                                    ServerCommunication.Send(s.Socket, request.SerializeToByteArray());
                                }
                            }
                        }

                        //response.Command = Command.wait;
                    }

                    if (!isReplicated)
                    {
                        FileTreeService.UpdateFileTree(FileTree.GetRootDirectory().SerializeToByteArray());
                    }
                }
                else
                {
                    string ip = null;
                    foreach (var ipPort in ServerList.GetIPPorts())
                    {
                        if (file.IPAddresses.Exists(x => x.Equals(ipPort)))
                        {
                            ip = ipPort;
                            break;
                        }
                    }
                    if (ip == null)
                    {
                        response.Message = string.Format(ResponseMessages.FileNotFound, filename, dirPath);
                    }
                    else
                    {
                        // send request to remote server
                        response.Message   = ip;
                        response.IsSuccess = true;
                        response.Command   = Command.forwarded;
                    }
                }
            }
            return(response);
        }