/* Entry point for un-sharing file with a user*/
        public void unShareFileWithUser(string clientId, string filename, string sharedWithUser)
        {
            Logger.Debug("Un Sharing file " + filename + " owned by user " + clientId + " with user " + sharedWithUser);
            UserFileSystem fs          = getUserFSFromMapSynchronized(clientId);
            bool           filepresent = fs.isFilePresentSynchronized(filename);

            fs.removeSharedUserFromFileSynchronized(filename, sharedWithUser);

            if (!filepresent)
            {
                throw new FileNotFoundException("File not present in memory for client : " + filename + " for client : " + clientId);
            }

            UserFileSystem sharedFS = getUserFSFromMapSynchronized(sharedWithUser);
            bool           deleted  = sharedFS.deleteSharedFileSynchronized(new SharedFile(clientId, filename));

            if (!deleted)
            {
                Logger.Debug("Shared file was not present , skipping deletion");
            }
        }
        /* Entry point for delete file*/
        public bool deleteFileSynchronized(string clientid, string filename)
        {
            Logger.Debug("Deleting file : " + filename + " owned by : " + clientid);

            //1) Remove the file from the file system of the client
            //2) Remove the file from the file system of all shared clients

            UserFileSystem fs = getUserFSFromMapSynchronized(clientid);

            if (fs == null)
            {
                throw new UserNotLoadedInMemoryException("User not loaded in memory :" + clientid);
            }

            bool delete = fs.deleteFileSynchronized(filename);

            List <string> sharedClients = fs.getFileMetaDataCloneSynchronized(filename).sharedwithclients;

            foreach (string sharedclient in sharedClients)
            {
                UserFileSystem fsShared = getUserFSFromMapSynchronized(sharedclient);

                if (fsShared != null)
                {
                    Logger.Debug("Removing file : " + filename + " from shared client fs : " + sharedclient);
                    fsShared.deleteSharedFileSynchronized(new SharedFile(clientid, filename));
                }
                else
                {
                    Logger.Warn("The shared user file system is missing from the memory, see what happened");
                }
            }


            return(delete);
        }
        public bool addFSToMapSynchronized(UserFileSystem fs, string clientid)
        {
            Logger.Debug ("Synchronized adding file system in map for client id  : " + clientid);

            lock (this.privateLock) {
                this.clientToFileSystemMap.Add(clientid, fs);
                return true;
            }
        }
        /*	Synchronized method to add user. Returns false if the user is already present
            Otherwise creates an empty file system and adds that user with that empty file system.
         */
        public bool addUserSynchronized(string clientid, string password, long versionNumber)
        {
            Logger.Debug ("Adding user with client id : " + clientid + " and password : "******" and version number :" + versionNumber
            );
            UserFileSystem fs = getUserFSFromMapSynchronized (clientid);

            if (fs != null) {
                return false;

            } else {
                Logger.Debug ("User : "******" present in map, adding");
                UserMetaData md = new UserMetaData(clientid, password, versionNumber,0);
                UserFileSystem filesystem = new UserFileSystem(md);
                addFSToMapSynchronized(filesystem, clientid);
            }
            return true;
        }
        /*
         * This Function is to be Called when a Entire User Needs to be Moved
        */
        public bool sendsynchUser(UserFileSystem file, OOBHandler oobhandle, Group group, List<Address> where)
        {
            try {
                Logger.Debug ("File Operations Synch - sendsynchUser >> BEGIN");
                bool operationResult = false;
                string fileName = FileServerComm.getInstance ().transManager.generateTransactionId (file.metadata.clientId);

                Transaction trans = new Transaction (fileName);

                if (true == FileServerComm.getInstance ().transManager.insertTransaction (trans)) {
                    try {
                        MemoryMappedFile transferFile = null;
                        int writtenBytesLength = 0;
                        transferFile = oobhandle.serializeIntoMemoryMappedFile (fileName, file, ref writtenBytesLength);

                        OOBTransaction oobtrabs = new OOBTransaction (fileName, IsisSystem.GetMyAddress (), writtenBytesLength);
                        oobhandle.sendOOBData (group, transferFile, fileName, where);

                        trans.waitTillSignalled ();
                        operationResult = !trans.isTimedOut;

                        if (operationResult) {
                            group.OrderedSend (FileServerComm.UpdateUser, oobtrabs);
                            trans.waitTillSignalled ();
                            operationResult = !trans.isTimedOut;
                        }
                        FileServerComm.getInstance ().transManager.removeAndGetTransaction (fileName);
                        operationResult = !trans.isTimedOut;
                    } catch (Exception e) {
                        Logger.Debug ("Exception during File Operations Synch - sendsynchUser" + e.ToString ());
                    }
                }

                return operationResult;
            } catch (Exception e) {
                Logger.Debug ("File Operations Synch - sendsynchUser >> Encountered Exception : " + e.ToString());
                return false;
            }
        }
        public void SaveObjectSynchronized(UserFileSystem filesystem)
        {
            lock (privateLock) {

            }
        }
Example #7
0
 public void SaveObjectSynchronized(UserFileSystem filesystem)
 {
     lock (privateLock) {
     }
 }
Example #8
0
        //Merge the user file system
        private UserFileSystem mergeUserFileSystems(UserFileSystem newfs, UserFileSystem oldfs)
        {
            Logger.Debug ("Merging user file systems for user id : " + newfs.metadata.clientId);

            /*
             * Rule of Fight Club ( UserFile system merge)
             *
             * 1) User meta data- The one with higher version number wins, although it will be mostly the
             * newer file system object since user meta data is always maintained in memory
             *
             * 2) SharedFileList - This will be picked from the newer file system object since we don't have versioning for it
             * and it is maintained in the memory always
             *
             * 3) File map -
             * 		a) If there is a new file which is not present in the old file system
             * 				If its marked for deletion  - Don't add it
             * 				If its not marked for deletion  - Add it
             * 		b) If there is file present in the new file system which is also present in the old file system
             * 				If the version number of the new file is higher
             * 						If its marked for deletion in the new file system delete that file
             * 						If its not marked for deletion, replace the file
             * 				If its version number is lower
             * 						TOO BAD
             * 		c) If there are files which are not present in the new file system which are present in old file system
             * 				Ideally this should not happen since the all file names will always remain in memory. In any case, take the file on disk.
             *
             *
             */

            if (newfs.metadata.versionNumber >= oldfs.metadata.versionNumber) {
                oldfs.metadata = newfs.metadata; //replace
            } else {
                Logger.Warn ("The version number for the new user metadata is lower than the old, FIX ME FIX ME");
            }

            oldfs.sharedFiles = newfs.sharedFiles; //replace the shared file list

            //now iterate over the file map, don't f**k up man
            foreach (KeyValuePair<string, UserFile> entry in newfs.filemap) {

                UserFile newfile = entry.Value;
                UserFile oldfile = null;
                string filename = entry.Key;

                if (oldfs.filemap.ContainsKey (filename)) {
                    oldfile = oldfs.filemap [filename];
                }

                if (oldfile == null) {  //case when there is new file and NO old file

                    if (newfile.filemetadata.markedForDeletion == false) {
                        oldfs.addFileSynchronized (newfile);
                    } else {
                        Logger.Debug ("File found marked for deleting, skipping it : " + filename);
                    }

                } else { // case where there is old file and new file

                    if (newfile.filemetadata.versionNumber >= oldfile.filemetadata.versionNumber) { //lets roll
                        if (newfile.filemetadata.markedForDeletion == true) { //remove this file now
                            Logger.Debug ("File marked for deletion, removing : " + filename);
                            oldfs.removeFromMap (filename); //this will decrement the size
                        } else {
                            //long sizediff = newfile.filemetadata.filesize - oldfile.filemetadata.filesize;
                            oldfs.filemap [filename] = newfile;
                            //oldfs.incrementTotalFileSystemSize (sizediff);
                        }
                    }
                }

            }

            return oldfs;
        }
Example #9
0
        //Merge the user file system
        private UserFileSystem mergeUserFileSystems(UserFileSystem newfs, UserFileSystem oldfs)
        {
            Logger.Debug("Merging user file systems for user id : " + newfs.metadata.clientId);

            /*
             * Rule of Fight Club ( UserFile system merge)
             *
             * 1) User meta data- The one with higher version number wins, although it will be mostly the
             * newer file system object since user meta data is always maintained in memory
             *
             * 2) SharedFileList - This will be picked from the newer file system object since we don't have versioning for it
             * and it is maintained in the memory always
             *
             * 3) File map -
             *      a) If there is a new file which is not present in the old file system
             *              If its marked for deletion  - Don't add it
             *              If its not marked for deletion  - Add it
             *      b) If there is file present in the new file system which is also present in the old file system
             *              If the version number of the new file is higher
             *                      If its marked for deletion in the new file system delete that file
             *                      If its not marked for deletion, replace the file
             *              If its version number is lower
             *                      TOO BAD
             *      c) If there are files which are not present in the new file system which are present in old file system
             *              Ideally this should not happen since the all file names will always remain in memory. In any case, take the file on disk.
             *
             *
             */

            if (newfs.metadata.versionNumber >= oldfs.metadata.versionNumber)
            {
                oldfs.metadata = newfs.metadata;                 //replace
            }
            else
            {
                Logger.Warn("The version number for the new user metadata is lower than the old, FIX ME FIX ME");
            }

            oldfs.sharedFiles = newfs.sharedFiles;             //replace the shared file list

            //now iterate over the file map, don't f**k up man
            foreach (KeyValuePair <string, UserFile> entry in newfs.filemap)
            {
                UserFile newfile  = entry.Value;
                UserFile oldfile  = null;
                string   filename = entry.Key;

                if (oldfs.filemap.ContainsKey(filename))
                {
                    oldfile = oldfs.filemap [filename];
                }

                if (oldfile == null)                    //case when there is new file and NO old file

                {
                    if (newfile.filemetadata.markedForDeletion == false)
                    {
                        oldfs.addFileSynchronized(newfile);
                    }
                    else
                    {
                        Logger.Debug("File found marked for deleting, skipping it : " + filename);
                    }
                }
                else                     // case where there is old file and new file

                {
                    if (newfile.filemetadata.versionNumber >= oldfile.filemetadata.versionNumber)     //lets roll
                    {
                        if (newfile.filemetadata.markedForDeletion == true)                           //remove this file now
                        {
                            Logger.Debug("File marked for deletion, removing : " + filename);
                            oldfs.removeFromMap(filename);                              //this will decrement the size
                        }
                        else
                        {
                            //long sizediff = newfile.filemetadata.filesize - oldfile.filemetadata.filesize;
                            oldfs.filemap [filename] = newfile;
                            //oldfs.incrementTotalFileSystemSize (sizediff);
                        }
                    }
                }
            }

            return(oldfs);
        }
Example #10
0
        void handleBootStrappingRequest(Group group, OOBHandler oobhandle, InMemoryFileSystem fileSystem, string requestName, Address recvdFrom)
        {
            if (state.currState == eBootStrapState.BootStrappingComplete)
            {
                Logger.Debug("handleBootStrappingRequest , Received from " + recvdFrom.ToStringVerboseFormat());

                BootStrappingCheckPoint initialStage = null;
                initialStage = new BootStrappingCheckPoint("Boot Strapping Begin", FileServerComm.BootStrapBegin,
                                                           IsisSystem.GetMyAddress(), SUCCESS,
                                                           0);

                Logger.Debug("Sending a BootStrapping Begin , Response to " + recvdFrom.ToStringVerboseFormat());
                group.RawP2PSend(recvdFrom, FileServerComm.BootStrapBegin, initialStage);

                MemoryMappedFile transferFile = null;
                int currentUserIndex          = 0;
                int numberOfUsersCurrentBatch = 0;

                List <string>      users          = fileSystem.GetInMemoryUserList();
                InMemoryFileSystem tempFileSystem = new InMemoryFileSystem(false);

                //Yayy Lets Begin Doing Some Boot Strapping
                try {
                    Logger.Debug("Number of Users to BootStrap and Send " + users.Count);
                    while (currentUserIndex < users.Count)
                    {
                        UserFileSystem userfilesys = fileSystem.GetClonedInMemoryUserFileSystem(users [currentUserIndex]);
                        numberOfUsersCurrentBatch++;

                        Logger.Debug("Adding User to the BootStrap : " + users[currentUserIndex]);

                        tempFileSystem.addFSToMapSynchronized(userfilesys, users [currentUserIndex]);
                        currentUserIndex++;

                        if (numberOfUsersCurrentBatch == BatchSize)
                        {
                            //Let's Make a OOB File and Transfer the Data
                            string currentFileName = FileServerComm.getInstance().transManager.generateTransactionId();

                            bool operationResult = false;

                            numberOfUsersCurrentBatch = 0;

                            Transaction trans = new Transaction(currentFileName);

                            FileServerComm.getInstance().transManager.insertTransaction(trans);

                            int writtenBytesLength = 0;
                            transferFile = oobhandle.serializeIntoMemoryMappedFile(currentFileName, tempFileSystem, ref writtenBytesLength);

                            BootStrappingCheckPoint continueBootStrap = null;
                            continueBootStrap = new BootStrappingCheckPoint(currentFileName, FileServerComm.BootStrapContinue,
                                                                            IsisSystem.GetMyAddress(), SUCCESS,
                                                                            writtenBytesLength);
                            List <Address> where = new List <Address>();
                            where.Add(recvdFrom);
                            where.Add(IsisSystem.GetMyAddress());
                            oobhandle.sendOOBData(group, transferFile, currentFileName, where);

                            trans.waitTillSignalled();
                            operationResult = !trans.isTimedOut;

                            if (operationResult)
                            {
                                group.RawP2PSend(recvdFrom, FileServerComm.BootStrapContinue, continueBootStrap);
                                trans.waitTillSignalled();
                                operationResult = !trans.isTimedOut;
                            }
                            else
                            {
                                Logger.Debug("Sending BootStraping Request Timed Out, Quit Out of BootStrapping partcicipation");
                                return;
                            }
                            tempFileSystem = new InMemoryFileSystem();
                        }
                    }

                    //Lets Throw out the Remaining Users
                    if (numberOfUsersCurrentBatch != 0)
                    {
                        string currentFileName = FileServerComm.getInstance().transManager.generateTransactionId();

                        Transaction trans = new Transaction(currentFileName);
                        FileServerComm.getInstance().transManager.insertTransaction(trans);

                        bool currentOperationResult = false;

                        int writtenBytesLength = 0;
                        transferFile = oobhandle.serializeIntoMemoryMappedFile(currentFileName, tempFileSystem, ref writtenBytesLength);
                        BootStrappingCheckPoint _continue = null;
                        _continue = new BootStrappingCheckPoint(currentFileName, FileServerComm.BootStrapContinue,
                                                                IsisSystem.GetMyAddress(), SUCCESS,
                                                                writtenBytesLength);

                        List <Address> where = new List <Address>();
                        where.Add(recvdFrom);
                        where.Add(IsisSystem.GetMyAddress());

                        oobhandle.sendOOBData(group, transferFile, currentFileName, where);

                        trans.waitTillSignalled();
                        currentOperationResult = !trans.isTimedOut;

                        if (currentOperationResult)
                        {
                            group.RawP2PSend(recvdFrom, FileServerComm.BootStrapContinue, _continue);

                            trans.waitTillSignalled();
                            currentOperationResult = !trans.isTimedOut;
                        }
                        else
                        {
                            Logger.Debug("Sending BootStraping Request Timed Out, Quit Out of BootStrapping partcicipation");
                            return;
                        }

                        tempFileSystem            = new InMemoryFileSystem();
                        numberOfUsersCurrentBatch = 0;
                    }

                    Logger.Debug("Sending a Boot Strap End Response to " + recvdFrom.ToString());

                    BootStrappingCheckPoint finalStage = null;
                    finalStage = new BootStrappingCheckPoint("Yayyy Finally Done", FileServerComm.BootStrapEnd,
                                                             IsisSystem.GetMyAddress(), SUCCESS,
                                                             0);

                    group.RawP2PSend(recvdFrom, FileServerComm.BootStrapEnd, finalStage);

                    return;
                } catch (Exception e) {
                    Logger.Debug("Caught an Exception : " + e.ToString());
                    BootStrappingCheckPoint _exception = null;
                    _exception = new BootStrappingCheckPoint(e.ToString(), FileServerComm.BootStrapException,
                                                             IsisSystem.GetMyAddress(), FAILURE,
                                                             0);
                    group.P2PSend(recvdFrom, FileServerComm.BootStrapException, _exception);
                    return;
                }
            }
            else
            {
                Logger.Debug("Node is Not in the State of Boot Strapping, Ignore this Request and Keep Calm");
            }
        }