Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Develop a simple client application that implements the functionalities describe

ID: 3683285 • Letter: D

Question

Develop a simple client application that implements the functionalities described below. Use JAVA.

After obtaining a token from the group server, a user can make use of the file server to manage his or her files. To implement the required functionality, you will need to develop a client application that implements the functionalities described below.

boolean connect(String server, int port)

Your implementation of this method is responsible for establishing a connection to the file server. No other methods provided by your file client application should be able to work until this connection is established!

o void disconnect()

This method is responsible for cleanly tearing down the connection to the currently connected file server. It should be invoked when exiting the client application, or any other time that the user wishes to disconnect from the file server.

o List listFiles(UserToken token)

This method generates a list of all files that can be accessed by members of the groups

specified in token. The user should not be able to see any files that are shared with groups to which he or she does not belong.

o boolean upload(String sourceFile, String destFile, String group, UserToken token)

This method allows the owner of token to upload a file to be shared with members of group, provided that he or she is also a member of group. The sourceFile parameter points to a file on the local file system, while destFile is simply a name used to identify the file on the server.

o boolean download(String sourceFile, String destFile, UserToken token)

This method allows to owner of token to download the specified file, provided that he or she is a member of the group with which this file is shared.

Explanation / Answer

Solution for Question:

This below java program is used the simple fileinterface class to implement the given methods.

1. Connect method will establish the connections with server.
2. disconnect method will disconnect the connection with server.
3. upload method will upload the file into the server once token has got.
4. list method will list the list of in the server folder
5. download method will download the list of files from server.

all these options are used with help of the switch

import java.util.Scanner;
import java.util.List;

public class FileClientUI
{
   FileClient fc = new FileClient();  
   public boolean launchUI(UserToken token, String serverAddress, int portNumber)
   {
       if (fc.connect(serverAddress, portNumber))
       {
           Scanner console = new Scanner(System.in); // Scanner object for input
           String userName = token.getSubject();
           int menuChoice = 0;
           boolean exitKey = false;
           final int MAXUSERLENGTH = 32;
           final int MAXGROUPLENGTH = 32;
           final int MAXPATHLENGTH = 256;
           List<String> aList;
           String groupName;
           String currentGroup = new String();
           String userPrompt;
           String sourceFileName;
           String destFileName;

          
           while (!exitKey)
           {
              
               if (currentGroup.length() > 0)
               {
                   userPrompt = userName + "/" + currentGroup;
               }
               else
               {
                   userPrompt = userName;
               }
               System.out.print("Enter 1 to list the groups you belong to, " +
                               "enter 2 to change the current group to traverse, " +
                               "enter 3 to list files, " +
                               "enter 4 to upload a file to the File Server, " +
                               "enter 5 to download a file from the File Server, " +
                               "enter 6 to delete a file from the File Server, " +
                               "enter 0 to disconnect from File Server... " +
                               userPrompt + "> ");
               String inputString = console.nextLine();
              
               try
               {
                   menuChoice = Integer.parseInt(inputString);
               }
               catch(Exception e)
               {
                   menuChoice = -1;
               }
              
               switch (menuChoice)
               {
                   case 1:
                       aList = fc.listGroups(token);
                       if (aList != null)
                       {
                           for (String s: aList)
                           {
                               System.out.println(s);
                           }
                       }
                       else
                       {
                           System.out.println("Error - user has no groups. Please add groups in Group Server.");
                       }
                       break;
                   case 2:
                       groupName = getNonEmptyString("Enter the group name to change to... > ", MAXGROUPLENGTH);
                       aList = fc.changeGroup(groupName, token);
                       if (aList != null)
                       {
                           for (String s: aList)
                           {
                               System.out.println("Changed to group " + s + ".");
                               currentGroup = s;
                           }
                       }
                       else
                       {
                           System.out.println("Error - please add groups to Group Server.");
                       }
                       break;
                   case 3:
                       aList = fc.listFiles(token);
                       if (aList != null && aList.size() != 0)
                       {
                           for (String s: aList)
                           {
                               System.out.println(s);
                           }
                       }
                       else
                       {
                           System.out.println("No files present.");
                       }
                       break;
                   case 4:
                       if (currentGroup.length() > 0)
                       {
                           sourceFileName = getNonEmptyString("Enter source file path... > ", MAXPATHLENGTH);
                           destFileName = getNonEmptyString("Enter destination file path... > ", MAXPATHLENGTH);
                           if (fc.upload(sourceFileName, destFileName, currentGroup, token))
                           {
                               System.out.println(destFileName + " successfully uploaded to group " + currentGroup + ".");
                           }
                           else
                           {
                               System.out.println("Error uploading " + destFileName + " to File Server.");
                           }
                       }
                       else
                       {
                           System.out.println("You must pick a group for your workspace (option 2).");
                       }
                       break;
                   case 5:
                       if (currentGroup.length() > 0)
                       {
                           sourceFileName = getNonEmptyString("Enter source file path... > ", MAXPATHLENGTH);
                           destFileName = getNonEmptyString("Enter destination file path... > ", MAXPATHLENGTH);
                           if (fc.download(sourceFileName, destFileName, currentGroup, token))
                           {
                               System.out.println(destFileName + " successfully downloaded.");
                           }
                       }
                       else
                       {
                           System.out.println("You must pick a group for your workspace (option 2).");
                       }
                       break;
                   case 6:
                       if (currentGroup.length() > 0)
                       {
                           sourceFileName = getNonEmptyString("Enter filename to delete... > ", MAXPATHLENGTH);
                           if (fc.delete(sourceFileName, currentGroup, token))
                           {
                               System.out.println(sourceFileName + " successfully deleted.");
                           }
                       }
                       else
                       {
                           System.out.println("You must pick a group for your workspace (option 2).");
                       }
                       break;
                   case 0:
                       System.out.println("Disconnecting from File Server...");
                       fc.disconnect();
                       exitKey = true;
                       break;
                   default:
                       System.out.println("Unknown command. Please try again.");
                       break;
               }
           }
          
           return true;
       }
       else // error connecting
       {
           System.out.println("Error connecting to File Server at " +
                           serverAddress + " port " + portNumber + ".");
           return false;
       }
   }
  
   public static String getNonEmptyString(String prompt, int maxLength)
   {
       String str = "";
       Scanner scan = new Scanner(System.in);
      
       System.out.print(prompt);
      
       while (str.length() == 0)
       {
           str = scan.nextLine();
          
           if (str.length() == 0)
           {
               System.out.print(prompt);
           }
           else if (str.length() > maxLength)
           {
               System.out.println("Maximum length allowed is " + maxLength + " characters. Please re-enter.");
               System.out.print(prompt);
               str = "";
           }
       }
      
       return str;
   }
}